Projet

Général

Profil

Paste
Télécharger (4,46 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.js @ 3aa14731

1
(function ($) {
2

    
3

    
4
Drupal.behaviors.dateSelect = {};
5

    
6
Drupal.behaviors.dateSelect.attach = function (context, settings) {
7
  var $widget = $('.form-type-date-select').parents('fieldset').once('date');
8
  var i;
9
  for (i = 0; i < $widget.length; i++) {
10
    new Drupal.date.EndDateHandler($widget[i]);
11
  }
12
};
13

    
14
Drupal.date = Drupal.date || {};
15

    
16
/**
17
 * Constructor for the EndDateHandler object.
18
 *
19
 * The EndDateHandler is responsible for synchronizing a date select widget's
20
 * end date with its start date. This behavior lasts until the user
21
 * interacts with the end date widget.
22
 *
23
 * @param widget
24
 *   The fieldset DOM element containing the from and to dates.
25
 */
26
Drupal.date.EndDateHandler = function (widget) {
27
  this.$widget = $(widget);
28
  this.$start = this.$widget.find('.form-type-date-select[class$=value]');
29
  this.$end = this.$widget.find('.form-type-date-select[class$=value2]');
30
  if (this.$end.length === 0) {
31
    return;
32
  }
33
  this.initializeSelects();
34
  // Only act on date fields where the end date is completely blank or already
35
  // the same as the start date. Otherwise, we do not want to override whatever
36
  // the default value was.
37
  if (this.endDateIsBlank() || this.endDateIsSame()) {
38
    this.bindClickHandlers();
39
    // Start out with identical start and end dates.
40
    this.syncEndDate();
41
  }
42
};
43

    
44
/**
45
 * Store all the select dropdowns in an array on the object, for later use.
46
 */
47
Drupal.date.EndDateHandler.prototype.initializeSelects = function () {
48
  var $starts = this.$start.find('select');
49
  var $end, $start, endId, i, id;
50
  this.selects = {};
51
  for (i = 0; i < $starts.length; i++) {
52
    $start = $($starts[i]);
53
    id = $start.attr('id');
54
    endId = id.replace('-value-', '-value2-');
55
    $end = $('#' + endId);
56
    this.selects[id] = {
57
      'id': id,
58
      'start': $start,
59
      'end': $end
60
    };
61
  }
62
};
63

    
64
/**
65
 * Returns true if all dropdowns in the end date widget are blank.
66
 */
67
Drupal.date.EndDateHandler.prototype.endDateIsBlank = function () {
68
  var id;
69
  for (id in this.selects) {
70
    if (this.selects.hasOwnProperty(id)) {
71
      if (this.selects[id].end.val() !== '') {
72
        return false;
73
      }
74
    }
75
  }
76
  return true;
77
};
78

    
79
/**
80
 * Returns true if the end date widget has the same value as the start date.
81
 */
82
Drupal.date.EndDateHandler.prototype.endDateIsSame = function () {
83
  var id;
84
  for (id in this.selects) {
85
    if (this.selects.hasOwnProperty(id)) {
86
      if (this.selects[id].end.val() != this.selects[id].start.val()) {
87
        return false;
88
      }
89
    }
90
  }
91
  return true;
92
};
93

    
94
/**
95
 * Add a click handler to each of the start date's select dropdowns.
96
 */
97
Drupal.date.EndDateHandler.prototype.bindClickHandlers = function () {
98
  var id;
99
  for (id in this.selects) {
100
    if (this.selects.hasOwnProperty(id)) {
101
      this.selects[id].start.bind('click.endDateHandler', this.startClickHandler.bind(this));
102
      this.selects[id].end.bind('focus', this.endFocusHandler.bind(this));
103
    }
104
  }
105
};
106

    
107
/**
108
 * Click event handler for each of the start date's select dropdowns.
109
 */
110
Drupal.date.EndDateHandler.prototype.startClickHandler = function (event) {
111
  this.syncEndDate();
112
};
113

    
114
/**
115
 * Focus event handler for each of the end date's select dropdowns.
116
 */
117
Drupal.date.EndDateHandler.prototype.endFocusHandler = function (event) {
118
  var id;
119
  for (id in this.selects) {
120
    if (this.selects.hasOwnProperty(id)) {
121
      this.selects[id].start.unbind('click.endDateHandler');
122
    }
123
  }
124
  $(event.target).unbind('focus', this.endFocusHandler);
125
};
126

    
127
Drupal.date.EndDateHandler.prototype.syncEndDate = function () {
128
  var id;
129
  for (id in this.selects) {
130
    if (this.selects.hasOwnProperty(id)) {
131
      this.selects[id].end.val(this.selects[id].start.val());
132
    }
133
  }
134
};
135

    
136
}(jQuery));
137

    
138
/**
139
 * Function.prototype.bind polyfill for older browsers.
140
 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
141
 */
142
if (!Function.prototype.bind) {
143
  Function.prototype.bind = function (oThis) {
144
    if (typeof this !== "function") // closest thing possible to the ECMAScript 5 internal IsCallable function
145
      throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");
146

    
147
    var aArgs = Array.prototype.slice.call(arguments, 1),
148
        fToBind = this,
149
        fNOP = function () {},
150
        fBound = function () {
151
          return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
152
        };
153

    
154
    fNOP.prototype = this.prototype;
155
    fBound.prototype = new fNOP();
156

    
157
    return fBound;
158
  };
159
}