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