Subversion Repositories SmartDukaan

Rev

Rev 2659 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2659 Rev 2672
Line 2... Line 2...
2
    /**
2
    /**
3
     * My Notes core method library
3
     * My Notes core method library
4
     **/
4
     **/
5
    var myNotes = {
5
    var myNotes = {
6
    	/**
6
    	/**
-
 
7
    	 * Properties
7
    	 * Object containing slideIds and corresponding notes as key-value pairs
8
    	 *  - notes: Object containing slideIds and corresponding notes as key-value pairs
-
 
9
    	 *  - defaultNoteText will be displayed if user has not saved a note on a slide
8
    	 **/
10
    	 **/
9
    	notes: null,
11
    	notes: null,
10
    	
12
    	
11
    	/**
-
 
12
    	 * defaultNoteText will be displayed if user has not saved a note on a slide
-
 
13
    	 **/
-
 
14
    	defaultNoteText: 'Click here to add notes',
13
    	defaultNoteText: 'Click here to add notes',
-
 
14
    	
-
 
15
    	sanitize: function(str) {
-
 
16
    	    return str.replace(/\n/g, " ");
-
 
17
    	},
15
 
18
		
16
    	/**
19
    	/**
17
    	 * getEntityId() will return Entity ID for the current product
20
    	 * getEntityId() will return Entity ID for the current product
18
    	 **/
21
    	 **/
19
    	getEntityId: function()	{
22
    	getEntityId: function()	{
20
    		return $('#product_id').val();
23
    		return $('#product_id').val();
21
    	},
24
    	},
22
    	
25
    	
-
 
26
    	getSlideId: function(notesSectionElement)	{
-
 
27
    		var cssClass = $(notesSectionElement).attr("class");
-
 
28
    		
-
 
29
    		idIndex = cssClass == 'mynotes-section' ? 1 : 3;
-
 
30
    		return $(notesSectionElement).attr('id').split('-')[idIndex];
-
 
31
    	},
-
 
32
    	
23
    	/**
33
    	/**
24
    	 * setNotes(notes) will accept an object and set this.notes
34
    	 * setNotes(notes) will accept an object and set this.notes
25
    	 **/
35
    	 **/
26
    	setNotes: function(notes)	{
36
    	setNotes: function(notes)	{
27
    		this.notes = notes;
37
    		this.notes = notes;
Line 41... Line 51...
41
    	populateNotes: function()	{
51
    	populateNotes: function()	{
42
			var notes = this.notes;
52
			var notes = this.notes;
43
			
53
			
44
			$('.mynotes-section').each(function(){
54
			$('.mynotes-section').each(function(){
45
				var slideId = $(this).attr('id').split('-')[1];
55
				var slideId = $(this).attr('id').split('-')[1];
-
 
56
				
46
				var text = notes.hasOwnProperty(slideId) ? notes[slideId] : myNotes.defaultNoteText;
57
				if(notes.hasOwnProperty(slideId) && $.trim(notes[slideId]) != "")	{
-
 
58
					
-
 
59
					$(this).children('div').html(notes[slideId]);
-
 
60
					$(this).children('textarea').val(notes[slideId]);
-
 
61
					
-
 
62
				} else	{
-
 
63
					$(this).children('div').html(myNotes.defaultNoteText);
-
 
64
				}
-
 
65
			});
-
 
66
    	},
-
 
67
    	
-
 
68
    	populateColorbox: function(){
-
 
69
    		var notes = this.notes;
-
 
70
			
-
 
71
			$('.mynotes-colorbox-section').each(function(){
-
 
72
				var slideId = $(this).attr('id').split('-')[3];
-
 
73
				
-
 
74
				if(notes.hasOwnProperty(slideId) && $.trim(notes[slideId]) != "")	{
-
 
75
					
47
				$(this).children('div').html(text);
76
					$(this).children('div').html(notes[slideId]);
48
				$(this).children('textarea').val(text);
77
					$(this).children('textarea').val(notes[slideId]);
-
 
78
					
-
 
79
				} else	{
-
 
80
					$(this).children('div').html(myNotes.defaultNoteText);
-
 
81
				}
49
			});
82
			});
50
    	},
83
    	},
51
    	
84
    	
52
    	/**
85
    	/**
53
    	 * getNotes() will fetch user's notes for this entity (all slides) from sever
86
    	 * getNotes() will fetch user's notes for this entity (all slides) from sever
Line 78... Line 111...
78
    			type: "POST",
111
    			type: "POST",
79
    			url: "/save-note/",
112
    			url: "/save-note/",
80
    			data: ({entity: entityId, slide: slideId, note: note}),
113
    			data: ({entity: entityId, slide: slideId, note: note}),
81
    			success: function(msg)	{
114
    			success: function(msg)	{
82
    				myNotes.setNoteForSlide(slideId, note);
115
    				myNotes.setNoteForSlide(slideId, note);
83
    	    		var myNotesPane = $('#mynotes-' + slideId);
-
 
84
    	    		$(myNotesPane).children('.note-display').html(note).show();
-
 
85
    	    		$(myNotesPane).children('.note-input').hide();
-
 
86
    			}
116
    			}
87
    		});
117
    		});
88
    	}
118
    	}
89
    };
119
    };
90
    /**
120
    /**
91
     * Loading user's notes for this product (entity) 
121
     * Loading user's notes for this product (entity) 
92
     **/
122
     **/
93
    myNotes.getNotes();
123
    myNotes.getNotes();
94
 
124
 
95
    
125
    
96
    /** Event Handlers **/
-
 
97
    
-
 
98
    /**
126
    /**
-
 
127
     ******************** Event Handlers **********************
99
     * When user clicks view pane, it will be transforms into a textarea
128
     * When user clicks view pane, it will be transforms into a textarea
-
 
129
     * 
-
 
130
     * When textarea looses focus, a server request will be issued to add/update
-
 
131
     * note for that slide, if the note text is changed
100
     **/
132
     **/
101
    $('.mynotes-section .note-display').click(function(){
133
    $('.mynotes-section div, .mynotes-colorbox-section div').click(function(){
102
    	$(this).hide().siblings('textarea').show().focus();
134
    	$(this).hide().siblings('textarea').show().focus();
103
    });
135
    });
104
    
136
    
105
    /**
-
 
106
     * When textarea looses focus, a server request will be issued to add/update
137
    $('.mynotes-section textarea, .mynotes-colorbox-section textarea').blur(function(){
107
     * note for that slide, if the note text is changed
138
    	var slideId = myNotes.getSlideId($(this).parent());
108
     **/
139
    	
109
    $('.mynotes-section .note-input').blur(function(){
-
 
110
    	var slideId = $(this).parent().attr('id').split('-')[1];
-
 
111
    	var text = $.trim($(this).val());
140
    	var text = $.trim($(this).val());
112
    	
141
    	
113
    	if (text != myNotes.defaultNoteText && text != myNotes.getNoteForSlide(slideId))	{
142
    	if (text != "" && text != myNotes.defaultNoteText && text != myNotes.getNoteForSlide(slideId))	{
-
 
143
    		var note = myNotes.sanitize(text);
-
 
144
    		
114
    		myNotes.saveNote(slideId, text);
145
    		myNotes.saveNote(slideId, note);
-
 
146
    		
-
 
147
    		$(this).siblings('div').html(note).show();
-
 
148
 
115
    	} else	{
149
    	} else	{
116
    		var myNotesPane = $('#mynotes-' + slideId);
-
 
117
    		$(myNotesPane).children('.note-display').show();
150
    		$(this).siblings('div').show();
118
    		$(myNotesPane).children('.note-input').hide();
-
 
119
    	}
151
    	}
-
 
152
		$(this).hide();
-
 
153
    });
-
 
154
    
-
 
155
    $('#view-mynotes').click(function(){
-
 
156
    	var mynotesColorbox = $('#mynotes-colorbox');
-
 
157
    	
-
 
158
    	$('#view-mynotes').colorbox({
-
 
159
    		width:"670px",
-
 
160
    		height:"485px",
-
 
161
    		inline: true,
-
 
162
    		href:"#mynotes-colorbox",
-
 
163
    		
-
 
164
    		onComplete: function(){
-
 
165
    			mynotesColorbox.show();
-
 
166
    			myNotes.populateColorbox();
-
 
167
    		},
-
 
168
    		onCleanup: function(){
-
 
169
    			mynotesColorbox.hide();
-
 
170
    		}
-
 
171
    	});
120
    });
172
    });
121
});
173
});
122
174