-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnotes.js
More file actions
829 lines (715 loc) · 29.7 KB
/
notes.js
File metadata and controls
829 lines (715 loc) · 29.7 KB
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
* Field Notes plugin
*
* Data attributes:
* - data-control="fieldnotes" - enables the plugin on an element
* - data-option="value" - an option with a value
*
* JavaScript API:
* $('a#someElement').fieldNotes({...})
*/
+function ($) {
"use strict";
var Base = $.wn.foundation.base,
BaseProto = Base.prototype
// FIELD REPEATER CLASS DEFINITION
// ============================
var Notes = function (element, options) {
this.options = options
this.$el = $(element)
this.$createNewNoteButton = this.$el.find('> .row > .toolbar [data-note-add]');
this.$removeNoteButton = this.$el.find('> .row > .toolbar [data-note-remove]');
this.$richEditorTextarea = null;
this.$notification = this.$el.find('> .row > .field-notes-form .field-notes-notification');
this.$savingTimeout = null;
this.$notificationTimeout = null;
this.$isSaving = false;
this.$loadContainer = this.$el.find('> .row > .field-notes-form .loading-indicator-container:first');
// To avoid saving twice, when user using the command to saving
this.$lastSavingNoteData = {
id: 0,
name: '',
content: '',
}
this.$oldHotkeyElements = [];
$.wn.foundation.controlUtils.markDisposable(element)
Base.call(this)
this.init()
}
Notes.prototype = Object.create(BaseProto)
Notes.prototype.constructor = Notes
Notes.DEFAULTS = {
alias: null,
autosaveDelay: 2000,
previewMode: false,
}
Notes.prototype.initRichEditor = function () {
//Because the richeditor will refresh
this.disposeRichEditor();
this.$richEditorTextarea = this.$el.find('> .row > .field-notes-form textarea:first');
if (this.$richEditorTextarea == null || this.$richEditorTextarea.length == 0) return;
if (!this.options.previewMode) {
this.$richEditorTextarea.on('froalaEditor.contentChanged', this.proxy(this.onAutoSavingNote));
this.$richEditorTextarea.on('keydown.oc.richeditor', this.proxy(this.stopDelaySaving));
}
}
Notes.prototype.disposeRichEditor = function () {
if (this.$richEditorTextarea == null || this.$richEditorTextarea.length == 0) return;
if (!this.options.previewMode) {
this.$richEditorTextarea.off('froalaEditor.contentChanged', this.proxy(this.onAutoSavingNote) );
this.$richEditorTextarea.off('keydown.oc.richeditor', this.proxy(this.stopDelaySaving));
this.$richEditorTextarea = null;
}
}
Notes.prototype.init = function () {
this.$el.on('click', '> .row > .field-notes-list > .field-notes-items > .field-notes-item > a', this.proxy(this.onClickNoteItem));
this.$el.on('click', '> .row > .toolbar [data-note-add]', this.proxy(this.clickCreateButton));
this.$el.on('click', '> .row > .toolbar [data-note-remove]', this.proxy(this.clickDeleteButton));
//Before the search bar will change the notes list, need to save the current not first
this.$el.on('focus', '> .row > .toolbar .loading-indicator-container [data-track-input]', this.proxy(this.onSearchInputFocus));
this.$el.on('ajaxDone', '> .row > .toolbar .loading-indicator-container [data-track-input]', this.proxy(this.onSearchResultsRefreshForm));
this.$el.on('focus', '> .row > .toolbar .loading-indicator-container .clear-input-text', this.proxy(this.onSearchInputClearButtonFocus));
this.$el.on('ajaxDone', '> .row > .toolbar [data-note-remove]', this.proxy(this.onNoteRemoveSuccess));
// For note auto saving
// this.$el.on('change', '> .row > .field-notes-form textarea:first', this.proxy(this.onAutoSavingNote));
this.initRichEditor();
this.$el.on('keyup', '> .row > .field-notes-form input:first' , this.proxy(this.onAutoSavingNoteName) );
this.$el.on('keydown', '> .row > .field-notes-form input:first' , this.proxy(this.disableEnterSubmit) );
/*
* Hotkeys
*/
// let hotkeyA = this.$el.find('> .row > .field-notes-form [data-notes-hotkey]');
this.$el.hotKey({
hotkey: 'ctrl+s, cmd+s',
hotkeyVisible: false,
callback: this.proxy(this.onCommandSaving)
})
let self = this;
$(window).load(function(){
// The first time loading, need to refresh the richeditor.
// Because it is invoked by <?= $this->makePartial('note_content'); ?> in _notes.htm
self.setLastSavingNoteData();
self.bindingOldHotkeys();
//Disable the create/delete button when there is only one 'new note' left
self.resetRemoveNoteButtonEnable();
self.resetCreateNewNoteButtonEnable();
});
this.$el.on('dispose-control', this.proxy(this.dispose))
}
Notes.prototype.dispose = function () {
// this.$sortable.sortable('destroy')
this.$el.off('click', '> .row > .field-notes-list > .field-notes-items > .field-notes-item > a', this.proxy(this.onClickNoteItem))
this.$el.off('click', '> .row > .toolbar [data-note-add]', this.proxy(this.clickCreateButton))
this.$el.off('click', '> .row > .toolbar [data-note-remove]', this.proxy(this.clickDeleteButton));
this.$el.off('focus', '> .row > .toolbar .loading-indicator-container [data-track-input]', this.proxy(this.onSearchInputFocus));
this.$el.off('ajaxDone', '> .row > .toolbar .loading-indicator-container [data-track-input]', this.proxy(this.onSearchResultsRefreshForm));
this.$el.off('focus', '> .row > .toolbar .loading-indicator-container .clear-input-text', this.proxy(this.onSearchInputClearButtonFocus));
this.$el.off('ajaxDone', '> .row > .toolbar [data-note-remove]', this.proxy(this.onNoteRemoveSuccess));
this.disposeRichEditor();
this.$el.off('keyup', '> .row > .field-notes-form input:first' , this.proxy(this.onAutoSavingNoteName) );
this.$el.off('keydown', '> .row > .field-notes-form input:first' , this.proxy(this.disableEnterSubmit) );
this.unbindOldHotkeyElements();
this.$el.hotKey('dispose');
this.$el.off('dispose-control', this.proxy(this.dispose));
this.$el.removeData('oc.notes');
this.$el = null
this.options = null
BaseProto.dispose.call(this)
}
// Deprecated
Notes.prototype.unbind = function () {
this.dispose()
}
/**
* Create a new default note when the note list is empty
*/
Notes.prototype.createDefaultNote = function() {
if (this.options.previewMode) {
return;
}
const notesList = this.$el.find('> .row > .field-notes-list');
if (notesList.find('> ul > li').length == 0) {
this.createNewNote(false);
}
}
Notes.prototype.onSearchInputFocus = function(ev) {
if (this.$savingTimeout == null ) return true;
const target = ev.target;
target.blur();
this.finishSavingProcess(function(){
const $target = $(target);
const val = $target.val();
$target.val('');
$target.val(val);
$target.focus();
});
return false;
}
Notes.prototype.onSearchInputClearButtonFocus = function(ev){
if (this.$savingTimeout == null ) return true;
const target = ev.target;
target.blur();
target.disabled = true;
this.finishSavingProcess(function(){
target.focus();
target.disabled = false;
}, function(){
target.disabled = false;
});
return false;
}
/**
* To remove the ctrl+s and command+s in the hotKeyString
* @returns string "" or "ctrl+a"
* @param hotkey hotkey @see input.hotkey.js
*/
Notes.prototype.removeConflictHotKeys = function(hotkey) {
let result = [];
const keys = hotkey.options.hotkey.toLowerCase().split(',');
for (var i = 0, len = keys.length; i < len; i++) {
let keysTrimmed = hotkey.trim(keys[i]);
if (keysTrimmed != 'ctrl+s'
&& keysTrimmed != 'cmd+s'
&& keysTrimmed != 'command+s'
&& keysTrimmed != 'meta+s') {
result.push(keysTrimmed);
}
}
return result.join(',');
}
Notes.prototype.hasConflictHotKeys = function (hotkeyString, specialKey) {
let checklist;
if(specialKey == 'ctrl+s') {
checklist = ['ctrl+s'];
}else if(specialKey == 'cmd+s'){
checklist = ['cmd+s', 'command+s', 'meta+s'];
}else{
checklist = ['ctrl+s', 'cmd+s', 'command+s', 'meta+s'];
}
let result = false;
for(let checkKey of checklist){
if (hotkeyString.includes(checkKey)){
result = true;
break;
}
}
return result;
}
/**
* There are three options:
* 1. Button has same conflict hotkeys: ctrl+s,cmd+s --> can't reinit, just remove callback
* 2. Button has only one hotkey: ctrl+s or cmd+s --> can't reinit, just remove callback
* 3. Button has one or two conflict hotkeys,
* but has other hotkeys: ctrl+s,cmd+p --> reset the new Hotkeys, reinit the hotkey conroller
*
*/
Notes.prototype.bindingOldHotkeys = function() {
if (this.options.previewMode) return;
let $form = this.$el.closest('form');
let self = this;
$form.find('[data-hotkey]').each(function(index,elem)
{
elem = $(elem);
if(elem != self.$el){
let oldHotkeyString = elem.data('hotkey').toLowerCase();
if ( self.hasConflictHotKeys(oldHotkeyString) ){
let hotkey = elem.data('oc.hotkey');
let newHotKeyString = self.removeConflictHotKeys(hotkey);
let oldCallback = hotkey.options.callback;
if(newHotKeyString){
// The button or a has other hotkeys
hotkey.options.hotkey = newHotKeyString;
hotkey.unregisterHandlers();
hotkey.init();
}else{
// can not init a new hotkey, because it is empty
hotkey.options.callback = null;
}
self.$oldHotkeyElements.push({
elem : elem,
callback: oldCallback,
newHotKeyString: newHotKeyString,
oldHotKeyString: oldHotkeyString
});
}
}
});
}
Notes.prototype.unbindOldHotkeyElements = function() {
if (this.options.previewMode) return;
for (let item of this.$oldHotkeyElements){
let elem = item.elem;
let hotkey = elem.data('oc.hotkey');
let oldCallback = item.callback;
if (item.newHotKeyString){
// reinit the hotkey with the old hotkey string
hotkey.options.hotkey = item.oldHotKeyString;
hotkey.unregisterHandlers();
hotkey.init();
}else{
hotkey.options.callback = oldCallback;
}
}
this.$oldHotkeyElements.length = 0;
}
Notes.prototype.testNoteOnFocus = function() {
const focusElem = document.activeElement;
const nameInput = this.$el.find('> .row > .field-notes-form input:first');
if (nameInput[0] == focusElem) {
return true;
}
//control-richeditor
let div = focusElem.parentElement;
if(div) { div = div.parentElement; }
return div && $(div).hasClass('control-richeditor')
}
Notes.prototype.onCommandSaving = function (element, target, ev) {
// Do not need to test conditions;
if (this.testNoteOnFocus()) {
this.stopDelaySaving();
if (this.testSelectedNote()) {
this.showNotification(langNotes.saving + ' ...', false );
this.postNoteData();
}
return false;
}
// call the old binding callback, such as Save button
for (let item of this.$oldHotkeyElements){
let key = ev.originalEvent.ctrlKey ? 'ctl+s' : 'cmd+s';
// Maybe the button or a only binding with ctrl+s or cmd+s
if (this.hasConflictHotKeys(item.oldHotKeyString , key)){
let elem = item.elem;
let hotkey = elem.data('oc.hotkey');
let oldCallback = item.callback;
if (oldCallback && ! (hotkey.options.hotkeyVisible && !hotkey.$el.is(':visible')) ){
oldCallback(elem, target, ev);
}
}
}
return false;
}
/**
* To remove the checked style of the Note List
*/
Notes.prototype.clearNoteListCheckedStyle = function () {
this.$el.find('> .row > .field-notes-list > .field-notes-items > .field-notes-item > a').removeClass('checked');
}
/**
* request the current selected note by note id
* update the note form
* @param jQueryObject item li
* @param bool activeNameFiled If activeNameFiled set true, it will auto set focus on the Name field
*/
Notes.prototype.fetchSelectedNote = function(item , activeNameFiled = true){
const aItem = item.find('a');
const self = this;
this.$loadContainer.loadIndicator();
aItem.request(this.makeEventHandler('onNoteActive'), {
success: function(data, textStatus, jqXHR){
// To do the default success function
this.success(data, textStatus, jqXHR);
//rebinding the editor event
self.initRichEditor();
self.setLastSavingNoteData(self.getNoteData());
self.$loadContainer.loadIndicator('hide');
if(activeNameFiled){
const nameInput = self.$el.find('> .row > .field-notes-form input:first');
//Place the cursor at the end of the input text
const val = nameInput.val();
nameInput.val('');
nameInput.val(val);
nameInput.focus();
}
},
error: function(jqXHR, textStatus, error){
this.error(jqXHR, textStatus, error);
self.$loadContainer.loadIndicator('hide');
}
});
}
Notes.prototype.onSearchResultsRefreshForm = function() {
this.initRichEditor(); // need to bind the events
this.setLastSavingNoteData(this.getNoteData());
this.resetCreateNewNoteButtonEnable();
this.resetRemoveNoteButtonEnable();
}
/**
* This method should be called when the Note form refresh
* Before active another note just like searching, creating and onClick, need to calls this first.
*/
Notes.prototype.finishSavingProcess = function(onSuccessCallback = function(){},
onErrorCallback = function(){}){
if (this.$savingTimeout == null ) {
onSuccessCallback();
return true;
}
//Do it manualy
this.stopDelaySaving();
if (this.testSelectedNote() && this.testSavingConditions()) {
this.showNotification(langNotes.saving + ' ...', false );
this.postNoteData(onSuccessCallback,onErrorCallback);
return false;
}else{
onSuccessCallback();
return true;
}
}
/**
*
* @param event ev window.event
* @param jQueryObject note the HTML a jQuery object
*/
Notes.prototype.onClickNoteItem = function (ev , note) {
const target = (ev) ? $(ev.currentTarget) : note;
if (target.hasClass('checked')) {
return;
}
const self = this;
this.finishSavingProcess(function () {
// modify the css sytle
self.clearNoteListCheckedStyle();
target.addClass('checked');
self.fetchSelectedNote(target.parent());
});
}
/**
* HTML ul scroll to the li
*/
Notes.prototype.notesListScrollToItem = function (item) {
this.$el.find('> .row > .field-notes-list > .field-notes-items').animate({
scrollTop: item.position().top
}, 'slow');
}
/**
* Make Event Handler, same as PHP $this->getEventHandler('xxx')
*/
Notes.prototype.makeEventHandler = function (methodName) {
return this.options.alias + "::" + methodName ;
}
Notes.prototype.clickCreateButton = function (ev) {
ev.preventDefault();
this.createNewNote();
}
Notes.prototype.setCreateNewNoteButtonEnable = function(enable) {
this.$createNewNoteButton.prop('disabled', !enable);
var title = enable ? langNotes.addNew : langNotes.finishSaving;
this.$createNewNoteButton.attr('title', title);
}
Notes.prototype.setRemoveNoteButtonEnable = function (enable) {
this.$removeNoteButton.prop('disabled', !enable);
var title = enable ? langNotes.delete : langNotes.mustExist;
this.$removeNoteButton.attr('title', title);
}
Notes.prototype.getFirstNoteItem = function() {
return this.$el.find('> .row > .field-notes-list > .field-notes-items li:first-child');
}
Notes.prototype.resetCreateNewNoteButtonEnable = function () {
const firstNoteItem = this.getFirstNoteItem();
if (firstNoteItem.length >0 && firstNoteItem[0].hasAttribute('data-note-unsaved') && firstNoteItem.next().length === 0) {
this.setCreateNewNoteButtonEnable(false);
}else{
this.setCreateNewNoteButtonEnable(true);
}
}
/**
* disable: When only new unsaved note exists in the list
*/
Notes.prototype.resetRemoveNoteButtonEnable = function(firstNoteItem = null) {
firstNoteItem = firstNoteItem || this.getFirstNoteItem();
if (firstNoteItem.length == 0 || (firstNoteItem[0].hasAttribute('data-note-unsaved') && firstNoteItem.next().length === 0)) {
this.setRemoveNoteButtonEnable(false);
} else {
this.setRemoveNoteButtonEnable(true);
}
}
Notes.prototype.createNewNote = function(activeNameField = true) {
this.setCreateNewNoteButtonEnable(false);
const self = this;
this.finishSavingProcess(function(){
let currentTime = new Date().toLocaleTimeString();
self.clearNoteListCheckedStyle();
self.$el.find('> .row > .field-notes-list > .field-notes-items').prepend('<li data-note-unsaved class="fade field-notes-item"><a href="javascript:;" data-request-data="id:0" class="checked"><div><h3>' + langNotes.new + '</h3><h4>' + currentTime + '<span></span></h4></div></a></li>');
let newNote = self.$el.find('> .row > .field-notes-list > .field-notes-items li:first-child');
self.fetchSelectedNote(newNote, activeNameField);
self.notesListScrollToItem(newNote);
self.resetRemoveNoteButtonEnable(newNote);
setTimeout(function () {
newNote.addClass(' show');
}, 10);
});
}
/**
*
* @param string name It can be shown in a message when catch some errors
* @param string value eg: id:1, id:7
*/
Notes.prototype.paramToObj = function (name, value) {
if (value === undefined) value = ''
if (typeof value == 'object') return value
try {
let data = JSON.parse(JSON.stringify(eval("({" + value + "})")))
return data;
}
catch (e) {
throw new Error('Error parsing the '+name+' attribute value. '+e)
}
}
Notes.prototype.clickDeleteButton = function (ev) {
let target = $(ev.target);
let noteData = this.getSelectedNoteIdData();
if (noteData){
target.data('request-data', noteData);
//Do not need to save anything
this.stopDelaySaving();
$(target).request( this.makeEventHandler('onNoteDelete'));
}
// To prevent the form submit, because it is a button inside the form
ev.preventDefault();
}
Notes.prototype.onNoteRemoveSuccess = function () {
let checkedItem = this.getCheckedItem();
$(checkedItem).parent().remove();
let firstNote = this.$el.find('> .row > .field-notes-list > .field-notes-items li:first-child');
this.resetRemoveNoteButtonEnable(firstNote);
if (firstNote.length > 0) {
this.notesListScrollToItem(firstNote);
this.onClickNoteItem(null, firstNote.find('a'));
//Check unsaved note
if (!firstNote[0].hasAttribute('data-note-unsaved')){
this.setCreateNewNoteButtonEnable(true);
}
}else{
//No note in the list now
this.setLastSavingNoteData({
id:0,
name:'',
content:''
});
this.clearNoteForm();
this.createDefaultNote();
}
$.wn.flashMsg({
text: langNotes.hasDeleted,
'class': 'success',
'interval': 2
});
}
/**
* Update the Checked Item in the title list
* @param array data {"id":1, "name":"Note Name", "updated_at": "2018-12-14 10:01:00" }
* @param {*} textStatus
* @param {*} jqXHR
*/
Notes.prototype.onNoteSaveSuccess = function (data, textStatus, jqXHR) {
// HTML a
let checkedItem = this.getCheckedItem();
if (checkedItem) {
checkedItem.find('h3').text(data['name']);
checkedItem.find('h4').html(data['updated_at'] + ' <span>' + data['abstract'] + '</span>');
checkedItem.attr('data-request-data', 'id:' + data['id']);
// Need to update the internal data same time.
checkedItem.data('request-data', 'id:' + data['id']);
let checkedli = checkedItem.parent();
if (checkedli[0].hasAttribute('data-note-unsaved')) {
checkedli.removeAttr('data-note-unsaved');
this.setCreateNewNoteButtonEnable(true);
}
this.resetRemoveNoteButtonEnable();
}
this.hideNotification();
}
/**
* @returns jQueryObject a jQueryObject
*/
Notes.prototype.getCheckedItem = function () {
return this.$el.find('> .row > .field-notes-list > .field-notes-items > .field-notes-item > .checked');
}
/**
* @returns string id:7 or null
*/
Notes.prototype.getSelectedNoteIdData = function () {
let checkedItem = this.getCheckedItem();
return (checkedItem.length>0) ? $(checkedItem).data('request-data') : null;
}
/**
* After delete the last note, need to clear name and content.
*/
Notes.prototype.clearNoteForm = function(){
let richeditorElem = this.$el.find('> .row > .field-notes-form [data-control="richeditor"]');
let richedior = richeditorElem.data('oc.richEditor');
richedior.setContent('');
this.$el.find('> .row > .field-notes-form input:first').val('');
}
/**
* The default Note title/name should be the valid first line of the content
* when the name field is empty
*/
Notes.prototype.setDefaultNoteName = function() {
const nameInput = this.$el.find('> .row > .field-notes-form input:first');
if (nameInput.val().trim().length > 0) return;
const htmlView = this.$richEditorTextarea.parent().find('.fr-view');
let content = htmlView[0].outerText.trim();
if (content.length === 0) return;
const lines = content.split(/\r|\n/);
let firstLine = '';
for (let i=0, len=lines.length; i < len; i++) {
firstLine = lines[i].trim();
if( firstLine.length > 0 ) break;
}
if (firstLine.length > 20) firstLine = firstLine.substring(0,20);
nameInput.val(firstLine);
}
Notes.prototype.getActiveNoteID = function() {
let noteIDData = this.getSelectedNoteIdData(); // id:10
return (noteIDData) ? noteIDData.split(':')[1] : 0;
}
Notes.prototype.getNoteData = function(){
let name = this.$el.find('> .row > .field-notes-form input:first').val();
let content = this.$richEditorTextarea.val();
return {
id: this.getActiveNoteID(),
name: name,
content: content
}
}
Notes.prototype.postNoteData = function(onSuccessCallback = function(){}, onErrorCallback = function(){}){
this.$isSaving = true;
// Get the note formdata
let $form = this.$el.closest('form');
let noteData = {
id: this.getActiveNoteID(),
_session_key: $form.find('[name="_session_key"]').val(),
_token: $form.find('[name="_token"]').val()
};
this.$el.find('.field-notes-form [name]').each(function (i) {
let $this = $(this);
noteData[$this.attr('name')] = $this.val();
});
let self = this;
$.request(this.makeEventHandler('onSaveNote'),{
data: noteData,
success: function(data, textStatus, jqXHR){
self.onNoteSaveSuccess(data, textStatus, jqXHR);
self.showNotification(langNotes.hasSaved, true, 500);
self.setLastSavingNoteData();
self.$isSaving = false;
onSuccessCallback();
},
error: function(jqXHR, textStatus, error){
this.error(jqXHR, textStatus, error);
self.$isSaving = false;
self.hideNotification();
onErrorCallback();
}
});
}
Notes.prototype.setLastSavingNoteData = function(postNoteData){
postNoteData = postNoteData || this.getNoteData();
this.$lastSavingNoteData.id = postNoteData.id;
this.$lastSavingNoteData.name = postNoteData.name;
this.$lastSavingNoteData.content = postNoteData.content;
}
Notes.prototype.testSelectedNote = function(){
let val = this.getSelectedNoteIdData() != null;
return val;
}
Notes.prototype.testSavingConditions = function(){
if (this.$isSaving || this.options.previewMode){
return false;
}
let noteData = this.getNoteData();
if (
this.$lastSavingNoteData.id == noteData.id
&& this.$lastSavingNoteData.name == noteData.name
&& this.$lastSavingNoteData.content == noteData.content
){
return false;
}
return true;
}
Notes.prototype.onAutoSavingNote = function(ev){
this.stopDelaySaving();
this.setDefaultNoteName();
const self = this;
// create a new timer;
this.$savingTimeout = setTimeout(function() {
if (self.testSelectedNote() && self.testSavingConditions()) {
self.showNotification(langNotes.saving + ' ...', false );
self.postNoteData();
}
}, this.options.autosaveDelay);
}
Notes.prototype.onAutoSavingNoteName = function(ev){
ev.preventDefault();
let target = $(ev.target);
let noteName = target.val();
if (noteName == ''){
this.setDefaultNoteName();
}
this.onAutoSavingNote(ev);
}
Notes.prototype.disableEnterSubmit = function(ev){
if (ev.keyCode == 13 || ev.which == 13) {
ev.preventDefault();
return ev;
}
}
/**
* Binding the keyDown event in richeditor.
*/
Notes.prototype.stopDelaySaving = function(){
if (this.$savingTimeout) {
clearTimeout(this.$savingTimeout);
this.$savingTimeout = null;
this.hideNotification();
}
}
/**
* @param string message
* @param string messageStyle error or info
* @param bool autoClose
* @param int time The amount of time, in milliseconds, to show the message animation.
*/
Notes.prototype.showNotification = function(message, autoClose = true , time = 3000 ) {
this.$notification.find('.loading-indicator div').text(message);
if(this.$notificationTimeout) clearTimeout(this.$notificationTimeout);
this.$notification.show();
if (autoClose){
let self = this;
this.$notificationTimeout = setTimeout(function(){
self.$notification.hide();
}, time);
}
}
Notes.prototype.hideNotification = function () {
this.$notification.hide();
}
// FIELD NOTES PLUGIN DEFINITION
// ============================
var old = $.fn.fieldNotes
$.fn.fieldNotes = function (option) {
var args = Array.prototype.slice.call(arguments, 1),
result
this.each(function () {
var $this = $(this)
var data = $this.data('oc.notes')
var options = $.extend({}, Notes.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('oc.notes', (data = new Notes(this, options)))
if (typeof option == 'string') result = data[option].apply(data, args)
if (typeof result != 'undefined') return false
})
return result ? result : this
}
$.fn.fieldNotes.Constructor = Notes
// FIELD NOTES NO CONFLICT
// =================
$.fn.fieldNotes.noConflict = function () {
$.fn.fieldNotes = old
return this
}
// FIELD NOTES DATA-API
// ===============
$(document).render(function () {
$('[data-control="fieldnotes"]').fieldNotes()
});
}(window.jQuery);