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