express-8.x-1.x-dev/themes/contrib/bootstrap/js/drupal.bootstrap.js
themes/contrib/bootstrap/js/drupal.bootstrap.js
/**
* @file
* Drupal Bootstrap object.
*/
/**
* All Drupal Bootstrap JavaScript APIs are contained in this namespace.
*
* @namespace
*/
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.bootstrap = {
settings: drupalSettings.bootstrap || {}
};
/**
* Wraps Drupal.checkPlain() to ensure value passed isn't empty.
*
* Encodes special characters in a plain-text string for display as HTML.
*
* @param {string} str
* The string to be encoded.
*
* @return {string}
* The encoded string.
*
* @ingroup sanitization
*/
Drupal.bootstrap.checkPlain = function (str) {
return str && Drupal.checkPlain(str) || '';
};
/**
* Extends a Bootstrap plugin constructor.
*
* @param {string} id
* A Bootstrap plugin identifier located in $.fn.
* @param {function} [callback]
* A callback to extend the plugin constructor.
*
* @return {function|boolean}
* The Bootstrap plugin or FALSE if the plugin does not exist.
*/
Drupal.bootstrap.extendPlugin = function (id, callback) {
// Immediately return if the plugin does not exist.
if (!$.fn[id] || !$.fn[id].Constructor) return false;
// Extend the plugin if a callback was provided.
if ($.isFunction(callback)) {
var ret = callback.apply($.fn[id].Constructor, [this.settings]);
if ($.isPlainObject(ret)) {
$.extend(true, $.fn[id].Constructor, ret);
}
}
// Add a jQuery UI like option getter/setter method.
if ($.fn[id].Constructor.prototype.option === void(0)) {
$.fn[id].Constructor.prototype.option = this.option;
}
return $.fn[id].Constructor;
};
/**
* Replaces a Bootstrap jQuery plugin definition.
*
* @param {string} id
* A Bootstrap plugin identifier located in $.fn.
* @param {function} [callback]
* A callback to replace the jQuery plugin definition. The callback must
* return a function that is used to construct a jQuery plugin.
*
* @return {function|boolean}
* The Bootstrap jQuery plugin definition or FALSE if the plugin does not
* exist.
*/
Drupal.bootstrap.replacePlugin = function (id, callback) {
// Immediately return if plugin does not exist or not a valid callback.
if (!$.fn[id] || !$.fn[id].Constructor || !$.isFunction(callback)) return false;
var constructor = $.fn[id].Constructor;
var plugin = callback.apply(constructor);
if ($.isFunction(plugin)) {
plugin.Constructor = constructor;
var old = $.fn[id];
plugin.noConflict = function () {
$.fn[id] = old;
return this;
};
$.fn[id] = plugin;
}
};
/**
* Map of supported events by regular expression.
*
* @type {Object<Event|MouseEvent|KeyboardEvent|TouchEvent,RegExp>}
*/
Drupal.bootstrap.eventMap = {
Event: /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
MouseEvent: /^(?:click|dblclick|mouse(?:down|enter|leave|up|over|move|out))$/,
KeyboardEvent: /^(?:key(?:down|press|up))$/,
TouchEvent: /^(?:touch(?:start|end|move|cancel))$/
};
/**
* Simulates a native event on an element in the browser.
*
* Note: This is a pretty complete modern implementation. If things are quite
* working the way you intend (in older browsers), you may wish to use the
* jQuery.simulate plugin. If it's available, this method will defer to it.
*
* @see https://github.com/jquery/jquery-simulate
*
* @param {HTMLElement} element
* A DOM element to dispatch event on.
* @param {String} type
* The type of event to simulate.
* @param {Object} [options]
* An object of options to pass to the event constructor. Typically, if
* an event is being proxied, you should just pass the original event
* object here. This allows, if the browser supports it, to be a truly
* simulated event.
*/
Drupal.bootstrap.simulate = function (element, type, options) {
// Defer to the jQuery.simulate plugin, if it's available.
if (typeof $.simulate === 'function') {
new $.simulate(element, type, options);
return;
}
var event;
var ctor;
for (var name in Drupal.bootstrap.eventMap) {
if (Drupal.bootstrap.eventMap[name].test(type)) {
ctor = name;
break;
}
}
if (!ctor) {
throw new SyntaxError('Only rudimentary HTMLEvents, KeyboardEvents and MouseEvents are supported: ' + type);
}
var opts = {bubbles: true, cancelable: true};
if (ctor === 'KeyboardEvent' || ctor === 'MouseEvent') {
$.extend(opts, {ctrlKey: !1, altKey: !1, shiftKey: !1, metaKey: !1});
}
if (ctor === 'MouseEvent') {
$.extend(opts, {button: 0, pointerX: 0, pointerY: 0, view: window});
}
if (options) {
$.extend(opts, options);
}
if (typeof window[ctor] === 'function') {
event = new window[ctor](type, opts);
element.dispatchEvent(event);
}
else if (document.createEvent) {
event = document.createEvent(ctor);
event.initEvent(type, opts.bubbles, opts.cancelable);
element.dispatchEvent(event);
}
else if (typeof element.fireEvent === 'function') {
event = $.extend(document.createEventObject(), opts);
element.fireEvent('on' + type, event);
}
else if (typeof element[type]) {
element[type]();
}
};
/**
* Provide jQuery UI like ability to get/set options for Bootstrap plugins.
*
* @param {string|object} key
* A string value of the option to set, can be dot like to a nested key.
* An object of key/value pairs.
* @param {*} [value]
* (optional) A value to set for key.
*
* @returns {*}
* - Returns nothing if key is an object or both key and value parameters
* were provided to set an option.
* - Returns the a value for a specific setting if key was provided.
* - Returns an object of key/value pairs of all the options if no key or
* value parameter was provided.
*
* @see https://github.com/jquery/jquery-ui/blob/master/ui/widget.js
*
* @todo This isn't fully working since Bootstrap plugins don't allow
* methods to return values.
*/
Drupal.bootstrap.option = function (key, value) {
var options = key;
var parts, curOption, i;
// Don't return a reference to the internal hash.
if (arguments.length === 0) {
return $.extend({}, this.options);
}
// Handle a specific option.
if (typeof key === "string") {
// Handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
options = {};
parts = key.split(".");
key = parts.shift();
if (parts.length) {
curOption = options[key] = $.extend({}, this.options[key]);
for (i = 0; i < parts.length - 1; i++) {
curOption[parts[i]] = curOption[parts[i]] || {};
curOption = curOption[parts[i]];
}
key = parts.pop();
if (arguments.length === 1) {
return curOption[key] === undefined ? null : curOption[key];
}
curOption[key] = value;
}
else {
if (arguments.length === 1) {
return this.options[key] === undefined ? null : this.options[key];
}
options[key] = value;
}
}
// Set the new option(s).
for (key in options) {
if (!options.hasOwnProperty(key)) continue;
this.options[key] = options[key];
}
return this;
};
})(window.jQuery, window.Drupal, window.drupalSettings);
