-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgtdfuncs.js
1267 lines (1205 loc) · 47.5 KB
/
gtdfuncs.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
/*jslint browser: true, devel: true, onevar: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, newcap: true, immed: true */
/*global GTD,unescape,Calendar,jQuery,confirm,window */
/* global unescape: because decodeURIcomponent corrupts some characters in AJAX
but this will change when we get proper i18n/utf8/mbcs handling in gtd-php
global GTD: object for holding public functions and public variables
global Calendar: javascript routine for handling calendar UI, in calendar.js
======================================================================================
*/
(function($) {
var freezediv, focusOn, grabKey, oldTablePosition, sort_column_index;
// ======================================================================================
$.fn.animateShow = function jquery_animateShow(aCallback) {
/*
* this function does the animation for items which have successfully been updated
* It works by extending jQuery, so that we can execute the function on any jQuery object
*/
var that = this,
disp = (!$.browser.msie && this.get(0).tagName.toLowerCase() === "tr") ?
"table-row" : "block";
this.css({ opacity: 0.01, display: disp }).
removeClass("togglehidden hidden").
addClass("updated").
show().
animate({ opacity: 1 }, 600);
setTimeout(function () {
that.removeClass("updated");
if ($.isFunction(aCallback)) { aCallback(that); }
}, 2000);
// always return the jQuery item we came in with, to allow chaining
return that;
};
// ======================================================================================
function search_keypress(e) {
if (e.keyCode===27) {
GTD.parentselect.close();
return false;
}
return true;
}
// ======================================================================================
function keyPressHandler(aEvent) {
/*
* event-handler for key presses, for when we are toggling the display of debug-log text
*
* aEvent: DOM event object
*/
if (aEvent.which !== grabKey) { return true; }
var debugtexts, targetNodeName;
if (aEvent.target && aEvent.target.nodeName) {
targetNodeName = aEvent.target.nodeName.toLowerCase();
if ( targetNodeName === "textarea" ||
(targetNodeName === "input" && aEvent.target.type &&
aEvent.target.type.toLowerCase() === "text")) {
return true;
}
}
debugtexts = $(".debug");
if ($(":visible",debugtexts).length) {
debugtexts.hide();
} else {
debugtexts.show();
}
aEvent.stopPropagation();
return false;
}
// ======================================================================================
function manualcellindex(cell) {
/*
* IE7 calculates cellIndex incorrectly when some cells are hidden,
* so a manual cellIndex is required.
* This won't handle COLSPAN gracefully. I expect.
*
* cell: the DOM object of the cell we want the index of
*/
var index=false;
$(cell).parents('tr').
find('th,td').
each(function mci_each(id){
if (this===cell) {
index=id; // found the cell we were looking for, so record its index
return false; // and quit the mci_each loop
}
});
return index;
}
// ======================================================================================
function checkSaneRecurrenceInterval(aEvent) {
/*
* check that we don't have a crazy recurrence interval
*
* aEvent: jQuery event object for the submit action
*/
var maxInterval, periodName,
fieldToFocus = null,
warning = "",
form = $("#itemform").find(".formerror").removeClass("formerror").end(),
passed = GTD.validate(form.get(0)),
intervalField = form.find("[name=INTERVAL]"),
interval = intervalField.val(),
tickleField = form.find("#tickledate"),
tickleVal = tickleField.val(),
tickleDate = Date.parseDate(tickleVal, "%Y-%m-%d") && tickleVal,
freqType = form.find("[name=FREQtype]:checked").val(),
deadlineVal = form.find("#deadline").val(),
deadline = Date.parseDate(deadlineVal, "%Y-%m-%d") && deadlineVal;
switch (freqType) {
case "DAILY":
maxInterval = 366;
periodName = "days";
break;
case "WEEKLYBYDAY": // no break needed, deliberately flows through to next case
case "WEEKLY":
maxInterval = 27;
periodName = "weeks";
break;
case "MONTHLYBYDAY": // no break needed, deliberately flows through to next case
case "MONTHLYBYWEEK": // no break needed, deliberately flows through to next case
case "MONTHLY":
maxInterval = 27;
periodName = "months";
break;
case "YEARLYBYDATE": // no break needed, deliberately flows through to next case
case "YEARLYBYWEEK": // no break needed, deliberately flows through to next case
case "YEARLY":
maxInterval = 6;
periodName = "years (note that gtd-php cannot handle gaps longer than 10 years)";
break;
case "NORECUR": // no break needed, deliberately flows through to default
default: // probably TEXT
maxInterval = 0;
break;
} // end of switch (freqType)
if (maxInterval && interval > maxInterval) {
// interval is outside recommended limits
warning = warning + "You have selected a recurrence of " + interval +
" " + periodName + ".\n";
GTD.showrecurbox(); // ensure recurrence box is shown
fieldToFocus = intervalField.addClass("formerror");
}
if (deadlineVal && tickleDate > deadline) {
warning = warning +
"The item will be suppressed until after the deadline has passed.\n";
fieldToFocus = tickleField.addClass("formerror");
}
if (warning) {
if (passed && !confirm(warning + "Are you sure that's what you want?")) {
passed = false;
}
if (!passed) {
// validation tests had previously failed, so add warning to error box
warning = warning.replace(/\n/g, "<br/>");
$("#errorMessage").append(warning);
fieldToFocus.select().get(0).focus();
}
}
return passed;
}
// ======================================================================================
function getDocPath(){
return window.location.pathname.replace(/^(.*\/)[^\/]*$/,"$1");
}
// ======================================================================================
/*
* sortTable amended for GTD-PHP
* Based on code from: http://kryogenix.org/code/browser/sorttable/
* Copyright (c) 1997-2007 Stuart Langridge
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function ts_getInnerText(el) {
var str,cs,l,i;
if (typeof el === "string") {return el;}
if (typeof el === "undefined") { return el; }
if (el.innerText) {return el.innerText;} //Not needed but it is faster
str = "";
cs = el.childNodes;
l = cs.length;
for (i = 0; i < l; i++) {
switch (cs[i].nodeType) {
case 1: //ELEMENT_NODE
str += ts_getInnerText(cs[i]);
break;
case 3: //TEXT_NODE
str += cs[i].nodeValue;
break;
}
}
return str;
}
function ts_makeSortable(table) {
var firstRow,cell,txt,i,max;
if (table.rows && table.rows.length > 0) {
firstRow = table.rows[0];
}
if (!firstRow) {return;}
// We have a first row: assume it's the header, and make its contents clickable links
max=firstRow.cells.length;
for (i=0;i<max;i++) {
cell = firstRow.cells[i];
if (cell.className.match('nosort')) {continue;}
txt = ts_getInnerText(cell);
cell.innerHTML = '<a href="#" class="sortheader" '+
'onclick="GTD.resortTable(this);return false;">' +
txt+'</a>';
}
}
function sortables_init() {
var tbls,ti,thisTbl;
// Find all tables with class sortable and make them sortable
if (!document.getElementsByTagName) {return;}
tbls = document.getElementsByTagName("table");
for (ti=0;ti<tbls.length;ti++) {
thisTbl = tbls[ti];
if (((' '+thisTbl.className+' ').indexOf("sortable") !== -1) && thisTbl.id) {
//initTable(thisTbl.id);
ts_makeSortable(thisTbl);
}
}
}
function getParent(el, pTagName) {
if (el === null) {return null;}
else if (el.nodeType === 1 && el.tagName.toLowerCase() === pTagName.toLowerCase()) { // Gecko bug, supposed to be uppercase
return el;
} else {return getParent(el.parentNode, pTagName);}
}
function ts_sort_date(a,b) {
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
var aa,bb,dt1,yr,dt2;
aa = ts_getInnerText(a.cells[sort_column_index]);
bb = ts_getInnerText(b.cells[sort_column_index]);
if (aa.length === 10) {
dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
} else {
yr = aa.substr(6,2);
if (parseInt(yr,10) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
}
if (bb.length === 10) {
dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
} else {
yr = bb.substr(6,2);
if (parseInt(yr,10) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
}
if (dt1===dt2) {return (a.rowIndex-b.rowIndex);}
if (dt1<dt2) {return -100;}
return 100;
}
/*
function ts_sort_currency(a,b) {
var aa,bb,retval;
aa = ts_getInnerText(a.cells[sort_column_index]).replace(/[^0-9.]/g,'');
bb = ts_getInnerText(b.cells[sort_column_index]).replace(/[^0-9.]/g,'');
retval = parseFloat(aa) - parseFloat(bb);
if (retval===0) {return (a.rowIndex-b.rowIndex);} else {return retval;}
}
function ts_sort_numeric(a,b) {
var aa,bb;
aa = parseFloat(ts_getInnerText(a.cells[sort_column_index]));
if (isNaN(aa)) {aa = 0;}
bb = parseFloat(ts_getInnerText(b.cells[sort_column_index]));
if (isNaN(bb)) {bb = 0;}
if (aa===bb) {return (a.rowIndex-b.rowIndex);} else {return aa-bb;}
}
*/
function ts_sort_caseinsensitive(a,b) {
var aa,bb;
aa = ts_getInnerText(a.cells[sort_column_index]).toLowerCase();
bb = ts_getInnerText(b.cells[sort_column_index]).toLowerCase();
if (aa===bb) {return (a.rowIndex-b.rowIndex);}
if (aa<bb) {return -100;}
return 100;
}
function ts_sort_checkbox(a,b) {
var aa,bb;
aa = a.cells[sort_column_index].firstChild.checked;
bb = b.cells[sort_column_index].firstChild.checked;
if (aa===bb) {return (a.rowIndex-b.rowIndex);}
if (aa) {return -100;}
return 100;
}
function ts_sort( lnk, savebodycursor, savethiscursor) {
var max, td, column, table, itm, itmh, newRows, sortfn, thisbody;
// Delete any arrows that may be showing
$('th',table).removeClass("sortup sortdown");
max=lnk.childNodes.length;
td = getParent(lnk,'TD') || getParent(lnk,'TH');
column = manualcellindex(td);
table = getParent(td,'TABLE');
thisbody=table.tBodies[0];
// Work out a type for the column
if (table.rows.length <= 1) {return;}
itm = ts_getInnerText(thisbody.rows[0].cells[column]);
itmh = thisbody.rows[0].cells[column].innerHTML;
sortfn = ts_sort_caseinsensitive;
if (itmh.match(/^<input.*(radio|checkbox).*>$/)) {sortfn = ts_sort_checkbox;}
else if (itm.match(/^\d\d[\/\-]\d\d[\/\-]\d\d\d\d$/)) {sortfn = ts_sort_date;}
else if (itm.match(/^\d\d[\/\-]\d\d[\/\-]\d\d$/)) {sortfn = ts_sort_date;}
//else if (itm.match(/^[£$]/)) {sortfn = ts_sort_currency;}
//else if (itm.match(/^[\d\.]+$/)) {sortfn = ts_sort_numeric;}
sort_column_index = column;
newRows = $( 'tbody tr:not(.sortbottom)', table ).get();
newRows.sort( sortfn );
// add sort marker to this column header
if (lnk.getAttribute("sortdir") === 'down') {
$(td).addClass("sortup");
newRows.reverse();
lnk.setAttribute('sortdir','up');
} else {
$(td).addClass("sortdown");
lnk.setAttribute('sortdir','down');
}
newRows = newRows.concat( $('tbody tr.sortbottom',table ).get() );
// We append rows that already exist to the tbody, so it moves them rather than creating new ones
$(thisbody).append( newRows );
document.body.style.cursor = savebodycursor;
lnk.style.cursor = savethiscursor;
}
/*
end of sorting functions
======================================================================================
start of declarations of public functions
*/
/* Copyright Mihai Bazon, 2002, 2003 | http://dynarch.com/mishoo/
* ---------------------------------------------------------------------------
* modified for gtd-php
*
* The DHTML Calendar
*
* Details and latest version at:
* http://dynarch.com/mishoo/calendar.epl
*
* This script is distributed under the GNU Lesser General Public License.
* Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
*
* This file defines helper functions for setting up the calendar. They are
* intended to help non-programmers get a working calendar on their site
* quickly. This script should not be seen as part of the calendar. It just
* shows you what one can do with the calendar, while in the same time
* providing a quick and simple method for setting it up. If you need
* exhaustive customization of the calendar creation process feel free to
* modify this code to suit your needs (this is recommended and much better
* than modifying calendar.js itself).
*/
// $Id: calendar-setup.js,v 1.2 2006/07/08 19:20:34 serge Exp $
/**
* This function "patches" an input field (or other element) to use a calendar
* widget for date selection.
*
* The "params" is a single object that can have the following properties:
*
* prop. name | description
* -------------------------------------------------------------------------------------------------
* inputField | the ID of an input field to store the date
* displayArea | the ID of a DIV or other element to show the date
* button | ID of a button or other element that will trigger the calendar
* eventName | event that will trigger the calendar, without the "on" prefix (default: "click")
* ifFormat | date format that will be stored in the input field
* daFormat | the date format that will be used to display the date in displayArea
* singleClick | (true/false) wether the calendar is in single click mode or not (default: true)
* firstDay | numeric: 0 to 6. "0" means display Sunday first, "1" means display Monday first, etc.
* align | alignment (default: "Br"); if you don't know what's this see the calendar documentation
* range | array with 2 elements. Default: [1900, 2999] -- the range of years available
* weekNumbers | (true/false) if it's true (default) the calendar will display week numbers
* flat | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID
* flatCallback | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar)
* disableFunc | function that receives a JS Date object and should return true if that date has to be disabled in the calendar
* onSelect | function that gets called when a date is selected. You don't _have_ to supply this (the default is generally okay)
* onClose | function that gets called when the calendar is closed. [default]
* onUpdate | function that gets called after the date is updated in the input field. Receives a reference to the calendar.
* date | the date that the calendar will be initially displayed to
* showsTime | default: false; if true the calendar will include a time selector
* timeFormat | the time format; can be "12" or "24", default is "12"
* electric | if true (default) then given fields/date areas are updated for each move; otherwise they're updated only on close
* step | configures the step of the years in drop-down boxes; default: 2
* position | configures the calendar absolute position; default: null
* cache | if "true" (but default: "false") it will reuse the same calendar object, where possible
* showOthers | if "true" (but default: "false") it will show days from other months too
*
* None of them is required, they all have default values. However, if you
* pass none of "inputField", "displayArea" or "button" you'll get a warning
* saying "nothing to setup".
*/
if (typeof Calendar!=='undefined') {
Calendar.setup = function Cal_setup(params) {
var tmp, i;
function param_default(pname, def) { if (typeof params[pname] === "undefined") { params[pname] = def; } }
param_default("inputField", null);
param_default("displayArea", null);
param_default("button", null);
param_default("eventName", "click");
param_default("ifFormat", "%Y-%m-%d");
param_default("daFormat", "%Y-%m-%d");
param_default("singleClick", true);
param_default("disableFunc", null);
param_default("dateStatusFunc", params.disableFunc); // takes precedence if both are defined
param_default("dateText", null);
param_default("firstDay", null);
param_default("align", "Br");
param_default("range", [1900, 2999]);
param_default("weekNumbers", true);
param_default("flat", null);
param_default("flatCallback", null);
param_default("onSelect", null);
param_default("onClose", null);
param_default("onUpdate", null);
param_default("date", null);
param_default("showsTime", false);
param_default("timeFormat", "24");
param_default("electric", true);
param_default("step", 2);
param_default("position", null);
param_default("cache", false);
param_default("showOthers", false);
param_default("multiple", null);
tmp = ["inputField", "displayArea", "button"];
for (i in tmp) {
if (typeof params[tmp[i]] === "string") {
params[tmp[i]] = document.getElementById(params[tmp[i]]);
}
}
if (!(params.flat || params.multiple || params.inputField || params.displayArea || params.button)) {
alert("Calendar.setup:\n Nothing to setup (no fields found). Please check your code");
return false;
}
function onSelect(cal) {
var p = cal.params, update = (cal.dateClicked || p.electric);
if (update && p.inputField) {
p.inputField.value = cal.date.print(p.ifFormat);
if (typeof p.inputField.onchange === "function") {
p.inputField.onchange();
}
}
if (update && p.displayArea) {
p.displayArea.innerHTML = cal.date.print(p.daFormat);
}
if (update && typeof p.onUpdate === "function") {
p.onUpdate(cal);
}
if (update && p.flat) {
if (typeof p.flatCallback === "function") {
p.flatCallback(cal);
}
}
if (update && p.singleClick && cal.dateClicked) {
cal.callCloseHandler();
}
}
//------------------------------------------------------------------------
function showCal() {
var i, d, ds,
dateEl = params.inputField || params.displayArea,
dateFmt = params.inputField ? params.ifFormat : params.daFormat,
mustCreate = false,
cal = window.calendar;
if (dateEl) {
params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
}
if (!(cal && params.cache)) {
window.calendar = cal = new Calendar(params.firstDay,
params.date,
params.onSelect || onSelect,
params.onClose || function cal_hide(cal) { cal.hide(); });
cal.showsTime = params.showsTime;
cal.time24 = (params.timeFormat === "24");
cal.weekNumbers = params.weekNumbers;
mustCreate = true;
} else {
if (params.date) {cal.setDate(params.date);}
cal.hide();
}
if (params.multiple) {
cal.multiple = {};
for (i = params.multiple.length; --i >= 0;) {
d = params.multiple[i];
ds = d.print("%Y%m%d");
cal.multiple[ds] = d;
}
}
cal.showsOtherMonths = params.showOthers;
cal.yearStep = params.step;
cal.setRange(params.range[0], params.range[1]);
cal.params = params;
cal.setDateStatusHandler(params.dateStatusFunc);
cal.getDateText = params.dateText;
cal.setDateFormat(dateFmt);
if (mustCreate) {cal.create();}
cal.refresh();
if (!params.position) {
cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
} else {
cal.showAt(params.position[0], params.position[1]);
}
return false;
}
//------------------------------------------------------------------------
/* these events are to show the calendar:
* if a button is present, it will be used as the trigger, else
* the input field itself will be used
*/
$(params.button || params.inputField).bind(params.eventName, showCal);
//if (params.displayArea) {$(params.displayArea).bind(params.eventName, showCal);}
};
}
// ======================================================================================
if (typeof window.GTD === "undefined") { window.GTD = {}; }
GTD = window.GTD;
// ======================================================================================
GTD.checkRecurBase = function gtd_checkRecurBase(aEvent) {
var ok,
that = this,
testDate = Date.parseDate($(this).val(), "%Y-%m-%d"),
now = new Date(),
today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
if ( testDate.valueOf() < today.valueOf() ) {
ok = confirm("Date is in the past - are you sure?");
if (ok) {
// we've warned once, and the user has said OK, so don't ask again
$("#deadline,#tickledate,#UNTIL").
unbind("change",GTD.checkRecurBase).
change(GTD.checkRecurrence);
}
else {
// owing to browser silliness with propagation of change events,
// we need to lag the refocussing of this field
setTimeout(function refocusAfterPastDate() {
that.focus();
that.select();
}, 100);
}
return ok && GTD.checkRecurrence(aEvent);
}
};
// ======================================================================================
GTD.checkRecurrence = function checkRecurrence(aEvent) {
/*
* if we have a recurrence pattern that's derived from a specific date,
* e.g. first Monday in September, then when the date field changes, ask the user
* if they want to update the recurrence pattern too
*
* aEvent: the jQuery event
*/
var that = aEvent.target,
thisform = $(that).parents('form'),
recurField = $("[name=FREQtype]:checked"),
freqtype = recurField.val();
if ( (that.id === 'tickledate' && thisform.find('#deadline').val() !== "" ) ||
("NORECUR DAILY WEEKLY MONTHLY YEARLY".match(freqtype)!==null) ) {
return true;
}
GTD.showrecurbox();
recurField.parents("tr,div").filter(":last").animateShow();
return true;
};
// ======================================================================================
GTD.completeToday = function gtd_completetoday(datefield) {
/*
* enter today's date into the completion date field
*
* datefield: string of the id of the date field
*/
var now,m,d,y,newdate;
now=new Date();
m = now.getMonth()+1;
d = now.getDate();
y = now.getFullYear();
m=(m < 10) ? ("0" + m) : m;
d=(d < 10) ? ("0" + d) : d;
newdate=""+y+"-"+m+"-"+d;
document.getElementById(datefield).value=newdate;
// return true;
};
// ======================================================================================
GTD.confirmDelete=function gtd_confirmedelete(elem) {
/*
* confirm that the user wishes to delete the current item in item.php
* elem:
*/
var btn,
form = $(elem).closest("form");
if (!confirm("Delete this item?")) { return false; }
form.find("#doDelete").val("y");
btn = form.find("[name=submit],input[type=submit]").eq(0);
if (btn.length) { return btn.click(); }
return form.submit();
};
// ======================================================================================
GTD.cookieGet=function gtd_cookieget(name,path) {
/*
* get a cookie
* returns: value of the cookie
*
* name: name of the cookie
*/
if (!document.cookie) { return null; }
var i, max, cookie, cookies,
testval=encodeURIComponent(name)+'=',
namelen=testval.length;
cookies = document.cookie.split(';');
max=cookies.length;
for (i=0; i<max; i++) {
cookie = cookies[i].replace(/^ *(.*) *$/,"$1");
// Does this cookie string begin with the name we want?
if (cookie.substring(0, namelen)===testval) {
return decodeURIComponent(cookie.substring(namelen));
}
}
return null;
};
//--------------------------------------------------
GTD.cookieSet=function gtd_cookieset(name,value,path,maxagedays) {
/*
* Set a cookie
*
* name: name of the cookie
* value: value of the cookie
* path: path of the cookie (defaults to current path)
* maxagedays: number of days until cookie should expire (defaults to 1 year)
*/
var maxage,expires;
if (path===undefined) {path=getDocPath();}
if (maxagedays===undefined) {maxagedays=365*3;} // 3 years
expires=new Date();
maxage=maxagedays * 24 * 60 * 60;
if (value === null) {
value = '';
maxage = -1;
expires.setTime(expires.getTime() - 1);
} else {
expires.setTime(expires.getTime() + maxage*1000);
}
document.cookie = encodeURIComponent(name)+'='+encodeURIComponent(value)+
'; max-age='+maxage+
'; expires='+expires.toGMTString()+
'; path='+path;
};
// ======================================================================================
GTD.createparent=function gtd_createparent(type) {
/*
* create a parent for the item we are currently creating/editing
*
* type: (single letter) type of parent to be created
*/
document.forms[0].afterCreate.value=
document.forms[0].referrer.value=
'item.php?nextId='+document.forms[0].itemId.value+'&type='+type;
document.forms[0].submit();
return false;
};
// ======================================================================================
GTD.debugInit=function gtd_debuginit(keyToCatch) {
/*
* initialise the key-handler, passing through the user-specified key that will toggle display of debug-logs
*
* keyToCatch: key to toggle, e.g. 'h'
*/
grabKey=keyToCatch.charCodeAt(0);
$(document).keypress(keyPressHandler);
};
// ======================================================================================
GTD.filtertoggle=function gtd_filtertoggle(which) {
/*
* toggle the enabled/disabled status of form elements in the filter form in listItems
*
* which:
*/
var box,max,isOn,i;
box=document.getElementById('everything');
if (which==='all') {
isOn=false;
} else {isOn=box.checked;}
max=box.form.elements.length;
for (i=0;i<max;i++) {
if (typeof box.form.elements[i].disabled!=='undefined') {
box.form.elements[i].disabled=isOn;
}
}
// always force the core filter items to be enabled:
document.getElementById('type').disabled=
document.getElementById('needle').disabled=
document.getElementById('filtersubmit').disabled=
document.getElementById('tags').disabled=
box.disabled=false;
box.focus();
return true;
};
// ======================================================================================
GTD.focusOnForm=function gtd_focusonform(id) {
/*
* pass the focus to a specific item on a form
*
* id: if present, this will specify the id of the form element to be focussed.
* In its absence, if we've previously called this function, then recall which
* element was focussed then (stored in global variable focusOn), and focus on that again.
* If there is no id, and no focusOn, then the first active form element will receive focus
*/
var tst,i;
if (typeof(id)==='string') {
document.getElementById(id).focus();
focusOn=id;
}
if(typeof(focusOn)==='string') {return;}
if (document.forms.length) {
for (i = 0; i < document.forms[0].length; i++) {
tst=document.forms[0].elements[i].type;
if ( (tst === "button") || (tst === "checkbox") || (
tst === "radio") || (tst === "select") ||
(tst === "select-one") || (tst === "text") ||
(tst === "textarea") ) {
if (!document.forms[0].elements[i].disabled) {
try {
document.forms[0].elements[i].focus();
} catch(err) {
continue;
}
break;
}
}
}
}
};
// ======================================================================================
GTD.freeze=function gtd_freeze(tofreeze) {
/*
* hide all on-screen elements with a blanket DIV laid over everything
*
* tofreeze: boolean - true if overlaying blanket, false if removing it
*/
if (typeof freezediv==='undefined') { // blanket DIV doesn't exist yet, so create it
freezediv=document.createElement('div');
freezediv.id="freezer";
freezediv.style.display="none";
document.body.appendChild(freezediv);
}
freezediv.style.display=(tofreeze)?"block":"none";
};
// ======================================================================================
GTD.initItem = function gtd_initItem(container) {
/*
* initialising an item form
*
* container: DOM element of item DIV being processed
*/
// initialise the calendars
var firstDayOfWeek = parseInt($("#firstDayOfWeek",container).val(), 10);
$("input.hasdate", container).each(
function setCalendarEvent() {
var that = this,
id = this.id;
Calendar.setup( { firstDay: firstDayOfWeek,
inputField: id,
button: id + "_trigger",
onUpdate: function calendarSelected(cal) {
return $(that).triggerHandler("change");
}
});
}
);
$("form", container).bind("submit", checkSaneRecurrenceInterval);
$("#deadline,#tickledate,#UNTIL", container).change(
($("[name=itemId]",container).val() === "0") ?
GTD.checkRecurBase :
GTD.checkRecurrence );
};
// ======================================================================================
GTD.ParentSelector=function Gtd_parentselector(ids,titles,types,onetype) {
/*
* constructor for the dynamic parent-selector in item.php
*
* ids:
* titles:
* types:
* onetype:
*/
var i,line,type,typename,useTypes,max,
box=document.getElementById('searchresults');
this.inSearch=false;
this.ptitleslc=[];
this.ptypes=[];
this.ptitles=[];
this.parentIds=[];
this.editingrow=-1;
this.parentIds=ids;
this.ptypes=types;
this.qtype=onetype;
if (box.hasChildNodes()) {box.removeChild(box.lastChild);}
useTypes=(types.length>0);
if (!useTypes) {
typename=GTD.typenames[onetype];
type=onetype;
}
max=titles.length;
for (i=0;i<max;i++) {
this.ptitles[i]=unescape(titles[i]);
this.ptitleslc[i]=this.ptitles[i].toLowerCase();
if (useTypes) {
typename=GTD.typenames[types[i]];
type=types[i];
}
line=this.makeline(this.parentIds[i],this.ptitles[i],type,typename,i,useTypes,onetype);
box.appendChild(line);
}
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.close=function gtd_ps_close() {
/*
* close the parent-selector
*/
$(document).unbind("keydown",search_keypress);
document.getElementById('searcher').style.display='none';
GTD.freeze(false);
document.getElementById('parenttable').style.position=oldTablePosition;
GTD.focusOnForm(0);
this.inSearch=false;
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.gocreateparent=function gtd_ps_gcp(id,title,type,typename,rownum) {
/*
* user has requested to create a new item, which will become the parent of the current item
*
* id:
* title:
* type:
* typename:
* rownum:
*/
return GTD.createparent(type);
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.gotparent=function gtd_ps_gp(id,title,type,typename,rownum) {
/*
* add the clicked parent to the list of the item's parents
*
* id:
* title:
* type:
* typename:
* rownum:
*/
var input;
if ( "0" === id ) {
this.gocreateparent(id,title,type,typename,rownum);
return false;
}
if (document.getElementById('parentrow'+id)) {return;}
try {
input = document.createElement("<input type='hidden' name='parentId[]' value='" + id + "' />");
}
catch (err) {
input = document.createElement('input');
input.type='hidden';
input.name='parentId[]';
input.value=id;
}
$("<tr>").
attr( "id", "parentrow" + id ).
append( $( "<td>" ).append( $("<a>").
attr( { href: "#", title: "remove as parent" } ).
click( function gtd_click_rp(){return GTD.removeParent(id);} ).
addClass( "remove" ).
text( "X" ) ) ).
append( $( "<td>" ).append( $( "<a>" + title + "</a>" ).
attr( { href: "itemReport.php?itemId="+id, title: "view parent" } ) ) ).
append( $( "<td>" ).text( typename ).append( input ) ).
appendTo( $("#parentlist") );
$("#parenttable").
after( $("<p>").
addClass( "warning" ).
text( "changes are not yet saved" ) ).
next(".warning").next(".warning").remove();
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.makeline=function gtd_ps_ml(id,title,type,typename,i,useTypes,onetype) {
/*
* create a line to go into the list of parents, displaying a specific parent
*
* id:
* title:
* type:
* typename:
* i:
* useTypes:
* onetype:
*/
var that=this,
line=$("<p>" + title + ( useTypes ? " ("+typename+")" : "" ) + "</p>").
css("display", (!useTypes || typename===onetype) ? "block" : "none" ).
prepend($('<a>').
attr("href", "#").
click(function anchor_click() {
that.gotparent(id,title,type,typename,"");
}).
text("+").
addClass("add") );
return line.get(0);
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.refinesearch=function gtd_ps_rs(needle) {
/*
* refine the list of parents displayed, based on a partial string entered by the user
*
* needle:
*/
var i,max,ok,box,skiptype,skipsearch,
searchstring=document.getElementById('searcherneedle').value.toLowerCase();
box=document.getElementById('searchresults');
if (typeof(needle)==='string') { // initialising
this.qtype=needle;
document.getElementById('searcherneedle').value='';
document.getElementById('radio'+this.qtype).checked=true;
} else if (needle.name==='qtype') {
this.qtype=needle.value;
}
skiptype=(this.ptypes.length<2 || this.qtype==='0');
skipsearch=(searchstring.length===0);
max=this.parentIds.length;
for (i=0;i<max;i++) {
ok= ( ( skipsearch ||
(this.ptitleslc[i].indexOf(searchstring)>-1) ||
this.parentIds[i]==='0') &&
(skiptype || this.ptypes[i]===this.qtype) );
box.childNodes[i].style.display=(ok)?'block':'none';
}
};
// -------------------------------------------------------------------------
GTD.ParentSelector.prototype.search=function gtd_ps_s() {
/*
* something to do with setting up the auto-search in the parent box
*
*/
var parenttable;
if (this.inSearch) {return false;}
this.inSearch=true;
GTD.freeze(true);
document.getElementById('searcher').style.display='block';
document.getElementById("searcherneedle").focus();
$(document).bind("keydown",search_keypress);
parenttable=document.getElementById('parenttable');
oldTablePosition=parenttable.style.position;
parenttable.style.position='fixed';
parenttable.style.left=0;
};
// -------------------------------------------------------------------------
GTD.removeParent=function gtd_rp(id) {
/*
* remove a parent from the displayed list of parents for this item
*
* id: string or integer- the itemId of the parent being removed
*/
var row=document.getElementById('parentrow'+id);
row.parentNode.removeChild(row);