-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.notes.js
618 lines (497 loc) · 16.2 KB
/
ui.notes.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/** @file
*
* Provide a UI for $.Notes and $.Note.
*
* The DOM element used to create a new ui.notes instance MAY have an 'id'
* attribute or the 'id' MAY be passed as an option.
*
* Requires:
* jquery.js
* jquery.notes.js
* ui.core.js
* ui.widget.js
*/
/*jslint nomen:false,laxbreak:true,white:false,onevar:false */
/*global jQuery:false */
(function($) {
/*****************************************************************************
* A UI widget for $.Notes
*
*/
$.widget('ui.notes', {
version: '0.0.1',
/* Change the prefix used by ui.widget._trigger() for events so we can bind
* to events like 'notes-change' instead of 'noteschange'.
*/
widgetEventPrefix: 'notes-',
options: {
notes: null, /* The associated $.Notes instance. May initially
* be a serialized version of a $.Notes
*/
// The selector for the container of notes widgets
container: '.notes-pane',
// Positioning information
position: {
my: 'top',
at: 'top',
of: null, /* The selector for the element we should sync with
* (e.g. the tagged/selected text within a
* sentence).
*/
using: null // A movement function (defaults to animate())
},
animSpeed: 200, // The speed (in ms) of animations
hidden: false, // Initially hidden?
// Template selector
template: '#tmpl-notes'
},
/** @brief Initialize a new instance.
*
* @triggers (with a 'notes-' prefix):
* 'change' -- 'noteAdded'
* 'change' -- 'noteRemoved'
* 'change' -- 'noteSaved'
*
* 'destroyed'
*/
_init: function() {
var self = this;
var opts = self.options;
self._isInitializing = true;
self.$container = $( opts.container );
if ( $.isPlainObject(opts.notes) )
{
if ((opts.notes.id === undefined) || (opts.notes.id === null))
{
opts.notes.id = self.element.attr('id');
}
// Generate a new $.Notes instance...
opts.notes = new $.Notes( opts.notes );
}
self._widgetCreate()
._bindEvents();
self._isInitializing = false;
return self;
},
/** @brief Return the id of our $.Notes instance.
*
* @return The id.
*/
id: function() {
var self = this;
var opts = self.options;
return opts.notes.getId();
},
/** @brief Return the number of $.Note instances.
*
* @return The count.
*/
notesCount: function() {
var self = this;
var opts = self.options;
return opts.notes.getNotesCount();
},
/** @brief Given a new $.Note instance, add the note to our container.
* @param note The new $.Note instance.
*
* @return this for a fluent interface.
*/
addNote: function(note) {
var self = this;
var opts = self.options;
// Create and append a new ui.note widget
var $note = $('<div />').note({note:note});
self.$body.append( $note );
if (self._isInitializing !== true)
{
var note = $note.note('option', 'note');
opts.notes.addNote(note);
self._trigger('change', null, 'noteAdded');
}
return self;
},
/** @brief Given a ui.Note widget, remove the note from our container.
* @param $note The jQuery DOM element that has a ui.Note widget
* attached.
*
* @return this for a fluent interface.
*/
removeNote: function($note) {
var self = this;
var opts = self.options;
// Create and append a new ui.note widget
var note = $note.note('option', 'note');
opts.notes.removeNote(note);
self._trigger('change', null, 'noteRemoved');
return self;
},
/** @brief Mark this instance as 'active'
*/
activate: function() {
var self = this;
var opts = self.options;
if (self.element.hasClass('notes-active')) { return; }
var $reply = self.$buttons.filter('[name=reply]');
if ((! self.$reply.hasClass('hint')) &&
(self.$reply.val().length > 0))
{
self.$buttons.show();
$reply.button('enable');
}
else
{
self.$buttons.hide();
$reply.button('disable');
self.$reply.addClass('hint')
.val( self.$reply.attr('title') );
}
self.element.addClass('notes-active', opts.animSpeed, function() {
//self.$reply.focus();
});
},
/** @brief Mark this instance as 'inactive'
*/
deactivate: function() {
var self = this;
var opts = self.options;
if (! self.element.hasClass('notes-active')) { return; }
// Cancel any note that is currently being edited
self.element.find('.note .buttons [name=cancel]').click();
self.element.removeClass('notes-active', opts.animSpeed);
},
/** @brief Show this notes container.
*/
show: function() {
var self = this;
var opts = self.options;
self.element
.fadeIn(opts.animSpeed)
.position( opts.position );
},
/** @brief Hide this notes container.
*/
hide: function(cb) {
var self = this;
var opts = self.options;
self.element
.fadeOut(opts.animSpeed, cb);
},
/** @brief Focus on the input area.
*/
focus: function() {
var self = this;
var opts = self.options;
self.$reply.trigger('focus');
},
/** @brief Return a serialized version of our underlying $.Notes instance.
*
* @return A serialized version of our underlying $.Notes instance.
*/
serialize: function() {
var self = this;
var opts = self.options;
return opts.notes.serialize();
},
/** @brief Destroy this widget. */
destroy: function() {
var self = this;
var opts = self.options;
self._unbindEvents()
.hide(function() {
self._widgetDestroy();
opts.notes.destroy();
});
},
/*******************************
* Private methods
*
*/
/** @brief Actually create our widget along with any sub-widgets
*/
_widgetCreate: function() {
var self = this;
var opts = self.options;
if (opts.position.using === null)
{
opts.position.using = function( to ) {
$(this).animate( {top: to.top}, opts.animSpeed );
};
}
self.element
.addClass('notes ui-corner-all')
.append( $( opts.template ).tmpl() )
.appendTo( self.$container );
if (opts.hidden === true)
{
self.element.hide();
}
else
{
self.element.position( opts.position );
}
self.$body = self.element.find('.notes-body');
self.$reply = self.element.find('.notes-reply');
self.$buttons = self.element.find('.notes-input-pane .buttons button');
self.$buttons.button();
// Generate a ui.Note widget for each current $.Note instance
$.each(opts.notes.getNotes(), function() {
self.addNote( this, true );
});
return self;
},
/** @brief Destroy this widget along with any sub-widgets
*/
_widgetDestroy: function() {
var self = this;
var opts = self.options;
// Destroy all contained note instances
self.$body.find('.note').note('destroy');
self.$buttons.button('destroy');
self.element.empty();
return self;
},
_bindEvents: function() {
var self = this;
var opts = self.options;
self._docClick = function(e) {
var $target = $(e.target);
if ($target !== self.element)
{
self.deactivate();
}
};
/*****************************************************
* General click handler for document. If we see
* this event, deactivate this notes widget.
*
*/
$(document).bind('click.ui-notes', self._docClick);
/*****************************************************
* Handle button clicks (reply/cancel) within the
* input pane.
*
*/
self.element.delegate('.notes-input-pane button',
'click.ui-notes', function(e) {
var $button = $(this);
switch ($button.attr('name'))
{
case 'reply':
var note = new $.Note({
text: self.$reply.val()
});
self.addNote(note);
self.$reply.val('');
break;
case 'cancel':
self.$reply.val('');
self.$reply.blur();
//self.deactivate();
break;
}
});
/*****************************************************
* Handle 'keyup' in the reply area.
*
* Enable/Disale the reply button based upon whether
* the new content is empty.
*
*/
self.$reply.bind('keyup.ui-notes', function(e) {
var $reply = self.$buttons.filter('[name=reply]');
if ((! self.$reply.hasClass('hint')) &&
(self.$reply.val().length > 0))
{
$reply.button('enable');
}
else
{
$reply.button('disable');
}
});
/*****************************************************
* Handle click-to-activate
*
*/
self.element.bind('click.ui-notes', function(e) {
self.activate();
return false;
});
/*****************************************************
* Handle the deletion of contained notes
*
*/
self.element.delegate('.note', 'note-destroyed', function(e, note) {
var $note = $(e.target);
self.removeNote($note);
});
/*****************************************************
* Handle focus/blur for the reply element
*
*/
self.$reply.bind('focus', function(e) {
self.$buttons.show();
var $reply = self.$buttons.filter('[name=reply]');
if (self.$reply.hasClass('hint'))
{
self.$reply.val('')
.removeClass('hint');
}
if (self.$reply.val().length > 0)
{
$reply.button('enable');
}
else
{
$reply.button('disable');
}
});
self.$reply.bind('blur', function(e) {
var $reply = self.$buttons.filter('[name=reply]');
if (self.$reply.val().length > 0)
{
$reply.button('enable');
}
else
{
self.$reply.addClass('hint')
.val( self.$reply.attr('title') );
self.$buttons.hide();
$reply.button('disable');
}
});
return self;
},
_unbindEvents: function() {
var self = this;
var opts = self.options;
self.element.unbind('.ui-notes');
self.element.undelegate('.notes-input-pane button', '.ui-notes');
self.$reply.unbind('.ui-notes');
$(document).unbind('click.ui-notes', self._docClick);
return self;
}
});
/*****************************************************************************
* A UI widget for $.Note
*
*/
$.widget('ui.note', {
version: '0.0.1',
/* Change the prefix used by ui.widget._trigger() for events so we can bind
* to events like 'note-change' instead of 'notechange'.
*/
widgetEventPrefix: 'note-',
options: {
note: null, /* The associated $.Note instance. May initially
* be a serialized version of a $.Note
*/
// Template Selector
template: '#tmpl-note'
},
/** @brief Return a serialized version of our underlying $.Notes instance.
*
* @return A serialized version of our underlying $.Notes instance.
*/
serialize: function() {
var self = this;
var opts = self.options;
return opts.note.serialize();
},
/** @brief Destroy this widget. */
destroy: function() {
var self = this;
var opts = self.options;
self._unbindEvents()
._widgetDestroy();
// Notify our container that this note has been destroyed.
self._trigger('destroyed', null, opts.note);
//self.element.trigger('destroyed', opts.note);
},
/*******************************
* Private methods
*
*/
/** @brief Initialize a new instance.
*/
_init: function() {
var self = this;
var opts = self.options;
if ( $.isPlainObject(opts.note) )
{
// Generate a new $.Note instance
opts.note = new $.Notes( opts.notes );
}
self._widgetCreate()
._bindEvents();
return self;
},
/** @brief Actually create our widget along with any sub-widgets
*/
_widgetCreate: function() {
var self = this;
var opts = self.options;
self.element
.addClass('note')
.append( $( opts.template ).tmpl( {note: opts.note} ) );
self.$comment = self.element.find('.comment');
self.$editArea = self.element.find('.edit');
self.$edit = self.$editArea.find('textarea');
self.$buttons = self.element.find('.buttons button');
self.$mainButtons = self.element.find('.buttons:last');
self.$buttons.button();
return self;
},
/** @brief Destroy this widget along with any sub-widgets
*/
_widgetDestroy: function() {
var self = this;
var opts = self.options;
self.$buttons.button('destroy');
self.element.empty();
return self;
},
_bindEvents: function() {
var self = this;
var opts = self.options;
self.$buttons.bind('click.ui-note', function(e) {
var $button = $(this);
switch ($button.attr('name'))
{
case 'edit':
self.$edit.val( self.$comment.text() );
self.$comment.hide();
self.$mainButtons.css('display', 'none');
self.$editArea.show();
self.$edit.focus();
break;
case 'delete':
//self.destroy();
self.element.slideUp(function() {
self.element.remove();
});
break;
case 'save':
// Save any changes in self.$edit
opts.note.setText( self.$edit.val() );
self.$comment.text( opts.note.getText() );
self._trigger('change', null, 'noteSaved');
// Fall-through to 'cancel' to restore non-edit mode
case 'cancel':
// Cancel 'edit'
self.$editArea.hide();
self.$comment.show();
self.$mainButtons.css('display', '');
break;
}
});
return self;
},
_unbindEvents: function() {
var self = this;
var opts = self.options;
self.$buttons.unbind('.ui-note');
return self;
}
});
}(jQuery));