Mercurial > defr > drupal > core
comparison misc/drupal.js @ 1:c1f4ac30525a 6.0
Drupal 6.0
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Tue, 23 Dec 2008 14:28:28 +0100 |
parents | |
children | 165d43f946a8 |
comparison
equal
deleted
inserted
replaced
0:5a113a1c4740 | 1:c1f4ac30525a |
---|---|
1 // $Id: drupal.js,v 1.41.2.1 2008/02/06 12:18:04 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 = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById; | |
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 if (Drupal.jsEnabled) { | |
40 // Execute all of them. | |
41 jQuery.each(Drupal.behaviors, function() { | |
42 this(context); | |
43 }); | |
44 } | |
45 }; | |
46 | |
47 /** | |
48 * Encode special characters in a plain-text string for display as HTML. | |
49 */ | |
50 Drupal.checkPlain = function(str) { | |
51 str = String(str); | |
52 var replace = { '&': '&', '"': '"', '<': '<', '>': '>' }; | |
53 for (var character in replace) { | |
54 str = str.replace(character, replace[character]); | |
55 } | |
56 return str; | |
57 }; | |
58 | |
59 /** | |
60 * Translate strings to the page language or a given language. | |
61 * | |
62 * See the documentation of the server-side t() function for further details. | |
63 * | |
64 * @param str | |
65 * A string containing the English string to translate. | |
66 * @param args | |
67 * An object of replacements pairs to make after translation. Incidences | |
68 * of any key in this array are replaced with the corresponding value. | |
69 * Based on the first character of the key, the value is escaped and/or themed: | |
70 * - !variable: inserted as is | |
71 * - @variable: escape plain text to HTML (Drupal.checkPlain) | |
72 * - %variable: escape text and theme as a placeholder for user-submitted | |
73 * content (checkPlain + Drupal.theme('placeholder')) | |
74 * @return | |
75 * The translated string. | |
76 */ | |
77 Drupal.t = function(str, args) { | |
78 // Fetch the localized version of the string. | |
79 if (Drupal.locale.strings && Drupal.locale.strings[str]) { | |
80 str = Drupal.locale.strings[str]; | |
81 } | |
82 | |
83 if (args) { | |
84 // Transform arguments before inserting them | |
85 for (var key in args) { | |
86 switch (key.charAt(0)) { | |
87 // Escaped only | |
88 case '@': | |
89 args[key] = Drupal.checkPlain(args[key]); | |
90 break; | |
91 // Pass-through | |
92 case '!': | |
93 break; | |
94 // Escaped and placeholder | |
95 case '%': | |
96 default: | |
97 args[key] = Drupal.theme('placeholder', args[key]); | |
98 break; | |
99 } | |
100 str = str.replace(key, args[key]); | |
101 } | |
102 } | |
103 return str; | |
104 }; | |
105 | |
106 /** | |
107 * Format a string containing a count of items. | |
108 * | |
109 * This function ensures that the string is pluralized correctly. Since Drupal.t() is | |
110 * called by this function, make sure not to pass already-localized strings to it. | |
111 * | |
112 * See the documentation of the server-side format_plural() function for further details. | |
113 * | |
114 * @param count | |
115 * The item count to display. | |
116 * @param singular | |
117 * The string for the singular case. Please make sure it is clear this is | |
118 * singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). | |
119 * Do not use @count in the singular string. | |
120 * @param plural | |
121 * The string for the plural case. Please make sure it is clear this is plural, | |
122 * to ease translation. Use @count in place of the item count, as in "@count | |
123 * new comments". | |
124 * @param args | |
125 * An object of replacements pairs to make after translation. Incidences | |
126 * of any key in this array are replaced with the corresponding value. | |
127 * Based on the first character of the key, the value is escaped and/or themed: | |
128 * - !variable: inserted as is | |
129 * - @variable: escape plain text to HTML (Drupal.checkPlain) | |
130 * - %variable: escape text and theme as a placeholder for user-submitted | |
131 * content (checkPlain + Drupal.theme('placeholder')) | |
132 * Note that you do not need to include @count in this array. | |
133 * This replacement is done automatically for the plural case. | |
134 * @return | |
135 * A translated string. | |
136 */ | |
137 Drupal.formatPlural = function(count, singular, plural, args) { | |
138 var args = args || {}; | |
139 args['@count'] = count; | |
140 // Determine the index of the plural form. | |
141 var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1); | |
142 | |
143 if (index == 0) { | |
144 return Drupal.t(singular, args); | |
145 } | |
146 else if (index == 1) { | |
147 return Drupal.t(plural, args); | |
148 } | |
149 else { | |
150 args['@count['+ index +']'] = args['@count']; | |
151 delete args['@count']; | |
152 return Drupal.t(plural.replace('@count', '@count['+ index +']')); | |
153 } | |
154 }; | |
155 | |
156 /** | |
157 * Generate the themed representation of a Drupal object. | |
158 * | |
159 * All requests for themed output must go through this function. It examines | |
160 * the request and routes it to the appropriate theme function. If the current | |
161 * theme does not provide an override function, the generic theme function is | |
162 * called. | |
163 * | |
164 * For example, to retrieve the HTML that is output by theme_placeholder(text), | |
165 * call Drupal.theme('placeholder', text). | |
166 * | |
167 * @param func | |
168 * The name of the theme function to call. | |
169 * @param ... | |
170 * Additional arguments to pass along to the theme function. | |
171 * @return | |
172 * Any data the theme function returns. This could be a plain HTML string, | |
173 * but also a complex object. | |
174 */ | |
175 Drupal.theme = function(func) { | |
176 for (var i = 1, args = []; i < arguments.length; i++) { | |
177 args.push(arguments[i]); | |
178 } | |
179 | |
180 return (Drupal.theme[func] || Drupal.theme.prototype[func]).apply(this, args); | |
181 }; | |
182 | |
183 /** | |
184 * Parse a JSON response. | |
185 * | |
186 * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message. | |
187 */ | |
188 Drupal.parseJson = function (data) { | |
189 if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) { | |
190 return { status: 0, data: data.length ? data : Drupal.t('Unspecified error') }; | |
191 } | |
192 return eval('(' + data + ');'); | |
193 }; | |
194 | |
195 /** | |
196 * Freeze the current body height (as minimum height). Used to prevent | |
197 * unnecessary upwards scrolling when doing DOM manipulations. | |
198 */ | |
199 Drupal.freezeHeight = function () { | |
200 Drupal.unfreezeHeight(); | |
201 var div = document.createElement('div'); | |
202 $(div).css({ | |
203 position: 'absolute', | |
204 top: '0px', | |
205 left: '0px', | |
206 width: '1px', | |
207 height: $('body').css('height') | |
208 }).attr('id', 'freeze-height'); | |
209 $('body').append(div); | |
210 }; | |
211 | |
212 /** | |
213 * Unfreeze the body height | |
214 */ | |
215 Drupal.unfreezeHeight = function () { | |
216 $('#freeze-height').remove(); | |
217 }; | |
218 | |
219 /** | |
220 * Wrapper to address the mod_rewrite url encoding bug | |
221 * (equivalent of drupal_urlencode() in PHP). | |
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).text())) { | |
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, '@text': xmlhttp.responseText }); | |
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; | |
265 } | |
266 | |
267 // Global Killswitch on the <html> element | |
268 if (Drupal.jsEnabled) { | |
269 // Global Killswitch on the <html> element | |
270 document.documentElement.className = 'js'; | |
271 // 'js enabled' cookie | |
272 document.cookie = 'has_js=1; path=/'; | |
273 // Attach all behaviors. | |
274 $(document).ready(function() { | |
275 Drupal.attachBehaviors(this); | |
276 }); | |
277 } | |
278 | |
279 /** | |
280 * The default themes. | |
281 */ | |
282 Drupal.theme.prototype = { | |
283 | |
284 /** | |
285 * Formats text for emphasized display in a placeholder inside a sentence. | |
286 * | |
287 * @param str | |
288 * The text to format (plain-text). | |
289 * @return | |
290 * The formatted text (html). | |
291 */ | |
292 placeholder: function(str) { | |
293 return '<em>' + Drupal.checkPlain(str) + '</em>'; | |
294 } | |
295 }; |