|
1 // $Id: drupal.js,v 1.41.2.4 2009/07/21 08:59:10 goba Exp $ |
|
2 |
|
3 var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'themes': {}, 'locale': {} }; |
|
4 |
|
5 /** |
|
6 * Set the variable that indicates if JavaScript behaviors should be applied |
|
7 */ |
|
8 Drupal.jsEnabled = true; |
|
9 |
|
10 /** |
|
11 * Attach all registered behaviors to a page element. |
|
12 * |
|
13 * Behaviors are event-triggered actions that attach to page elements, enhancing |
|
14 * default non-Javascript UIs. Behaviors are registered in the Drupal.behaviors |
|
15 * object as follows: |
|
16 * @code |
|
17 * Drupal.behaviors.behaviorName = function () { |
|
18 * ... |
|
19 * }; |
|
20 * @endcode |
|
21 * |
|
22 * Drupal.attachBehaviors is added below to the jQuery ready event and so |
|
23 * runs on initial page load. Developers implementing AHAH/AJAX in their |
|
24 * solutions should also call this function after new page content has been |
|
25 * loaded, feeding in an element to be processed, in order to attach all |
|
26 * behaviors to the new content. |
|
27 * |
|
28 * Behaviors should use a class in the form behaviorName-processed to ensure |
|
29 * the behavior is attached only once to a given element. (Doing so enables |
|
30 * the reprocessing of given elements, which may be needed on occasion despite |
|
31 * the ability to limit behavior attachment to a particular element.) |
|
32 * |
|
33 * @param context |
|
34 * An element to attach behaviors to. If none is given, the document element |
|
35 * is used. |
|
36 */ |
|
37 Drupal.attachBehaviors = function(context) { |
|
38 context = context || document; |
|
39 // Execute all of them. |
|
40 jQuery.each(Drupal.behaviors, function() { |
|
41 this(context); |
|
42 }); |
|
43 }; |
|
44 |
|
45 /** |
|
46 * Encode special characters in a plain-text string for display as HTML. |
|
47 */ |
|
48 Drupal.checkPlain = function(str) { |
|
49 str = String(str); |
|
50 var replace = { '&': '&', '"': '"', '<': '<', '>': '>' }; |
|
51 for (var character in replace) { |
|
52 var regex = new RegExp(character, 'g'); |
|
53 str = str.replace(regex, replace[character]); |
|
54 } |
|
55 return str; |
|
56 }; |
|
57 |
|
58 /** |
|
59 * Translate strings to the page language or a given language. |
|
60 * |
|
61 * See the documentation of the server-side t() function for further details. |
|
62 * |
|
63 * @param str |
|
64 * A string containing the English string to translate. |
|
65 * @param args |
|
66 * An object of replacements pairs to make after translation. Incidences |
|
67 * of any key in this array are replaced with the corresponding value. |
|
68 * Based on the first character of the key, the value is escaped and/or themed: |
|
69 * - !variable: inserted as is |
|
70 * - @variable: escape plain text to HTML (Drupal.checkPlain) |
|
71 * - %variable: escape text and theme as a placeholder for user-submitted |
|
72 * content (checkPlain + Drupal.theme('placeholder')) |
|
73 * @return |
|
74 * The translated string. |
|
75 */ |
|
76 Drupal.t = function(str, args) { |
|
77 // Fetch the localized version of the string. |
|
78 if (Drupal.locale.strings && Drupal.locale.strings[str]) { |
|
79 str = Drupal.locale.strings[str]; |
|
80 } |
|
81 |
|
82 if (args) { |
|
83 // Transform arguments before inserting them |
|
84 for (var key in args) { |
|
85 switch (key.charAt(0)) { |
|
86 // Escaped only |
|
87 case '@': |
|
88 args[key] = Drupal.checkPlain(args[key]); |
|
89 break; |
|
90 // Pass-through |
|
91 case '!': |
|
92 break; |
|
93 // Escaped and placeholder |
|
94 case '%': |
|
95 default: |
|
96 args[key] = Drupal.theme('placeholder', args[key]); |
|
97 break; |
|
98 } |
|
99 str = str.replace(key, args[key]); |
|
100 } |
|
101 } |
|
102 return str; |
|
103 }; |
|
104 |
|
105 /** |
|
106 * Format a string containing a count of items. |
|
107 * |
|
108 * This function ensures that the string is pluralized correctly. Since Drupal.t() is |
|
109 * called by this function, make sure not to pass already-localized strings to it. |
|
110 * |
|
111 * See the documentation of the server-side format_plural() function for further details. |
|
112 * |
|
113 * @param count |
|
114 * The item count to display. |
|
115 * @param singular |
|
116 * The string for the singular case. Please make sure it is clear this is |
|
117 * singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). |
|
118 * Do not use @count in the singular string. |
|
119 * @param plural |
|
120 * The string for the plural case. Please make sure it is clear this is plural, |
|
121 * to ease translation. Use @count in place of the item count, as in "@count |
|
122 * new comments". |
|
123 * @param args |
|
124 * An object of replacements pairs to make after translation. Incidences |
|
125 * of any key in this array are replaced with the corresponding value. |
|
126 * Based on the first character of the key, the value is escaped and/or themed: |
|
127 * - !variable: inserted as is |
|
128 * - @variable: escape plain text to HTML (Drupal.checkPlain) |
|
129 * - %variable: escape text and theme as a placeholder for user-submitted |
|
130 * content (checkPlain + Drupal.theme('placeholder')) |
|
131 * Note that you do not need to include @count in this array. |
|
132 * This replacement is done automatically for the plural case. |
|
133 * @return |
|
134 * A translated string. |
|
135 */ |
|
136 Drupal.formatPlural = function(count, singular, plural, args) { |
|
137 var args = args || {}; |
|
138 args['@count'] = count; |
|
139 // Determine the index of the plural form. |
|
140 var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1); |
|
141 |
|
142 if (index == 0) { |
|
143 return Drupal.t(singular, args); |
|
144 } |
|
145 else if (index == 1) { |
|
146 return Drupal.t(plural, args); |
|
147 } |
|
148 else { |
|
149 args['@count['+ index +']'] = args['@count']; |
|
150 delete args['@count']; |
|
151 return Drupal.t(plural.replace('@count', '@count['+ index +']')); |
|
152 } |
|
153 }; |
|
154 |
|
155 /** |
|
156 * Generate the themed representation of a Drupal object. |
|
157 * |
|
158 * All requests for themed output must go through this function. It examines |
|
159 * the request and routes it to the appropriate theme function. If the current |
|
160 * theme does not provide an override function, the generic theme function is |
|
161 * called. |
|
162 * |
|
163 * For example, to retrieve the HTML that is output by theme_placeholder(text), |
|
164 * call Drupal.theme('placeholder', text). |
|
165 * |
|
166 * @param func |
|
167 * The name of the theme function to call. |
|
168 * @param ... |
|
169 * Additional arguments to pass along to the theme function. |
|
170 * @return |
|
171 * Any data the theme function returns. This could be a plain HTML string, |
|
172 * but also a complex object. |
|
173 */ |
|
174 Drupal.theme = function(func) { |
|
175 for (var i = 1, args = []; i < arguments.length; i++) { |
|
176 args.push(arguments[i]); |
|
177 } |
|
178 |
|
179 return (Drupal.theme[func] || Drupal.theme.prototype[func]).apply(this, args); |
|
180 }; |
|
181 |
|
182 /** |
|
183 * Parse a JSON response. |
|
184 * |
|
185 * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message. |
|
186 */ |
|
187 Drupal.parseJson = function (data) { |
|
188 if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) { |
|
189 return { status: 0, data: data.length ? data : Drupal.t('Unspecified error') }; |
|
190 } |
|
191 return eval('(' + data + ');'); |
|
192 }; |
|
193 |
|
194 /** |
|
195 * Freeze the current body height (as minimum height). Used to prevent |
|
196 * unnecessary upwards scrolling when doing DOM manipulations. |
|
197 */ |
|
198 Drupal.freezeHeight = function () { |
|
199 Drupal.unfreezeHeight(); |
|
200 var div = document.createElement('div'); |
|
201 $(div).css({ |
|
202 position: 'absolute', |
|
203 top: '0px', |
|
204 left: '0px', |
|
205 width: '1px', |
|
206 height: $('body').css('height') |
|
207 }).attr('id', 'freeze-height'); |
|
208 $('body').append(div); |
|
209 }; |
|
210 |
|
211 /** |
|
212 * Unfreeze the body height |
|
213 */ |
|
214 Drupal.unfreezeHeight = function () { |
|
215 $('#freeze-height').remove(); |
|
216 }; |
|
217 |
|
218 /** |
|
219 * Wrapper around encodeURIComponent() which avoids Apache quirks (equivalent of |
|
220 * drupal_urlencode() in PHP). This function should only be used on paths, not |
|
221 * on query string arguments. |
|
222 */ |
|
223 Drupal.encodeURIComponent = function (item, uri) { |
|
224 uri = uri || location.href; |
|
225 item = encodeURIComponent(item).replace(/%2F/g, '/'); |
|
226 return (uri.indexOf('?q=') != -1) ? item : item.replace(/%26/g, '%2526').replace(/%23/g, '%2523').replace(/\/\//g, '/%252F'); |
|
227 }; |
|
228 |
|
229 /** |
|
230 * Get the text selection in a textarea. |
|
231 */ |
|
232 Drupal.getSelection = function (element) { |
|
233 if (typeof(element.selectionStart) != 'number' && document.selection) { |
|
234 // The current selection |
|
235 var range1 = document.selection.createRange(); |
|
236 var range2 = range1.duplicate(); |
|
237 // Select all text. |
|
238 range2.moveToElementText(element); |
|
239 // Now move 'dummy' end point to end point of original range. |
|
240 range2.setEndPoint('EndToEnd', range1); |
|
241 // Now we can calculate start and end points. |
|
242 var start = range2.text.length - range1.text.length; |
|
243 var end = start + range1.text.length; |
|
244 return { 'start': start, 'end': end }; |
|
245 } |
|
246 return { 'start': element.selectionStart, 'end': element.selectionEnd }; |
|
247 }; |
|
248 |
|
249 /** |
|
250 * Build an error message from ahah response. |
|
251 */ |
|
252 Drupal.ahahError = function(xmlhttp, uri) { |
|
253 if (xmlhttp.status == 200) { |
|
254 if (jQuery.trim(xmlhttp.responseText)) { |
|
255 var message = Drupal.t("An error occurred. \n@uri\n@text", {'@uri': uri, '@text': xmlhttp.responseText }); |
|
256 } |
|
257 else { |
|
258 var message = Drupal.t("An error occurred. \n@uri\n(no information available).", {'@uri': uri }); |
|
259 } |
|
260 } |
|
261 else { |
|
262 var message = Drupal.t("An HTTP error @status occurred. \n@uri", {'@uri': uri, '@status': xmlhttp.status }); |
|
263 } |
|
264 return message.replace(/\n/g, '<br />'); |
|
265 } |
|
266 |
|
267 // Global Killswitch on the <html> element |
|
268 $(document.documentElement).addClass('js'); |
|
269 // Attach all behaviors. |
|
270 $(document).ready(function() { |
|
271 Drupal.attachBehaviors(this); |
|
272 }); |
|
273 |
|
274 /** |
|
275 * The default themes. |
|
276 */ |
|
277 Drupal.theme.prototype = { |
|
278 |
|
279 /** |
|
280 * Formats text for emphasized display in a placeholder inside a sentence. |
|
281 * |
|
282 * @param str |
|
283 * The text to format (plain-text). |
|
284 * @return |
|
285 * The formatted text (html). |
|
286 */ |
|
287 placeholder: function(str) { |
|
288 return '<em>' + Drupal.checkPlain(str) + '</em>'; |
|
289 } |
|
290 }; |