Blame | Last modification | View Log | RSS feed
$(function(){/*** My Notes core method library**/var myNotes = {/*** Object containing slideIds and corresponding notes as key-value pairs**/notes: null,/*** defaultNoteText will be displayed if user has not saved a note on a slide**/defaultNoteText: 'Click here to add notes',/*** getEntityId() will return Entity ID for the current product**/getEntityId: function() {return $('#product_id').val();},/*** setNotes(notes) will accept an object and set this.notes**/setNotes: function(notes) {this.notes = notes;},getNoteForSlide: function(slideId) {return this.notes.hasOwnProperty(slideId) ? this.notes[slideId] : null;},setNoteForSlide: function(slideId, note){this.notes[slideId] = note;},/*** populateNotes() will populate mynotes section on each slide with notes data**/populateNotes: function() {var notes = this.notes;$('.mynotes-section').each(function(){console.log(this);var slideId = $(this).attr('id').split('-')[1];var text = notes.hasOwnProperty(slideId) ? notes[slideId] : myNotes.defaultNoteText;$(this).children('div').html(text);$(this).children('textarea').val(text);});},/*** getNotes() will fetch user's notes for this entity (all slides) from sever* and populate the slides.**/getNotes: function() {var entityId = this.getEntityId();jQuery.ajax({type: "GET",url: "/my-notes/" + entityId,success: function(notesJSON) {var notes = eval(notesJSON);myNotes.setNotes(notes);myNotes.populateNotes();}});},/*** saveNote() sends server request to save newly entered/modified note* for a particular slide**/saveNote: function(slideId, note) {console.log(slideId, note);var entityId = this.getEntityId();jQuery.ajax({type: "POST",url: "/save-note/",data: ({entity: entityId, slide: slideId, note: note}),success: function(msg) {myNotes.setNoteForSlide(slideId, note);var myNotesPane = $('#mynotes-' + slideId);$(myNotesPane).children('.note-display').html(note).show();$(myNotesPane).children('.note-input').hide();}});}};/*** Loading user's notes for this product (entity)**/myNotes.getNotes();/** Event Handlers **//*** When user clicks view pane, it will be transforms into a textarea**/$('.mynotes-section .note-display').click(function(){$(this).hide().siblings('textarea').show().focus();});/*** When textarea looses focus, a server request will be issued to add/update* note for that slide, if the note text is changed**/$('.mynotes-section .note-input').blur(function(){var slideId = $(this).parent().attr('id').split('-')[1];var text = $.trim($(this).val());if (text != myNotes.defaultNoteText && text != myNotes.getNoteForSlide(slideId)) {myNotes.saveNote(slideId, text);} else {var myNotesPane = $('#mynotes-' + slideId);$(myNotesPane).children('.note-display').show();$(myNotesPane).children('.note-input').hide();}});});