Mercurial > hg > cmmr2012-drupal-site
comparison core/misc/drupal.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 | |
8 window.Drupal = { behaviors: {}, locale: {} }; | |
9 | |
10 (function (Drupal, drupalSettings, drupalTranslations) { | |
11 Drupal.throwError = function (error) { | |
12 setTimeout(function () { | |
13 throw error; | |
14 }, 0); | |
15 }; | |
16 | |
17 Drupal.attachBehaviors = function (context, settings) { | |
18 context = context || document; | |
19 settings = settings || drupalSettings; | |
20 var behaviors = Drupal.behaviors; | |
21 | |
22 Object.keys(behaviors || {}).forEach(function (i) { | |
23 if (typeof behaviors[i].attach === 'function') { | |
24 try { | |
25 behaviors[i].attach(context, settings); | |
26 } catch (e) { | |
27 Drupal.throwError(e); | |
28 } | |
29 } | |
30 }); | |
31 }; | |
32 | |
33 Drupal.detachBehaviors = function (context, settings, trigger) { | |
34 context = context || document; | |
35 settings = settings || drupalSettings; | |
36 trigger = trigger || 'unload'; | |
37 var behaviors = Drupal.behaviors; | |
38 | |
39 Object.keys(behaviors || {}).forEach(function (i) { | |
40 if (typeof behaviors[i].detach === 'function') { | |
41 try { | |
42 behaviors[i].detach(context, settings, trigger); | |
43 } catch (e) { | |
44 Drupal.throwError(e); | |
45 } | |
46 } | |
47 }); | |
48 }; | |
49 | |
50 Drupal.checkPlain = function (str) { | |
51 str = str.toString().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '''); | |
52 return str; | |
53 }; | |
54 | |
55 Drupal.formatString = function (str, args) { | |
56 var processedArgs = {}; | |
57 | |
58 Object.keys(args || {}).forEach(function (key) { | |
59 switch (key.charAt(0)) { | |
60 case '@': | |
61 processedArgs[key] = Drupal.checkPlain(args[key]); | |
62 break; | |
63 | |
64 case '!': | |
65 processedArgs[key] = args[key]; | |
66 break; | |
67 | |
68 default: | |
69 processedArgs[key] = Drupal.theme('placeholder', args[key]); | |
70 break; | |
71 } | |
72 }); | |
73 | |
74 return Drupal.stringReplace(str, processedArgs, null); | |
75 }; | |
76 | |
77 Drupal.stringReplace = function (str, args, keys) { | |
78 if (str.length === 0) { | |
79 return str; | |
80 } | |
81 | |
82 if (!Array.isArray(keys)) { | |
83 keys = Object.keys(args || {}); | |
84 | |
85 keys.sort(function (a, b) { | |
86 return a.length - b.length; | |
87 }); | |
88 } | |
89 | |
90 if (keys.length === 0) { | |
91 return str; | |
92 } | |
93 | |
94 var key = keys.pop(); | |
95 var fragments = str.split(key); | |
96 | |
97 if (keys.length) { | |
98 for (var i = 0; i < fragments.length; i++) { | |
99 fragments[i] = Drupal.stringReplace(fragments[i], args, keys.slice(0)); | |
100 } | |
101 } | |
102 | |
103 return fragments.join(args[key]); | |
104 }; | |
105 | |
106 Drupal.t = function (str, args, options) { | |
107 options = options || {}; | |
108 options.context = options.context || ''; | |
109 | |
110 if (typeof drupalTranslations !== 'undefined' && drupalTranslations.strings && drupalTranslations.strings[options.context] && drupalTranslations.strings[options.context][str]) { | |
111 str = drupalTranslations.strings[options.context][str]; | |
112 } | |
113 | |
114 if (args) { | |
115 str = Drupal.formatString(str, args); | |
116 } | |
117 return str; | |
118 }; | |
119 | |
120 Drupal.url = function (path) { | |
121 return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path; | |
122 }; | |
123 | |
124 Drupal.url.toAbsolute = function (url) { | |
125 var urlParsingNode = document.createElement('a'); | |
126 | |
127 try { | |
128 url = decodeURIComponent(url); | |
129 } catch (e) {} | |
130 | |
131 urlParsingNode.setAttribute('href', url); | |
132 | |
133 return urlParsingNode.cloneNode(false).href; | |
134 }; | |
135 | |
136 Drupal.url.isLocal = function (url) { | |
137 var absoluteUrl = Drupal.url.toAbsolute(url); | |
138 var protocol = location.protocol; | |
139 | |
140 if (protocol === 'http:' && absoluteUrl.indexOf('https:') === 0) { | |
141 protocol = 'https:'; | |
142 } | |
143 var baseUrl = protocol + '//' + location.host + drupalSettings.path.baseUrl.slice(0, -1); | |
144 | |
145 try { | |
146 absoluteUrl = decodeURIComponent(absoluteUrl); | |
147 } catch (e) {} | |
148 try { | |
149 baseUrl = decodeURIComponent(baseUrl); | |
150 } catch (e) {} | |
151 | |
152 return absoluteUrl === baseUrl || absoluteUrl.indexOf(baseUrl + '/') === 0; | |
153 }; | |
154 | |
155 Drupal.formatPlural = function (count, singular, plural, args, options) { | |
156 args = args || {}; | |
157 args['@count'] = count; | |
158 | |
159 var pluralDelimiter = drupalSettings.pluralDelimiter; | |
160 var translations = Drupal.t(singular + pluralDelimiter + plural, args, options).split(pluralDelimiter); | |
161 var index = 0; | |
162 | |
163 if (typeof drupalTranslations !== 'undefined' && drupalTranslations.pluralFormula) { | |
164 index = count in drupalTranslations.pluralFormula ? drupalTranslations.pluralFormula[count] : drupalTranslations.pluralFormula.default; | |
165 } else if (args['@count'] !== 1) { | |
166 index = 1; | |
167 } | |
168 | |
169 return translations[index]; | |
170 }; | |
171 | |
172 Drupal.encodePath = function (item) { | |
173 return window.encodeURIComponent(item).replace(/%2F/g, '/'); | |
174 }; | |
175 | |
176 Drupal.theme = function (func) { | |
177 if (func in Drupal.theme) { | |
178 var _Drupal$theme; | |
179 | |
180 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | |
181 args[_key - 1] = arguments[_key]; | |
182 } | |
183 | |
184 return (_Drupal$theme = Drupal.theme)[func].apply(_Drupal$theme, args); | |
185 } | |
186 }; | |
187 | |
188 Drupal.theme.placeholder = function (str) { | |
189 return '<em class="placeholder">' + Drupal.checkPlain(str) + '</em>'; | |
190 }; | |
191 })(Drupal, window.drupalSettings, window.drupalTranslations); |