// JavaScript should be made compatible with libraries other than jQuery by
// wrapping it in an anonymous closure.
-(function (Drupal, drupalSettings, drupalTranslations) {
+(function(Drupal, drupalSettings, drupalTranslations) {
/**
* Helper to rethrow errors asynchronously.
*
* @param {Error|string} error
* The error to be thrown.
*/
- Drupal.throwError = function (error) {
+ Drupal.throwError = function(error) {
setTimeout(() => {
throw error;
}, 0);
*
* @throws {Drupal~DrupalBehaviorError}
*/
- Drupal.attachBehaviors = function (context, settings) {
+ Drupal.attachBehaviors = function(context, settings) {
context = context || document;
settings = settings || drupalSettings;
const behaviors = Drupal.behaviors;
// Execute all of them.
- Object.keys(behaviors || {}).forEach((i) => {
+ Object.keys(behaviors || {}).forEach(i => {
if (typeof behaviors[i].attach === 'function') {
// Don't stop the execution of behaviors in case of an error.
try {
behaviors[i].attach(context, settings);
- }
- catch (e) {
+ } catch (e) {
Drupal.throwError(e);
}
}
* @see Drupal~behaviorDetach
* @see Drupal.attachBehaviors
*/
- Drupal.detachBehaviors = function (context, settings, trigger) {
+ Drupal.detachBehaviors = function(context, settings, trigger) {
context = context || document;
settings = settings || drupalSettings;
trigger = trigger || 'unload';
const behaviors = Drupal.behaviors;
// Execute all of them.
- Object.keys(behaviors || {}).forEach((i) => {
+ Object.keys(behaviors || {}).forEach(i => {
if (typeof behaviors[i].detach === 'function') {
// Don't stop the execution of behaviors in case of an error.
try {
behaviors[i].detach(context, settings, trigger);
- }
- catch (e) {
+ } catch (e) {
Drupal.throwError(e);
}
}
*
* @ingroup sanitization
*/
- Drupal.checkPlain = function (str) {
- str = str.toString()
+ Drupal.checkPlain = function(str) {
+ str = str
+ .toString()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
*
* @see Drupal.t
*/
- Drupal.formatString = function (str, args) {
+ Drupal.formatString = function(str, args) {
// Keep args intact.
const processedArgs = {};
// Transform arguments before inserting them.
- Object.keys(args || {}).forEach((key) => {
+ Object.keys(args || {}).forEach(key => {
switch (key.charAt(0)) {
// Escaped only.
case '@':
* @return {string}
* The replaced string.
*/
- Drupal.stringReplace = function (str, args, keys) {
+ Drupal.stringReplace = function(str, args, keys) {
if (str.length === 0) {
return str;
}
* The formatted string.
* The translated string.
*/
- Drupal.t = function (str, args, options) {
+ Drupal.t = function(str, args, options) {
options = options || {};
options.context = options.context || '';
// Fetch the localized version of the string.
- if (typeof drupalTranslations !== 'undefined' && drupalTranslations.strings && drupalTranslations.strings[options.context] && drupalTranslations.strings[options.context][str]) {
+ if (
+ typeof drupalTranslations !== 'undefined' &&
+ drupalTranslations.strings &&
+ drupalTranslations.strings[options.context] &&
+ drupalTranslations.strings[options.context][str]
+ ) {
str = drupalTranslations.strings[options.context][str];
}
* @return {string}
* The full URL.
*/
- Drupal.url = function (path) {
+ Drupal.url = function(path) {
return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path;
};
* @see https://grack.com/blog/2009/11/17/absolutizing-url-in-javascript
* @see https://github.com/jquery/jquery-ui/blob/1.11.4/ui/tabs.js#L53
*/
- Drupal.url.toAbsolute = function (url) {
+ Drupal.url.toAbsolute = function(url) {
const urlParsingNode = document.createElement('a');
// Decode the URL first; this is required by IE <= 6. Decoding non-UTF-8
// strings may throw an exception.
try {
url = decodeURIComponent(url);
- }
- catch (e) {
+ } catch (e) {
// Empty.
}
*
* @see https://github.com/jquery/jquery-ui/blob/1.11.4/ui/tabs.js#L58
*/
- Drupal.url.isLocal = function (url) {
+ Drupal.url.isLocal = function(url) {
// Always use browser-derived absolute URLs in the comparison, to avoid
// attempts to break out of the base path using directory traversal.
let absoluteUrl = Drupal.url.toAbsolute(url);
- let protocol = location.protocol;
+ let { protocol } = window.location;
// Consider URLs that match this site's base URL but use HTTPS instead of HTTP
// as local as well.
if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) {
protocol = 'https:';
}
- let baseUrl = `${protocol}//${location.host}${drupalSettings.path.baseUrl.slice(0, -1)}`;
+ let baseUrl = `${protocol}//${
+ window.location.host
+ }${drupalSettings.path.baseUrl.slice(0, -1)}`;
// Decoding non-UTF-8 strings may throw an exception.
try {
absoluteUrl = decodeURIComponent(absoluteUrl);
- }
- catch (e) {
+ } catch (e) {
// Empty.
}
try {
baseUrl = decodeURIComponent(baseUrl);
- }
- catch (e) {
+ } catch (e) {
// Empty.
}
* @return {string}
* A translated string.
*/
- Drupal.formatPlural = function (count, singular, plural, args, options) {
+ Drupal.formatPlural = function(count, singular, plural, args, options) {
args = args || {};
args['@count'] = count;
const pluralDelimiter = drupalSettings.pluralDelimiter;
- const translations = Drupal.t(singular + pluralDelimiter + plural, args, options).split(pluralDelimiter);
+ const translations = Drupal.t(
+ singular + pluralDelimiter + plural,
+ args,
+ options,
+ ).split(pluralDelimiter);
let index = 0;
// Determine the index of the plural form.
- if (typeof drupalTranslations !== 'undefined' && drupalTranslations.pluralFormula) {
- index = count in drupalTranslations.pluralFormula ? drupalTranslations.pluralFormula[count] : drupalTranslations.pluralFormula.default;
- }
- else if (args['@count'] !== 1) {
+ if (
+ typeof drupalTranslations !== 'undefined' &&
+ drupalTranslations.pluralFormula
+ ) {
+ index =
+ count in drupalTranslations.pluralFormula
+ ? drupalTranslations.pluralFormula[count]
+ : drupalTranslations.pluralFormula.default;
+ } else if (args['@count'] !== 1) {
index = 1;
}
* @return {string}
* The encoded path.
*/
- Drupal.encodePath = function (item) {
+ Drupal.encodePath = function(item) {
return window.encodeURIComponent(item).replace(/%2F/g, '/');
};
* Any data the theme function returns. This could be a plain HTML string,
* but also a complex object.
*/
- Drupal.theme = function (func, ...args) {
+ Drupal.theme = function(func, ...args) {
if (func in Drupal.theme) {
return Drupal.theme[func](...args);
}
* @return {string}
* The formatted text (html).
*/
- Drupal.theme.placeholder = function (str) {
+ Drupal.theme.placeholder = function(str) {
return `<em class="placeholder">${Drupal.checkPlain(str)}</em>`;
};
-}(Drupal, window.drupalSettings, window.drupalTranslations));
+})(Drupal, window.drupalSettings, window.drupalTranslations);