1 |
599a39cd
|
Assos Assos
|
|
2 |
|
|
|
3 |
|
|
|
4 |
85ad3d82
|
Assos Assos
|
(function ($) {
|
5 |
|
|
|
6 |
599a39cd
|
Assos Assos
|
Drupal.behaviors.dateYearRange = {};
|
7 |
85ad3d82
|
Assos Assos
|
|
8 |
599a39cd
|
Assos Assos
|
Drupal.behaviors.dateYearRange.attach = function (context, settings) {
|
9 |
|
|
var $textfield, $textfields, i;
|
10 |
85ad3d82
|
Assos Assos
|
|
11 |
599a39cd
|
Assos Assos
|
|
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 |
85ad3d82
|
Assos Assos
|
|
19 |
599a39cd
|
Assos Assos
|
Drupal.dateYearRange = {};
|
20 |
85ad3d82
|
Assos Assos
|
|
21 |
599a39cd
|
Assos Assos
|
|
22 |
85ad3d82
|
Assos Assos
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
599a39cd
|
Assos Assos
|
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 |
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
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 |
85ad3d82
|
Assos Assos
|
}
|
54 |
599a39cd
|
Assos Assos
|
else {
|
55 |
|
|
original = this.defaultValue;
|
56 |
|
|
}
|
57 |
|
|
return original;
|
58 |
|
|
};
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getStartValue = function () {
|
64 |
|
|
var direction = this.getDirection();
|
65 |
|
|
var start;
|
66 |
|
|
switch (direction) {
|
67 |
|
|
case 'back':
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
start = Math.min(this.getOriginal('int'), -10);
|
71 |
85ad3d82
|
Assos Assos
|
break;
|
72 |
|
|
|
73 |
599a39cd
|
Assos Assos
|
case 'forward':
|
74 |
|
|
start = 0;
|
75 |
85ad3d82
|
Assos Assos
|
break;
|
76 |
599a39cd
|
Assos Assos
|
}
|
77 |
|
|
return start;
|
78 |
|
|
};
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
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 |
85ad3d82
|
Assos Assos
|
break;
|
91 |
|
|
|
92 |
599a39cd
|
Assos Assos
|
case 'forward':
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
if (originalString.indexOf('+') === -1) {
|
96 |
|
|
end = 10;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
else {
|
101 |
|
|
end = Math.max(this.getOriginal('int'), 10);
|
102 |
|
|
}
|
103 |
|
|
break;
|
104 |
85ad3d82
|
Assos Assos
|
}
|
105 |
599a39cd
|
Assos Assos
|
return end;
|
106 |
|
|
};
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
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 |
|
|
|
119 |
|
|
value = i;
|
120 |
|
|
if (i > 0) {
|
121 |
85ad3d82
|
Assos Assos
|
value = '+' + i;
|
122 |
|
|
}
|
123 |
599a39cd
|
Assos Assos
|
|
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 |
85ad3d82
|
Assos Assos
|
}
|
135 |
599a39cd
|
Assos Assos
|
|
136 |
|
|
$option = $('<option class="custom-option">' + Drupal.t('Other') + '</option>').val('');
|
137 |
85ad3d82
|
Assos Assos
|
$dropdown.append($option);
|
138 |
|
|
|
139 |
599a39cd
|
Assos Assos
|
|
140 |
|
|
|
141 |
|
|
$dropdown.bind('change', $.proxy(this.handleDropdownChange, this));
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
this._setInitialDropdownValue($dropdown);
|
145 |
|
|
return $dropdown;
|
146 |
|
|
};
|
147 |
|
|
|
148 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype._setInitialDropdownValue = function ($dropdown) {
|
149 |
|
|
var textfieldValue = this.getOriginal();
|
150 |
|
|
|
151 |
|
|
var possible = $dropdown.find('option[value="' + textfieldValue + '"]');
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
if (possible.length) {
|
155 |
|
|
$dropdown.val(textfieldValue);
|
156 |
|
|
this.hideTextfield();
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
else {
|
161 |
|
|
$dropdown.val('');
|
162 |
|
|
}
|
163 |
|
|
};
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
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 |
|
|
|
185 |
|
|
|
186 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype.handleDropdownChange = function () {
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
this.syncTextfield();
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
if ($(':selected', this.$dropdown).hasClass('custom-option')) {
|
194 |
|
|
this.revealTextfield();
|
195 |
|
|
}
|
196 |
|
|
else {
|
197 |
|
|
this.hideTextfield();
|
198 |
|
|
}
|
199 |
|
|
};
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype.revealTextfield = function () {
|
205 |
|
|
this.$textfield.show();
|
206 |
|
|
this.$description.show();
|
207 |
|
|
};
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype.hideTextfield = function () {
|
213 |
|
|
this.$textfield.hide();
|
214 |
|
|
this.$description.hide();
|
215 |
|
|
};
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
Drupal.dateYearRange.SelectListWithCustomOption.prototype.syncTextfield = function () {
|
224 |
|
|
var value = this.$dropdown.val();
|
225 |
|
|
this.$textfield.val(value);
|
226 |
|
|
};
|
227 |
85ad3d82
|
Assos Assos
|
|
228 |
|
|
})(jQuery); |