Projet

Général

Profil

Révision 599a39cd

Ajouté par Assos Assos il y a environ 3 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/date/date_api/date_year_range.js
1
/**
2
 * @file
3
 */
1 4
(function ($) {
2 5

  
3
Drupal.behaviors.dateYearRange = {};
4

  
5
Drupal.behaviors.dateYearRange.attach = function (context, settings) {
6
  var $textfield, $textfields, i;
6
  Drupal.behaviors.dateYearRange = {};
7 7

  
8
  // Turn the years back and forward fieldsets into dropdowns.
9
  $textfields = $('input.select-list-with-custom-option', context).once('date-year-range');
10
  for (i = 0; i < $textfields.length; i++) {
11
    $textfield = $($textfields[i]);
12
    new Drupal.dateYearRange.SelectListWithCustomOption($textfield);
13
  }
14
};
8
  Drupal.behaviors.dateYearRange.attach = function (context, settings) {
9
    var $textfield, $textfields, i;
15 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
  };
16 18

  
17
Drupal.dateYearRange = {};
19
  Drupal.dateYearRange = {};
18 20

  
19
/**
21
  /**
20 22
 * Constructor for the SelectListWithCustomOption object.
21 23
 *
22 24
 * This object is responsible for turning the years back and forward textfields
23 25
 * into dropdowns with an 'other' option that lets the user enter a custom
24 26
 * value.
25 27
 */
26
Drupal.dateYearRange.SelectListWithCustomOption = function ($textfield) {
27
  this.$textfield = $textfield;
28
  this.$description = $textfield.next('div.description');
29
  this.defaultValue = $textfield.val();
30
  this.$dropdown = this.createDropdown();
31
  this.$dropdown.insertBefore($textfield);
32
};
33

  
34
/**
35
 * Get the value of the textfield as it existed on page load.
36
 *
37
 * @param {String} type
38
 *   The type of the variable to be returned. Defaults to string.
39
 * @return
40
 *   The original value of the textfield. Returned as an integer, if the type
41
 *   parameter was 'int'.
42
 */
43
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getOriginal = function (type) {
44
  var original;
45
  if (type === 'int') {
46
    original = parseInt(this.defaultValue, 10);
47
    if (window.isNaN(original)) {
48
      original = 0;
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
      }
49 53
    }
50
  }
51
  else {
52
    original = this.defaultValue;
53
  }
54
  return original;
55
};
56

  
57
/**
58
 * Get the correct first value for the dropdown.
59
 */
60
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getStartValue = function () {
61
  var direction = this.getDirection();
62
  var start;
63
  switch (direction) {
64
    case 'back':
65
      // For the 'years back' dropdown, the first option should be -10, unless
66
      // the default value of the textfield is even smaller than that.
67
      start = Math.min(this.getOriginal('int'), -10);
68
      break;
69
    case 'forward':
70
      start = 0;
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 71
      break;
72
  }
73
  return start;
74
};
75 72

  
76
/**
77
 * Get the correct last value for the dropdown.
78
 */
79
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getEndValue = function () {
80
  var direction = this.getDirection();
81
  var end;
82
  var originalString = this.getOriginal();
83
  switch (direction) {
84
    case 'back':
85
      end = 0;
73
      case 'forward':
74
        start = 0;
86 75
      break;
87
    case 'forward':
88
      // If the original value of the textfield is an absolute year such as
89
      // 2020, don't try to include it in the dropdown.
90
      if (originalString.indexOf('+') === -1) {
91
        end = 10;
92
      }
93
      // If the original value is a relative value (+x), we want it to be
94
      // included in the possible dropdown values.
95
      else {
96
        end = Math.max(this.getOriginal('int'), 10);
97
      }
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;
98 90
      break;
99
  }
100
  return end;
101
};
102 91

  
103
/**
104
 * Create a dropdown select list with the correct options for this textfield.
105
 */
106
Drupal.dateYearRange.SelectListWithCustomOption.prototype.createDropdown = function () {
107
  var $dropdown = $('<select>').addClass('form-select date-year-range-select');
108
  var $option, i, value;
109
  var start = this.getStartValue();
110
  var end = this.getEndValue();
111
  var direction = this.getDirection();
112
  for (i = start; i <= end; i++) {
113
    // Make sure we include the +/- sign in the option value.
114
    value = i;
115
    if (i > 0) {
116
      value = '+' + i;
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;
117 104
    }
118
    // Zero values must have a + or - in front.
119
    if (i === 0) {
120
      if (direction === 'back') {
121
        value = '-' + i;
122
      }
123
      else {
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) {
124 121
        value = '+' + i;
125 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);
126 134
    }
127
    $option = $('<option>' + Drupal.formatPlural(value, '@count year from now', '@count years from now') + '</option>').val(value);
135
    // Create an 'Other' option.
136
    $option = $('<option class="custom-option">' + Drupal.t('Other') + '</option>').val('');
128 137
    $dropdown.append($option);
129
  }
130
  // Create an 'Other' option.
131
  $option = $('<option class="custom-option">' + Drupal.t('Other') + '</option>').val('');
132
  $dropdown.append($option);
133

  
134
  // When the user changes the selected option in the dropdown, perform
135
  // appropriate actions (such as showing or hiding the textfield).
136
  $dropdown.bind('change', $.proxy(this.handleDropdownChange, this));
137

  
138
  // Set the initial value of the dropdown.
139
  this._setInitialDropdownValue($dropdown);
140
  return $dropdown;
141
};
142

  
143
Drupal.dateYearRange.SelectListWithCustomOption.prototype._setInitialDropdownValue = function ($dropdown) {
144
  var textfieldValue = this.getOriginal();
145
  // Determine whether the original textfield value exists in the dropdown.
146
  var possible = $dropdown.find('option[value="' + textfieldValue + '"]');
147
  // If the original textfield value is one of the dropdown options, preselect
148
  // it and hide the 'other' textfield.
149
  if (possible.length) {
150
    $dropdown.val(textfieldValue);
151
    this.hideTextfield();
152
  }
153
  // If the original textfield value isn't one of the dropdown options, choose
154
  // the 'Other' option in the dropdown.
155
  else {
156
    $dropdown.val('');
157
  }
158
};
159

  
160
/**
161
 * Determine whether this is the "years back" or "years forward" textfield.
162
 */
163
Drupal.dateYearRange.SelectListWithCustomOption.prototype.getDirection = function () {
164
  if (this.direction) {
165
    return this.direction;
166
  }
167
  var direction;
168
  if (this.$textfield.hasClass('back')) {
169
    direction = 'back';
170
  }
171
  else if (this.$textfield.hasClass('forward')) {
172
    direction = 'forward';
173
  }
174
  this.direction = direction;
175
  return direction;
176
};
177

  
178
/**
179
 * Change handler for the dropdown, to modify the textfield as appropriate.
180
 */
181
Drupal.dateYearRange.SelectListWithCustomOption.prototype.handleDropdownChange = function () {
182
  // Since the dropdown changed, we need to make the content of the textfield
183
  // match the (new) selected option.
184
  this.syncTextfield();
185

  
186
  // Show the textfield if the 'Other' option was selected, and hide it if one
187
  // of the preset options was selected.
188
  if ($(':selected', this.$dropdown).hasClass('custom-option')) {
189
    this.revealTextfield();
190
  }
191
  else {
192
    this.hideTextfield();
193
  }
194
};
195

  
196
/**
197
 * Display the textfield and its description.
198
 */
199
Drupal.dateYearRange.SelectListWithCustomOption.prototype.revealTextfield = function () {
200
  this.$textfield.show();
201
  this.$description.show();
202
};
203 138

  
204
/**
205
 * Hide the textfield and its description.
206
 */
207
Drupal.dateYearRange.SelectListWithCustomOption.prototype.hideTextfield = function () {
208
  this.$textfield.hide();
209
  this.$description.hide();
210
};
211

  
212
/**
213
 * Copy the selected value of the dropdown to the textfield.
214
 *
215
 * FAPI doesn't know about the JS-only dropdown, so the textfield needs to
216
 * reflect the value of the dropdown.
217
 */
218
Drupal.dateYearRange.SelectListWithCustomOption.prototype.syncTextfield = function () {
219
  var value = this.$dropdown.val();
220
  this.$textfield.val(value);
221
};
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
  };
222 227

  
223 228
})(jQuery);

Formats disponibles : Unified diff