drupal.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. window.Drupal = { behaviors: {}, locale: {} };
  8. (function (Drupal, drupalSettings, drupalTranslations) {
  9. Drupal.throwError = function (error) {
  10. setTimeout(function () {
  11. throw error;
  12. }, 0);
  13. };
  14. Drupal.attachBehaviors = function (context, settings) {
  15. context = context || document;
  16. settings = settings || drupalSettings;
  17. var behaviors = Drupal.behaviors;
  18. Object.keys(behaviors || {}).forEach(function (i) {
  19. if (typeof behaviors[i].attach === 'function') {
  20. try {
  21. behaviors[i].attach(context, settings);
  22. } catch (e) {
  23. Drupal.throwError(e);
  24. }
  25. }
  26. });
  27. };
  28. Drupal.detachBehaviors = function (context, settings, trigger) {
  29. context = context || document;
  30. settings = settings || drupalSettings;
  31. trigger = trigger || 'unload';
  32. var behaviors = Drupal.behaviors;
  33. Object.keys(behaviors || {}).forEach(function (i) {
  34. if (typeof behaviors[i].detach === 'function') {
  35. try {
  36. behaviors[i].detach(context, settings, trigger);
  37. } catch (e) {
  38. Drupal.throwError(e);
  39. }
  40. }
  41. });
  42. };
  43. Drupal.checkPlain = function (str) {
  44. str = str.toString().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
  45. return str;
  46. };
  47. Drupal.formatString = function (str, args) {
  48. var processedArgs = {};
  49. Object.keys(args || {}).forEach(function (key) {
  50. switch (key.charAt(0)) {
  51. case '@':
  52. processedArgs[key] = Drupal.checkPlain(args[key]);
  53. break;
  54. case '!':
  55. processedArgs[key] = args[key];
  56. break;
  57. default:
  58. processedArgs[key] = Drupal.theme('placeholder', args[key]);
  59. break;
  60. }
  61. });
  62. return Drupal.stringReplace(str, processedArgs, null);
  63. };
  64. Drupal.stringReplace = function (str, args, keys) {
  65. if (str.length === 0) {
  66. return str;
  67. }
  68. if (!Array.isArray(keys)) {
  69. keys = Object.keys(args || {});
  70. keys.sort(function (a, b) {
  71. return a.length - b.length;
  72. });
  73. }
  74. if (keys.length === 0) {
  75. return str;
  76. }
  77. var key = keys.pop();
  78. var fragments = str.split(key);
  79. if (keys.length) {
  80. for (var i = 0; i < fragments.length; i++) {
  81. fragments[i] = Drupal.stringReplace(fragments[i], args, keys.slice(0));
  82. }
  83. }
  84. return fragments.join(args[key]);
  85. };
  86. Drupal.t = function (str, args, options) {
  87. options = options || {};
  88. options.context = options.context || '';
  89. if (typeof drupalTranslations !== 'undefined' && drupalTranslations.strings && drupalTranslations.strings[options.context] && drupalTranslations.strings[options.context][str]) {
  90. str = drupalTranslations.strings[options.context][str];
  91. }
  92. if (args) {
  93. str = Drupal.formatString(str, args);
  94. }
  95. return str;
  96. };
  97. Drupal.url = function (path) {
  98. return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path;
  99. };
  100. Drupal.url.toAbsolute = function (url) {
  101. var urlParsingNode = document.createElement('a');
  102. try {
  103. url = decodeURIComponent(url);
  104. } catch (e) {}
  105. urlParsingNode.setAttribute('href', url);
  106. return urlParsingNode.cloneNode(false).href;
  107. };
  108. Drupal.url.isLocal = function (url) {
  109. var absoluteUrl = Drupal.url.toAbsolute(url);
  110. var protocol = window.location.protocol;
  111. if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) {
  112. protocol = 'https:';
  113. }
  114. var baseUrl = protocol + '//' + window.location.host + drupalSettings.path.baseUrl.slice(0, -1);
  115. try {
  116. absoluteUrl = decodeURIComponent(absoluteUrl);
  117. } catch (e) {}
  118. try {
  119. baseUrl = decodeURIComponent(baseUrl);
  120. } catch (e) {}
  121. return absoluteUrl === baseUrl || absoluteUrl.indexOf(baseUrl + '/') === 0;
  122. };
  123. Drupal.formatPlural = function (count, singular, plural, args, options) {
  124. args = args || {};
  125. args['@count'] = count;
  126. var pluralDelimiter = drupalSettings.pluralDelimiter;
  127. var translations = Drupal.t(singular + pluralDelimiter + plural, args, options).split(pluralDelimiter);
  128. var index = 0;
  129. if (typeof drupalTranslations !== 'undefined' && drupalTranslations.pluralFormula) {
  130. index = count in drupalTranslations.pluralFormula ? drupalTranslations.pluralFormula[count] : drupalTranslations.pluralFormula.default;
  131. } else if (args['@count'] !== 1) {
  132. index = 1;
  133. }
  134. return translations[index];
  135. };
  136. Drupal.encodePath = function (item) {
  137. return window.encodeURIComponent(item).replace(/%2F/g, '/');
  138. };
  139. Drupal.theme = function (func) {
  140. if (func in Drupal.theme) {
  141. var _Drupal$theme;
  142. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  143. args[_key - 1] = arguments[_key];
  144. }
  145. return (_Drupal$theme = Drupal.theme)[func].apply(_Drupal$theme, args);
  146. }
  147. };
  148. Drupal.theme.placeholder = function (str) {
  149. return '<em class="placeholder">' + Drupal.checkPlain(str) + '</em>';
  150. };
  151. })(Drupal, window.drupalSettings, window.drupalTranslations);