Projet

Général

Profil

Paste
Télécharger (6,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / ui / rules.autocomplete.js @ 950416da

1

    
2
// Registers the rules namespace.
3
Drupal.rules = Drupal.rules || {};
4

    
5
(function($) {
6
  Drupal.behaviors.rules_autocomplete = {
7
    attach: function(context) {
8
      var autocomplete_settings = Drupal.settings.rules_autocomplete;
9

    
10
      $('input.rules-autocomplete').once(function() {
11
        var input = this;
12
        new Drupal.rules.autocomplete(input, autocomplete_settings[$(input).attr('id')]);
13
      });
14
    }
15
  };
16

    
17
  /**
18
   * Rules autocomplete object.
19
   */
20
  Drupal.rules.autocomplete = function(input, settings) {
21
    this.id = settings.inputId;
22
    this.uri = settings.source;
23
    this.jqObject = $('#' + this.id);
24
    this.cache = new Array();
25
    this.jqObject.addClass('ui-corner-left');
26

    
27
    this.opendByFocus = false;
28
    this.focusOpens = true;
29
    this.groupSelected = false;
30

    
31
    this.button = $('<span>&nbsp;</span>');
32
    this.button.attr( {
33
      'tabIndex': -1,
34
      'title': 'Show all items'
35
    });
36
    this.button.insertAfter(this.jqObject);
37

    
38
    this.button.button( {
39
      icons: {
40
        primary: 'ui-icon-triangle-1-s'
41
      },
42
      text: false
43
    });
44

    
45
    // Don't round the left corners.
46
    this.button.removeClass('ui-corner-all');
47
    this.button.addClass('ui-corner-right ui-button-icon rules-autocomplete-button');
48

    
49
    this.jqObject.autocomplete();
50
    this.jqObject.autocomplete("option", "minLength", 0);
51
    // Add a custom class, so we can style the autocomplete box without
52
    // interfering with other jquery autocomplete widgets.
53
    this.jqObject.autocomplete("widget").addClass('rules-autocomplete');
54

    
55
    // Save the current rules_autocomplete object, so it can be used in
56
    // handlers.
57
    var instance = this;
58

    
59
    // Event handlers
60
    this.jqObject.focus(function() {
61
      if (instance.focusOpens) {
62
        instance.toggle();
63
        instance.opendByFocus = true;
64
      }
65
      else {
66
        instance.focusOpens = true;
67
      }
68
    });
69

    
70
    // Needed when the window is closed but the textfield has the focus.
71
    this.jqObject.click(function() {
72
      // Since the focus event happens earlier then the focus event, we need to
73
      // check here, if the window should be opened.
74
      if (!instance.opendByFocus) {
75
        instance.toggle();
76
      }
77
      else {
78
        instance.opendByFocus = false;
79
      }
80
    });
81

    
82
    this.jqObject.bind("autocompleteselect", function(event, ui) {
83
      // If a group was selected then set the groupSelected to true for the
84
      // overridden close function from jquery autocomplete.
85
      if (ui.item.value.substring(ui.item.value.length - 1, ui.item.value.length) == ":") {
86
        instance.groupSelected = true;
87
      }
88
      instance.focusOpens = false;
89
      instance.opendByFocus = false;
90
    });
91

    
92
    this.jqObject.autocomplete("option", "source", function(request, response) {
93
      if (request.term in instance.cache) {
94
        response(instance.cache[request.term]);
95
        return;
96
      }
97
      $.ajax( {
98
        url: instance.uri + '/' + request.term,
99
        dataType: "json",
100
        success: function(data) {
101
          instance.success(data, request, response);
102
        }
103
      });
104
    });
105

    
106
    // Newer versions of jQuery UI use element.data('ui-autocomplete'), older
107
    // versions use element.data('autocomplete').
108
    var autocompleteDataKey = typeof(this.jqObject.data('autocomplete')) === 'object' ? 'autocomplete' : 'ui-autocomplete';
109

    
110
    // Since jquery autocomplete by default strips html text by using .text()
111
    // we need our own _renderItem function to display html content.
112
    this.jqObject.data(autocompleteDataKey)._renderItem = function(ul, item) {
113
      return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
114
    };
115

    
116
    // Override close function
117
    this.jqObject.data(autocompleteDataKey).close = function (event) {
118
      var value = this.element.val();
119
      // If the selector is not a group, then trigger the close event an and
120
      // hide the menu.
121
      if (value === undefined || instance.groupSelected === false) {
122
        clearTimeout(this.closing);
123
        if (this.menu.element.is(":visible")) {
124
          this._trigger("close", event);
125
          this.menu.element.hide();
126
          // Use deactivate for older versions of jQuery UI.
127
          if (typeof(this.menu.deactivate) === 'function') {
128
            this.menu.deactivate();
129
          }
130
        }
131
      }
132
      else {
133
        // Else keep all open and trigger a search for the group.
134
        instance.jqObject.autocomplete("search", instance.jqObject.val());
135
        // After the suggestion box was opened again, we want to be able to
136
        // close it.
137
        instance.groupSelected = false;
138
      }
139
    };
140

    
141
    this.button.click(function() {
142
      instance.toggle();
143
    });
144

    
145
  };
146

    
147
  /**
148
   * Success function for Rules autocomplete object.
149
   */
150
  Drupal.rules.autocomplete.prototype.success = function(data, request, response) {
151
    var list = new Array();
152
    jQuery.each(data, function(index, value) {
153
      list.push( {
154
        label: value,
155
        value: index
156
      });
157
    });
158

    
159
    this.cache[request.term] = list;
160
    response(list);
161
  };
162

    
163
  /**
164
   * Open the autocomplete window.
165
   * @param searchFor The term for will be searched for. If undefined then the
166
   *                  entered input text will be used.
167
   */
168
  Drupal.rules.autocomplete.prototype.open = function(searchFor) {
169
    // If searchFor is undefined, we want to search for the passed argument.
170
    this.jqObject.autocomplete("search", ((searchFor === undefined) ? this.jqObject.val() : searchFor));
171
    this.button.addClass("ui-state-focus");
172
  };
173

    
174
  /**
175
   * Close the autocomplete window.
176
   */
177
  Drupal.rules.autocomplete.prototype.close = function() {
178
    this.jqObject.autocomplete("close");
179
    this.button.removeClass("ui-state-focus");
180
  };
181

    
182
  /**
183
   * Toggle the autocomplete window.
184
   */
185
  Drupal.rules.autocomplete.prototype.toggle = function() {
186
    if (this.jqObject.autocomplete("widget").is(":visible")) {
187
      this.close();
188
      this.focusOpens = true;
189
    }
190
    else {
191
      var groups = this.jqObject.val().split(":");
192
      var selector = "";
193
      for (var i=0; i<groups.length-1; i++) {
194
        selector = selector.concat(groups[i]) + ":";
195
      }
196
      this.focusOpens = false;
197
      this.jqObject.focus();
198
      this.open(selector);
199
    }
200
  };
201

    
202
})(jQuery);