-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.note.js
433 lines (364 loc) · 11 KB
/
jquery.note.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/** @file
*
* jQuery class/objects representing a single, user-generated note as well as
* a group of one or more comments.
*
* Requires:
* jquery.js
* jquery.user.js
*/
/*jslint nomen:false,laxbreak:true,white:false,onevar:false */
/*global jQuery:false */
(function($) {
/********************************************************
* Unique array sorting
*
*/
var hasDuplicates = false;
var sortCompare = function( a, b ) {
if ( a === b )
{
hasDuplicates = true;
return 0;
}
return ( (a < b) ? -1 : 1 );
};
/** @brief Perform a unique sort on the given array.
* @param ar The array to sort;
*
* @return The sorted array with duplicates removed.
*/
$.uniqueSort = function( ar ) {
hasDuplicates = false;
ar.sort( sortCompare );
if (hasDuplicates)
{
for (var idex = 1; idex < ar.length; idex++)
{
if (ar[idex] === ar[idex - 1])
{
ar.splice( idex--, 1 );
}
}
}
return ar;
};
/******************************************************************************
* Note
*
*/
/** @brief A note comprised of one or more comments and tags. */
$.Note = function(props) {
var defaults = {
id: null,
comments: [],
tags: null
};
return this.init( $.extend(defaults, true, props || {}) );
};
$.Note.prototype = {
/** @brief Initialize a new Note instance.
* @param props The properties of this instance:
* id: The unique id of this instance;
* comments: An array of $.Comment objects,
* serialized $.Comment objects, or empty
* to initialize an empty set;
* tags: An array of tag strings;
*/
init: function(props) {
var self = this;
self.props = {};
$.each(props, function(key,val) {
self.set(key, val);
});
return self;
this.props = props;
var commentInstances = [];
$.each(this.props.comments, function() {
var comment = this;
if ($.isPlainObject(comment)) { comment = new $.Comment(comment); }
commentInstances.push( comment );
});
if (commentInstances.length < 1)
{
// Create a single, empty comment
commentInstances.push( new $.Comment() );
}
this.props.comments = commentInstances;
return this;
},
/** @brief Generic getting of a property.
* @param key The property to get;
*
* @return The value of the property (or undefined if invalid).
*/
get: function(key)
{
var res;
var method = 'get'+ key[0].toUpperCase() + key.substr(1);
if ($.isFunction( self[method] ))
{
res = self[method](val);
}
return res;
},
/** @brief Generic setting of a property.
* @param key The property to set;
* @param val The property value;
*/
set: function(key, val)
{
var self = this;
var method = 'set'+ key[0].toUpperCase() + key.substr(1);
if ($.isFunction( self[method] ))
{
self[method](val);
}
},
getId: function() { return this.props.id; },
getComments: function() { return this.props.comments; },
getCommentCount:function() { return this.props.comments.length; },
getTags: function()
{
var self = this;
//if (! self.props.tags)
{
// Create a full set of tags from all comments.
self.props.tags = [];
$.each(self.props.comments, function() {
self.props.tags = self.props.tags.concat( this.getTags() );
});
self.props.tags = $.uniqueSort( self.props.tags );
}
return self.props.tags;
},
getTagCount: function()
{
return this.getTags().length;
},
/** @brief (Re)set the id.
* @param id The new id;
*/
setId: function(id)
{
this.props.id = id;
},
/** @brief (Re)set all comments.
* @param comments An array of $.Comment instances
* (or serialized versions);
*/
setComments: function(comments)
{
var self = this;
self.props.comments = [];
self.props.tags = [];
$.each(comments, function() {
self.addComment(this);
});
if (self.props.comments.length < 1)
{
// Create a single, empty comment
self.addComment( new $.Comment() );
}
},
/** @brief Add a new comment to this note.
* @param comment A $.Comment instance or properties to create one.
*
* @return The comment instance that was added.
*/
addComment: function(comment) {
if ($.isPlainObject(comment)) { comment = new $.Comment(comment); }
this.props.tags = $.uniqueSort( this.getTags()
.concat( comment.getTags() ) );
this.props.comments.push(comment);
return comment;
},
/** @brief Remove a comments from this note.
* @param comment A $.Comment instance to remove.
*
* @return this for a fluent interface.
*/
removeComment: function(comment) {
var self = this;
if (comment instanceof $.Comment)
{
var targetIdex = -1;
$.each(self.props.comments, function(idex) {
if (this === comment)
{
targetIdex = idex;
return false;
}
});
if (targetIdex >= 0)
{
self.props.comments.splice(targetIdex, 1);
comment.destroy();
/* Clear our 'tags' since removing exactly this set isn't
* straight forward. If the tags are needed, they will be
* recreated on the next call to getTags().
*/
self.props.tags = null;
}
}
return self;
},
/** @brief Return a serialized version of this instance suitable
* for creating a duplicate instance via our constructor.
*
* @return The serialized version
*/
serialize: function() {
var serialized = {
id: this.props.id,
comments: [],
tags: this.getTags()
};
$.each(this.props.comments, function() {
serialized.comments.push( this.serialize() );
});
return serialized;
},
/** @brief Destroy this instance.
*/
destroy: function() {
var self = this;
var props = self.props;
if ($.isArray(props.comments))
{
$.each(props.comments, function() {
this.destroy();
});
}
delete props.comments;
delete props.tags;
}
};
/******************************************************************************
* Comment
*
*/
/** @brief A single, attributable comment.
* @param props The properties of this comment:
* author: The $.User instance or serialize $.User
* representing the author of this comment;
* text: The text of this comment;
* created: The date/time the comment was created;
*/
$.Comment = function(props) {
var defaults = {
author: null,
text: '',
tags: [],
created:new Date()
};
return this.init( $.extend(defaults, true, props || {}) );
};
$.Comment.prototype = {
/** @brief Initialize a new Comment instance.
* @param props The properties of this comment:
* author: The Unique ID of the author
* text: The text of this comment
* created: The date/time the comment was created
*/
init: function(props) {
var self = this;
self.props = {};
$.each(props, function(key,val) {
self.set(key, val);
});
return self;
},
/** @brief Generic getting of a property.
* @param key The property to get;
*
* @return The value of the property (or undefined if invalid).
*/
get: function(key)
{
var res;
var method = 'get'+ key[0].toUpperCase() + key.substr(1);
if ($.isFunction( self[method] ))
{
res = self[method](val);
}
return res;
},
/** @brief Generic setting of a property.
* @param key The property to set;
* @param val The property value;
*/
set: function(key, val)
{
var self = this;
var method = 'set'+ key[0].toUpperCase() + key.substr(1);
if ($.isFunction( self[method] ))
{
self[method](val);
}
},
getAuthor: function() { return this.props.author; },
getText: function() { return this.props.text; },
getTags: function() { return this.props.tags; },
getTagCount:function() { return this.props.tags; },
getCreated: function() { return this.props.created; },
setAuthor: function(author)
{
if ( (! author) || (! (author instanceof $.User)) )
{
author = new $.User( author );
}
this.props.author = author;
},
setText: function(text)
{
var tags = [];
this.props.text = text;
if (text)
{
var matches = text.match(/#[\w\._\-]+/g);
$.each(matches, function(idex, str) {
tags.push( str.substr(1) );
});
}
this.props.tags = tags;
},
setCreated: function(created)
{
if (! created) { return; }
if (! (created instanceof Date))
{
try {
created = new Date(created);
} catch(e) {}
}
this.props.created = created;
},
/** @brief Return a serialized version of this instance suitable
* for creating a duplicate instance via our constructor.
*
* @return The serialized version.
*/
serialize: function() {
var serialized = {
author: (this.props.author
? this.props.author.serialize()
: null),
text: this.props.text,
created:this.props.created
};
return serialized;
},
destroy: function() {
var self = this;
var props = self.props;
if (props.author && (props.author instanceof $.User))
{
props.author.destroy();
}
delete props.author;
delete props.text;
delete props.created;
}
};
}(jQuery));