root / drupal7 / sites / all / modules / date / date_api / date_year_range.js @ 599a39cd
1 |
/**
|
---|---|
2 |
* @file
|
3 |
*/
|
4 |
(function ($) { |
5 |
|
6 |
Drupal.behaviors.dateYearRange = {}; |
7 |
|
8 |
Drupal.behaviors.dateYearRange.attach = function (context, settings) { |
9 |
var $textfield, $textfields, i; |
10 |
|
11 |
// Turn the years back and forward fieldsets into dropdowns.
|
12 |
$textfields = $('input.select-list-with-custom-option', context).once('date-year-range'); |
13 |
for (i = 0; i < $textfields.length; i++) { |
14 |
$textfield = $($textfields[i]); |
15 |
new Drupal.dateYearRange.SelectListWithCustomOption($textfield); |
16 |
} |
17 |
}; |
18 |
|
19 |
Drupal.dateYearRange = {}; |
20 |
|
21 |
/**
|
22 |
* Constructor for the SelectListWithCustomOption object.
|
23 |
*
|
24 |
* This object is responsible for turning the years back and forward textfields
|
25 |
* into dropdowns with an 'other' option that lets the user enter a custom
|
26 |
* value.
|
27 |
*/
|
28 |
Drupal.dateYearRange.SelectListWithCustomOption = function ($textfield) { |
29 |
this.$textfield = $textfield; |
30 |
this.$description = $textfield.next('div.description'); |
31 |
this.defaultValue = $textfield.val(); |
32 |
this.$dropdown = this.createDropdown(); |
33 |
this.$dropdown.insertBefore($textfield); |
34 |
}; |
35 |
|
36 |
/**
|
37 |
* Get the value of the textfield as it existed on page load.
|
38 |
*
|
39 |
* @param {String} type
|
40 |
* The type of the variable to be returned. Defaults to string.
|
41 |
*
|
42 |
* @return
|
43 |
* The original value of the textfield. Returned as an integer, if the type
|
44 |
* parameter was 'int'.
|
45 |
*/
|
46 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getOriginal = function (type) { |
47 |
var original;
|
48 |
if (type === 'int') { |
49 |
original = parseInt(this.defaultValue, 10); |
50 |
if (window.isNaN(original)) {
|
51 |
original = 0;
|
52 |
} |
53 |
} |
54 |
else {
|
55 |
original = this.defaultValue;
|
56 |
} |
57 |
return original;
|
58 |
}; |
59 |
|
60 |
/**
|
61 |
* Get the correct first value for the dropdown.
|
62 |
*/
|
63 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getStartValue = function () { |
64 |
var direction = this.getDirection(); |
65 |
var start;
|
66 |
switch (direction) {
|
67 |
case 'back': |
68 |
// For the 'years back' dropdown, the first option should be -10, unless
|
69 |
// the default value of the textfield is even smaller than that.
|
70 |
start = Math.min(this.getOriginal('int'), -10); |
71 |
break;
|
72 |
|
73 |
case 'forward': |
74 |
start = 0;
|
75 |
break;
|
76 |
} |
77 |
return start;
|
78 |
}; |
79 |
|
80 |
/**
|
81 |
* Get the correct last value for the dropdown.
|
82 |
*/
|
83 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getEndValue = function () { |
84 |
var direction = this.getDirection(); |
85 |
var end;
|
86 |
var originalString = this.getOriginal(); |
87 |
switch (direction) {
|
88 |
case 'back': |
89 |
end = 0;
|
90 |
break;
|
91 |
|
92 |
case 'forward': |
93 |
// If the original value of the textfield is an absolute year such as
|
94 |
// 2020, don't try to include it in the dropdown.
|
95 |
if (originalString.indexOf('+') === -1) { |
96 |
end = 10;
|
97 |
} |
98 |
// If the original value is a relative value (+x), we want it to be
|
99 |
// included in the possible dropdown values.
|
100 |
else {
|
101 |
end = Math.max(this.getOriginal('int'), 10); |
102 |
} |
103 |
break;
|
104 |
} |
105 |
return end;
|
106 |
}; |
107 |
|
108 |
/**
|
109 |
* Create a dropdown select list with the correct options for this textfield.
|
110 |
*/
|
111 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.createDropdown = function () { |
112 |
var $dropdown = $('<select>').addClass('form-select date-year-range-select'); |
113 |
var $option, i, value; |
114 |
var start = this.getStartValue(); |
115 |
var end = this.getEndValue(); |
116 |
var direction = this.getDirection(); |
117 |
for (i = start; i <= end; i++) {
|
118 |
// Make sure we include the +/- sign in the option value.
|
119 |
value = i; |
120 |
if (i > 0) { |
121 |
value = '+' + i;
|
122 |
} |
123 |
// Zero values must have a + or - in front.
|
124 |
if (i === 0) { |
125 |
if (direction === 'back') { |
126 |
value = '-' + i;
|
127 |
} |
128 |
else {
|
129 |
value = '+' + i;
|
130 |
} |
131 |
} |
132 |
$option = $('<option>' + Drupal.formatPlural(value, '@count year from now', '@count years from now') + '</option>').val(value); |
133 |
$dropdown.append($option); |
134 |
} |
135 |
// Create an 'Other' option.
|
136 |
$option = $('<option class="custom-option">' + Drupal.t('Other') + '</option>').val(''); |
137 |
$dropdown.append($option); |
138 |
|
139 |
// When the user changes the selected option in the dropdown, perform
|
140 |
// appropriate actions (such as showing or hiding the textfield).
|
141 |
$dropdown.bind('change', $.proxy(this.handleDropdownChange, this)); |
142 |
|
143 |
// Set the initial value of the dropdown.
|
144 |
this._setInitialDropdownValue($dropdown); |
145 |
return $dropdown; |
146 |
}; |
147 |
|
148 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype._setInitialDropdownValue = function ($dropdown) { |
149 |
var textfieldValue = this.getOriginal(); |
150 |
// Determine whether the original textfield value exists in the dropdown.
|
151 |
var possible = $dropdown.find('option[value="' + textfieldValue + '"]'); |
152 |
// If the original textfield value is one of the dropdown options, preselect
|
153 |
// it and hide the 'other' textfield.
|
154 |
if (possible.length) {
|
155 |
$dropdown.val(textfieldValue);
|
156 |
this.hideTextfield();
|
157 |
} |
158 |
// If the original textfield value isn't one of the dropdown options, choose
|
159 |
// the 'Other' option in the dropdown.
|
160 |
else {
|
161 |
$dropdown.val(''); |
162 |
} |
163 |
}; |
164 |
|
165 |
/**
|
166 |
* Determine whether this is the "years back" or "years forward" textfield.
|
167 |
*/
|
168 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getDirection = function () { |
169 |
if (this.direction) { |
170 |
return this.direction; |
171 |
} |
172 |
var direction;
|
173 |
if (this.$textfield.hasClass('back')) { |
174 |
direction = 'back';
|
175 |
} |
176 |
else if (this.$textfield.hasClass('forward')) { |
177 |
direction = 'forward';
|
178 |
} |
179 |
this.direction = direction;
|
180 |
return direction;
|
181 |
}; |
182 |
|
183 |
/**
|
184 |
* Change handler for the dropdown, to modify the textfield as appropriate.
|
185 |
*/
|
186 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.handleDropdownChange = function () { |
187 |
// Since the dropdown changed, we need to make the content of the textfield
|
188 |
// match the (new) selected option.
|
189 |
this.syncTextfield();
|
190 |
|
191 |
// Show the textfield if the 'Other' option was selected, and hide it if one
|
192 |
// of the preset options was selected.
|
193 |
if ($(':selected', this.$dropdown).hasClass('custom-option')) { |
194 |
this.revealTextfield();
|
195 |
} |
196 |
else {
|
197 |
this.hideTextfield();
|
198 |
} |
199 |
}; |
200 |
|
201 |
/**
|
202 |
* Display the textfield and its description.
|
203 |
*/
|
204 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.revealTextfield = function () { |
205 |
this.$textfield.show(); |
206 |
this.$description.show(); |
207 |
}; |
208 |
|
209 |
/**
|
210 |
* Hide the textfield and its description.
|
211 |
*/
|
212 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.hideTextfield = function () { |
213 |
this.$textfield.hide(); |
214 |
this.$description.hide(); |
215 |
}; |
216 |
|
217 |
/**
|
218 |
* Copy the selected value of the dropdown to the textfield.
|
219 |
*
|
220 |
* FAPI doesn't know about the JS-only dropdown, so the textfield needs to
|
221 |
* reflect the value of the dropdown.
|
222 |
*/
|
223 |
Drupal.dateYearRange.SelectListWithCustomOption.prototype.syncTextfield = function () { |
224 |
var value = this.$dropdown.val(); |
225 |
this.$textfield.val(value); |
226 |
}; |
227 |
|
228 |
})(jQuery); |