| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
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 = 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 var regex = new RegExp(character, 'g'); 55 str = str.replace(regex, replace[character]); 56 } 57 return str; 58 }; 59 60 /** 61 * Translate strings to the page language or a given language. 62 * 63 * See the documentation of the server-side t() function for further details. 64 * 65 * @param str 66 * A string containing the English string to translate. 67 * @param args 68 * An object of replacements pairs to make after translation. Incidences 69 * of any key in this array are replaced with the corresponding value. 70 * Based on the first character of the key, the value is escaped and/or themed: 71 * - !variable: inserted as is 72 * - @variable: escape plain text to HTML (Drupal.checkPlain) 73 * - %variable: escape text and theme as a placeholder for user-submitted 74 * content (checkPlain + Drupal.theme('placeholder')) 75 * @return 76 * The translated string. 77 */ 78 Drupal.t = function(str, args) { 79 // Fetch the localized version of the string. 80 if (Drupal.locale.strings && Drupal.locale.strings[str]) { 81 str = Drupal.locale.strings[str]; 82 } 83 84 if (args) { 85 // Transform arguments before inserting them 86 for (var key in args) { 87 switch (key.charAt(0)) { 88 // Escaped only 89 case '@': 90 args[key] = Drupal.checkPlain(args[key]); 91 break; 92 // Pass-through 93 case '!': 94 break; 95 // Escaped and placeholder 96 case '%': 97 default: 98 args[key] = Drupal.theme('placeholder', args[key]); 99 break; 100 } 101 str = str.replace(key, args[key]); 102 } 103 } 104 return str; 105 }; 106 107 /** 108 * Format a string containing a count of items. 109 * 110 * This function ensures that the string is pluralized correctly. Since Drupal.t() is 111 * called by this function, make sure not to pass already-localized strings to it. 112 * 113 * See the documentation of the server-side format_plural() function for further details. 114 * 115 * @param count 116 * The item count to display. 117 * @param singular 118 * The string for the singular case. Please make sure it is clear this is 119 * singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). 120 * Do not use @count in the singular string. 121 * @param plural 122 * The string for the plural case. Please make sure it is clear this is plural, 123 * to ease translation. Use @count in place of the item count, as in "@count 124 * new comments". 125 * @param args 126 * An object of replacements pairs to make after translation. Incidences 127 * of any key in this array are replaced with the corresponding value. 128 * Based on the first character of the key, the value is escaped and/or themed: 129 * - !variable: inserted as is 130 * - @variable: escape plain text to HTML (Drupal.checkPlain) 131 * - %variable: escape text and theme as a placeholder for user-submitted 132 * content (checkPlain + Drupal.theme('placeholder')) 133 * Note that you do not need to include @count in this array. 134 * This replacement is done automatically for the plural case. 135 * @return 136 * A translated string. 137 */ 138 Drupal.formatPlural = function(count, singular, plural, args) { 139 var args = args || {}; 140 args['@count'] = count; 141 // Determine the index of the plural form. 142 var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1); 143 144 if (index == 0) { 145 return Drupal.t(singular, args); 146 } 147 else if (index == 1) { 148 return Drupal.t(plural, args); 149 } 150 else { 151 args['@count['+ index +']'] = args['@count']; 152 delete args['@count']; 153 return Drupal.t(plural.replace('@count', '@count['+ index +']')); 154 } 155 }; 156 157 /** 158 * Generate the themed representation of a Drupal object. 159 * 160 * All requests for themed output must go through this function. It examines 161 * the request and routes it to the appropriate theme function. If the current 162 * theme does not provide an override function, the generic theme function is 163 * called. 164 * 165 * For example, to retrieve the HTML that is output by theme_placeholder(text), 166 * call Drupal.theme('placeholder', text). 167 * 168 * @param func 169 * The name of the theme function to call. 170 * @param ... 171 * Additional arguments to pass along to the theme function. 172 * @return 173 * Any data the theme function returns. This could be a plain HTML string, 174 * but also a complex object. 175 */ 176 Drupal.theme = function(func) { 177 for (var i = 1, args = []; i < arguments.length; i++) { 178 args.push(arguments[i]); 179 } 180 181 return (Drupal.theme[func] || Drupal.theme.prototype[func]).apply(this, args); 182 }; 183 184 /** 185 * Parse a JSON response. 186 * 187 * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message. 188 */ 189 Drupal.parseJson = function (data) { 190 if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) { 191 return { status: 0, data: data.length ? data : Drupal.t('Unspecified error') }; 192 } 193 return eval('(' + data + ');'); 194 }; 195 196 /** 197 * Freeze the current body height (as minimum height). Used to prevent 198 * unnecessary upwards scrolling when doing DOM manipulations. 199 */ 200 Drupal.freezeHeight = function () { 201 Drupal.unfreezeHeight(); 202 var div = document.createElement('div'); 203 $(div).css({ 204 position: 'absolute', 205 top: '0px', 206 left: '0px', 207 width: '1px', 208 height: $('body').css('height') 209 }).attr('id', 'freeze-height'); 210 $('body').append(div); 211 }; 212 213 /** 214 * Unfreeze the body height 215 */ 216 Drupal.unfreezeHeight = function () { 217 $('#freeze-height').remove(); 218 }; 219 220 /** 221 * Wrapper around encodeURIComponent() which avoids Apache quirks (equivalent of 222 * drupal_urlencode() in PHP). This function should only be used on paths, not 223 * on query string arguments. 224 */ 225 Drupal.encodeURIComponent = function (item, uri) { 226 uri = uri || location.href; 227 item = encodeURIComponent(item).replace(/%2F/g, '/'); 228 return (uri.indexOf('?q=') != -1) ? item : item.replace(/%26/g, '%2526').replace(/%23/g, '%2523').replace(/\/\//g, '/%252F'); 229 }; 230 231 /** 232 * Get the text selection in a textarea. 233 */ 234 Drupal.getSelection = function (element) { 235 if (typeof(element.selectionStart) != 'number' && document.selection) { 236 // The current selection 237 var range1 = document.selection.createRange(); 238 var range2 = range1.duplicate(); 239 // Select all text. 240 range2.moveToElementText(element); 241 // Now move 'dummy' end point to end point of original range. 242 range2.setEndPoint('EndToEnd', range1); 243 // Now we can calculate start and end points. 244 var start = range2.text.length - range1.text.length; 245 var end = start + range1.text.length; 246 return { 'start': start, 'end': end }; 247 } 248 return { 'start': element.selectionStart, 'end': element.selectionEnd }; 249 }; 250 251 /** 252 * Build an error message from ahah response. 253 */ 254 Drupal.ahahError = function(xmlhttp, uri) { 255 if (xmlhttp.status == 200) { 256 if (jQuery.trim($(xmlhttp.responseText).text())) { 257 var message = Drupal.t("An error occurred. \n@uri\n@text", {'@uri': uri, '@text': xmlhttp.responseText }); 258 } 259 else { 260 var message = Drupal.t("An error occurred. \n@uri\n(no information available).", {'@uri': uri, '@text': xmlhttp.responseText }); 261 } 262 } 263 else { 264 var message = Drupal.t("An HTTP error @status occurred. \n@uri", {'@uri': uri, '@status': xmlhttp.status }); 265 } 266 return message; 267 } 268 269 // Global Killswitch on the <html> element 270 if (Drupal.jsEnabled) { 271 // Global Killswitch on the <html> element 272 $(document.documentElement).addClass('js'); 273 // 'js enabled' cookie 274 document.cookie = 'has_js=1; path=/'; 275 // Attach all behaviors. 276 $(document).ready(function() { 277 Drupal.attachBehaviors(this); 278 }); 279 } 280 281 /** 282 * The default themes. 283 */ 284 Drupal.theme.prototype = { 285 286 /** 287 * Formats text for emphasized display in a placeholder inside a sentence. 288 * 289 * @param str 290 * The text to format (plain-text). 291 * @return 292 * The formatted text (html). 293 */ 294 placeholder: function(str) { 295 return '<em>' + Drupal.checkPlain(str) + '</em>'; 296 } 297 };
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |