1 |
85ad3d82
|
Assos Assos
|
|
2 |
|
|
|
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 |
|
|
|
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> </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 |
|
|
|
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 |
|
|
|
52 |
|
|
|
53 |
|
|
this.jqObject.autocomplete("widget").addClass('rules-autocomplete');
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
var instance = this;
|
58 |
|
|
|
59 |
|
|
|
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 |
|
|
|
71 |
|
|
this.jqObject.click(function() {
|
72 |
|
|
|
73 |
|
|
|
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 |
|
|
|
84 |
|
|
|
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 |
384fc62a
|
Assos Assos
|
|
107 |
|
|
|
108 |
|
|
var autocompleteDataKey = typeof(this.jqObject.data('autocomplete')) === 'object' ? 'autocomplete' : 'ui-autocomplete';
|
109 |
|
|
|
110 |
85ad3d82
|
Assos Assos
|
|
111 |
|
|
|
112 |
384fc62a
|
Assos Assos
|
this.jqObject.data(autocompleteDataKey)._renderItem = function(ul, item) {
|
113 |
85ad3d82
|
Assos Assos
|
return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
|
114 |
|
|
};
|
115 |
|
|
|
116 |
|
|
|
117 |
384fc62a
|
Assos Assos
|
this.jqObject.data(autocompleteDataKey).close = function (event) {
|
118 |
85ad3d82
|
Assos Assos
|
var value = this.element.val();
|
119 |
|
|
|
120 |
|
|
|
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 |
384fc62a
|
Assos Assos
|
|
127 |
|
|
if (typeof(this.menu.deactivate) === 'function') {
|
128 |
|
|
this.menu.deactivate();
|
129 |
|
|
}
|
130 |
85ad3d82
|
Assos Assos
|
}
|
131 |
|
|
}
|
132 |
|
|
else {
|
133 |
|
|
|
134 |
|
|
instance.jqObject.autocomplete("search", instance.jqObject.val());
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
instance.groupSelected = false;
|
138 |
|
|
}
|
139 |
|
|
};
|
140 |
|
|
|
141 |
|
|
this.button.click(function() {
|
142 |
|
|
instance.toggle();
|
143 |
|
|
});
|
144 |
|
|
|
145 |
|
|
};
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
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 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
Drupal.rules.autocomplete.prototype.open = function(searchFor) {
|
169 |
|
|
|
170 |
|
|
this.jqObject.autocomplete("search", ((searchFor === undefined) ? this.jqObject.val() : searchFor));
|
171 |
|
|
this.button.addClass("ui-state-focus");
|
172 |
|
|
};
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
Drupal.rules.autocomplete.prototype.close = function() {
|
178 |
|
|
this.jqObject.autocomplete("close");
|
179 |
|
|
this.button.removeClass("ui-state-focus");
|
180 |
|
|
};
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
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); |