| 1 |
// $Id: hierarchical_select.js,v 1.99 2009/10/30 23:39:34 wimleers Exp $
|
| 2 |
|
| 3 |
(function($) {
|
| 4 |
|
| 5 |
Drupal.behaviors.HierarchicalSelect = function (context) {
|
| 6 |
$('.hierarchical-select-wrapper:not(.hierarchical-select-wrapper-processed)', context)
|
| 7 |
.addClass('hierarchical-select-wrapper-processed').each(function() {
|
| 8 |
var hsid = $(this).attr('id').replace(/^hierarchical-select-(\d+)-wrapper$/, "$1");
|
| 9 |
Drupal.HierarchicalSelect.initialize(hsid);
|
| 10 |
});
|
| 11 |
};
|
| 12 |
|
| 13 |
Drupal.HierarchicalSelect = {};
|
| 14 |
|
| 15 |
Drupal.HierarchicalSelect.state = [];
|
| 16 |
|
| 17 |
Drupal.HierarchicalSelect.context = function() {
|
| 18 |
return $("form .hierarchical-select-wrapper");
|
| 19 |
};
|
| 20 |
|
| 21 |
Drupal.HierarchicalSelect.initialize = function(hsid) {
|
| 22 |
// Prevent JS errors when Hierarchical Select is loaded dynamically.
|
| 23 |
if (undefined == Drupal.settings.HierarchicalSelect || undefined == Drupal.settings.HierarchicalSelect.settings[hsid]) {
|
| 24 |
return false;
|
| 25 |
}
|
| 26 |
|
| 27 |
// If you set Drupal.settings.HierarchicalSelect.pretendNoJS to *anything*,
|
| 28 |
// and as such, Hierarchical Select won't initialize its Javascript! It
|
| 29 |
// will seem as if your browser had Javascript disabled.
|
| 30 |
if (undefined != Drupal.settings.HierarchicalSelect.pretendNoJS) {
|
| 31 |
return false;
|
| 32 |
}
|
| 33 |
|
| 34 |
// Turn off Firefox' autocomplete feature. This causes Hierarchical Select
|
| 35 |
// form items to be disabled after a hard refresh.
|
| 36 |
// See http://drupal.org/node/453048 and
|
| 37 |
// http://www.ryancramer.com/journal/entries/radio_buttons_firefox/
|
| 38 |
if ($.browser.mozilla) {
|
| 39 |
$('#hierarchical-select-'+ hsid +'-wrapper').parents('form').attr('autocomplete', 'off');
|
| 40 |
}
|
| 41 |
|
| 42 |
if (this.cache != null) {
|
| 43 |
this.cache.initialize();
|
| 44 |
}
|
| 45 |
|
| 46 |
Drupal.settings.HierarchicalSelect.settings[hsid]['updatesEnabled'] = true;
|
| 47 |
Drupal.HierarchicalSelect.state[hsid] = {};
|
| 48 |
|
| 49 |
this.transform(hsid);
|
| 50 |
if (Drupal.settings.HierarchicalSelect.settings[hsid].resizable) {
|
| 51 |
this.resizable(hsid);
|
| 52 |
}
|
| 53 |
Drupal.HierarchicalSelect.attachBindings(hsid);
|
| 54 |
|
| 55 |
if (this.cache != null && this.cache.status()) {
|
| 56 |
this.cache.load(hsid);
|
| 57 |
}
|
| 58 |
|
| 59 |
Drupal.HierarchicalSelect.log(hsid);
|
| 60 |
};
|
| 61 |
|
| 62 |
Drupal.HierarchicalSelect.log = function(hsid, messages) {
|
| 63 |
// Only perform logging if logging is enabled.
|
| 64 |
if (Drupal.settings.HierarchicalSelect.initialLog == undefined || Drupal.settings.HierarchicalSelect.initialLog[hsid] == undefined) {
|
| 65 |
return;
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
Drupal.HierarchicalSelect.state[hsid].log = [];
|
| 69 |
}
|
| 70 |
|
| 71 |
// Store the log messages. The first call to this function may not contain a
|
| 72 |
// message: the initial log included in the initial HTML rendering should be
|
| 73 |
// used instead..
|
| 74 |
if (Drupal.HierarchicalSelect.state[hsid].log.length == 0) {
|
| 75 |
Drupal.HierarchicalSelect.state[hsid].log.push(Drupal.settings.HierarchicalSelect.initialLog[hsid]);
|
| 76 |
}
|
| 77 |
else {
|
| 78 |
Drupal.HierarchicalSelect.state[hsid].log.push(messages);
|
| 79 |
}
|
| 80 |
|
| 81 |
// Print the log messages.
|
| 82 |
console.log("HIERARCHICAL SELECT " + hsid);
|
| 83 |
var logIndex = Drupal.HierarchicalSelect.state[hsid].log.length - 1;
|
| 84 |
for (var i = 0; i < Drupal.HierarchicalSelect.state[hsid].log[logIndex].length; i++) {
|
| 85 |
console.log(Drupal.HierarchicalSelect.state[hsid].log[logIndex][i]);
|
| 86 |
}
|
| 87 |
console.log(' ');
|
| 88 |
};
|
| 89 |
|
| 90 |
Drupal.HierarchicalSelect.transform = function(hsid) {
|
| 91 |
var removeString = $('#hierarchical-select-'+ hsid +'-wrapper .dropbox .dropbox-remove:first', Drupal.HierarchicalSelect.context).text();
|
| 92 |
|
| 93 |
$('#hierarchical-select-'+ hsid +'-wrapper', Drupal.HierarchicalSelect.context)
|
| 94 |
// Remove the .nojs div.
|
| 95 |
.find('.nojs').remove().end()
|
| 96 |
// Find all .dropbox-remove cells in the dropbox table.
|
| 97 |
.find('.dropbox .dropbox-remove')
|
| 98 |
// Hide the children of these table cells. We're not removing them because
|
| 99 |
// we want to continue to use the "Remove" checkboxes.
|
| 100 |
.find('*').css('display', 'none').end() // We can't use .hide() because of collapse.js: http://drupal.org/node/351458#comment-1258303.
|
| 101 |
// Put a "Remove" link there instead.
|
| 102 |
.append('<a href="">'+ removeString +'</a>');
|
| 103 |
};
|
| 104 |
|
| 105 |
Drupal.HierarchicalSelect.resizable = function(hsid) {
|
| 106 |
var $selectsWrapper = $('#hierarchical-select-' + hsid + '-wrapper .hierarchical-select .selects', Drupal.HierarchicalSelect.context);
|
| 107 |
|
| 108 |
// No select wrapper present: the user is creating a new item.
|
| 109 |
if ($selectsWrapper.length == 0) {
|
| 110 |
return;
|
| 111 |
}
|
| 112 |
|
| 113 |
// Append the drag handle ("grippie").
|
| 114 |
$selectsWrapper.append($('<div class="grippie"></div>'));
|
| 115 |
|
| 116 |
// jQuery object that contains all selects in the hierarchical select, to
|
| 117 |
// speed up DOM manipulation during dragging.
|
| 118 |
var $selects = $selectsWrapper.find('select');
|
| 119 |
|
| 120 |
var defaultHeight = Drupal.HierarchicalSelect.state[hsid].defaultHeight = $selects.slice(0, 1).height();
|
| 121 |
var defaultSize = Drupal.HierarchicalSelect.state[hsid].defaultSize = $selects.slice(0, 1).attr('size');
|
| 122 |
defaultSize = (defaultSize == 0) ? 1 : defaultSize;
|
| 123 |
var margin = Drupal.HierarchicalSelect.state[hsid].margin = parseInt($selects.slice(0, 1).css('margin-bottom').replace(/^(\d+)px$/, "$1"));
|
| 124 |
|
| 125 |
// Bind the drag event.
|
| 126 |
$('.grippie', $selectsWrapper)
|
| 127 |
.mousedown(startDrag)
|
| 128 |
.dblclick(function() {
|
| 129 |
if (Drupal.HierarchicalSelect.state[hsid].resizedHeight == undefined) {
|
| 130 |
Drupal.HierarchicalSelect.state[hsid].resizedHeight = defaultHeight;
|
| 131 |
}
|
| 132 |
var resizedHeight = Drupal.HierarchicalSelect.state[hsid].resizedHeight = (Drupal.HierarchicalSelect.state[hsid].resizedHeight > defaultHeight + 2) ? defaultHeight : 4.6 / defaultSize * defaultHeight;
|
| 133 |
Drupal.HierarchicalSelect.resize($selects, defaultHeight, resizedHeight, defaultSize, margin);
|
| 134 |
});
|
| 135 |
|
| 136 |
function startDrag(e) {
|
| 137 |
staticOffset = $selects.slice(0, 1).height() - e.pageY;
|
| 138 |
$selects.css('opacity', 0.25);
|
| 139 |
$(document).mousemove(performDrag).mouseup(endDrag);
|
| 140 |
return false;
|
| 141 |
}
|
| 142 |
|
| 143 |
function performDrag(e) {
|
| 144 |
var resizedHeight = staticOffset + e.pageY;
|
| 145 |
Drupal.HierarchicalSelect.resize($selects, defaultHeight, resizedHeight, defaultSize, margin);
|
| 146 |
return false;
|
| 147 |
}
|
| 148 |
|
| 149 |
function endDrag(e) {
|
| 150 |
var height = $selects.slice(0, 1).height();
|
| 151 |
|
| 152 |
$(document).unbind("mousemove", performDrag).unbind("mouseup", endDrag);
|
| 153 |
$selects.css('opacity', 1);
|
| 154 |
if (height != Drupal.HierarchicalSelect.state[hsid].resizedHeight) {
|
| 155 |
Drupal.HierarchicalSelect.state[hsid].resizedHeight = (height > defaultHeight) ? height : defaultHeight;
|
| 156 |
}
|
| 157 |
}
|
| 158 |
};
|
| 159 |
|
| 160 |
Drupal.HierarchicalSelect.resize = function($selects, defaultHeight, resizedHeight, defaultSize, margin) {
|
| 161 |
if (resizedHeight == undefined) {
|
| 162 |
resizedHeight = defaultHeight;
|
| 163 |
}
|
| 164 |
|
| 165 |
$selects
|
| 166 |
.attr('size', (resizedHeight > defaultHeight) ? 2 : defaultSize)
|
| 167 |
.height(Math.max(defaultHeight + margin, resizedHeight)); // Without the margin component, the height() method would allow the select to be sized to low: defaultHeight - margin.
|
| 168 |
};
|
| 169 |
|
| 170 |
Drupal.HierarchicalSelect.disableForm = function(hsid) {
|
| 171 |
// Disable *all* submit buttons in this form, as well as all input-related
|
| 172 |
// elements of the current hierarchical select.
|
| 173 |
$('form:has(#hierarchical-select-' + hsid +'-wrapper) input[type=submit]')
|
| 174 |
.add('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select .selects select')
|
| 175 |
.add('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select input')
|
| 176 |
.attr('disabled', true);
|
| 177 |
|
| 178 |
// Add the 'waiting' class. Default style: make everything transparent.
|
| 179 |
$('#hierarchical-select-' + hsid +'-wrapper').addClass('waiting');
|
| 180 |
|
| 181 |
// Indicate that the user has to wait.
|
| 182 |
$('body').css('cursor', 'wait');
|
| 183 |
};
|
| 184 |
|
| 185 |
Drupal.HierarchicalSelect.enableForm = function(hsid) {
|
| 186 |
// This method undoes everything the disableForm() method did.
|
| 187 |
|
| 188 |
$e = $('form:has(#hierarchical-select-' + hsid +'-wrapper) input[type=submit]');
|
| 189 |
$e = $e.add('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select input[type!=submit]');
|
| 190 |
|
| 191 |
// Don't enable the selects again if they've been disabled because the
|
| 192 |
// dropbox limit was exceeded.
|
| 193 |
dropboxLimitExceeded = $('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select-dropbox-limit-warning').length > 0;
|
| 194 |
if (!dropboxLimitExceeded) {
|
| 195 |
$e = $e.add($('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select .selects select'));
|
| 196 |
}
|
| 197 |
$e.attr('disabled', false);
|
| 198 |
|
| 199 |
// Don't enable the 'Add' button again if it's been disabled because the
|
| 200 |
// dropbox limit was exceeded.
|
| 201 |
if (dropboxLimitExceeded) {
|
| 202 |
$('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select input[type=submit]')
|
| 203 |
.attr('disabled', true);
|
| 204 |
}
|
| 205 |
|
| 206 |
$('#hierarchical-select-' + hsid +'-wrapper').removeClass('waiting');
|
| 207 |
|
| 208 |
$('body').css('cursor', 'auto');
|
| 209 |
};
|
| 210 |
|
| 211 |
Drupal.HierarchicalSelect.throwError = function(hsid, message) {
|
| 212 |
// Show the error to the user.
|
| 213 |
alert(message);
|
| 214 |
|
| 215 |
// Log the error.
|
| 216 |
Drupal.HierarchicalSelect.log(hsid, [ message ]);
|
| 217 |
|
| 218 |
// Re-enable the form to allow the user to retry, but reset the selection to
|
| 219 |
// the level label if possible, otherwise the "<none>" option if possible.
|
| 220 |
var $select = $('#hierarchical-select-' + hsid +'-wrapper .hierarchical-select .selects select:first');
|
| 221 |
var levelLabelOption = $('option[value^=label_]', $select).val();
|
| 222 |
if (levelLabelOption !== undefined) {
|
| 223 |
$select.val(levelLabelOption);
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
var noneOption = $('option[value=none]', $select).val();
|
| 227 |
if (noneOption !== undefined) {
|
| 228 |
$select.val(noneOption);
|
| 229 |
}
|
| 230 |
}
|
| 231 |
Drupal.HierarchicalSelect.enableForm(hsid);
|
| 232 |
};
|
| 233 |
|
| 234 |
Drupal.HierarchicalSelect.prepareGETSubmit = function(hsid) {
|
| 235 |
// Remove the name attributes of all form elements that end up in GET,
|
| 236 |
// except for the "flat select" form element.
|
| 237 |
$('#hierarchical-select-'+ hsid +'-wrapper', Drupal.HierarchicalSelect.context)
|
| 238 |
.find('input, select')
|
| 239 |
.not('.flat-select')
|
| 240 |
.removeAttr('name');
|
| 241 |
|
| 242 |
// Update the name attribute of the "flat select" form element
|
| 243 |
var $flatSelect = $('#hierarchical-select-'+ hsid +'-wrapper .flat-select', Drupal.HierarchicalSelect.context);
|
| 244 |
var newName = $flatSelect.attr('name').replace(/^([a-zA-Z0-9_\-]*)(?:\[flat_select\]){1}(\[\])?$/, "$1$2");
|
| 245 |
$flatSelect.attr('name', newName);
|
| 246 |
|
| 247 |
Drupal.HierarchicalSelect.triggerEvents(hsid, 'prepared-GET-submit', {});
|
| 248 |
};
|
| 249 |
|
| 250 |
Drupal.HierarchicalSelect.attachBindings = function(hsid) {
|
| 251 |
var addOpString = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select input', Drupal.HierarchicalSelect.context).val();
|
| 252 |
var createNewItemOpString = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .create-new-item-create', Drupal.HierarchicalSelect.context).val();
|
| 253 |
var cancelNewItemOpString = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .create-new-item-cancel', Drupal.HierarchicalSelect.context).val();
|
| 254 |
|
| 255 |
var data = {};
|
| 256 |
data.hsid = hsid;
|
| 257 |
|
| 258 |
$('#hierarchical-select-'+ hsid +'-wrapper', this.context)
|
| 259 |
// "disable-updates" event
|
| 260 |
.unbind('disable-updates').bind('disable-updates', data, function(e) {
|
| 261 |
Drupal.settings.HierarchicalSelect.settings[e.data.hsid]['updatesEnabled'] = false;
|
| 262 |
})
|
| 263 |
|
| 264 |
// "enforce-update" event
|
| 265 |
.unbind('enforce-update').bind('enforce-update', data, function(e, extraPost) {
|
| 266 |
Drupal.HierarchicalSelect.update(e.data.hsid, 'enforced-update', { extraPost: extraPost });
|
| 267 |
})
|
| 268 |
|
| 269 |
// "prepare-GET-submit" event
|
| 270 |
.unbind('prepare-GET-submit').bind('prepare-GET-submit', data, function(e) {
|
| 271 |
Drupal.HierarchicalSelect.prepareGETSubmit(e.data.hsid);
|
| 272 |
})
|
| 273 |
|
| 274 |
// "update-hierarchical-select" event
|
| 275 |
.find('.hierarchical-select .selects select').unbind().change(function(_hsid) {
|
| 276 |
return function() {
|
| 277 |
if (Drupal.settings.HierarchicalSelect.settings[_hsid]['updatesEnabled']) {
|
| 278 |
Drupal.HierarchicalSelect.update(_hsid, 'update-hierarchical-select', { select_id : $(this).attr('id') });
|
| 279 |
}
|
| 280 |
};
|
| 281 |
}(hsid)).end()
|
| 282 |
|
| 283 |
// "create-new-item" event
|
| 284 |
.find('.hierarchical-select .create-new-item .create-new-item-create').unbind().click(function(_hsid) {
|
| 285 |
return function() {
|
| 286 |
Drupal.HierarchicalSelect.update(_hsid, 'create-new-item', { opString : createNewItemOpString });
|
| 287 |
return false; // Prevent the browser from POSTing the page.
|
| 288 |
};
|
| 289 |
}(hsid)).end()
|
| 290 |
|
| 291 |
// "cancel-new-item" event"
|
| 292 |
.find('.hierarchical-select .create-new-item .create-new-item-cancel').unbind().click(function(_hsid) {
|
| 293 |
return function() {
|
| 294 |
Drupal.HierarchicalSelect.update(_hsid, 'cancel-new-item', { opString : cancelNewItemOpString });
|
| 295 |
return false; // Prevent the browser from POSTing the page (in case of the "Cancel" button).
|
| 296 |
};
|
| 297 |
}(hsid)).end()
|
| 298 |
|
| 299 |
// "add-to-dropbox" event
|
| 300 |
.find('.hierarchical-select .add-to-dropbox').unbind().click(function(_hsid) {
|
| 301 |
return function() {
|
| 302 |
Drupal.HierarchicalSelect.update(_hsid, 'add-to-dropbox', { opString : addOpString });
|
| 303 |
return false; // Prevent the browser from POSTing the page.
|
| 304 |
};
|
| 305 |
}(hsid)).end()
|
| 306 |
|
| 307 |
// "remove-from-dropbox" event
|
| 308 |
// (anchors in the .dropbox-remove cells in the .dropbox table)
|
| 309 |
.find('.dropbox .dropbox-remove a').unbind().click(function(_hsid) {
|
| 310 |
return function() {
|
| 311 |
var isDisabled = $('#hierarchical-select-'+ hsid +'-wrapper', Drupal.HierarchicalSelect.context).attr('disabled');
|
| 312 |
|
| 313 |
// If the hierarchical select is disabled, then ignore this click.
|
| 314 |
if (isDisabled) {
|
| 315 |
return false;
|
| 316 |
}
|
| 317 |
|
| 318 |
// Check the (hidden, because JS is enabled) checkbox that marks this
|
| 319 |
// dropbox entry for removal.
|
| 320 |
$(this).parent().find('input[type=checkbox]').attr('checked', true);
|
| 321 |
Drupal.HierarchicalSelect.update(_hsid, 'remove-from-dropbox', {});
|
| 322 |
return false; // Prevent the browser from POSTing the page.
|
| 323 |
};
|
| 324 |
}(hsid));
|
| 325 |
};
|
| 326 |
|
| 327 |
Drupal.HierarchicalSelect.preUpdateAnimations = function(hsid, updateType, lastUnchanged, callback) {
|
| 328 |
switch (updateType) {
|
| 329 |
case 'update-hierarchical-select':
|
| 330 |
// Drop out the selects of the levels deeper than the select of the
|
| 331 |
// level that just changed.
|
| 332 |
var animationDelay = Drupal.settings.HierarchicalSelect.settings[hsid]['animationDelay'];
|
| 333 |
var $animatedSelects = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context).slice(lastUnchanged);
|
| 334 |
if ($animatedSelects.size() > 0) {
|
| 335 |
$animatedSelects.hide();
|
| 336 |
for (var i = 0; i < $animatedSelects.size(); i++) {
|
| 337 |
if (i < $animatedSelects.size() - 1) {
|
| 338 |
$animatedSelects.slice(i, i + 1).hide("drop", { direction: "left" }, animationDelay);
|
| 339 |
}
|
| 340 |
else {
|
| 341 |
$animatedSelects.slice(i, i + 1).hide("drop", { direction: "left" }, animationDelay, callback);
|
| 342 |
}
|
| 343 |
}
|
| 344 |
}
|
| 345 |
else if (callback) {
|
| 346 |
callback();
|
| 347 |
}
|
| 348 |
break;
|
| 349 |
default:
|
| 350 |
if (callback) {
|
| 351 |
callback();
|
| 352 |
}
|
| 353 |
break;
|
| 354 |
}
|
| 355 |
};
|
| 356 |
|
| 357 |
Drupal.HierarchicalSelect.postUpdateAnimations = function(hsid, updateType, lastUnchanged, callback) {
|
| 358 |
if (Drupal.settings.HierarchicalSelect.settings[hsid].resizable) {
|
| 359 |
// Restore the resize.
|
| 360 |
Drupal.HierarchicalSelect.resize(
|
| 361 |
$('#hierarchical-select-' + hsid + '-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context),
|
| 362 |
Drupal.HierarchicalSelect.state[hsid].defaultHeight,
|
| 363 |
Drupal.HierarchicalSelect.state[hsid].resizedHeight,
|
| 364 |
Drupal.HierarchicalSelect.state[hsid].defaultSize,
|
| 365 |
Drupal.HierarchicalSelect.state[hsid].margin
|
| 366 |
);
|
| 367 |
}
|
| 368 |
|
| 369 |
switch (updateType) {
|
| 370 |
case 'update-hierarchical-select':
|
| 371 |
var $createNewItemInput = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .create-new-item-input', Drupal.HierarchicalSelect.context);
|
| 372 |
|
| 373 |
if ($createNewItemInput.size() == 0) {
|
| 374 |
// Give focus to the level below the one that has changed, if it
|
| 375 |
// exists.
|
| 376 |
if (!$.browser.mozilla) { // Don't give focus in Firefox: the user would have to click twice before he can make a selection.
|
| 377 |
$('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context)
|
| 378 |
.slice(lastUnchanged, lastUnchanged + 1)
|
| 379 |
.focus();
|
| 380 |
}
|
| 381 |
}
|
| 382 |
else {
|
| 383 |
// Give focus to the input field of the "create new item/level"
|
| 384 |
// section, if it exists, and also select the existing text.
|
| 385 |
$createNewItemInput.focus();
|
| 386 |
$createNewItemInput[0].select();
|
| 387 |
}
|
| 388 |
// Hide the loaded selects after the one that was just changed, then
|
| 389 |
// drop them in.
|
| 390 |
var animationDelay = Drupal.settings.HierarchicalSelect.settings[hsid]['animationDelay'];
|
| 391 |
var $animatedSelects = $('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context).slice(lastUnchanged);
|
| 392 |
if ($animatedSelects.size() > 0) {
|
| 393 |
$animatedSelects.hide();
|
| 394 |
for (var i = 0; i < $animatedSelects.size(); i++) {
|
| 395 |
if (i < $animatedSelects.size() - 1) {
|
| 396 |
$animatedSelects.slice(i, i + 1).show("drop", { direction: "left" }, animationDelay);
|
| 397 |
}
|
| 398 |
else {
|
| 399 |
$animatedSelects.slice(i, i + 1).show("drop", { direction: "left" }, animationDelay, callback);
|
| 400 |
}
|
| 401 |
}
|
| 402 |
}
|
| 403 |
else if (callback) {
|
| 404 |
callback();
|
| 405 |
}
|
| 406 |
break;
|
| 407 |
|
| 408 |
case 'create-new-item':
|
| 409 |
// Make sure that other Hierarchical Selects that represent the same
|
| 410 |
// hierarchy are also updated, to make sure that they have the newly
|
| 411 |
// created item!
|
| 412 |
var cacheId = Drupal.settings.HierarchicalSelect.settings[hsid].cacheId;
|
| 413 |
for (var otherHsid in Drupal.settings.HierarchicalSelect.settings) {
|
| 414 |
if (Drupal.settings.HierarchicalSelect.settings[otherHsid].cacheId == cacheId) {
|
| 415 |
$('#hierarchical-select-'+ otherHsid +'-wrapper')
|
| 416 |
.trigger('enforce-update');
|
| 417 |
}
|
| 418 |
}
|
| 419 |
// TRICKY: NO BREAK HERE!
|
| 420 |
|
| 421 |
case 'cancel-new-item':
|
| 422 |
// After an item/level has been created/cancelled, reset focus to the
|
| 423 |
// beginning of the hierarchical select.
|
| 424 |
$('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context)
|
| 425 |
.slice(0, 1)
|
| 426 |
.focus();
|
| 427 |
|
| 428 |
if (callback) {
|
| 429 |
callback();
|
| 430 |
}
|
| 431 |
break;
|
| 432 |
|
| 433 |
default:
|
| 434 |
if (callback) {
|
| 435 |
callback();
|
| 436 |
}
|
| 437 |
break;
|
| 438 |
}
|
| 439 |
};
|
| 440 |
|
| 441 |
Drupal.HierarchicalSelect.triggerEvents = function(hsid, updateType, settings) {
|
| 442 |
$('#hierarchical-select-'+ hsid +'-wrapper', Drupal.HierarchicalSelect.context)
|
| 443 |
.trigger(updateType, [ hsid, settings ]);
|
| 444 |
};
|
| 445 |
|
| 446 |
Drupal.HierarchicalSelect.update = function(hsid, updateType, settings) {
|
| 447 |
var post = $('form:has(#hierarchical-select-' + hsid +'-wrapper)', Drupal.HierarchicalSelect.context).formToArray();
|
| 448 |
|
| 449 |
// Pass the hierarchical_select id via POST.
|
| 450 |
post.push({ name : 'hsid', value : hsid });
|
| 451 |
|
| 452 |
// If a cache system is installed, let the server know if it's running
|
| 453 |
// properly. If it is running properly, the server will send back additional
|
| 454 |
// information to maintain a lazily-loaded cache.
|
| 455 |
if (Drupal.HierarchicalSelect.cache != null) {
|
| 456 |
post.push({ name : 'client_supports_caching', value : Drupal.HierarchicalSelect.cache.status() });
|
| 457 |
}
|
| 458 |
|
| 459 |
// updateType is one of:
|
| 460 |
// - 'none' (default)
|
| 461 |
// - 'update-hierarchical-select'
|
| 462 |
// - 'enforced-update'
|
| 463 |
// - 'create-new-item'
|
| 464 |
// - 'remove-from-dropbox'
|
| 465 |
switch (updateType) {
|
| 466 |
case 'update-hierarchical-select':
|
| 467 |
var value = $('#'+ settings.select_id).val();
|
| 468 |
var lastUnchanged = parseInt(settings.select_id.replace(/^.*-hierarchical-select-selects-(\d+)$/, "$1")) + 1;
|
| 469 |
var optionClass = $('#'+ settings.select_id).find('option[value='+ value +']').attr('class');
|
| 470 |
|
| 471 |
// Don't do anything (also no callback to the server!) when the selected
|
| 472 |
// item is:
|
| 473 |
// - the '<none>' option and the renderFlatSelect setting is disabled, or
|
| 474 |
// - a level label, or
|
| 475 |
// - an option of class 'has-no-children', and
|
| 476 |
// (the renderFlatSelect setting is disabled or the dropbox is enabled)
|
| 477 |
// and
|
| 478 |
// (the createNewLevels setting is disabled).
|
| 479 |
if ((value == 'none' && Drupal.settings.HierarchicalSelect.settings[hsid]['renderFlatSelect'] == false)
|
| 480 |
|| value.match(/^label_\d+$/)
|
| 481 |
|| (optionClass == 'has-no-children'
|
| 482 |
&&
|
| 483 |
(
|
| 484 |
(Drupal.settings.HierarchicalSelect.settings[hsid]['renderFlatSelect'] == false
|
| 485 |
|| $('#hierarchical-select-'+ hsid +'-wrapper .dropbox').length > 0
|
| 486 |
)
|
| 487 |
&&
|
| 488 |
Drupal.settings.HierarchicalSelect.settings[hsid]['createNewLevels'] == false
|
| 489 |
)
|
| 490 |
)
|
| 491 |
)
|
| 492 |
{
|
| 493 |
Drupal.HierarchicalSelect.preUpdateAnimations(hsid, updateType, lastUnchanged, function() {
|
| 494 |
// Remove the sublevels.
|
| 495 |
$('#hierarchical-select-'+ hsid +'-wrapper .hierarchical-select .selects select', Drupal.HierarchicalSelect.context)
|
| 496 |
.slice(lastUnchanged)
|
| 497 |
.remove();
|
| 498 |
|
| 499 |
// The selection of this hierarchical select has changed!
|
| 500 |
Drupal.HierarchicalSelect.triggerEvents(hsid, 'change-hierarchical-select', settings);
|
| 501 |
});
|
| 502 |
return;
|
| 503 |
}
|
| 504 |
break;
|
| 505 |
|
| 506 |
case 'enforced-update':
|
| 507 |
post = post.concat(settings.extraPost);
|
| 508 |
break;
|
| 509 |
|
| 510 |
case 'create-new-item':
|
| 511 |
case 'cancel-new-item':
|
| 512 |
case 'add-to-dropbox':
|
| 513 |
post.push({ name : 'op', value : settings.opString });
|
| 514 |
break;
|
| 515 |
}
|
| 516 |
|
| 517 |
// Construct the URL the request should be made to. GET arguments may not be
|
| 518 |
// forgotten.
|
| 519 |
var url = Drupal.settings.HierarchicalSelect.basePath + Drupal.settings.HierarchicalSelect.settings[hsid]['path'];
|
| 520 |
if (Drupal.settings.HierarchicalSelect.getArguments.length > 0) {
|
| 521 |
url += '?' + Drupal.settings.HierarchicalSelect.getArguments;
|
| 522 |
}
|
| 523 |
|
| 524 |
// Construct the object that contains the options for a callback to the
|
| 525 |
// server. If a client-side cache is found however, it's possible that this
|
| 526 |
// won't be used.
|
| 527 |
var ajaxOptions = {
|
| 528 |
url: url,
|
| 529 |
type: 'POST',
|
| 530 |
dataType: 'json',
|
| 531 |
data: post,
|
| 532 |
beforeSend: function() {
|
| 533 |
Drupal.HierarchicalSelect.triggerEvents(hsid, 'before-' + updateType, settings);
|
| 534 |
Drupal.HierarchicalSelect.disableForm(hsid);
|
| 535 |
},
|
| 536 |
error: function (XMLHttpRequest, textStatus, errorThrown) {
|
| 537 |
// When invalid HTML is received in Safari, jQuery calls this function.
|
| 538 |
Drupal.HierarchicalSelect.throwError(hsid, Drupal.t('Received an invalid response from the server.'));
|
| 539 |
},
|
| 540 |
success: function(response) {
|
| 541 |
// When invalid HTML is received in Firefox, jQuery calls this function.
|
| 542 |
if ($('.hierarchical-select-wrapper > *', $(response.output)).length == 0) {
|
| 543 |
Drupal.HierarchicalSelect.throwError(hsid, Drupal.t('Received an invalid response from the server.'));
|
| 544 |
return;
|
| 545 |
}
|
| 546 |
|
| 547 |
// Replace the old HTML with the (relevant part of) retrieved HTML.
|
| 548 |
$('#hierarchical-select-'+ hsid +'-wrapper', Drupal.HierarchicalSelect.context)
|
| 549 |
.removeClass('hierarchical-select-wrapper-processed')
|
| 550 |
.html($('.hierarchical-select-wrapper > *', $(response.output)));
|
| 551 |
|
| 552 |
// Attach behaviors. This is just after the HTML has been updated, so
|
| 553 |
// it's as soon as we can.
|
| 554 |
Drupal.attachBehaviors(Drupal.HierarchicalSelect.context);
|
| 555 |
|
| 556 |
// Transform the hierarchical select and/or dropbox to the JS variant,
|
| 557 |
// make it resizable again and re-enable the disabled form items.
|
| 558 |
Drupal.HierarchicalSelect.enableForm(hsid);
|
| 559 |
|
| 560 |
Drupal.HierarchicalSelect.postUpdateAnimations(hsid, updateType, lastUnchanged, function() {
|
| 561 |
// Update the client-side cache when:
|
| 562 |
// - information for in the cache is provided in the response, and
|
| 563 |
// - the cache system is available, and
|
| 564 |
// - the cache system is running.
|
| 565 |
if (response.cache != null && Drupal.HierarchicalSelect.cache != null && Drupal.HierarchicalSelect.cache.status()) {
|
| 566 |
Drupal.HierarchicalSelect.cache.sync(hsid, response.cache);
|
| 567 |
}
|
| 568 |
|
| 569 |
if (response.log != undefined) {
|
| 570 |
Drupal.HierarchicalSelect.log(hsid, response.log);
|
| 571 |
}
|
| 572 |
|
| 573 |
Drupal.HierarchicalSelect.triggerEvents(hsid, updateType, settings);
|
| 574 |
|
| 575 |
if (updateType == 'update-hierarchical-select') {
|
| 576 |
// The selection of this hierarchical select has changed!
|
| 577 |
Drupal.HierarchicalSelect.triggerEvents(hsid, 'change-hierarchical-select', settings);
|
| 578 |
}
|
| 579 |
});
|
| 580 |
}
|
| 581 |
};
|
| 582 |
|
| 583 |
// Use the client-side cache to update the hierarchical select when:
|
| 584 |
// - the hierarchical select is being updated (i.e. no add/remove), and
|
| 585 |
// - the renderFlatSelect setting is disabled, and
|
| 586 |
// - the createNewItems setting is disabled, and
|
| 587 |
// - the cache system is available, and
|
| 588 |
// - the cache system is running.
|
| 589 |
// Otherwise, perform a normal dynamic form submit.
|
| 590 |
if (updateType == 'update-hierarchical-select'
|
| 591 |
&& Drupal.settings.HierarchicalSelect.settings[hsid]['renderFlatSelect'] == false
|
| 592 |
&& Drupal.settings.HierarchicalSelect.settings[hsid]['createNewItems'] == false
|
| 593 |
&& Drupal.HierarchicalSelect.cache != null
|
| 594 |
&& Drupal.HierarchicalSelect.cache.status())
|
| 595 |
{
|
| 596 |
Drupal.HierarchicalSelect.cache.updateHierarchicalSelect(hsid, value, settings, lastUnchanged, ajaxOptions);
|
| 597 |
}
|
| 598 |
else {
|
| 599 |
Drupal.HierarchicalSelect.preUpdateAnimations(hsid, updateType, lastUnchanged, function() {
|
| 600 |
$.ajax(ajaxOptions);
|
| 601 |
});
|
| 602 |
}
|
| 603 |
};
|
| 604 |
|
| 605 |
})(jQuery);
|