Subversion Repositories SmartDukaan

Rev

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

Rev 2672 Rev 2718
Line 5... Line 5...
5
    var myNotes = {
5
    var myNotes = {
6
    	/**
6
    	/**
7
    	 * Properties
7
    	 * Properties
8
    	 *  - notes: 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
9
    	 *  - defaultNoteText will be displayed if user has not saved a note on a slide
-
 
10
    	 *  - maxNoteLength indicates the max number of characters allowed in each note
-
 
11
    	 *  - maxNoteLengthWarning is the template message which is show to user while he's typing
10
    	 **/
12
    	 **/
11
    	notes: null,
13
    	notes: null,
12
    	
14
    	
13
    	defaultNoteText: 'Click here to add notes',
15
    	defaultNoteText: 'Click here to add notes',
14
    	
16
    	
-
 
17
    	maxNoteLength: 400,
-
 
18
    	
-
 
19
    	maxNoteLengthWarning: '# chars remaining',
-
 
20
    	
-
 
21
    	/**
-
 
22
    	 * sanitize() will remove new line character from the string passed as parameter.
-
 
23
    	 * This removal is required as any new line char in JSON leads to unexpected
-
 
24
    	 * execution the code upon eval()
-
 
25
    	 **/
15
    	sanitize: function(str) {
26
    	sanitize: function(str) {
16
    	    return str.replace(/\n/g, " ");
27
    	    return str.replace(/\n/g, " ");
17
    	},
28
    	},
18
		
29
		
19
    	/**
30
    	/**
Line 21... Line 32...
21
    	 **/
32
    	 **/
22
    	getEntityId: function()	{
33
    	getEntityId: function()	{
23
    		return $('#product_id').val();
34
    		return $('#product_id').val();
24
    	},
35
    	},
25
    	
36
    	
-
 
37
    	/**
-
 
38
    	 * getSlideId() returns slideId associated with a particular notes section
-
 
39
    	 **/
26
    	getSlideId: function(notesSectionElement)	{
40
    	getSlideId: function(notesSectionElement)	{
27
    		var cssClass = $(notesSectionElement).attr("class");
41
    		var cssClass = $(notesSectionElement).attr("class");
28
    		
42
    		// NOTE: idIndex will depend on the structure of cssClass
29
    		idIndex = cssClass == 'mynotes-section' ? 1 : 3;
43
    		idIndex = cssClass == 'mynotes-section' ? 1 : 2;
30
    		return $(notesSectionElement).attr('id').split('-')[idIndex];
44
    		return $(notesSectionElement).attr('id').split('-')[idIndex];
31
    	},
45
    	},
32
    	
46
    	
33
    	/**
47
    	/**
34
    	 * setNotes(notes) will accept an object and set this.notes
48
    	 * setNotes(notes) will accept an object and set this.notes
35
    	 **/
49
    	 **/
36
    	setNotes: function(notes)	{
50
    	setNotes: function(notes)	{
37
    		this.notes = notes;
51
    		this.notes = notes;
38
    	},
52
    	},
39
 
53
 
-
 
54
    	/**
-
 
55
    	 * getNoteForSlide() returns note associated with a given slide
-
 
56
    	 **/
40
    	getNoteForSlide: function(slideId)	{
57
    	getNoteForSlide: function(slideId)	{
41
    		return this.notes.hasOwnProperty(slideId) ? this.notes[slideId] : null;
58
    		return this.notes.hasOwnProperty(slideId) ? this.notes[slideId] : null;
42
    	},
59
    	},
43
    	
60
    	
-
 
61
    	/**
-
 
62
    	 * getNoteForSlide() returns note associated with a given slide
-
 
63
    	 **/
44
    	setNoteForSlide: function(slideId, note){
64
    	setNoteForSlide: function(slideId, note){
45
    		this.notes[slideId] = note;
65
    		this.notes[slideId] = note;
46
    	},
66
    	},
47
    	
67
    	
48
    	/**
68
    	/**
Line 67... Line 87...
67
    	
87
    	
68
    	populateColorbox: function(){
88
    	populateColorbox: function(){
69
    		var notes = this.notes;
89
    		var notes = this.notes;
70
			
90
			
71
			$('.mynotes-colorbox-section').each(function(){
91
			$('.mynotes-colorbox-section').each(function(){
72
				var slideId = $(this).attr('id').split('-')[3];
92
				var slideId = myNotes.getSlideId($(this));
73
				
93
				
74
				if(notes.hasOwnProperty(slideId) && $.trim(notes[slideId]) != "")	{
94
				if(notes.hasOwnProperty(slideId) && $.trim(notes[slideId]) != "")	{
75
					
95
					
76
					$(this).children('div').html(notes[slideId]);
96
					$(this).children('div').html(notes[slideId]);
77
					$(this).children('textarea').val(notes[slideId]);
97
					$(this).children('textarea').val(notes[slideId]);
Line 89... Line 109...
89
    	getNotes: function()	{
109
    	getNotes: function()	{
90
    		var entityId = this.getEntityId();
110
    		var entityId = this.getEntityId();
91
    		
111
    		
92
    		jQuery.ajax({
112
    		jQuery.ajax({
93
    			type: "GET",
113
    			type: "GET",
94
    			url: "/my-notes/" + entityId,
114
    			url: "/my-notes/" + entityId + "-json",
95
    			success: function(notesJSON)	{
115
    			success: function(notesJSON)	{
96
    				var notes = eval(notesJSON);
116
    				var notes = eval(notesJSON);
97
    				myNotes.setNotes(notes);
117
    				myNotes.setNotes(notes);
98
    				myNotes.populateNotes();
118
    				myNotes.populateNotes();
99
    			}
119
    			}
100
    		});
120
    		});
101
    	},
121
    	},
102
    	
122
    	
-
 
123
    	limitNoteLength: function(textareaElement)	{
-
 
124
    		var currentNoteLength = $(textareaElement).val().length;
-
 
125
        	
-
 
126
        	if (this.maxNoteLength < currentNoteLength)	{
-
 
127
        		$(textareaElement).val($(textareaElement).val().substr(0, myNotes.maxNoteLength));
-
 
128
        		var msg = this.maxNoteLengthWarning.replace(/#/g, 0);
-
 
129
        	} else	{
-
 
130
        		var msg = this.maxNoteLengthWarning.replace(/#/g, myNotes.maxNoteLength - currentNoteLength);
-
 
131
        	}
-
 
132
        	$(textareaElement).siblings('p').children('span').html(msg + ' (Max ' + this.maxNoteLength + ')');
-
 
133
    	},
-
 
134
    	
103
    	/**
135
    	/**
104
    	 * saveNote() sends server request to save newly entered/modified note
136
    	 * saveNote() sends server request to save newly entered/modified note
105
    	 * for a particular slide
137
    	 * for a particular slide
106
    	 **/
138
    	 **/
107
    	saveNote: function(slideId, note)	{
139
    	saveNote: function(slideId, note)	{
Line 113... Line 145...
113
    			data: ({entity: entityId, slide: slideId, note: note}),
145
    			data: ({entity: entityId, slide: slideId, note: note}),
114
    			success: function(msg)	{
146
    			success: function(msg)	{
115
    				myNotes.setNoteForSlide(slideId, note);
147
    				myNotes.setNoteForSlide(slideId, note);
116
    			}
148
    			}
117
    		});
149
    		});
-
 
150
    	},
-
 
151
    	
-
 
152
    	updateNoteInViews: function(slideId, note)	{
-
 
153
    		$('#mynotes-' + slideId + ', #mynotes-colorbox-' + slideId).children('div').html(note);
118
    	}
154
    	}
119
    };
155
    };
120
    /**
156
    /**
121
     * Loading user's notes for this product (entity) 
157
     * Loading user's notes for this product (entity) 
122
     **/
158
     **/
Line 128... Line 164...
128
     * When user clicks view pane, it will be transforms into a textarea
164
     * When user clicks view pane, it will be transforms into a textarea
129
     * 
165
     * 
130
     * When textarea looses focus, a server request will be issued to add/update
166
     * When textarea looses focus, a server request will be issued to add/update
131
     * note for that slide, if the note text is changed
167
     * note for that slide, if the note text is changed
132
     **/
168
     **/
133
    $('.mynotes-section div, .mynotes-colorbox-section div').click(function(){
169
    $('.mynotes-section div, .mynotes-colorbox-section div').live('click', function(){
-
 
170
    	var textareaElement = $(this).hide().siblings('p').show().siblings('textarea');
134
    	$(this).hide().siblings('textarea').show().focus();
171
    	$(textareaElement).show().focus();
-
 
172
    	myNotes.limitNoteLength(textareaElement);
135
    });
173
    });
136
    
174
    
137
    $('.mynotes-section textarea, .mynotes-colorbox-section textarea').blur(function(){
175
    $('.mynotes-section textarea, .mynotes-colorbox-section textarea').live('blur', function(){
138
    	var slideId = myNotes.getSlideId($(this).parent());
176
    	var slideId = myNotes.getSlideId($(this).parent());
139
    	
177
    	
140
    	var text = $.trim($(this).val());
178
    	var text = $.trim($(this).val());
141
    	
179
    	
142
    	if (text != "" && text != myNotes.defaultNoteText && text != myNotes.getNoteForSlide(slideId))	{
180
    	if (text != "" && text != myNotes.defaultNoteText && text != myNotes.getNoteForSlide(slideId))	{
143
    		var note = myNotes.sanitize(text);
181
    		var note = myNotes.sanitize(text);
144
    		
182
    		
145
    		myNotes.saveNote(slideId, note);
183
    		myNotes.saveNote(slideId, note);
146
    		
184
    		myNotes.updateNoteInViews(slideId, note);
147
    		$(this).siblings('div').html(note).show();
185
    		$(this).siblings('div').show();
148
 
186
 
149
    	} else	{
187
    	} else	{
150
    		$(this).siblings('div').show();
188
    		$(this).siblings('div').show();
151
    	}
189
    	}
152
		$(this).hide();
190
		$(this).hide().siblings('p').hide();
-
 
191
		
-
 
192
    }).live('keyup', function(){
-
 
193
    	myNotes.limitNoteLength($(this));
153
    });
194
    });
154
    
195
    
155
    $('#view-mynotes').click(function(){
196
    $('#view-mynotes, a.view-my-notes').live('click', function(){
156
    	var mynotesColorbox = $('#mynotes-colorbox');
-
 
157
    	
-
 
158
    	$('#view-mynotes').colorbox({
197
    	$.colorbox({
159
    		width:"670px",
198
    		width:"670px",
160
    		height:"485px",
199
    		height:"485px",
161
    		inline: true,
200
    		iframe: false,
162
    		href:"#mynotes-colorbox",
201
    		href:"/my-notes/" + myNotes.getEntityId() + "-html",
163
    		
202
    		
164
    		onComplete: function(){
203
    		onComplete: function(){
165
    			mynotesColorbox.show();
204
    			$('#mynotes-colorbox').show();
166
    			myNotes.populateColorbox();
205
    			myNotes.populateColorbox();
167
    		},
206
    		},
168
    		onCleanup: function(){
207
    		onCleanup: function(){
169
    			mynotesColorbox.hide();
208
    			$('#mynotes-colorbox').hide();
170
    		}
209
    		}
171
    	});
210
    	});
172
    });
211
    });
173
});
212
});
174
213