/
sheet.js
1617 lines (1550 loc) · 57.7 KB
/
sheet.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
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var tagDDType = 'sstack/tag';
var noteDDType = 'sstack/note';
var timeDDType = 'sstack/time';
var _buildIcon = function(name, cl) {//Builds img html
return '<div class="icon'+(cl? ' '+cl: '')+'" style="background-image: url(\'img/icons/'+name+'.png\');"/>';
};
var Sheet = function(sheet, element, proxy, menuPlace) {//
this.data = sheet;
this.autotags = this.data.autotags;
if (!this.autotags && this.data.id) {
this.autotags = 's:'+this.data.id+' ';
};
this.root = element;
this.proxy = proxy;
this.mediaGap = 8;
this.menuPlace = menuPlace;
this.areaPanel = $('<div/>').addClass('area_wrap').appendTo(this.root).hide();
this.area = $('<textarea/>').addClass('form_control').appendTo(this.areaPanel);
this.area.autoGrow(10);
var menuDiv = $(document.createElement('div')).addClass('sheet_menu').appendTo(this.root).hide();
this.menu = new Buttons({
root: menuDiv,
rows: [0, '2.5em'],
maxElements: 3,
safe: true,
});
this.menu.addButton({
caption: '+ Tag',
classNameInner: 'button_create',
handler: _.bind(function() {//Add tag
this.startTextEdit(this.selected.id, this.selected.div, 'tag');
}, this)
});
this.menu.addButton({
caption: 'Edit',
handler: _.bind(function() {//Edit note
this.editNote(this.selected, this.selected.div);
}, this)
});
this.menu.addButton({
caption: 'Remove',
classNameInner: 'button_remove',
handler: _.bind(function() {//
this.removeNote(this.selected);
}, this)
});
this.menu.addButton({
caption: '|',
width: 3,
row: 1,
handler: _.bind(function() {//
this.showNoteMenu();
return true;
}, this)
});
this.area.bind('keydown', _.bind(function(e) {
if (e.which == 13) {//Enter
var val = this.area.val() || '';
if (_.endsWith(val, '\n') || e.ctrlKey) {//Double enter or Ctrl enter - finish
this.area.val('');
this.area.blur();
this.editAreaDone(_.trim(val));
return false;
};
};
return true;
}, this));
this.area.bind('keyup', _.bind(function(e) {
this.updated();
return true;
}, this));
this.textPanel = $('<div/>').addClass('input_wrap').appendTo(this.root).hide();
this.text = $('<input type="text"/>').addClass('form_control').appendTo(this.textPanel);
this.text.bind('keydown', _.bind(function(e) {
if (e.which == 13) {//Enter
this.editTextDone(this.text.val());
return false;
};
return true;
}, this))
this.editing = false;
this.selected = null;
this.expandedNotes = {};
$(document.createElement('div')).addClass('clear').appendTo(this.root);
this.extra = {};
if (this.data.display && this['prepare_'+this.data.display]) {
this['prepare_'+this.data.display].call(this);
};
this.reload();
};
Sheet.prototype.removeNote = function(note) {
if (note.selectedTag) {
_showQuestion('Remove tag?', _.bind(function (index) {
if (0 == index) {
this.proxy('removeTag', _.bind(function(id, err) {//
if (id) {
this.reload();
};
}, this), [note.id, note.selectedTag.id]);
};
}, this))
} else {
_showQuestion('Remove note?', _.bind(function (index) {
if (0 == index) {
this.proxy('removeNote', _.bind(function(id, err) {//Removed
if (id) {
this.reload();
};
}, this), [note.id]);
};
}, this));
}
this.updated();
};
Sheet.prototype.showNoteMenu = function() {
var items = [];
if (this.selected && this.selected.selectedTag) {
items.push({
caption: 'Edit tag',
handler: _.bind(function() {
this.startTextEdit(this.selected.id, this.selected.div, 'tag', this.selected.selectedTag.id);
return true;
}, this),
});
items.push({
caption: 'Select tag',
handler: _.bind(function() {
this.proxy('openTag', null, [this.selected.selectedTag.id]);
return true;
}, this),
});
this.launchTagMethod(this.selected, 'menu', this.selected.selectedTag.id, items);
};
items.push({
caption: 'Open note',
handler: _.bind(function() {
this.openNote(this.selected);
return true;
}, this),
});
new PopupMenu({
element: this.menuPlace || this.root,
items: items,
});
this.updated();
};
Sheet.prototype.keypress = function(e) {
if (this.editing) {
return true;
};
switch (e.keyCode) {
case 36:
this.moveSelection(-2);
return false;
case 35:
this.moveSelection(2);
return false;
case 38:
this.moveSelection(-1);
return false;
case 40:
this.moveSelection(1);
return false;
case 37:
this.moveTagSelection(-1);
return false;
case 39:
this.moveTagSelection(1);
return false;
case 78:
case 45:
this.newNote();
return false;
case 32: // space
case 113: // F2
if (this.selected) {
if (this.selected.selectedTag) {
this.startTextEdit(this.selected.id, this.selected.div, 'tag', this.selected.selectedTag.id);
} else {
this.editNote(this.selected, this.selected.div);
}
};
return false;
case 82: //r
if (!e.ctrlKey) {
this.reload(this.selected? this.selected.id: null);
return false;
};
break;
case 68: // d
if (this.selected) {
this.removeNote(this.selected);
return false;
};
break;
case 84: //t
if (!e.ctrlKey && this.selected) {
this.startTextEdit(this.selected.id, this.selected.div, 'tag');
return false;
};
break;
case 77: //m
if (!e.ctrlKey && this.selected) {
this.showNoteMenu();
return false;
};
break;
case 13:
if (this.selected) {
if (this.selected.selectedTag) {
this.proxy('openTag', null, [this.selected.selectedTag.id]);
} else {
this.openNote(this.selected, e.ctrlKey? false: true);
}
};
return false;
}
log('keypress', e.keyCode);
};
Sheet.prototype.moveTagSelection = function(dir) {
if (!this.selected || this.selected.tags.length == 0) {
return false;
};
for (var i = 0; i < this.selected.tag_objects.length; i++) {
var t = this.selected.tag_objects[i];
if (t == this.selected.selectedTag) {
if (dir == 1 && i<this.selected.tags.length-1) {
this.selectTag(this.selected.tag_objects[i+1]);
return true;
};
if (dir == -1 && i>0) {
this.selectTag(this.selected.tag_objects[i-1]);
return true;
};
};
};
if (dir == -1) {
this.selectTag(this.selected.tag_objects[this.selected.tag_objects.length-1]);
};
if (dir == 1) {
this.selectTag(this.selected.tag_objects[0]);
};
};
Sheet.prototype.moveSelection = function(dir) {
var notes = this.root.find('.note');
if (!this.selected) {
if (notes.size()>0) {
this.selectNote(this.notes[notes.eq(0).attr('id')]);
return true;
} else {
return false;
}
};
if (notes.size()>0) {
if (dir == 2) {
// Last
this.selectNote(this.notes[notes.eq(notes.size()-1).attr('id')]);
return false;
};
if (dir == -2) {
// First
this.selectNote(this.notes[notes.eq(0).attr('id')]);
return false;
};
};
notes.each(_.bind(function (index, item) {
if ($(item).attr('id') == this.selected.divID) {
// Found
if (dir == -1 && index>0) {
// Move up
this.selectNote(this.notes[notes.eq(index-1).attr('id')]);
};
if (dir == 1 && index<notes.size()-1) {
// Move down
this.selectNote(this.notes[notes.eq(index+1).attr('id')]);
};
return false;
};
}, this))
};
Sheet.prototype.launchTagMethod = function(note, method, tag) {
for (var j = 0; j < note.tags_captions.length; j++) {//Display tags
var t = note.tags_captions[j];
if (tag && t.id != tag) {
continue;
};
if (t.display) {
var m = 'tag_'+method+'_'+t.display;
if (this[m]) {
var params = [note, t.id];
for (var i = 3; i < arguments.length; i++) {
params.push(arguments[i]);
};
var result = this[m].apply(this, params);
if (result === true) {
return true;
};
};
};
};
return false;
};
Sheet.prototype.tag_unselect_file = function(note, tag) {
if (note.fileCreated) {
note.div.find('.file').remove();
note.fileCreated = false;
};
};
Sheet.prototype.tag_select_file = function(note, tag) {
if (!note.fileCreated) {
note.fileCreated = true;
var frame = $(document.createElement('div')).addClass('file note_frame note_line_hide').appendTo(note.div);
// log('Show file', tag);
var name = tag.substr(2);
if (_.endsWith(name, '.jpg')) {
var loading = $(document.createElement('div')).addClass('file_loading note_line_hide').appendTo(frame).text('Loading...');
this.proxy('getAttachment', _.bind(function (err, uri) {
loading.remove();
if (err) {
return _showInfo(err);
};
var img = $(document.createElement('img')).addClass('file_image note_image').appendTo(frame);
img.bind('load', _.bind(function () {
var width = note.div.innerWidth()-2*this.mediaGap;
var iw = img.width();
var ih = img.height();
var mul = width / iw;
iw *= mul;
ih *= mul;
img.width(Math.floor(iw)).height(Math.floor(ih));
this.updated();
}, this));
img.attr('src', uri);
}, this), [name]);
};
};
};
Sheet.prototype.tag_menu_geo = function(note, tag, items) {
items.push({
caption: 'Edit location',
handler: _.bind(function () {
this.proxy('editMap', _.bind(function () {
this.reload(note.id);
}, this), [[{
id: note.id,
data: tag
}]]);
return true;
}, this)
})
};
Sheet.prototype.tag_menu_sheet = function(note, tag, items) {
items.push({
caption: 'Open sheet',
handler: _.bind(function () {
this.proxy('openMap', _.bind(function () {
}, this), [tag.substr(2)]);
return true;
}, this)
})
};
Sheet.prototype.tag_unselect_geo = function(note, tag) {
if (note.geoCreated) {
note.div.find('.geo').remove();
note.geoCreated = false;
};
};
Sheet.prototype.tag_select_geo = function(note, tag) {
// log('Ready to show geo', tag);
if (!note.geoCreated) {
var point = splitPoint(tag);
note.geoCreated = true;
if (point.lat && point.lon) {
var width = note.div.innerWidth()-2*this.mediaGap;
var height = Math.floor(width*0.75);
var frame = $(document.createElement('div')).addClass('geo note_frame note_line_hide').appendTo(note.div);
var img = $(document.createElement('img')).addClass('geo_image note_image').appendTo(frame);
img.attr('src', 'http://maps.google.com/maps/api/staticmap?center='+point.lat+','+point.lon+'&zoom=15&size='+width+'x'+height+'&sensor=true&markers=color:red|size:mid|'+point.lat+','+point.lon);
img.bind('click', _.bind(function (e) {
var link = '';
if (CURRENT_PLATFORM_MOBILE) {
link = 'geo:'+point.lat+','+point.lon;
} else {
link = 'http://maps.google.com/maps?q='+point.lat+','+point.lon+'&z=18';
};
this.proxy('openLink', _.bind(function(res) {
}, this), [link, e.ctrlKey]);
return false;
}, this))
img.width(width).height(height);
};
};
};
var splitPoint = function (tag) {
var arr = tag.split(':');
var point = {};
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var vp = item.split('=');
if (vp.length == 2) {
point[vp[0]] = vp[1];
};
};
return point;
};
Sheet.prototype.tag_unselect_path = function(note, tag) {
if (note.geoCreated) {
note.div.find('.path').remove();
note.geoCreated = false;
};
};
Sheet.prototype.tag_build_after_ok = function(note, tag, afterStr) {
if (tag.length>3) {
afterStr.splice(0, 0, tag.substr(3));
};
};
Sheet.prototype.tag_show_link = function(note, tag, div) {
var a = $(document.createElement('a')).addClass('tag_link').appendTo(div.empty()).text('link').attr('href', '#');
a.bind('click', _.bind(function (e) {
if (e.shiftKey) {
return true;
};
this.proxy('openLink', _.bind(function(res) {
}, this), [tag.substr(2), e.ctrlKey]);
return false;
}, this))
};
Sheet.prototype.tag_menu_path = function(note, tag, items) {
items.push({
caption: 'Edit path',
handler: _.bind(function () {
this.proxy('loadNotes', _.bind(function(list, err) {//
if (list && list.length>0) {//Display list
var path = [];
for (var i = 0; i < list.length; i++) {
var n = list[i];
var tags = n.tags || [];
// log('Tags', tags, n);
var pointTag = null;
for (var j = 0; j < tags.length; j++) {
if (_.startsWith(tags[j], 'g:')) {
pointTag = tags[j];
break;
};
};
if (!pointTag) {
continue;
};
path.push({
id: n.id,
data: pointTag
})
};
this.proxy('editMap', _.bind(function () {
this.reload(note.id);
}, this), [path]);
} else {
_showInfo('Path not found')
}
}, this), ['n:'+note.id+' g:*']);
return true;
}, this)
})
};
Sheet.prototype.tag_show_note_contact = function(note, tag, text, lines) {
var first_line = $(document.createElement('div')).addClass('note_line').appendTo(text);
$(document.createElement('div')).addClass('note_contact_title').appendTo(first_line).text(tag.substr('contact:'.length));
var second_line = $(document.createElement('div')).addClass('note_line').appendTo(text);
var table = $(document.createElement('div')).addClass('note_contact_table').css('display', 'table').appendTo(second_line);
for (var i = 0; i < lines.length; i++) {
var line = lines[i]
var nameWords = [];
var valueWords = [];
var signFound = false;
for (var j = 0; j < line.length; j++) {
var word = line[j];
if (!signFound) {
nameWords.push(word);
if (word.type == 'text' && _.endsWith(word.text, ':')) {
signFound = true;
};
} else {
valueWords.push(word);
}
};
var row = $(document.createElement('div')).addClass('note_contact_row').css('display', 'table-row').appendTo(table);
var nameCell = $(document.createElement('div')).addClass('note_contact_cell note_contact_name_cell').css('display', 'table-cell').appendTo(row);
var valueCell = $(document.createElement('div')).addClass('note_contact_cell note_contact_value_cell').css('display', 'table-cell').appendTo(row);
this.renderLine(note, nameWords, nameCell);
this.renderLine(note, valueWords, valueCell);
};
return true;
};
Sheet.prototype.tag_edit_note_contact = function(note, tag) {
this.proxy('editContact', _.bind(function(text, name) {
this.proxy('editNoteField', _.bind(function(id, err) {//
if (id) {
this.proxy('addTag', _.bind(function(id, err) {//
this.reload(note.id);
}, this), [id, tag, 'contact:'+name]);
};
}, this), [note.id, text]);
this.reload(note.id);
}, this), [note, tag]);
return true;
};
Sheet.prototype.tag_select_path = function(note, tag) {
// log('Ready to show geo', tag);
if (!note.pathCreated) {
var point = splitPoint(tag);
note.pathCreated = true;
var frame = $(document.createElement('div')).addClass('path note_frame note_line_hide').appendTo(note.div);
this.proxy('loadNotes', _.bind(function(list, err) {//
if (list && list.length>0) {//Display list
// log('Points:', list.length);
var path = ['color:0x0000ff', 'weight: 3'];
for (var i = 0; i < list.length; i++) {
var n = list[i];
var tags = n.tags || [];
// log('Tags', tags, n);
var pointTag = null;
for (var j = 0; j < tags.length; j++) {
if (_.startsWith(tags[j], 'g:')) {
pointTag = tags[j];
break;
};
};
if (!pointTag) {
continue;
};
var point = splitPoint(pointTag);
if (point.lat && point.lon) {
path.push(''+point.lat+','+point.lon);
}
};
var width = note.div.innerWidth()-2*this.mediaGap;
var height = Math.floor(width*0.75);
var img = $(document.createElement('img')).addClass('geo_image note_image').appendTo(frame);
img.attr('src', 'http://maps.google.com/maps/api/staticmap?size='+width+'x'+height+'&sensor=true&path='+path.join('|'));
img.width(width).height(height);
} else {
_showInfo('Path not found')
}
}, this), ['n:'+note.id+' g:*']);
};
};
Sheet.prototype.editTextDone = function(val) {//Edit tag/link/ref
var method = null;
if (this.editField == 'tag') {
method = 'addTag';
val = _.trim(val || '');
var params = [this.editID];
if (this.editValue) {
params.push(this.editValue, val);
} else {
params.push(val);
}
this.proxy(method, _.bind(function(id, err) {//
if (id) {
this.reload();
};
}, this), params);
} else {
val = _.trim(val || '');
this.proxy('editNoteField', _.bind(function(id, err) {//
if (id) {
this.reload();
};
}, this), [this.editID, val, this.editField]);
};
this.text.val('');
this.text.blur();
};
var fixFloat = function (fl, round) {
if (!fl) {
return 0;
};
if (!round) {
return fl;
};
return Math.round(fl*round)/round;
};
Sheet.prototype.pointToTag = function(point) {
return 'g:lat='+fixFloat(point.lat)+':lon='+fixFloat(point.lon)+':sp='+fixFloat(point.speed, 100)+':acc='+fixFloat(point.acc, 100)+':alt='+fixFloat(point.alt, 100)+':tstamp='+point.created;
};
Sheet.prototype.importNotes = function(provider) {
provider.list(_.bind(function (err, list) {
if (err) {
return _showError(err);
};
var items = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
log('item', item.id, item.title);
items.push({
taskID: item.id,
caption: item.title
});
};
new PopupMenu({
element: this.menuPlace || this.root,
items: items,
handler: _.bind(function (item) {
// log('Getting task:', item.taskID);
provider.get(item.taskID, _.bind(function (err, task) {
if (err) {
return _showError(err);
};
var tags = this.autotags;
// log('Task', task, _.keys(task));
if (task.created>0) {
var dt = new Date(task.created);
tags += ' d:'+dt.format('yyyymmdd')+' t:'+(dt.getHours()*100+Math.floor(dt.getMinutes()/15)*15);
};
if (task.id) {
delete task.id;
};
if (task.title) {
task.text = task.title;
delete task.title;
};
var media = null;
if (task.media) {
media = task.media;
delete task.media;
};
var points = null;
if (task.point) {
tags +=' '+this.pointToTag(task.point);
delete task.point;
};
if (task.points) {
points = task.points;
delete task.points;
tags += ' p:';
};
// log('Create note', task, tags);
this.proxy('putNote', _.bind(function(id, err) {//
if (id) {
this.reload(id);
_showInfo('Item imported');
if (media) {
this.proxy('createAttachment', _.bind(function (err) {
if (!err) {
this.reload(id);
};
}, this), [id, media]);
};
provider.done(item.taskID, function (err) {
});
for (var i = 0; points && i < points.length; i++) {
var point = points[i];
var n = {
text: new Date(point.created).format('m/d/yy h:MMt')
}
this.proxy('putNote', _.bind(function (id, err) {
}, this), [n, 'n:'+id+' '+this.pointToTag(point)]);
};
};
}, this), [task, tags]);
}, this))
return true;
}, this)
});
}, this))
};
Sheet.prototype.editAreaDone = function(val) {//Creates new note
if (this.editID) {//Edit existing
this.proxy('editNoteField', _.bind(function(id, err) {//
if (id) {
this.reload(id);
};
}, this), [this.editID, val]);
} else {//Create new
this.proxy('createNote', _.bind(function(id, err) {//
if (id) {
this.reload(id);
};
}, this), [val, (this.ignoreAutoTags? '': this.autotags)+(this.newTags? ' '+this.newTags: '')]);
};
};
var applyColor = function (div, color, gradient) {
if (!color) {
return false;
}
var step = 32;
// if (!gradient) {
div.css('background-color', 'rgb('+color[0]+', '+color[1]+', '+color[2]+')').css('border-color', 'rgb('+(color[0]-3*step)+', '+(color[1]-3*step)+', '+(color[2]-3*step)+')');
// } else {
// div.addClass('has_gradient').css('background-image', '-webkit-gradient(linear, left top, left bottom, color-stop(0%, rgb('+(color[0]+step)+', '+(color[1]+step)+', '+(color[2]+step)+')), color-stop(20%, rgb('+(color[0]-step)+', '+(color[1]-step)+', '+(color[2]-step)+')), color-stop(100%, rgb('+(color[0]+step)+', '+(color[1]+step)+', '+(color[2]+step)+')))').css('border-color', 'rgb('+(color[0]-3*step)+', '+(color[1]-3*step)+', '+(color[2]-3*step)+')')
// }
return true;
}
Sheet.prototype.selectNote = function(note) {
if (this.selected != note) {
this.root.find('.note_selected').removeClass('note_selected');
if (this.selected) {
this.launchTagMethod(this.selected, 'unselect');
};
this.launchTagMethod(note, 'select');
};
this.selected = note;
note.div.addClass('note_selected');
note.div.after(this.menu.element.detach().show());
note.div.focus();
note.selectedTag = null;
this.root.find('.note_tag').removeClass('note_tag_selected');
this.root.find('.note_line_show').removeClass('note_line_show');
var note_inline = note.note_inline;
if (note_inline) {
note_inline.detach();
};
note.div.children('.note_line_hide').add(note.div.children('.note_text').find('.note_line_hide')).addClass('note_line_show');
if (note_inline) {
note_inline.appendTo(note.div);
};
// log('Select note', note.tags);
this.moveNowLine();
this.updated();
};
Sheet.prototype.unselectNote = function() {
this.root.find('.note_line_show').removeClass('note_line_show');
if (this.selected) {
this.selected.div.find('.note_tag').removeClass('note_tag_selected');
this.selected.div.removeClass('note_selected');
this.selected.selectedTag = null;
};
this.selected = null;
this.menu.element.hide();
this.updated();
};
Sheet.prototype.selectTag = function(t) {
log('Click tag:', t);
this.selected.selectedTag = t;
this.selected.div.find('.note_tag').removeClass('note_tag_selected');
t.div.addClass('note_tag_selected');
this.updated();
};
Sheet.prototype.showTag = function(note, t, parent, remove) {//
var tag = $('<div/>').addClass('note_tag draggable').attr('draggable', 'true').appendTo(parent);
if (t.tag_display) {
tag.addClass('note_tag_display_'+t.tag_display);
};
applyColor(tag, t.color, true);
t.div = tag;
tag.text(t.caption);
this.launchTagMethod(note, 'show', t.id, tag);
tag.bind('dblclick', {tag: t}, _.bind(function(e) {
e.preventDefault();
this.newNote(t.id);
return false;
}, this));
tag.bind('click', {div: tag, tag: t}, _.bind(function(e) {//
// log('Click on tag', t);
if (this.selected != note) {
return true;
};
if (e.shiftKey) {
this.startTextEdit(this.selected.id, this.selected.div, 'tag', t.id);
return false;
};
//e.data.div.siblings('.note_tag').find('.note_button').hide();
//e.data.div.find('.note_button').show();
// if (note.selectedTag == t && CURRENT_PLATFORM_MOBILE) {//Show menu
// new PopupMenu({
// element: this.root,
// items: [{
// caption: 'Select notes',
// handler: _.bind(function() {
// this.proxy('openTag', null, [t.id]);
// return true;
// }, this),
// }, {
// caption: 'Remove tag',
// handler: _.bind(function() {
// this.proxy('removeTag', _.bind(function(id, err) {//
// if (id) {
// this.reload();
// };
// }, this), [note.id, t.id]);
// return true;
// }, this),
// }, {
// caption: 'Create note',
// handler: _.bind(function() {
// this.newNote(t.id);
// return true;
// }, this),
// }]
// });
// };
this.selectTag(t);
if (e.ctrlKey) {
this.proxy('openTag', null, [e.data.tag.id]);
};
return false;
}, this));
tag.bind('dragstart', {tag: t}, _.bind(function(e) {//
dd.setDDTarget(e, tagDDType, e.data.tag.id);
e.stopPropagation();
return true;
}, this));
this.enableNoteDrop(tag, _.bind(function(n) {
if (n.id) {//Add this tag to note
this.proxy('addTag', _.bind(function(id, err) {//
if (id) {
this.reload();
};
}, this), [n.id, t.id]);
} else {//Create note with tag
this.proxy('putNote', _.bind(function(id, err) {
if (id) {
this.proxy('addTag', _.bind(function(id, err) {//
if (id) {
this.reload();
};
}, this), [id, t.id]);
};
}, this), [n]);
};
}, this))
//tag.bind('dragenter', _.bind(function(e) {
//log('tag drag enter', dd.hasDDTarget(e, tagDDType));
//if (dd.hasDDTarget(e, tagDDType)) {
//e.preventDefault();
//};
//}, this));
this.enableTagDrop(tag, _.bind(function(tag, text) {
this.proxy('addTag', _.bind(function(id, err) {//
if (id) {
this.reload(id);
};
}, this), [note.id, t.id, tag]);
}, this));
// tag.bind('dragover', _.bind(function(e) {
// //log('tag drag over', dd.hasDDTarget(e, tagDDType));
// if (dd.hasDDTarget(e, tagDDType)) {
// e.preventDefault();
// };
// }, this));
// tag.bind('drop', {note: note, tag: t}, _.bind(function(e) {
// var drop = dd.getDDTarget(e, tagDDType);
// if (drop) {
// this.proxy('addTag', _.bind(function(id, err) {//
// if (id) {
// this.reload();
// };
// }, this), [e.data.note.id, e.data.tag.id, drop]);
// e.stopPropagation();
// e.preventDefault();
// return false;
// };
// }, this));
};
Sheet.prototype.editNote = function(note, div) {
if (this.launchTagMethod(note, 'edit_note', null)) {
_showInfo('External editor is used')
return;
};
this.editID = note.id;
this.areaPanel.detach().insertAfter(div);
this.areaPanel.show();
this.editing = true;
this.area.val(note.text || '').focus();
this.updated();
};
Sheet.prototype.enableTagDrop = function(div, handler, id) {//Called when tag dropped
div.bind('dragover', _.bind(function(e) {
ctrlKey = e.ctrlKey;
if (dd.hasDDTarget(e, tagDDType)) {
e.preventDefault();
};
if (dd.hasDDTarget(e, 'text/uri-list')) {
e.preventDefault();
};
}, this)).bind('drop', _.bind(function(e) {//Dropped
log('Enable tag drop');
var drop = dd.getDDTarget(e, 'text/uri-list');
if (drop) {//URL - new note
var text = dd.getDDTarget(e, 'text/html');
if (text) {
text = $(text).text();
};
if (!text) {
text = dd.getDDTarget(e, 'text/plain') || drop;
};
handler('l:'+drop, text);
e.stopPropagation();
e.preventDefault();
return false;
};
var drop = dd.getDDTarget(e, tagDDType);
if (drop) {
handler(drop);
e.stopPropagation();
e.preventDefault();
return false;
};
}, this));
};
Sheet.prototype.enableNoteDrop = function(div, handler, id, special_drop_handler) {//Called when note or text is dropped
var filesDD = 'application/x-vnd.adobe.air.file-list';
var ctrlKey = false;
div.bind('dragover', _.bind(function(e) {
ctrlKey = e.ctrlKey;
if (dd.hasDDTarget(e, noteDDType)) {
e.preventDefault();
};
if (dd.hasDDTarget(e, filesDD)) {
e.preventDefault();
};
}, this)).bind('drop', _.bind(function(e) {//Dropped
log('Enable note drop');
var drop = dd.getDDTarget(e, noteDDType);
if (drop) {//Only ID - note drop
// log('Dropped note', drop);
handler({id: drop});
e.stopPropagation();
e.preventDefault();
return false;
};
drop = dd.getDDTarget(e, filesDD);
if (drop) {//URL - new note
//log('Drop file', e.ctrlKey, e.shiftKey, ctrlKey, e.originalEvent.dataTransfer.dropEffect);
// if (ctrlKey) {//Copy links
// for (var i = 0; i < drop.length; i++) {//Copy and create file
// var f = drop[i];
// log('Dropped file link', f.nativePath);
// handler({text: f.name, link: f.nativePath});
// };
// } else {//Copy files
// var files = this.proxy('copyFile', drop);
// if (!files) {
// log('File copy error');
// return false;
// };
// for (var i = 0; i < files.length; i++) {//
// log('Dropped file storage', files[i].text);
// handler(files[i]);
// };
// };
if (drop.length == 1) {
this.proxy('createAttachment', _.bind(function (err) {
this.reload(id);
}, this), [id, drop[0].nativePath]);
};
e.stopPropagation();
e.preventDefault();
return false;
};
drop = dd.getDDTarget(e, 'text/html');
if (drop) {
drop = $(drop).text();
};
if (drop) {
drop = dd.getDDTarget(e, 'text/plain');
};
if (drop && _.trim(drop)) {
log('Dropped text', drop);
handler({text: _.trim(drop)});