forked from signalpoint/date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.js
829 lines (734 loc) · 27.5 KB
/
date.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
/**
* 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.
$.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 += ' 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.
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()));
// Determine if values 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 == '' && instance.settings.default_value != '') {
items[delta].default_value = instance.settings.default_value;
}
if (!value2_set) {
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;
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 (!value2_set && items[delta].default_value2 != '') {
switch (items[delta].default_value2) {
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;
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;
}
}
// Grab the current date.
var date = new Date();
// Grab the item date, if it is set.
//var item_date = null;
//var item_date2 = null;
//if (value_set) { item_date = new Date(items[delta].value); }
//if (value2_set) { item_date2 = new Date(items[delta].value2); }
// Depending on 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) {
// Grab the item date, if it is set.
var item_date = null;
if (value_set && _value == 'value') {
if (items[delta].value.indexOf('|') != -1) {
var parts = items[delta].value.split('|');
item_date = new Date(parts[0]);
}
else { item_date = new Date(items[delta].value); }
}
if (value2_set && _value == 'value2') { item_date = new Date(items[delta].item.value2); }
// Are we doing a 12 or 24 hour format? We'll assume military 24 hour by default, unless we prove otherwise.
var military = date_military(instance);
// For each grain of the granulatiry, add a child for each. As we build the
// children widgets we'll set them aside one by one that way we can present
// the inputs in a desirable order.
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 + ")"
};
switch (grain) {
// YEAR
case 'year':
// 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.
_widget_year = {
prefix: theme('date_label', { title: 'Year' }),
type: 'date_select',
value: year,
attributes: attributes,
options: options
};
break;
// MONTH
case 'month':
// 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.
_widget_month = {
prefix: theme('date_label', { title: 'Month' }),
type: 'date_select',
value: month,
attributes: attributes,
options: options
};
break;
// DAY
case 'day':
// Determine the current month.
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.
_widget_day = {
prefix: theme('date_label', { title: 'Day' }),
type: 'date_select',
value: day,
attributes: attributes,
options: options
};
break;
// HOUR
case 'hour':
// 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()); }
// Build and theme the select list.
_widget_hour = {
prefix: theme('date_label', { title: 'Hour' }),
type: 'date_select',
value: hour,
attributes: attributes,
options: options
};
// Add an am/pm selector if we're not in military time.
if (!military) {
_widget_ampm = {
type: 'select',
attributes: {
id: attributes.id.replace(grain, 'ampm'),
onclick: attributes.onchange.replace(grain, 'ampm')
},
options: {
am: 'am',
pm: 'pm'
}
};
}
break;
// MINUTE
case 'minute':
// 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.
_widget_minute = {
prefix: theme('date_label', { title: 'Minute' }),
type: 'date_select',
value: minute,
attributes: attributes,
options: options
};
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' ? 'From' : 'To';
items[delta].children.push({ markup: theme('header', { text: text + ': ' }) });
}
// 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_field_widget_form - ' + error);
}
}
/**
*
* @param value
* @returns {Date}
*/
function date_prepare(value) {
try {
// @see http://stackoverflow.com/a/16664730/763010
return new Date(Date.parse(value.replace(/-/g,'/')));
}
catch (error) {
console.log('date_prepare() - ' + error);
}
}
/**
* Given a field instance this will return true if it is configured for a 24 hour format, false 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) {
try {
// 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.
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 (typeof device !== 'undefined' && device.platform == 'iOS') {
if (!todate) { parts[0] = parts[0].replace(' ', 'T'); }
else {
if (todate_already_set) { parts[1] = parts[1].replace(' ', 'T'); }
}
}
if (!todate) { date = new Date(parts[0]); }
else {
if (todate_already_set) { date = new Date(parts[1]); }
else { date = new Date(); }
}
}
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':
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.
date.setMinutes(_date_minute_increment_adjust(increment, date.getMinutes()));
// 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; }
$('#' + 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;
}
/**
* Implements hook_assemble_form_state_into_field().
*/
function date_assemble_form_state_into_field(entity_type, bundle, form_state_value, field, instance, langcode, delta, field_key) {
try {
field_key.use_delta = false;
// Grab our "to date" setting for the field.
var todate = field.settings.todate;
// On iOS we must place a 'T' on the date.
if (typeof device !== 'undefined' && device.platform == 'iOS') {
form_state_value = form_state_value.replace(/ /g, 'T');
}
var result = {};
var values = ['value'];
if (!empty(todate)) { values.push('value2'); }
$.each(values, function(_index, _value) {
result[_value] = {};
// Is there a "to date" already set on the current value?
var todate_already_set = form_state_value.indexOf('|') != -1 ? true : false;
// Perpare the value part(s).
var parts = [];
if (todate_already_set) { parts = form_state_value.split('|'); }
else { parts.push(form_state_value); }
$.each(field.settings.granularity, function(grain, value) {
var date = null;
if (_value == 'value') { date = new Date(parts[0]); }
else if (_value == 'value2') { date = new Date(parts[1]); }
if (value) {
switch (grain) {
case 'year':
result[_value].year = date.getFullYear();
break;
case 'month':
result[_value].month = parseInt(date.getMonth()) + 1;
break;
case 'day':
result[_value].day = parseInt(date.getDate());
break;
case 'hour':
result[_value].hour = parseInt(date.getHours());
if (!date_military(instance)) {
if (result[_value].hour >= 12) {
result[_value].hour = result[_value].hour % 12;
result[_value].ampm = 'pm';
}
}
break;
case 'minute':
result[_value].minute = '' + parseInt(date.getMinutes());
if (result[_value].minute.length == 1) { result[_value].minute = '0' + result[_value].minute; }
break;
}
}
});
});
return result;
}
catch (error) {
console.log('date_assemble_form_state_into_field - ' + error);
}
}
/**
*
*/
function theme_datetime(variables) {
try {
//dpm(variables);
var html = '';
// Make this a hidden field since the widget will just populate a value.
variables.attributes.type = 'hidden';
html += '<input ' + drupalgap_attributes(variables.attributes) + '/>';
// Render the widget based on its type.
var widget_type = variables.field_info_instance.widget.type;
var widget_function = 'theme_' + widget_type;
if (drupalgap_function_exists(widget_function)) {
var fn = window[widget_function];
html += fn.call(null, variables);
}
else {
console.log('WARNING: theme_datetime() - unsupported widget type! (' +
widget_type +
')'
);
}
return html;
}
catch (error) { drupalgap_error(error); }
}
/**
*
*/
function theme_date_select(variables) {
try { return theme('select', variables); }
catch (error) { drupalgap_error(error); }
}
/**
*
*/
function theme_date_label(variables) {
try {
return '<div ' + drupalgap_attributes(variables.attributes) + '><strong>' +
variables.title +
'</strong></div>';
}
catch (error) { console.log('theme_date_label - ' + error); }
}