1 |
85ad3d82
|
Assos Assos
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
(function ($) {
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
Drupal.behaviors.shsWidgetCreate = {
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
attach: function (context, settings) {
|
15 |
|
|
var self = this;
|
16 |
|
|
$('input.shs-enabled')
|
17 |
|
|
.not('.shs-processed')
|
18 |
|
|
.once('shs')
|
19 |
|
|
.addClass('element-invisible')
|
20 |
|
|
.each(function() {
|
21 |
|
|
var $field = $(this);
|
22 |
|
|
var fieldName = $(this).attr('name');
|
23 |
|
|
|
24 |
|
|
if (fieldName in settings.shs) {
|
25 |
|
|
var fieldSettings = {};
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
$.each(settings.shs[fieldName], function(hash, setting) {
|
30 |
|
|
fieldSettings = setting;
|
31 |
|
|
});
|
32 |
|
|
var level = 0;
|
33 |
|
|
var parent_id = 0;
|
34 |
|
|
|
35 |
|
|
$field.parent('.form-item').not('.shs-wrapper-processed').once('shs-wrapper');
|
36 |
|
|
|
37 |
|
|
$.each(fieldSettings.parents, function(index, parent) {
|
38 |
|
|
level++;
|
39 |
|
|
|
40 |
|
|
$select = shsElementCreate($field.attr('id'), fieldSettings, level);
|
41 |
|
|
if ($field.hasClass('error')) {
|
42 |
|
|
|
43 |
|
|
$select.addClass('error');
|
44 |
|
|
}
|
45 |
|
|
$select.appendTo($field.parent());
|
46 |
|
|
|
47 |
|
|
getTermChildren($select, fieldSettings, parent_id, parent.tid, $field.attr('id'));
|
48 |
|
|
|
49 |
|
|
if (fieldSettings.multiple) {
|
50 |
|
|
parent_id = parent['tid'];
|
51 |
|
|
}
|
52 |
|
|
else {
|
53 |
|
|
parent_id = parent.tid;
|
54 |
|
|
}
|
55 |
|
|
});
|
56 |
|
|
var addNextLevel = false;
|
57 |
|
|
if ((level > 1 || parent_id) && (fieldSettings.settings.create_new_terms && fieldSettings.settings.create_new_levels)) {
|
58 |
|
|
|
59 |
|
|
addNextLevel = true;
|
60 |
|
|
}
|
61 |
|
|
if (fieldSettings.default_value && (fieldSettings.default_value.tid == parent_id)) {
|
62 |
|
|
addNextLevel = true;
|
63 |
|
|
}
|
64 |
|
|
if (addNextLevel) {
|
65 |
|
|
|
66 |
|
|
$select = shsElementCreate($field.attr('id'), fieldSettings, level);
|
67 |
|
|
$select.appendTo($field.parent());
|
68 |
|
|
|
69 |
|
|
getTermChildren($select, fieldSettings, parent_id, 0, $field.attr('id'));
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
});
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
getTermChildren = function($element, settings, parent_value, default_value, base_id) {
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
if (!$.isArray(parent_value) && typeof parent_value != "object") {
|
94 |
|
|
parent_value = [parent_value];
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
if (!$.isArray(default_value) && typeof default_value == "object") {
|
99 |
|
|
var arr = new Array;
|
100 |
|
|
$.each(default_value, function(delta, value){
|
101 |
|
|
arr.push(value);
|
102 |
|
|
});
|
103 |
|
|
default_value = arr;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
$.ajax({
|
107 |
|
|
url: Drupal.settings.basePath + 'js/shs/json',
|
108 |
|
|
type: 'POST',
|
109 |
|
|
dataType: 'json',
|
110 |
|
|
cache: true,
|
111 |
|
|
data: {
|
112 |
|
|
callback: 'shs_json_term_get_children',
|
113 |
|
|
arguments: {
|
114 |
|
|
vid: settings.vid,
|
115 |
|
|
parent: parent_value,
|
116 |
|
|
settings: settings.settings
|
117 |
|
|
}
|
118 |
|
|
},
|
119 |
|
|
success: function(data) {
|
120 |
|
|
if (data.success == true) {
|
121 |
|
|
if ($element.prop) {
|
122 |
|
|
var options = $element.prop('options');
|
123 |
|
|
}
|
124 |
|
|
else {
|
125 |
|
|
var options = $element.attr('options');
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
if (data.data.length == 0 && !(settings.settings.create_new_terms && (settings.settings.create_new_levels || (parent_value + default_value == 0)))) {
|
129 |
|
|
|
130 |
|
|
$element.remove();
|
131 |
|
|
return;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
$('option', $element).remove();
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
if (!settings.settings.required || (settings.settings.required && parent_value != 0 && !settings.multiple)) {
|
139 |
|
|
options[options.length] = new Option(Drupal.t('- None -'), 0);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
if (settings.settings.create_new_terms) {
|
143 |
|
|
|
144 |
|
|
options[options.length] = new Option(Drupal.t('<Add new item>', {}, {context: 'shs'}), '_add_new_');
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
$.each(data.data, function(key, term) {
|
149 |
|
|
options[options.length] = new Option(term.label, term.tid);
|
150 |
|
|
});
|
151 |
|
|
|
152 |
|
|
$element.val(default_value);
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
if (!elementConvertToChosen($element, settings)) {
|
156 |
|
|
|
157 |
|
|
$element.fadeIn();
|
158 |
|
|
$element.css('display','inline-block');
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
if (data.data.length == 0 && settings.settings.required && settings.settings.create_new_terms && (settings.settings.create_new_levels || (parent_value + default_value == 0))) {
|
164 |
|
|
updateElements($element, base_id, settings, 1);
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
},
|
168 |
|
|
error: function(xhr, status, error) {
|
169 |
|
|
}
|
170 |
|
|
});
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
termAddNew = function($triggering_element, $container, term, base_id, level) {
|
188 |
|
|
$.ajax({
|
189 |
|
|
url: Drupal.settings.basePath + 'js/shs/json',
|
190 |
|
|
type: 'POST',
|
191 |
|
|
dataType: 'json',
|
192 |
|
|
cache: true,
|
193 |
|
|
data: {
|
194 |
|
|
callback: 'shs_json_term_add',
|
195 |
|
|
arguments: {
|
196 |
|
|
vid: term.vid,
|
197 |
|
|
parent: term.parent,
|
198 |
|
|
name: term.name
|
199 |
|
|
}
|
200 |
|
|
},
|
201 |
|
|
success: function(data) {
|
202 |
|
|
if (data.success == true && data.data.tid) {
|
203 |
|
|
if ($triggering_element.prop) {
|
204 |
|
|
var options = $triggering_element.prop('options');
|
205 |
|
|
}
|
206 |
|
|
else {
|
207 |
|
|
var options = $triggering_element.attr('options');
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
options[options.length] = new Option(data.data.name, data.data.tid);
|
212 |
|
|
|
213 |
|
|
$triggering_element.val(data.data.tid);
|
214 |
|
|
|
215 |
|
|
updateFieldValue($triggering_element, base_id, level);
|
216 |
|
|
}
|
217 |
|
|
},
|
218 |
|
|
error: function(xhr, status, error) {
|
219 |
|
|
|
220 |
|
|
$triggering_element.val(0);
|
221 |
|
|
},
|
222 |
|
|
complete: function(xhr, status) {
|
223 |
|
|
|
224 |
|
|
$container.remove();
|
225 |
|
|
|
226 |
|
|
$triggering_element.fadeIn();
|
227 |
|
|
$triggering_element.css('display','inline-block');
|
228 |
|
|
}
|
229 |
|
|
});
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
updateElements = function($triggering_element, base_id, settings, level) {
|
245 |
|
|
|
246 |
|
|
$triggering_element.nextAll('select').each(function() {
|
247 |
|
|
if (Drupal.settings.chosen) {
|
248 |
|
|
|
249 |
|
|
var elem_id = $(this).attr('id');
|
250 |
|
|
$('#' + elem_id.replace(/-/g, '_') + '_chzn').remove();
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
$(this).remove();
|
254 |
|
|
});
|
255 |
|
|
|
256 |
|
|
$triggering_element.nextAll('.shs-term-add-new-wrapper').remove();
|
257 |
|
|
|
258 |
|
|
if ($triggering_element.val() == '_add_new_') {
|
259 |
|
|
|
260 |
|
|
$triggering_element.hide();
|
261 |
|
|
|
262 |
|
|
$container = $('<div>')
|
263 |
|
|
.addClass('shs-term-add-new-wrapper')
|
264 |
|
|
.addClass('clearfix');
|
265 |
|
|
|
266 |
|
|
$container.appendTo($triggering_element.parent());
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
$nameField = $('<input type="text">')
|
270 |
|
|
.attr('maxlength', 255)
|
271 |
|
|
.attr('size', 10)
|
272 |
|
|
.addClass('shs-term-name')
|
273 |
|
|
.addClass('form-text');
|
274 |
|
|
$nameField.appendTo($container);
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
$buttons = $('<div>')
|
278 |
|
|
.addClass('buttons');
|
279 |
|
|
$buttons.appendTo($container);
|
280 |
|
|
$cancel = $('<a>')
|
281 |
|
|
.attr('href', '#')
|
282 |
|
|
.html(Drupal.t('Cancel'))
|
283 |
|
|
.bind('click', function(event) {
|
284 |
|
|
event.preventDefault();
|
285 |
|
|
|
286 |
|
|
$container.remove();
|
287 |
|
|
|
288 |
|
|
$triggering_element.val(0);
|
289 |
|
|
|
290 |
|
|
$triggering_element.fadeIn();
|
291 |
|
|
$triggering_element.css('display','inline-block');
|
292 |
|
|
});
|
293 |
|
|
$cancel.appendTo($buttons);
|
294 |
|
|
if (level == 1 && settings.settings.required && $('option', $triggering_element).length == 1) {
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
$cancel.hide();
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
$save = $('<a>')
|
301 |
|
|
.attr('href', '#')
|
302 |
|
|
.html(Drupal.t('Save'))
|
303 |
|
|
.bind('click', function(event) {
|
304 |
|
|
event.preventDefault();
|
305 |
|
|
|
306 |
|
|
var termName = $(this).parents('.shs-term-add-new-wrapper').find('input.shs-term-name').val();
|
307 |
|
|
|
308 |
|
|
var term = {
|
309 |
|
|
vid: settings.vid,
|
310 |
|
|
parent: $triggering_element.prev('select').val() || 0,
|
311 |
|
|
name: termName
|
312 |
|
|
};
|
313 |
|
|
if (termName.length > 0) {
|
314 |
|
|
termAddNew($triggering_element, $container, term, base_id, level);
|
315 |
|
|
}
|
316 |
|
|
else {
|
317 |
|
|
|
318 |
|
|
$container.remove();
|
319 |
|
|
|
320 |
|
|
$triggering_element.val(0);
|
321 |
|
|
|
322 |
|
|
$triggering_element.fadeIn();
|
323 |
|
|
$triggering_element.css('display','inline-block');;
|
324 |
|
|
}
|
325 |
|
|
});
|
326 |
|
|
$save.appendTo($buttons);
|
327 |
|
|
}
|
328 |
|
|
else if ($triggering_element.val() != 0) {
|
329 |
|
|
level++;
|
330 |
|
|
$element_new = shsElementCreate(base_id, settings, level);
|
331 |
|
|
$element_new.appendTo($triggering_element.parent());
|
332 |
|
|
|
333 |
|
|
getTermChildren($element_new, settings, $triggering_element.val(), 0, base_id);
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
updateFieldValue($triggering_element, base_id, level, settings.multiple);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
shsElementCreate = function(base_id, settings, level) {
|
354 |
|
|
|
355 |
|
|
if (settings.multiple) {
|
356 |
|
|
$element = $('<select>')
|
357 |
|
|
.attr('id', base_id + '-select-' + level)
|
358 |
|
|
.attr('multiple', 'multiple')
|
359 |
|
|
.addClass('shs-select')
|
360 |
|
|
|
361 |
|
|
.addClass('form-select')
|
362 |
|
|
.addClass('shs-select-level-' + level)
|
363 |
|
|
.bind('change', function() {
|
364 |
|
|
updateElements($(this), base_id, settings, level);
|
365 |
|
|
})
|
366 |
|
|
.hide();
|
367 |
|
|
}
|
368 |
|
|
else {
|
369 |
|
|
$element = $('<select>')
|
370 |
|
|
.attr('id', base_id + '-select-' + level)
|
371 |
|
|
.addClass('shs-select')
|
372 |
|
|
|
373 |
|
|
.addClass('form-select')
|
374 |
|
|
.addClass('shs-select-level-' + level)
|
375 |
|
|
.bind('change', function() {
|
376 |
|
|
updateElements($(this), base_id, settings, level);
|
377 |
|
|
})
|
378 |
|
|
.hide();
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
return $element;
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
updateFieldValue = function($triggering_element, base_id, level, multiple) {
|
395 |
|
|
|
396 |
|
|
$field_orig = $('#' + base_id);
|
397 |
|
|
$field_orig.val(0);
|
398 |
|
|
|
399 |
|
|
if ($triggering_element.val() == 0 || $triggering_element.val() == '_add_new_') {
|
400 |
|
|
if (level > 1) {
|
401 |
|
|
|
402 |
|
|
$field_orig.val($triggering_element.prev('select').val());
|
403 |
|
|
}
|
404 |
|
|
}
|
405 |
|
|
else {
|
406 |
|
|
var new_val = $triggering_element.val();
|
407 |
|
|
if (level > 1 && multiple) {
|
408 |
|
|
var new_value = '';
|
409 |
|
|
for (i = 0; i < level - 1; i++) {
|
410 |
|
|
var prev_value = $('.shs-select:eq(' + i + ')').val();
|
411 |
|
|
if (i == 0) {
|
412 |
|
|
new_value = prev_value;
|
413 |
|
|
}
|
414 |
|
|
else {
|
415 |
|
|
new_value = new_value + '+' + prev_value;
|
416 |
|
|
}
|
417 |
|
|
}
|
418 |
|
|
new_val = new_value;
|
419 |
|
|
}
|
420 |
|
|
|
421 |
|
|
if ($.isArray(new_val)) {
|
422 |
|
|
$field_orig.val(new_val.join(','));
|
423 |
|
|
}
|
424 |
|
|
else {
|
425 |
|
|
$field_orig.val(new_val);
|
426 |
|
|
}
|
427 |
|
|
}
|
428 |
|
|
}
|
429 |
|
|
|
430 |
|
|
|
431 |
|
|
|
432 |
|
|
|
433 |
|
|
|
434 |
|
|
|
435 |
|
|
elementConvertToChosen = function($element, settings) {
|
436 |
|
|
if (Drupal.settings.chosen) {
|
437 |
|
|
var minWidth = Drupal.settings.chosen.minimum_width;
|
438 |
|
|
|
439 |
|
|
var options = {};
|
440 |
|
|
options.search_contains = Drupal.settings.chosen.search_contains;
|
441 |
|
|
options.placeholder_text_multiple = Drupal.settings.chosen.placeholder_text_multiple;
|
442 |
|
|
options.placeholder_text_single = Drupal.settings.chosen.placeholder_text_single;
|
443 |
|
|
options.no_results_text = Drupal.settings.chosen.no_results_text;
|
444 |
|
|
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
var selector = Drupal.settings.chosen.selector.replace(/:visible/, '');
|
448 |
|
|
|
449 |
|
|
if ((settings.settings.use_chosen == 'always') || ((settings.settings.use_chosen == 'chosen') && ($element.is(selector) && $element.find('option').size() >= Drupal.settings.chosen.minimum))) {
|
450 |
|
|
$element.css({
|
451 |
|
|
width : ($element.width() < minWidth) ? minWidth : $element.width()
|
452 |
|
|
}).chosen(options);
|
453 |
|
|
return true;
|
454 |
|
|
}
|
455 |
|
|
}
|
456 |
|
|
return false;
|
457 |
|
|
}
|
458 |
|
|
|
459 |
|
|
})(jQuery); |