root / drupal7 / sites / all / modules / rules / ui / rules.autocomplete.js @ 64ad485a
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> </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 |
// overriden 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 |
// Since jquery autocomplete by default strips html text by using .text()
|
107 |
// we need our own _renderItem function to display html content.
|
108 |
this.jqObject.data("autocomplete")._renderItem = function(ul, item) { |
109 |
return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul); |
110 |
}; |
111 |
|
112 |
// Override close function
|
113 |
this.jqObject.data("autocomplete").close = function (event) { |
114 |
var value = this.element.val(); |
115 |
// If the selector is not a group, then trigger the close event an and
|
116 |
// hide the menu.
|
117 |
if (value === undefined || instance.groupSelected === false) { |
118 |
clearTimeout(this.closing);
|
119 |
if (this.menu.element.is(":visible")) { |
120 |
this._trigger("close", event); |
121 |
this.menu.element.hide();
|
122 |
this.menu.deactivate();
|
123 |
} |
124 |
} |
125 |
else {
|
126 |
// Else keep all open and trigger a search for the group.
|
127 |
instance.jqObject.autocomplete("search", instance.jqObject.val());
|
128 |
// After the suggestion box was opened again, we want to be able to
|
129 |
// close it.
|
130 |
instance.groupSelected = false;
|
131 |
} |
132 |
}; |
133 |
|
134 |
this.button.click(function() { |
135 |
instance.toggle(); |
136 |
}); |
137 |
|
138 |
}; |
139 |
|
140 |
/**
|
141 |
* Success function for Rules autocomplete object.
|
142 |
*/
|
143 |
Drupal.rules.autocomplete.prototype.success = function(data, request, response) { |
144 |
var list = new Array(); |
145 |
jQuery.each(data, function(index, value) {
|
146 |
list.push( { |
147 |
label: value,
|
148 |
value: index
|
149 |
}); |
150 |
}); |
151 |
|
152 |
this.cache[request.term] = list;
|
153 |
response(list); |
154 |
}; |
155 |
|
156 |
/**
|
157 |
* Open the autocomplete window.
|
158 |
* @param searchFor The term for will be searched for. If undefined then the
|
159 |
* entered input text will be used.
|
160 |
*/
|
161 |
Drupal.rules.autocomplete.prototype.open = function(searchFor) { |
162 |
// If searchFor is undefined, we want to search for the passed argument.
|
163 |
this.jqObject.autocomplete("search", ((searchFor === undefined) ? this.jqObject.val() : searchFor)); |
164 |
this.button.addClass("ui-state-focus"); |
165 |
}; |
166 |
|
167 |
/**
|
168 |
* Close the autocomplete window.
|
169 |
*/
|
170 |
Drupal.rules.autocomplete.prototype.close = function() { |
171 |
this.jqObject.autocomplete("close"); |
172 |
this.button.removeClass("ui-state-focus"); |
173 |
}; |
174 |
|
175 |
/**
|
176 |
* Toogle the autcomplete window.
|
177 |
*/
|
178 |
Drupal.rules.autocomplete.prototype.toggle = function() { |
179 |
if (this.jqObject.autocomplete("widget").is(":visible")) { |
180 |
this.close();
|
181 |
this.focusOpens = true; |
182 |
} |
183 |
else {
|
184 |
var groups = this.jqObject.val().split(":"); |
185 |
var selector = ""; |
186 |
for (var i=0; i<groups.length-1; i++) { |
187 |
selector = selector.concat(groups[i]) + ":";
|
188 |
} |
189 |
this.focusOpens = false; |
190 |
this.jqObject.focus();
|
191 |
this.open(selector);
|
192 |
} |
193 |
}; |
194 |
|
195 |
})(jQuery); |