5 (function ($, Drupal, Bootstrap, Attributes, drupalSettings) {
9 * Only process this once.
11 Bootstrap.once('modal.jquery.ui.bridge', function (settings) {
13 var rtl = document.documentElement.getAttribute('dir').toLowerCase() === 'rtl';
15 // Override drupal.dialog button classes. This must be done on DOM ready
16 // since core/drupal.dialog technically depends on this file and has not
17 // yet set their default settings.
19 drupalSettings.dialog.buttonClass = 'btn';
20 drupalSettings.dialog.buttonPrimaryClass = 'btn-primary';
23 var relayEvent = function ($element, name, stopPropagation) {
25 if (stopPropagation === void 0 || stopPropagation) {
28 var parts = name.split('.').filter(Boolean);
29 var type = parts.shift();
30 e.target = $element[0];
31 e.currentTarget = $element[0];
32 e.namespace = parts.join('.');
39 * Proxy $.fn.dialog to $.fn.modal.
41 Bootstrap.createPlugin('dialog', function (options) {
42 // When only options are passed, jQuery UI dialog treats this like a
43 // initialization method. Destroy any existing Bootstrap modal and
44 // recreate it using the contents of the dialog HTML.
45 if (arguments.length === 1 && typeof options === 'object') {
46 this.each(function () {
47 // This part gets a little tricky. Core can potentially already
48 // semi-process this "dialog" if was created using an Ajax command
49 // (i.e. prepareDialogButtons in drupal.ajax.js). Because of this,
50 // we cannot simply dump the existing dialog content into a newly
51 // created modal because that would destroy any existing event
52 // bindings. Instead, we have to create this in steps and "move"
53 // (append) the existing content as needed.
56 // Create a new modal to get a complete template.
57 var $modal = $(Drupal.theme('bootstrapModal', {attributes: Attributes.create(this).remove('style')}));
59 // Store a reference to the content inside the existing dialog.
60 // This references the actual DOM node elements which will allow
61 // jQuery to "move" then when appending below. Using $.fn.children()
62 // does not return any text nodes present and $.fn.html() only returns
63 // a string representation of the content, which effectively destroys
64 // any prior event bindings or processing.
65 var $existing = $this.contents();
67 // Destroy any existing Bootstrap Modal data that may have been saved.
68 $this.removeData('bs.modal');
70 // Set the attributes of the dialog to that of the newly created modal.
71 $this.attr(Attributes.create($modal).toPlainObject());
73 // Append the newly created modal markup.
74 $this.append($modal.html());
76 // Move the existing HTML into the modal markup that was just appended.
77 $this.find('.modal-body').append($existing);
80 // Indicate that the modal is a jQuery UI dialog bridge.
81 options.jQueryUiBridge = true;
83 // Proxy just the options to the Bootstrap Modal plugin.
84 return $.fn.modal.apply(this, [options]);
87 // Otherwise, proxy all arguments to the Bootstrap Modal plugin.
88 return $.fn.modal.apply(this, arguments);
92 * Extend the Bootstrap Modal plugin constructor class.
94 Bootstrap.extendPlugin('modal', function () {
99 // By default, this option is disabled. It's only flagged when a modal
100 // was created using $.fn.dialog above.
101 jQueryUiBridge: false
106 * Handler for $.fn.dialog('close').
111 this.hide.apply(this, arguments);
113 // For some reason (likely due to the transition event not being
114 // registered properly), the backdrop doesn't always get removed
115 // after the above "hide" method is invoked . Instead, ensure the
116 // backdrop is removed after the transition duration by manually
117 // invoking the internal "hideModal" method shortly thereafter.
118 setTimeout(function () {
119 if (!_this.isShown && _this.$backdrop) {
122 }, (Modal.TRANSITION_DURATION !== void 0 ? Modal.TRANSITION_DURATION : 300) + 10);
126 * Creates any necessary buttons from dialog options.
128 createButtons: function () {
129 this.$footer.find('.modal-buttons').remove();
130 var buttons = this.options.dialogOptions && this.options.dialogOptions.buttons || [];
131 if (buttons.length) {
132 var $buttons = $('<div class="modal-buttons"/>').appendTo(this.$footer);
133 for (var i = 0, l = buttons.length; i < l; i++) {
134 var button = buttons[i];
135 var $button = $(Drupal.theme('bootstrapModalDialogButton', button));
136 if (typeof button.click === 'function') {
137 $button.on('click', button.click);
139 $buttons.append($button);
145 * Initializes the Bootstrap Modal.
148 // Relay necessary events.
149 if (this.options.jQueryUiBridge) {
150 this.$element.on('hide.bs.modal', relayEvent(this.$element, 'dialogbeforeclose', false));
151 this.$element.on('hidden.bs.modal', relayEvent(this.$element, 'dialogclose', false));
152 this.$element.on('show.bs.modal', relayEvent(this.$element, 'dialogcreate', false));
153 this.$element.on('shown.bs.modal', relayEvent(this.$element, 'dialogopen', false));
156 // Create a footer if one doesn't exist.
157 // This is necessary in case dialog.ajax.js decides to add buttons.
158 if (!this.$footer[0]) {
159 this.$footer = $(Drupal.theme('bootstrapModalFooter', {}, true)).insertAfter(this.$dialogBody);
163 this.createButtons();
165 // Hide the footer if there are no children.
166 if (!this.$footer.children()[0]) {
170 // Now call the parent init method.
173 // Handle autoResize option (this is a drupal.dialog option).
174 if (this.options.dialogOptions && this.options.dialogOptions.autoResize && this.options.dialogOptions.position) {
175 this.position(this.options.dialogOptions.position);
178 // If show is enabled and currently not shown, show it.
179 if (this.options.show && !this.isShown) {
185 * Handler for $.fn.dialog('instance').
187 instance: function () {
188 Bootstrap.unsupported('method', 'instance', arguments);
192 * Handler for $.fn.dialog('isOpen').
194 isOpen: function () {
195 return !!this.isShown;
199 * Maps dialog options to the modal.
201 * @param {Object} options
202 * The options to map.
204 mapDialogOptions: function (options) {
205 var dialogOptions = {};
206 var mappedOptions = {};
208 // Handle CSS properties.
209 var cssUnitRegExp = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)?$/;
210 var parseCssUnit = function (value, defaultUnit) {
211 var parts = ('' + value).match(cssUnitRegExp);
212 return parts && parts[1] !== void 0 ? parts[1] + (parts[2] || defaultUnit || 'px') : null;
215 var cssProperties = ['height', 'maxHeight', 'maxWidth', 'minHeight', 'minWidth', 'width'];
216 for (var i = 0, l = cssProperties.length; i < l; i++) {
217 var prop = cssProperties[i];
218 if (options[prop] !== void 0) {
219 var value = parseCssUnit(options[prop]);
221 dialogOptions[prop] = value;
222 styles[prop] = value;
227 // Apply mapped CSS styles to the modal-content container.
228 this.$content.css(styles);
230 // Handle deprecated "dialogClass" option by merging it with "classes".
232 'ui-dialog': 'modal-content',
233 'ui-dialog-titlebar': 'modal-header',
234 'ui-dialog-title': 'modal-title',
235 'ui-dialog-titlebar-close': 'close',
236 'ui-dialog-content': 'modal-body',
237 'ui-dialog-buttonpane': 'modal-footer'
239 if (options.dialogClass) {
240 if (options.classes === void 0) {
241 options.classes = {};
243 if (options.classes['ui-dialog'] === void 0) {
244 options.classes['ui-dialog'] = '';
246 var dialogClass = options.classes['ui-dialog'].split(' ');
247 dialogClass.push(options.dialogClass);
248 options.classes['ui-dialog'] = dialogClass.join(' ');
249 delete options.dialogClass;
255 'beforeClose', 'close',
257 'drag', 'dragStart', 'dragStop',
260 'resize', 'resizeStart', 'resizeStop'
262 for (i = 0, l = events.length; i < l; i++) {
263 var event = events[i].toLowerCase();
264 if (options[event] === void 0 || typeof options[event] !== 'function') continue;
265 this.$element.on('dialog' + event, options[event]);
268 // Handle the reset of the options.
269 for (var name in options) {
270 if (!options.hasOwnProperty(name) || options[name] === void 0) continue;
274 Bootstrap.unsupported('option', name, options.appendTo);
278 mappedOptions.show = !!options.autoOpen;
281 // This is really a drupal.dialog option, not jQuery UI.
283 dialogOptions.autoResize = !!options.autoResize;
287 dialogOptions.buttons = options.buttons;
291 dialogOptions.classes = options.classes;
292 for (var key in options.classes) {
293 if (options.classes.hasOwnProperty(key) && classesMap[key] !== void 0) {
294 // Run through Attributes to sanitize classes.
295 var attributes = Attributes.create().addClass(options.classes[key]).toPlainObject();
296 var selector = '.' + classesMap[key];
297 this.$element.find(selector).addClass(attributes['class']);
302 case 'closeOnEscape':
303 dialogOptions.closeOnEscape = options.closeOnEscape;
304 mappedOptions.keyboard = !!options.closeOnEscape;
305 this.$close[options.closeOnEscape ? 'show' : 'hide']();
306 if (!options.closeOnEscape && options.modal) {
307 mappedOptions.backdrop = options.modal = 'static';
312 Bootstrap.unsupported('option', name, options.closeText);
316 dialogOptions.draggable = options.draggable;
319 handle: '.modal-header',
320 drag: relayEvent(this.$element, 'dialogdrag'),
321 start: relayEvent(this.$element, 'dialogdragstart'),
322 end: relayEvent(this.$element, 'dialogdragend')
324 .draggable(options.draggable ? 'enable' : 'disable');
328 if (options.hide === false || options.hide === true) {
329 this.$element[options.hide ? 'addClass' : 'removeClass']('fade');
330 mappedOptions.animation = options.hide;
333 Bootstrap.unsupported('option', name + ' (complex animation)', options.hide);
338 mappedOptions.backdrop = options.modal;
339 dialogOptions.modal = !!options.modal;
341 // If not a modal and no initial position, center it.
342 if (!options.modal && !options.position) {
343 this.position({ my: 'center', of: window });
348 dialogOptions.position = options.position;
349 this.position(options.position);
352 // Resizable support (must initialize first).
354 dialogOptions.resizeable = options.resizable;
357 resize: relayEvent(this.$element, 'dialogresize'),
358 start: relayEvent(this.$element, 'dialogresizestart'),
359 end: relayEvent(this.$element, 'dialogresizeend')
361 .resizable(options.resizable ? 'enable' : 'disable');
365 if (options.show === false || options.show === true) {
366 this.$element[options.show ? 'addClass' : 'removeClass']('fade');
367 mappedOptions.animation = options.show;
370 Bootstrap.unsupported('option', name + ' (complex animation)', options.show);
375 dialogOptions.title = options.title;
376 this.$dialog.find('.modal-title').text(options.title);
382 // Add the supported dialog options to the mapped options.
383 mappedOptions.dialogOptions = dialogOptions;
385 return mappedOptions;
389 * Handler for $.fn.dialog('moveToTop').
391 moveToTop: function () {
392 Bootstrap.unsupported('method', 'moveToTop', arguments);
396 * Handler for $.fn.dialog('option').
398 option: function () {
399 var clone = {options: $.extend({}, this.options)};
401 // Apply the parent option method to the clone of current options.
402 this.super.apply(clone, arguments);
404 // Merge in the cloned mapped options.
405 $.extend(true, this.options, this.mapDialogOptions(clone.options));
408 position: function(position) {
409 // Reset modal styling.
416 // Position the modal.
417 this.$element.position(position);
421 * Handler for $.fn.dialog('open').
424 this.show.apply(this, arguments);
428 * Handler for $.fn.dialog('widget').
430 widget: function () {
431 return this.$element;
438 * Extend Drupal theming functions.
440 $.extend(Drupal.theme, /** @lend Drupal.theme */ {
443 * Renders a jQuery UI Dialog compatible button element.
445 * @param {Object} button
446 * The button object passed in the dialog options.
449 * The modal dialog button markup.
451 * @see http://api.jqueryui.com/dialog/#option-buttons
452 * @see http://api.jqueryui.com/button/
454 bootstrapModalDialogButton: function (button) {
455 var attributes = Attributes.create();
458 var iconPosition = button.iconPosition || 'beginning';
459 iconPosition = (iconPosition === 'end' && !rtl) || (iconPosition === 'beginning' && rtl) ? 'after' : 'before';
461 var iconAttributes = Attributes.create()
462 .addClass(['ui-icon', button.icon])
463 .set('aria-hidden', 'true');
464 icon = '<span' + iconAttributes + '></span>';
468 var value = button.text;
469 attributes.set('value', iconPosition === 'before' ? icon + value : value + icon);
472 attributes[button.disabled ? 'set' :'remove']('disabled', 'disabled');
474 if (button.classes) {
475 attributes.addClass(Object.keys(button.classes).map(function(key) { return button.classes[key]; }));
477 if (button['class']) {
478 attributes.addClass(button['class']);
481 return Drupal.theme('button', attributes);
489 })(window.jQuery, window.Drupal, window.Drupal.bootstrap, window.Attributes, window.drupalSettings);