drupal.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. for (var i in behaviors) {
  19. if (behaviors.hasOwnProperty(i) && 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. for (var i in behaviors) {
  34. if (behaviors.hasOwnProperty(i) && 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, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  45. return str;
  46. };
  47. Drupal.formatString = function (str, args) {
  48. var processedArgs = {};
  49. for (var key in args) {
  50. if (args.hasOwnProperty(key)) {
  51. switch (key.charAt(0)) {
  52. case '@':
  53. processedArgs[key] = Drupal.checkPlain(args[key]);
  54. break;
  55. case '!':
  56. processedArgs[key] = args[key];
  57. break;
  58. default:
  59. processedArgs[key] = Drupal.theme('placeholder', args[key]);
  60. break;
  61. }
  62. }
  63. }
  64. return Drupal.stringReplace(str, processedArgs, null);
  65. };
  66. Drupal.stringReplace = function (str, args, keys) {
  67. if (str.length === 0) {
  68. return str;
  69. }
  70. if (!Array.isArray(keys)) {
  71. keys = [];
  72. for (var k in args) {
  73. if (args.hasOwnProperty(k)) {
  74. keys.push(k);
  75. }
  76. }
  77. keys.sort(function (a, b) {
  78. return a.length - b.length;
  79. });
  80. }
  81. if (keys.length === 0) {
  82. return str;
  83. }
  84. var key = keys.pop();
  85. var fragments = str.split(key);
  86. if (keys.length) {
  87. for (var i = 0; i < fragments.length; i++) {
  88. fragments[i] = Drupal.stringReplace(fragments[i], args, keys.slice(0));
  89. }
  90. }
  91. return fragments.join(args[key]);
  92. };
  93. Drupal.t = function (str, args, options) {
  94. options = options || {};
  95. options.context = options.context || '';
  96. if (typeof drupalTranslations !== 'undefined' && drupalTranslations.strings && drupalTranslations.strings[options.context] && drupalTranslations.strings[options.context][str]) {
  97. str = drupalTranslations.strings[options.context][str];
  98. }
  99. if (args) {
  100. str = Drupal.formatString(str, args);
  101. }
  102. return str;
  103. };
  104. Drupal.url = function (path) {
  105. return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path;
  106. };
  107. Drupal.url.toAbsolute = function (url) {
  108. var urlParsingNode = document.createElement('a');
  109. try {
  110. url = decodeURIComponent(url);
  111. } catch (e) {}
  112. urlParsingNode.setAttribute('href', url);
  113. return urlParsingNode.cloneNode(false).href;
  114. };
  115. Drupal.url.isLocal = function (url) {
  116. var absoluteUrl = Drupal.url.toAbsolute(url);
  117. var protocol = location.protocol;
  118. if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) {
  119. protocol = 'https:';
  120. }
  121. var baseUrl = protocol + '//' + location.host + drupalSettings.path.baseUrl.slice(0, -1);
  122. try {
  123. absoluteUrl = decodeURIComponent(absoluteUrl);
  124. } catch (e) {}
  125. try {
  126. baseUrl = decodeURIComponent(baseUrl);
  127. } catch (e) {}
  128. return absoluteUrl === baseUrl || absoluteUrl.indexOf(baseUrl + '/') === 0;
  129. };
  130. Drupal.formatPlural = function (count, singular, plural, args, options) {
  131. args = args || {};
  132. args['@count'] = count;
  133. var pluralDelimiter = drupalSettings.pluralDelimiter;
  134. var translations = Drupal.t(singular + pluralDelimiter + plural, args, options).split(pluralDelimiter);
  135. var index = 0;
  136. if (typeof drupalTranslations !== 'undefined' && drupalTranslations.pluralFormula) {
  137. index = count in drupalTranslations.pluralFormula ? drupalTranslations.pluralFormula[count] : drupalTranslations.pluralFormula.default;
  138. } else if (args['@count'] !== 1) {
  139. index = 1;
  140. }
  141. return translations[index];
  142. };
  143. Drupal.encodePath = function (item) {
  144. return window.encodeURIComponent(item).replace(/%2F/g, '/');
  145. };
  146. Drupal.theme = function (func) {
  147. var args = Array.prototype.slice.apply(arguments, [1]);
  148. if (func in Drupal.theme) {
  149. return Drupal.theme[func].apply(this, args);
  150. }
  151. };
  152. Drupal.theme.placeholder = function (str) {
  153. return '<em class="placeholder">' + Drupal.checkPlain(str) + '</em>';
  154. };
  155. })(Drupal, window.drupalSettings, window.drupalTranslations);