/
date.js
126 lines (100 loc) · 2.83 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
//require <jquery.packed.js>
//require <jquery-ui.min.js>
//require <xatajax.core.js>
//require <xataface/lang.js>
(function(){
var $ = jQuery;
var _ = xataface.lang.get;
var findwidgets = XataJax.load('xataface.findwidgets');
findwidgets.date = date;
function date(/**Object*/ o){
var self = this;
this.el = null;
this.name = null;
this.from = $('<input type="text">');
this.to = $('<input type="text">');
this.rangePanel = $('<div>').css('text-align','right');
$(this.rangePanel).append(_('findwidgets.date.from', 'From') + ' ').append(this.from).append(' ' + _('findwidgets.date.to', 'to')+ ' ').append(this.to);
this.btn = $('<button>').addClass('advanced-button').text('...').click(function(){
self.toggleRange();
return false;
});
}
$.extend(date.prototype, {
install: install,
toggleRange: toggleRange,
showRange: showRange,
hideRange: hideRange
});
function install(/**HTMLElement*/ el){
var self = this;
this.el = el;
this.rangePanel.insertAfter(this.el).hide();
this.btn.insertAfter(this.el);
$.each([this.from, this.to], function(){
var dates = $(this).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
onSelect: function( selectedDate ) {
var option = this == self.from ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
$(this).change();
}
});
});
$.each([this.from, this.to], function(){
$(this).change(function(){
var from = $(self.from).val();
var to = $(self.to).val();
if ( from && to ){
$(self.el).val(from+'..'+to);
} else if ( from ){
$(self.el).val('>='+from);
} else if ( to ){
$(self.el).val('<='+to);
} else {
$(self.el).val();
}
});
});
this.name = $(this.el).attr('name');
}
function toggleRange(){
if ( $(this.el).is(':visible') ) this.showRange();
else this.hideRange();
}
function showRange(){
$(this.el).hide();
var val = $(this.el).val();
if ( val.match(/\.\./) ){
var parts = val.split('..');
$(this.from).val(parts[0]);
$(this.to).val(parts[1]);
} else if ( val.match(/^</) ){
$(this.from).val('');
val = val.replace(/^[^0-9]+/, '');
$(this.to).val(val);
} else if ( val.match(/^>/) ){
$(this.to).val('');
val = val.replace(/^[^0-9]+/, '');
$(this.from).val(val);
} else {
$(this.to).val(val);
$(this.from).val(val);
}
$(this.rangePanel).show();
$(window.xataface).trigger('afterShowDateRangeWidget', {
'widget' : this
});
}
function hideRange(){
$(this.el).show();
$(this.rangePanel).hide();
}
})();