-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdate.js
More file actions
1416 lines (1261 loc) · 47.5 KB
/
date.js
File metadata and controls
1416 lines (1261 loc) · 47.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
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
/**
* Implements hook_field_formatter_view().
*/
function date_field_formatter_view(entity_type, entity, field, instance, langcode, items, display) {
try {
//console.log(field);
//console.log(instance);
//console.log(display);
//console.log(items);
//console.log('date_formats', drupalgap.date_formats);
//console.log('date_types', drupalgap.date_types);
var element = {};
// What type of display are we working with?
// Manage Display - Format
// date_default = Date and time
// format_interval = Time ago
var type = display.type;
if (type == 'date_default') {
var format = null;
if (drupalgap.date_formats[display.settings.format_type]) {
// Since we're unable to locate the format to use within the field or the
// instance, we'll just use the first format type in the collection.
var format_type = drupalgap.date_formats[display.settings.format_type];
$.each(format_type, function(index, object) {
format_type = object;
return false;
});
format = format_type.format;
}
else {
// This is (probably) a custom date format, grab the format that
// the drupalgap.module has bundled within the date_types.
format = drupalgap.date_types[display.settings.format_type].format;
}
// Strip out any characters from the format that are not included in the granularity.
format = date_format_cleanse(format, instance.settings.granularity);
// Now iterate over the items and render them using the format.
// @TODO might need to do the "T" stuff for iOS and/or Safari
$.each(items, function(delta, item) {
var value2_present = typeof item.value2 !== 'undefined' ? true: false;
var label = value2_present ? 'From: ' : '';
var d = date_prepare(item.value);
element[delta] = {
markup: '<div class="value">' + label + date(format, d.getTime()) + '</div>'
};
if (value2_present) {
var d2 = date_prepare(item.value2);
element[delta].markup += '<div class="value2">To: ' + date(format, d2.getTime()) + '</div>';
}
});
}
else if (type == 'format_interval') {
var interval = display.settings.interval;
var interval_display = display.settings.interval_display;
var now = new Date();
$.each(items, function(delta, item) {
var d = date_prepare(item.value);
if (interval_display == 'time ago' || interval_display == 'raw time ago') {
var markup = drupalgap_format_interval(
(now.getTime() - d.getTime()) / 1000,
interval
);
if (interval_display == 'time ago') { markup += ' ' + t('ago'); }
element[delta] = { markup: markup };
}
else {
console.log('WARNING: date_field_formatter_view - unsupported interval_display (' + interval_display + ')');
}
});
}
else {
console.log('WARNING: date_field_formatter_view - unsupported type (' + type + ')');
}
return element;
}
catch (error) { console.log('date_field_formatter_view - ' + error); }
}
/**
* Implements hook_field_widget_form().
*/
function date_field_widget_form(form, form_state, field, instance, langcode, items, delta, element) {
try {
//console.log(form);
//console.log(form_state);
//console.log(field);
//console.log(instance);
//console.log(langcode);
//console.log(items);
//console.log(delta);
//console.log(element);
// Convert the item into a hidden field that will have its value populated dynamically by the widget. We'll store
// the value (and potential value2) within the element using this format: YYYY-MM-DD HH:MM:SS|YYYY-MM-DD HH:MM:SS
items[delta].type = 'hidden';
// Determine if the "to date" is disabled, optional or required.
var todate = field.settings.todate; // '', 'optional', 'required'
// Grab the minute increment.
var increment = parseInt(instance.widget.settings.increment);
var d = new Date();
d.setMinutes(_date_minute_increment_adjust(increment, d.getMinutes()));
// Check and set default values, and items[delta] values.
var date_values_set = _date_widget_check_and_set_defaults(items, delta, instance, d);
var value_set = date_values_set.value_set;
var value2_set = date_values_set.value2_set;
// If we have a value2, append it to our hidden input's value and default value. We need to set the value attribute
// on this item, otherwise the DG FAPI will default it to the item's value, which is only the first part of the
// date.
if (value2_set && items[delta].value.indexOf('|') == -1) {
items[delta].value += '|' + items[delta].value2;
if (!items[delta].attributes) { items[delta].attributes = {}; }
items[delta].attributes.value = items[delta].value;
}
// Grab the current date.
var date = new Date();
// Depending if we are collecting an end date or not, build a widget for each date value.
var values = ['value'];
if (!empty(todate)) { values.push('value2'); }
$.each(values, function(_index, _value) {
// Get the item date and offset, if any.
var date_and_offset = _date_get_item_and_offset(items, delta, _value, value_set, value2_set, field);
var item_date = date_and_offset.item_date;
var offset = date_and_offset.offset;
//var timezone = date_and_offset.timezone ? date_and_offset.timezone : null;
//var timezone_db = date_and_offset.timezone_db ? date_and_offset.timezone_db : null;
//if (timezone && offset) {
// var difference = drupalgap.time_zones[timezone] - offset;
//}
// Are we doing a 12 or 24 hour format?
var military = date_military(instance);
// For each grain of the granularity, add it as a child to the form element. As we
// build the child widgets we'll set them aside one by one that way we can present
// the inputs in a desirable order later at render time.
var _widget_year = null;
var _widget_month = null;
var _widget_day = null;
var _widget_hour = null;
var _widget_minute = null;
var _widget_second = null;
var _widget_ampm = null;
$.each(field.settings.granularity, function(grain, value) {
if (value) {
// Build a unique html element id for this select list. Set up an
// onclick handler and send it the id of the hidden input that will
// hold the date value.
var id = items[delta].id;
if (_value == 'value2') { id += '2'; } // "To date"
id += '-' + grain;
var attributes = {
id: id,
onchange: "date_select_onchange(this, '" + items[delta].id + "', '" + grain + "', " + military + ", " + increment + ", " + offset + ")"
};
switch (grain) {
// YEAR
case 'year':
_widget_year = _date_grain_widget_year(date, instance, attributes, value_set, value2_set, item_date);
break;
// MONTH
case 'month':
_widget_month = _date_grain_widget_month(date, instance, attributes, value_set, value2_set, item_date);
break;
// DAY
case 'day':
_widget_day = _date_grain_widget_day(date, instance, attributes, value_set, value2_set, item_date);
break;
// HOUR
case 'hour':
_widget_hour = _date_grain_widget_hour(date, instance, attributes, value_set, value2_set, item_date, military);
// Add an am/pm selector if we're not in military time. Hang onto the old value so we
// can prevent the +/- 12 adjustment from happening if the user selects the same
// thing twice.
if (!military) {
var onclick = attributes.onchange.replace(grain, 'ampm') +
'; this.date_ampm_old_value = this.value;';
var ampm_value = parseInt(item_date.getHours()) < 12 ? 'am' : 'pm';
_widget_ampm = {
type: 'select',
attributes: {
id: attributes.id.replace(grain, 'ampm'),
onclick: onclick,
date_ampm_original_value: ampm_value
},
value: ampm_value,
options: {
am: 'am',
pm: 'pm'
}
};
}
break;
// MINUTE
case 'minute':
_widget_minute = _date_grain_widget_minute(date, instance, attributes, value_set, value2_set, item_date, _value, increment);
break;
// SECOND
case 'second':
_widget_second = _date_grain_widget_second(date, instance, attributes, value_set, value2_set, item_date, _value);
break;
default:
console.log('WARNING: date_field_widget_form() - unsupported grain! (' + grain + ')');
break;
}
}
});
// Show the "from" or "to" label?
if (!empty(todate)) {
var text = _value != 'value2' ? t('From') : t('To');
items[delta].children.push({ markup: theme('header', { text: text + ': ' }) });
}
// Wrap the widget with some better UX.
_date_grain_widgets_ux_wrap(
items,
delta,
_widget_year,
_widget_month,
_widget_day,
_widget_hour,
_widget_minute,
_widget_second,
_widget_ampm
);
});
// If the field base is configured for the "date's timezone handling", add a timezone picker to the widget.
if (date_tz_handling_is_date(field)) {
var tz_options = {};
$.each(drupalgap.time_zones, function(tz, _offset) { tz_options[tz] = tz; });
var _widget_tz_handling = {
type: 'select',
options: tz_options,
title: t('Timezone'),
attributes: {
id: items[delta].id + '-timezone'
}
};
if (value_set && items[delta].item.timezone) { // Set timezone for existing value.
_widget_tz_handling.value = items[delta].item.timezone;
}
else if (!value_set && field.settings.timezone_db) { // Set timezone for new value.
_widget_tz_handling.value = field.settings.timezone_db;
}
items[delta].children.push(_widget_tz_handling);
}
}
catch (error) {
console.log('date_field_widget_form - ' + error);
}
}
function _date_grain_widget_year(date, instance, attributes, value_set, value2_set, item_date) {
try {
// Determine the current year and the range of year(s) to provide
// as options. The range can either be relative, absolute or both,
// e.g. -3:+3, 2000:2010, 2000:+3
var year = parseInt(date.getFullYear());
var year_range = instance.widget.settings.year_range;
var parts = year_range.split(':');
// Determine the low end year integer value.
var low = parts[0];
var low_absolute = true;
if (low.indexOf('-') != -1 || low.indexOf('+') != -1) { low_absolute = false; }
if (!low_absolute) {
if (low.indexOf('+') != -1) {
low = low.replace('+', '');
}
low = parseInt(low) + year;
}
else { low = parseInt(low); }
if (!low) { low = year; }
// Determine the high end year integer value.
var high = parts[1];
var high_absolute = true;
if (high.indexOf('-') != -1 || high.indexOf('+') != -1) { high_absolute = false; }
if (!high_absolute) {
if (high.indexOf('+') != -1) {
high = high.replace('+', '');
}
high = parseInt(high) + year;
}
else { high = parseInt(high); }
if (!high) { high = year; }
// Build the options.
var options = {};
for (var i = low; i <= high; i++) {
options[i] = i;
}
// Parse the year from the item's value, if it is set.
if (value_set) { year = parseInt(item_date.getFullYear()); }
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Year') }),
type: 'date_select',
value: year,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_year', error); }
}
function _date_grain_widget_month(date, instance, attributes, value_set, value2_set, item_date) {
try {
// Determine the current month.
var month = parseInt(date.getMonth()) + 1;
// Build the options.
var options = {};
for (var i = 1; i <= 12; i++) {
options[i] = '' + i;
}
// Parse the month from the item's value, if it is set.
if (value_set) { month = parseInt(item_date.getMonth()) + 1; }
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Month') }),
type: 'date_select',
value: month,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_month', error); }
}
function _date_grain_widget_day(date, instance, attributes, value_set, value2_set, item_date) {
try {
// Determine the current day.
var day = parseInt(date.getDate());
// Build the options.
var options = {};
for (var i = 1; i <= 31; i++) {
options[i] = '' + i;
}
// Parse the day from the item's value, if it is set.
if (value_set) { day = parseInt(item_date.getDate()); }
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Day') }),
type: 'date_select',
value: day,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_day', error); }
}
function _date_grain_widget_hour(date, instance, attributes, value_set, value2_set, item_date, military) {
try {
// Determine the current hour.
var hour = parseInt(date.getHours());
// Build the options, paying attention to 12 vs 24 hour format.
var options = {};
var max = military ? 23 : 12;
var min = military ? 0 : 1;
for (var i = min; i <= max; i++) { options[i] = '' + i; }
// Parse the hour from the item's value, if it is set.
if (value_set) {
hour = parseInt(item_date.getHours());
if (!military) {
if (hour > 12) { hour -= 12; }
else if (hour === 0) { hour = 12; }
}
}
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Hour') }),
type: 'date_select',
value: hour,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_hour', error); }
}
function _date_grain_widget_minute(date, instance, attributes, value_set, value2_set, item_date, _value, increment) {
try {
// Determine the current minute.
var minute = parseInt(date.getMinutes());
// Build the options.
var options = {};
for (var i = 0; i <= 59; i += increment) {
var text = '' + i;
if (text.length == 1) { text = '0' + text; }
options[i] = text;
}
// Parse the minute from the item's value, if it is set.
if (value_set && _value == 'value') { minute = parseInt(item_date.getMinutes()); }
else if (value2_set && _value == 'value2') { minute = parseInt(item_date.getMinutes()); }
if (increment != 1) {
minute = _date_minute_increment_adjust(increment, minute);
}
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Minute') }),
type: 'date_select',
value: minute,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_minute', error); }
}
function _date_grain_widget_second(date, instance, attributes, value_set, value2_set, item_date, _value) {
try {
// Determine the current second.
var second = parseInt(date.getSeconds());
// Build the options.
var options = {};
for (var i = 0; i <= 59; i ++) {
var text = '' + i;
if (text.length == 1) { text = '0' + text; }
options[i] = text;
}
// Parse the second from the item's value, if it is set.
if (value_set && _value == 'value') { second = parseInt(item_date.getSeconds()); }
else if (value2_set && _value == 'value2') { second = parseInt(item_date.getSeconds()); }
// Build and theme the select list.
return {
prefix: theme('date_label', { title: t('Second') }),
type: 'date_select',
value: second,
attributes: attributes,
options: options
};
}
catch (error) { console.log('_date_grain_widget_second', error); }
}
function _date_grain_widgets_ux_wrap(items, delta, _widget_year, _widget_month, _widget_day, _widget_hour, _widget_minute, _widget_second, _widget_ampm) {
try {
// Add the children widgets in the order of "y-m-d h-i-s", and wrap them in
// jQM grids as necessary to help with UX...
// YMD
var ymd_grid = null;
if (_widget_month && !_widget_day) { ymd_grid = 'ui-grid-a'; }
else if (_widget_month && _widget_day) { ymd_grid = 'ui-grid-b'; }
if (ymd_grid) {
items[delta].children.push({ markup: '<div class="' + ymd_grid + '">' });
}
if (_widget_year) {
if (ymd_grid) {
_widget_year.prefix = '<div class="ui-block-a">' + _widget_year.prefix;
_widget_year.suffix = '</div>';
}
items[delta].children.push(_widget_year);
}
if (_widget_month) {
if (ymd_grid) {
_widget_month.prefix = '<div class="ui-block-b">' + _widget_month.prefix;
_widget_month.suffix = '</div>';
}
items[delta].children.push(_widget_month);
}
if (_widget_day) {
if (ymd_grid) {
var _block_class = _widget_month ? 'ui-block-c' : 'ui-block-b';
_widget_day.prefix = '<div class="' + _block_class + '">' + _widget_day.prefix;
_widget_day.suffix = '</div>';
}
items[delta].children.push(_widget_day);
}
if (ymd_grid) { items[delta].children.push({ markup: '</div>' }); }
// HIS
var his_grid = null;
if (_widget_hour) {
if (_widget_minute && !_widget_second) { his_grid = 'ui-grid-a'; }
else if (_widget_minute && _widget_second) { his_grid = 'ui-grid-b'; }
}
else {
if (_widget_minute && _widget_second) { his_grid = 'ui-grid-b'; }
}
if (his_grid) {
items[delta].children.push({ markup: '<div class="' + his_grid + '">' });
}
if (_widget_hour) {
if (his_grid) {
_widget_hour.prefix = '<div class="ui-block-a">' + _widget_hour.prefix;
_widget_hour.suffix = '</div>';
}
items[delta].children.push(_widget_hour);
}
if (_widget_minute) {
if (his_grid) {
var _block_class = 'ui-block-a';
if (_widget_hour) { _block_class = 'ui-block-b'; }
_widget_minute.prefix = '<div class="' + _block_class + '">' + _widget_minute.prefix;
_widget_minute.suffix = '</div>';
}
items[delta].children.push(_widget_minute);
}
if (_widget_second) { items[delta].children.push(_widget_second); }
if (_widget_ampm) { items[delta].children.push(_widget_ampm); }
if (ymd_grid) { items[delta].children.push({ markup: '</div>' }); }
}
catch (error) { console.log('_date_grain_widgets_ux_wrap', error); }
}
/**
*
* @param value
* @returns {Date}
*/
function date_prepare(value, offset) {
try {
// @see http://stackoverflow.com/a/16664730/763010
if (date_apple_device()) { value = date_apple_cleanse(value); }
var replaced = value.replace(/-/g,'/');
// Parse the date from the string. Note that iOS doesn't like date parse,
// we break it into parts instead.
if (!date_apple_device()) {
return new Date(Date.parse(replaced));
}
else {
var a = replaced.split(/[^0-9]/);
var d = new Date(a[0], a[1]-1, a[2], a[3], a[4], a[5]);
return d;
}
}
catch (error) { console.log('date_prepare() - ' + error); }
}
/**
* Returns true if the device is an Apple device
*/
function date_apple_device() {
return (typeof device !== 'undefined' && device.platform == 'iOS') ||
(navigator.vendor && navigator.vendor.indexOf('Apple') > -1)
}
/**
* Given a date string, this will cleanse it for use with JavaScript Date on an Apple device.
*/
function date_apple_cleanse(input) {
return input.replace(/ /g, 'T');
}
/**
* Given a field instance this will return true if it is configured for a 24 hour format, false otherwise. We'll assume
* military 24 hour by default, unless we prove otherwise.
* @param instance
* @returns {boolean}
*/
function date_military(instance) {
// We know we have a 12 hour format if the date input format string contains a 'g' or an 'h'.
// @see http://php.net/manual/en/function.date.php
var military = true;
if (instance.widget.settings.input_format && (
instance.widget.settings.input_format.indexOf('g') != -1 ||
instance.widget.settings.input_format.indexOf('h') != -1
)) { military = false; }
return military;
}
/**
* Handles the onchange event for date select lists. It is given a reference
* to the select list, the id of the hidden date field, and the grain of the
* input.
*/
function date_select_onchange(input, id, grain, military, increment, offset) {
try {
// @TODO - we may need the time zone offset placed here as well!
// Are we setting a "to date"?
var todate = $(input).attr('id').indexOf('value2') != -1 ? true : false;
// Grab the current value (which may include both the "from" and "to" dates
// separated by a pipe '|')
var current_val = $('#' + id).val();
// Is there a "to date" already set on the current value?
var todate_already_set = current_val.indexOf('|') != -1 ? true : false;
// Prepare the value part(s).
var parts = [];
if (todate_already_set) { parts = current_val.split('|'); }
else { parts.push(current_val); }
// Get the date for the current value, or just default to now.
//console.log('parts before', parts);
var date = null;
if (!current_val) { date = new Date(); }
else {
// In case they set the "to date" before the "from date", give the "from date" a default value.
if (!todate && empty(parts[0])) { parts[0] = date_yyyy_mm_dd_hh_mm_ss(); }
//Fixes iOS bug spaces must be replaced with T's
if (date_apple_device()) {
if (!todate) {
parts[0] = date_apple_cleanse(parts[0]);
}
else {
if (todate_already_set) {
parts[1] = date_apple_cleanse(parts[1]);
}
}
}
if (!todate) { date = new Date(parts[0]); }
else {
if (todate_already_set) { date = new Date(parts[1]); }
else { date = new Date(); }
}
if (date_apple_device() && offset) { date = date_item_adjust_offset(date, offset); }
}
//console.log('parts after', parts);
var input_val = $(input).val();
switch (grain) {
case 'year':
date.setYear(input_val);
break;
case 'month':
date.setMonth(input_val - 1);
break;
case 'day':
date.setDate(input_val);
break;
case 'hour':
if (!military) {
input_val = parseInt(input_val);
var ampm_input = $('#' + $(input).attr('id').replace(grain, 'ampm'));
var ampm_input_value = $(ampm_input).val();
switch (ampm_input_value) {
case 'am':
if (input_val == 12) { input_val = 0; }
date.setHours(input_val);
break;
case 'pm':
if (input_val == 12) { input_val = 0; }
date.setHours(input_val + 12);
break;
}
}
else { date.setHours(input_val); }
break;
case 'minute':
date.setMinutes(input_val);
break;
case 'ampm':
// Stop if they picked the same val twice.
if (input.date_ampm_old_value == input_val ||
(
typeof input.date_ampm_old_value === 'undefined' &&
$(input).attr('date_ampm_original_value') == input_val
)
) { return; }
// Adjust the hours by +/- 12 as needed.
if (input_val == 'pm') {
if (date.getHours() < 12) { date.setHours(date.getHours() + 12); }
else { date.setHours(date.getHours()); }
}
else if (input_val == 'am') { date.setHours(date.getHours() - 12); }
break;
}
// Adjust the minutes.
//console.log('before', date);
date.setMinutes(_date_minute_increment_adjust(increment, date.getMinutes()));
//console.log('after', date);
// Finally set the value.
var _value = date_yyyy_mm_dd_hh_mm_ss(date_yyyy_mm_dd_hh_mm_ss_parts(date));
if (!todate) { parts[0] = _value; }
else { parts[1] = _value; }
//console.log('value', _value, date, parts);
$('#' + id).val(parts.join('|'));
}
catch (error) { drupalgap_error(error); }
}
/**
*
*/
function _date_minute_increment_adjust(increment, minute) {
try {
switch (increment) {
case 5:
if (minute < 5) { minute = 0; }
else if (minute < 10) { minute = 5; }
else if (minute < 15) { minute = 10; }
else if (minute < 20) { minute = 15; }
else if (minute < 25) { minute = 20; }
else if (minute < 30) { minute = 25; }
else if (minute < 35) { minute = 30; }
else if (minute < 40) { minute = 35; }
else if (minute < 45) { minute = 40; }
else if (minute < 50) { minute = 45; }
else if (minute < 55) { minute = 50; }
else if (minute < 60) { minute = 55; }
break;
case 10:
if (minute < 10) { minute = 0; }
else if (minute < 20) { minute = 10; }
else if (minute < 30) { minute = 20; }
else if (minute < 40) { minute = 30; }
else if (minute < 50) { minute = 40; }
else if (minute < 60) { minute = 50; }
break;
case 15:
if (minute < 15) { minute = 0; }
else if (minute < 30) { minute = 15; }
else if (minute < 45) { minute = 30; }
else if (minute < 60) { minute = 45; }
break;
case 30:
if (minute < 30) { minute = 0; }
else if (minute < 60) { minute = 30; }
break;
}
return minute;
}
catch (error) { console.log('_date_minute_increment_adjust - ' + error); }
}
/**
* Given a date format string and the granularity settings from the date's field info field, this will remove any
* characters from the format that are not allowed in the granularity of the date.
* @param format
* @param granularity
*/
function date_format_cleanse(format, granularity) {
for (grain in granularity) {
if (!granularity.hasOwnProperty(grain)) { continue; }
var item = granularity[grain];
if (item) { continue; } // Skip any collected grains.
var characters = []; // @see http://php.net/manual/en/function.date.php
switch (grain) {
case 'year':
characters = ['L', 'o', 'Y', 'y'];
break;
case 'month':
characters = ['F', 'm', 'M', 'n', 't'];
break;
case 'day':
characters = ['d', 'D', 'j', 'l', 'L', 'N', 'S', 'w', 'z'];
break;
case 'hour':
characters = [' - ', 'g:', 'G:', 'h:', 'H:', 'g', 'G', 'h', 'H'];
break;
case 'minute':
characters = ['i:', 'i'];
break;
case 'second':
characters = ['s'];
break;
}
if (characters.length) {
for (var i = 0; i < characters.length; i++) {
var character = characters[i];
format = format.replace(character, '');
}
}
}
return format;
}
function date_item_adjust_offset(d, offset) {
d = new Date(d.toUTCString());
d = d.getTime() / 1000;
d -= parseInt(offset);
return new Date(d * 1000);
}
/**
* Returns all the date time zone objects from system connect.
* @returns {Object}
*/
function date_time_zones() {
return drupalgap.time_zones;
}
/**
* Returns a specific date time zone object from system connect, or the site's default if none is provided.
* @param {String} timezone
* @returns {Object}
*/
function date_get_time_zone(timezone) {
if (timezone) { return date_time_zones()[timezone]; }
else { return date_time_zones()[date_site_time_zone_name()]; }
}
function date_site_time_zone_name() {
return drupalgap.site_settings.date_default_timezone;
}
/**
* Given a date field base, this will return true if its time zone handling is set to date.
* @param field
* @returns {*|boolean}
*/
function date_tz_handling_is_date(field) {
return field.settings.tz_handling && field.settings.tz_handling == 'date' && drupalgap.time_zones;
}
function _date_get_item_and_offset(items, delta, _value, value_set, value2_set, field) {
try {
// Grab the item date and offset, if they are set, otherwise grab the current date/time.
var item_date = null;
var offset = null;
if (value_set && _value == 'value') {
if (items[delta].value.indexOf('|') != -1) {
var parts = items[delta].value.split('|');
item_date = new Date(!date_apple_device() ? parts[0] : date_apple_cleanse(parts[0]));
}
else {
item_date = new Date(!date_apple_device() ? items[delta].value : date_apple_cleanse(items[delta].value));
}
if (items[delta].item && items[delta].item.offset) {
offset = items[delta].item.offset;
}
}
if (value2_set && _value == 'value2') {
item_date = new Date(!date_apple_device() ? items[delta].item.value2 : date_apple_cleanse(items[delta].item.value2));
if (items[delta].item && items[delta].item.offset2) {
offset = items[delta].item.offset2;
}
}
if (!value_set && !value2_set && !item_date) { item_date = new Date(); }
// If we're on an Apple device, convert the date using the offset values from Drupal if there are any.
if (date_apple_device() && offset) {
item_date = date_item_adjust_offset(item_date, offset);
}
// Build the result object.
var result = {
item_date: item_date,
offset: offset,
timezone: null,
timezone_db: null
};
// If time zone handling is enabled on the date level and we have a value and an item date...
if (date_tz_handling_is_date(field) && (value_set || value2_set) && item_date) {
// Set aside the date and site timezones.
result.timezone = items[delta].item.timezone;
result.timezone_db = items[delta].item.timezone_db;
// Drupal delivers to us the value and value2 pre-rendered and adjusted for the site's time zone. Drupal also
// provides us with with the date item's time zone name and the date's time zone offset, we need to convert the
// item_date to the date's time zone, because at this point item_date has already been converted to the device's
// time zone. We do this by first subtracting off the site's timezone offset in milliseconds from the item date's
// milliseconds, then add the original item date's offset to this. Essentially convert to UTC, then convert to the
// time zone mentioned on the item's value.
var adjust = item_date.valueOf() - date_get_time_zone()*1000 + offset*1000;
item_date = new Date(adjust);
result.item_date = item_date;
}
return result;
}
catch (error) { console.log('_date_get_item_and_offset', error); }
}
function _date_widget_check_and_set_defaults(items, delta, instance, d) {
try {
// Determine if value and value_2 have been set for this item.
var value_set = true;
var value2_set = true;
if (typeof items[delta].value === 'undefined' || items[delta].value == '') {
value_set = false;
}
if (
typeof items[delta].item === 'undefined' ||
typeof items[delta].item.value2 === 'undefined' ||
items[delta].item.value2 == ''
) { value2_set = false; }
// If the value isn't set, check if a default value is available.
if (!value_set && (items[delta].default_value == '' || !items[delta].default_value) && instance.settings.default_value != '') {
items[delta].default_value = instance.settings.default_value;
}
if (!value2_set && (items[delta].default_value2 == '' || !items[delta].default_value2) && instance.settings.default_value2 != '') {
items[delta].default_value2 = instance.settings.default_value2;
}
// If the value isn't set and we have a default value, let's set it.
if (!value_set && items[delta].default_value != '') {
switch (items[delta].default_value) {
case 'now':
var now = date_yyyy_mm_dd_hh_mm_ss(date_yyyy_mm_dd_hh_mm_ss_parts(d));
items[delta].value = now;
items[delta].default_value = now;
value_set = true;
break;
case 'blank':
items[delta].value = '';
items[delta].default_value = '';
break;
default:
console.log('WARNING: date_field_widget_form() - unsupported default value: ' + items[delta].default_value);
break;
}
if (value_set) { // Spoof the item.
if (!items[delta].item) { items[delta].item = {}; }
items[delta].item.value = items[delta].value;
}
}
if (!value2_set && items[delta].default_value2 != '') {
switch (items[delta].default_value2) {
case 'now':
var now = date_yyyy_mm_dd_hh_mm_ss(date_yyyy_mm_dd_hh_mm_ss_parts(d));
items[delta].value2 = now;
items[delta].default_value2 = now;
value2_set = true;
break;
case 'same':
var now = date_yyyy_mm_dd_hh_mm_ss(date_yyyy_mm_dd_hh_mm_ss_parts(d));
items[delta].value2 = now;
items[delta].default_value2 = now;
if (!empty(items[delta].value)) { items[delta].value += '|'; }
items[delta].value += items[delta].value2;
if (!empty(items[delta].default_value)) { items[delta].default_value += '|'; }
items[delta].default_value += items[delta].default_value2;
value2_set = true;
break;
case 'blank':
items[delta].value2 = '';
items[delta].default_value2 = '';
break;
default:
console.log('WARNING: date_field_widget_form() - unsupported default value 2: ' + items[delta].default_value2);
break;
}
if (value2_set) { // Spoof the item.
if (!items[delta].item) { items[delta].item = {}; }
items[delta].item.value2 = items[delta].value2;
}
}
return {
value_set: value_set,
value2_set: value2_set
};
}
catch (error) { console.log('_date_widget_check_and_set_defaults', error); }
}
/**