This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
ui.js
executable file
·2187 lines (1733 loc) · 57.6 KB
/
ui.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
/* Nitro UI
*
* Copyright (C) 2012 Caffeinated Code <http://caffeinatedco.de>
* Copyright (C) 2012 Jono Cooper
* Copyright (C) 2012 George Czabania
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Caffeinated Code nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Define system (python|js)
var app = 'js';
/********************************************************************************
MAIN INIT
********************************************************************************/
var $body, $addBTN, $editBTN, $deleteBTN, $logbookBTN, $settingsBTN, $syncBTN, $tasks, $sidebar, $userLists, $addListBTN, $warning;
$(document).ready(function () {
"use strict";
// Cached selectors
$body = $('body');
$addBTN = $('#addBTN');
$editBTN = $('#editBTN');
$deleteBTN = $('#deleteBTN');
$logbookBTN = $('#logbookBTN');
$settingsBTN = $('#settingsBTN');
$syncBTN = $('#syncBTN');
$tasks = $('#tasks');
$sidebar = $('#sidebar');
$userLists = $('#userLists');
$addListBTN = $('#addListBTN');
$warning = $('#warning');
// Make sure the database is up to date
cli.timestamp.upgrade();
//Reschedule Schedule
cli.scheduled.update();
// Check the dates on the Today list
cli.calc.todayQueue.refresh();
// Load different versions
switch (app) {
case 'js':
// Loads Translation
$.getJSON('js/translations/' + cli.storage.prefs.lang + '.json', function (data) {
ui.language(data);
});
break;
case 'python':
// Loads Translation
document.title = 'null';
document.title = 'load|' + cli.storage.prefs.lang + '.json';
break;
}
// Sets up keyboard shortcuts
ui.external.key();
// Theme init
cli.storage.prefs.theme = cli.storage.prefs.theme || 'default';
$('link.theme')
.attr('href', 'css/themes/' + cli.storage.prefs.theme + '.css')
.ready(function () {
$(window).resize();
});
// GPU init
if (cli.storage.prefs.gpu) {
toggleFx();
}
});
$(window).resize(function () {
"use strict";
if (cli.storage.prefs.theme === 'wunderlist') {
$userLists.height($(window).height() - $('#userLists').offset().top - 36);
} else {
$userLists.height($(window).height() - $('#userLists').offset().top);
}
});
var toggleFx = function () {
"use strict";
deCSS3.init(document.querySelectorAll('.deCSS3-Style'));
$.fx.off = !$.fx.off;
};
/********************************************************************************
THE UI
********************************************************************************/
var ui = {
listEditMode: 'none',
listClicks: 0,
/**************************************************************
INIT
**************************************************************/
init: function () {
"use strict";
//Loading Button
if (app == 'web') {
$('#loading button').click(function() {
$(this).remove();
$('#loading').css('z-index', '0');
$('#everything').css({'opacity': '1', '-webkit-transform': 'scale(1)'});
})
} else {
$('#loading').remove();
};
// Hide datepicker
$('#date').hide(0);
// Makes lists show up
ui.lists.populate();
/**********************************
PANEL BUTTONS
**********************************/
// ADD TASK BUTTON
$addBTN.click(function () {
if (!$(this).hasClass('disabled')) {
// Closes current task
$('#overlay').click();
if (ui.lists.selected() == 'scheduled') {
$('#scheduledDialog .inner').fadeToggle(150).attr('data-type', 'add');
$('#scheduledDialog').toggle(0);
ui.tasks.scheduled('add');
} else {
// Adds a new task via the CLI
cli.addTask($.i18n._('newTask'), ui.lists.selected());
// Show task in UI
$('#tasks ul').first().prepend(ui.tasks.draw(cli.storage.tasks.length - 1));
$('#tasks .explanation').remove();
//Update list count if task is added to today
if (ui.lists.selected() === 'today') {
ui.lists.updateCount();
} else {
ui.lists.updateCount();
}
//Expands Task
ui.tasks.selected.expand($('#T' + (cli.storage.tasks.length - 1)));
};
};
});
// EDIT TASK BUTTON
$editBTN.click(function () {
// Only run if at least one task is selected or expanded
if ($('#tasks .selected').length || $('#tasks .expanded').length) {
// Expand Selected
if ($('#tasks .selected').length) {
if ($('#tasks .expanded').length) {
ui.tasks.selected.collapse($('#tasks li.expanded'));
}
ui.tasks.selected.expand($('#tasks .selected'));
// Close expanded
} else {
if ($('#tasks .expanded').length) {
ui.tasks.selected.collapse($('#tasks li.expanded'), "stay_selected");
}
}
}
});
// DELETE TASK BUTTON
$deleteBTN.click(function () {
var id = ui.tasks.selected.getID();
if (cli.storage.prefs.deleteWarnings) {
cli.deleteTask(id);
$('#T' + id).remove();
ui.lists.updateCount();
//Disable edit and delete button if there is no expanded or selected task
if (!$('#tasks .selected').length && !$('#tasks .expanded').length) {
$editBTN.addClass('disabled');
$deleteBTN.addClass('disabled');
}
} else if ($tasks.find('.selected').length) {
$warning
.find('p').html($.i18n._('taskWarning')).parent()
.find('.yes').html($.i18n._('yesTask')).parent().fadeIn(75)
.find('.yes').off('click')
.on('click', function () {
$(this).parent().fadeOut(75);
cli.deleteTask(id);
$('#T' + id).remove();
ui.lists.updateCount();
//Disable edit and delete button if there is no expanded or selected task
if (!$('#tasks .selected').length && !$('#tasks .expanded').length) {
$editBTN.addClass('disabled');
$deleteBTN.addClass('disabled');
}
$(document).off('click');
})
.parent().find('.no').off('click')
.on('click', function () {
$(this).parent().fadeOut(75);
$(document).off('click');
});
//Closes when not clicked in square box
setTimeout(function () {
$(document).click(function () {
$('#warning').fadeOut(75);
$(document).off('click');
});
}, 100);
}
});
/**********************************
SIDEBAR
**********************************/
// ADD LIST BUTTON
$addListBTN.click(function () {
// Appends to UI
$('#userLists').append('\
<li id="L' + (cli.storage.lists.items.length) + '">\
<div class="editIcon" title="' + $.i18n._('titleEditList') + '"></div>\
<div class="delete" title="' + $.i18n._('titleDeleteList') + '"></div>\
<div class="count">0</div>\
<div class="view">\
<p>' + $.i18n._('newList') + '</p>\
</div>\
<div class="edit">\
<input type="text" value="' + $.i18n._('newList') + '">\
<div class="save" title="' + $.i18n._('titleSaveList') + '">\
</div>\
</li>');
// Makes it droppable
$('#sidebar ul li').droppable('destroy').droppable(ui.lists.dropOptions);
// Adds to CLI
cli.list('', $.i18n._('newList')).add();
// Clicks the new list
$('#L' + (cli.storage.lists.items.length - 1)).click();
ui.lists.openEditMode();
});
// DELETE LIST BUTTON
$body.on('click', '#userLists li .delete', function () {
if (cli.storage.prefs.deleteWarnings) {
// Delete straight away, no questions asked
ui.lists.remove(ui.lists.selected());
} else {
// Display warning modal
$('#warning')
.click(function (e) {
e.stopPropagation();
return false;
})
.find('p').html($.i18n._('listWarning')).parent()
.find('.yes').html($.i18n._('yesList')).parent().fadeIn(75)
.find('.yes').off('click')
.on('click', function () {
$(this).parent().fadeOut(75);
var id = ui.lists.selected();
ui.lists.remove(id);
$(document).off('click');
})
.parent().find('.no').off('click')
.on('click', function () {
$(document).off('click');
$(this).parent().fadeOut(75);
});
//Closes when not clicked in square box
setTimeout(function () {
$(document).click(function () {
$('#warning').fadeOut(75);
$(document).off('click');
});
}, 100);
}
});
// SELECTING A LIST
$body.on('click', '#sidebar ul li', function () {
if (!$(this).hasClass('selected')) {
$editBTN.addClass('disabled');
$deleteBTN.addClass('disabled');
$addBTN.removeClass('disabled');
// Changes selected
$('#sidebar ul li.selected').removeClass('selected');
$(this).addClass('selected');
ui.lists.closeEditMode();
// Sends the id of the list to populate
ui.tasks.populate(ui.lists.selected());
}
});
// EDITING A LIST
$body.on('click', '#sidebar .editIcon', function () {
ui.lists.openEditMode();
});
// Enter Key closes list editing
$body.on('keydown', '#sidebar input', function (e) {
if (e.keyCode === 13) {
ui.lists.closeEditMode();
}
});
// Clicking the save button closes enter mode
$body.on('click', '#sidebar ul li .save', function () {
ui.lists.closeEditMode();
});
// LOGBOOK BUTTON
$logbookBTN.click(function () {
$addBTN.addClass('disabled');
$editBTN.addClass('disabled');
$deleteBTN.addClass('disabled');
$sidebar.find('.selected').removeClass('selected');
ui.tasks.populate('logbook');
});
/**********************************
SETTINGS
**********************************/
// DROP DOWN MENU
$settingsBTN.find('img').click(function () {
$settingsBTN.find('ul').slideToggle(150);
});
$settingsBTN.find('li').click(function () {
$settingsBTN.find('ul').fadeOut(100);
ui.external.cmd($(this).attr('class'));
});
// CHECK BOXES [DELETE WARNINGS & LOW GRAPHICS MODE]
$('#tabGeneral form input').change(function () {
cli.storage.prefs.deleteWarnings = $('#deleteWarnings').prop('checked');
cli.storage.prefs.gpu = $('#gpu').prop('checked');
cli.storage.save();
if ($(this).attr('id') === 'gpu') {
toggleFx();
}
console.log('Settings Saved');
});
// NEXT AMOUNT
$('#nextAmount').change(function () {
cli.storage.prefs.nextAmount = this.value;
cli.storage.save();
if (ui.lists.selected() === 'next') {
ui.tasks.populate('next');
}
console.log('Settings Saved');
});
// THEME
$('#theme').change(function () {
// Get value
var theme = $(this)[0].value;
// Set CSS file
$('link.theme')
.attr('href', 'css/themes/' + theme + '.css')
.ready(function () {
$(window).resize();
});
//Saves Theme
cli.storage.prefs.theme = theme;
cli.storage.save();
});
/**********************************
SCHEDULED TASKS
**********************************/
$('#scheduledDialog .inner .create').click(function() {
var id = $(this).parent().attr('data-type');
//Calculates Date
var date = parseInt($('#reviewNo').val());
var unit = $('#reviewLength').val();
//Zeros Days
var today = new Date();
today.setSeconds(0);
today.setMinutes(0);
today.setHours(0);
var tmpdate = new Date()
//tmpdate.setSeconds(0);
//tmpdate.setMinutes(0);
//tmpdate.setHours(0);
if (unit == 'days') {
today.setDate(today.getDate() + date);
} else if (unit == 'weeks') {
today.setDate(today.getDate() + (date * 7));
} else if (unit == 'months') {
today.setMonth(today.getMonth() + date);
} else if (unit == 'years') {
today.setYear(today.getFullYear() + date);
};
if (id == 'add') {
//Creates a new Scheduled Task
cli.scheduled.add('New Task', 'scheduled');
//Edits Data inside of it
var task = cli.scheduled.edit(cli.storage.lists.scheduled.length -1);
//Calculates Difference
//task.date = (Math.round((today.getTime() - tmpdate.getTime()) / 1000 / 60 / 60 /24));
task.next = cli.calc.dateConvert(today);
task.list = $('#reviewAction').val();
//Saves
cli.scheduled.edit(cli.storage.lists.scheduled.length -1, task);
} else {
if (id.substr(1,1) == 's') {
var task = cli.scheduled.edit(id.substr(2));
//Edits Values
task.next = cli.calc.dateConvert(today);
task.list = $('#reviewAction').val();
//Saves
cli.scheduled.edit(id.substr(2), task);
}
}
//Closes
$('#scheduledDialog .inner').fadeOut(150);
$('#scheduledDialog').hide(0);
//Reschedule Schedule
cli.scheduled.update();
});
$('#scheduledDialog .cancel').click(function() {
$addBTN.click();
});
/**********************************
CUSTOM BACKGROUNDS
**********************************/
// REMOVE CUSTOM BACKGROUND
$('#removeBG').click(function () {
localStorage.removeItem('background');
$tasks[0].style.backgroundImage = 'none';
});
// DRAG AND DROP
$body.bind({
dragover: function () {
// Stop the window from opening the file
return false;
},
drop: function (e) {
// Get the files from the event
e = e || window.event;
e.preventDefault();
e = e.originalEvent || e;
if (e.hasOwnProperty('files') || e.hasOwnProperty('dataTransfer')) {
var files = (e.files || e.dataTransfer.files);
setBG(files[0]);
return false;
}
}
});
// BUTTON UPLOAD
$('#chooseBG').change(function (e) {
var files = $(this)[0].files;
setBG(files[0]);
});
// Takes a file and sets it as the background
var setBG = function (f) {
var reader = new FileReader();
reader.onload = function (event) {
if (app === 'js') {
localStorage.removeItem('background');
localStorage.setItem('background', event.target.result);
} else {
cli.storage.prefs.background = event.target.result;
}
$tasks[0].style.backgroundImage = 'url(' + event.target.result + ')';
};
reader.readAsDataURL(f);
};
// BACKGROUND SIZE
$('#backgroundSize').change(function () {
cli.storage.prefs.bg.size = this.value;
switch (this.value) {
case 'tile':
$tasks.removeClass('shrink zoom').addClass('tile');
break;
case 'shrink':
$tasks.removeClass('tile zoom').addClass('shrink');
break;
case 'zoom':
$tasks.removeClass('tile shrink').addClass('zoom');
break;
}
cli.storage.save();
});
// HEADING COLOR
$('#headingColor').change(function () {
cli.storage.prefs.bg.color = this.value;
cli.storage.save();
$('#tasks h2, #tasks p').removeClass('light dark').addClass($(this)[0].value);
});
/**********************************
SYNC
**********************************/
// SYNC TYPE
$('#sync').change(function () {
var sync = this.value;
switch (sync) {
case 'timer':
/*var sync_timer = setInterval(function () {
cli.storage.sync();
}, 20000);
sync_timer;*/
break;
case 'never':
$syncBTN.addClass('disabled');
// clearInterval(sync_timer);
break;
default:
// clearInterval(sync_timer);
}
cli.storage.prefs.sync = sync;
});
// SYNC BUTTON
$syncBTN.click(function () {
if (!$(this).is('.running') && cli.storage.prefs.sync !== 'never') {
ui.sync.running('on');
cli.storage.sync();
}
});
/**********************************
LOADING PREFERENCES
**********************************/
$('#deleteWarnings').prop('checked', cli.storage.prefs.deleteWarnings);
$('#gpu').prop('checked', cli.storage.prefs.gpu);
$('#nextAmount').val(cli.storage.prefs.nextAmount);
$('#theme').val(cli.storage.prefs.theme);
$('#backgroundSize').val(cli.storage.prefs.bg.size);
$('#headingColor').val(cli.storage.prefs.bg.color);
$('#sync').val(cli.storage.prefs.sync);
// CUSTOM BACKGROUND
if (localStorage.hasOwnProperty('background')) {
$tasks[0].style.backgroundImage = 'url(' + localStorage.getItem('background') + ')';
} else if (cli.storage.prefs.hasOwnProperty('background')) {
$tasks[0].style.backgroundImage = 'url(' + cli.storage.prefs.background + ')';
}
$tasks.addClass(cli.storage.prefs.bg.size);
// LANGUAGE
$('#tabLanguage a.current').removeClass('current');
$('#tabLanguage .language a').each(function () {
if ($(this).data('value') === cli.storage.prefs.lang) {
$(this).addClass('current');
}
});
$('#tabLanguage').bind('click', function (e) {
if ($(e.srcElement).is('.language a')) {
cli.storage.prefs.lang = $(e.srcElement).data('value');
cli.storage.save();
window.location.reload();
return false;
}
});
/**********************************
SEARCH
**********************************/
$('#search').keyup(function () {
// Disable buttons
$addBTN.addClass('disabled');
$editBTN.addClass('disabled');
$deleteBTN.addClass('disabled');
// Get query and run search
var query = this.value,
results = cli.populate('search', query, ui.lists.selected());
var taskResults = '';
// Display results
for(var i = 0; i < results.length; i++) {
taskResults += ui.tasks.draw(results[i]);
}
if(results.length) {
$tasks.html('<h2 class="' + cli.storage.prefs.bg.color + '">' + $.i18n._('searchResults') + query + '</h2><ul>' + taskResults + '</ul>')
} else {
$tasks.html('<h2 class="' + cli.storage.prefs.bg.color + '">' + $.i18n._('noResults') + '</h2><ul></ul>');
}
});
/**********************************
REORDING TASKS
**********************************/
$('#userLists').sortable({
containment: 'parent',
axis: 'y',
distance: 20,
placeholder: 'listPlaceholder',
stop: function (event, ui) {
//Saves Everything, including order
var listOrder = [];
//Loops through lists & adds the to an array
$('#userLists').children().map(function () {
listOrder.push($(this).attr('id').substr(1).toNum());
});
//Saves
cli.list().order(listOrder);
}
});
$('#sidebar ul li').droppable(ui.lists.dropOptions);
/**********************************
SHOW MORE TASKS
**********************************/
$body.on('click', '#tasks .expandList', function () {
var $parent = $(this).parent();
var items = cli.populate('list', $parent.attr('id'));
$parent.find('li, p').remove();
for (var i in items) {
if($('#T' + items[i]).length) {
} else {
$parent.addClass('wholeList').append(ui.tasks.draw(items[i]));
}
}
});
/**********************************
OVERLAY
**********************************/
// OVERLAY
$('#settingsOverlay').click(function () {
$(this).hide(0);
$('#prefsDialog, #aboutDialog, #donateDialog').fadeOut(100);
});
// OVERLAY
$('#overlay').click(function () {
if ($('#date').css('display') !== 'block') {
$deleteBTN.addClass('disabled');
$editBTN.addClass('disabled');
$tasks.find('.selected').removeClass('selected');
ui.tasks.selected.collapse($('#tasks .expanded'));
$('.settings').fadeOut(150);
}
});
/**********************************
TASKS
**********************************/
// KEYBOARD SHORTCUTS
$body.on('keydown', '#tasks input', function (e) {
if(e.keyCode === 13) {
// $(this).parent().html($(this).val());
$editBTN.click();
return false;
}
});
$body.on('keydown', '#tasks input, #tasks textarea', function (e) {
if (e.keyCode === 27) {
$editBTN.click();
return false;
}
});
// CHECKBOX
$body.on('change', '#tasks input[type=checkbox]', function () {
var id = $(this).closest('li').attr('id').substr(1).toNum();
cli.logbook(id);
if ($(this).prop('checked')) {
$('#T' + id).addClass('logged');
} else {
$('#T' + id).removeClass('logged');
}
if ($('#T' + id + ' input[type=checkbox]').attr('checked') !== 'checked') {
cli.today(id).calculate();
}
// Update list count
ui.lists.updateCount();
});
// PRIORITY
$body.on('click', '#tasks .priority', function () {
// Get id of task
var id = $(this).closest('li').attr('id').substr(1).toNum();
// Remove old checkbox colour
$('#T' + id).removeClass(cli.priority(id).get());
// Update priority
cli.priority(id).set();
// Display priority (HTML and Class)
$(this)
.html($.i18n._(cli.priority(id).get()))
.removeClass()
.addClass(cli.priority(id).get() + ' priority');
// Add new checkbox colour
$('#T' + id).addClass(cli.priority(id).get());
});
// EDITING A TASK
$body.on('click', '#tasks .todotxt', function (e) {
$editBTN.removeClass('disabled');
$deleteBTN.removeClass('disabled');
ui.tasks.clicks++;
setTimeout(function () {
ui.tasks.clicks = 0;
}, 300);
var id = $(this).closest('li').attr('id').substr(1).toNum();
if ($('#T' + id).hasClass('expanded')) {
if (ui.tasks.clicks === 2 && id === ui.tasks.lastClicked) {
//Close on double click
$('#tasks .expanded .todotxt').blur();
ui.tasks.selected.collapse($('#tasks .expanded'), "stay_selected");
}
} else {
$('#tasks .selected:not(#T' + id + ')').removeClass('selected');
$('#T' + id).toggleClass('selected');
if (ui.tasks.clicks === 1) {
ui.tasks.lastClicked = id;
} else if (ui.tasks.clicks === 2 && id === ui.tasks.lastClicked) {
ui.tasks.selected.collapse($('#tasks .expanded'));
if (!$(this).parent().hasClass('expanded')) {
ui.tasks.selected.expand($('#T' + id));
}
}
/*if ($('#tasks ul li.selected').length == 0 && $('#tasks ul li.margin').length == 0) {
$editBTN.addClass('disabled');
};*/
}
});
/**********************************
SELECTED TASK
**********************************/
// SAVE NAME WHEN INPUT LOSES FOCUS
$body.on('blur', '#tasks .todotxt input', function () {
var id = $(this).closest('li').attr('id').substr(1).toNum();
//Edit Data
if (id.toString().substr(0, 1) === 's') {
var data = cli.scheduled.edit([id.toString().substr(1)]);
data.content = convertStringToLink(this.value);
cli.scheduled.edit([id.toString().substr(1)], data);
} else {
var data = cli.taskData(id).display();
data.content = convertStringToLink(this.value);
cli.taskData(id).edit(data);
}
});
// SHOW IN TODAY BUTTON
$body.on('click', '#tasks ul li .labels .today', function () {
var id = $(this).closest('li').attr('id').substr(1).toNum();
if ($(this).hasClass('inToday')) {
// Takes out of today
// Checkbox & Label
$('#T' + id).removeClass('today');
$(this).removeClass('inToday').html($.i18n._('showInToday'));
// We need to remove the other node in Next
if (ui.lists.selected() === 'next') {
$('#next li.hidden').map(function () {
if ($(this).attr('data-id').toNum() === id) {
$(this).remove();
}
});
}
//Cli
cli.today(id).remove();
} else {
//Adds to Today
//Checkbox & Label
$('#T' + id).addClass('today');
$(this).addClass('inToday').html($.i18n._('removeFromToday'));
if (ui.lists.selected() === 'next') {
if (cli.taskData(id).display().list !== 'next') {
$('#next').append('<li class="hidden" data-id="' + id + '"></li>');
}
}
cli.today(id).add();
}
if (ui.lists.selected() === 'today') {
ui.tasks.populate(ui.lists.selected());
} else {
ui.tasks.sortStop();
}
//Updates counts
ui.lists.updateCount();
});
// SET DUE DATE
$('#datepicker').datepicker({
onSelect: function () {
var id = $('#tasks .expanded').attr('id').substr(1).toNum();
//Saves Date
var task = cli.taskData(id).display();
task.date = cli.calc.dateConvert($('#datepicker').datepicker('getDate'));
//Draws the date label
$('.date').removeClass().addClass('date ' + cli.calc.prettyDate.difference(task.date)[1]);
cli.taskData(id).edit(task);
//Edits UI
$('#T' + id + ' .date').html(cli.calc.prettyDate.difference(task.date)[0]);
//Runs a bunch of Today functions
cli.calc.date(id);
cli.today(id).calculate();
if (cli.taskData(id).display().today === 'yesAuto' || cli.taskData(id).display().today === 'manual') {
$('#tasks li.expanded div.labels span.today').addClass('inToday').html($.i18n._('removeFromToday'));
} else if (cli.taskData(id).display().today === 'noAuto' || cli.taskData(id).display().today === 'false') {
$('#tasks li.expanded div.labels span.today').removeClass('inToday').html($.i18n._('showInToday'));
}
ui.lists.updateCount();
}
}).children().show();
// REMOVE DATE
$('#removeDate').click(function () {
var id = $('#tasks ul li.expanded').attr('id').substr(1).toNum();
//Hides window thing
$('#date').hide(0);
$('#task' + id + ' .labels .date').html($.i18n._('setDueDate'));
$(document).off('click');
//Removes Date
var data = cli.taskData(id).display();
data.date = '';
//Changes to false if yesAuto or noAuto
if (data.today === 'yesAuto' || data.today === 'noAuto') {
$('#task' + id + ' .labels .today').html($.i18n._('showInToday')).removeClass('inToday');
data.today = 'false';
}
cli.taskData(id).edit(data);
//Runs a bunch of Today functions
cli.calc.date(id);
cli.today(id).calculate();
if (ui.lists.selected() === 'today') {
ui.tasks.populate(ui.lists.selected());
} else {
ui.tasks.sortStop();
}
ui.lists.updateCount();