| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 2 var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'themes': {}, 'locale': {} }; 3 4 /** 5 * Set the variable that indicates if JavaScript behaviors should be applied 6 */ 7 Drupal.jsEnabled = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById; 8 9 /** 10 * Attach all registered behaviors to a page element. 11 * 12 * Behaviors are event-triggered actions that attach to page elements, enhancing 13 * default non-Javascript UIs. Behaviors are registered in the Drupal.behaviors 14 * object as follows: 15 * @code 16 * Drupal.behaviors.behaviorName = function () { 17 * ... 18 * }; 19 * @endcode 20 * 21 * Drupal.attachBehaviors is added below to the jQuery ready event and so 22 * runs on initial page load. Developers implementing AHAH/AJAX in their 23 * solutions should also call this function after new page content has been 24 * loaded, feeding in an element to be processed, in order to attach all 25 * behaviors to the new content. 26 * 27 * Behaviors should use a class in the form behaviorName-processed to ensure 28 * the behavior is attached only once to a given element. (Doing so enables 29 * the reprocessing of given elements, which may be needed on occasion despite 30 * the ability to limit behavior attachment to a particular element.) 31 * 32 * @param context 33 * An element to attach behaviors to. If none is given, the document element 34 * is used. 35 */ 36 Drupal.attachBehaviors = function(context) { 37 context = context || document; 38 if (Drupal.jsEnabled) { 39 // Execute all of them. 40 jQuery.each(Drupal.behaviors, function() { 41 this(context); 42 }); 43 } 44 }; 45 46 /** 47 * Encode special characters in a plain-text string for display as HTML. 48 */ 49 Drupal.checkPlain = function(str) { 50 str = String(str); 51 var replace = { '&': '&', '"': '"', '<': '<', '>': '>' }; 52 for (var character in replace) { 53 var regex = new RegExp(character, 'g'); 54 str = str.replace(regex, 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 +']'), args); 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 around encodeURIComponent() which avoids Apache quirks (equivalent of 221 * drupal_urlencode() in PHP). This function should only be used on paths, not 222 * on query string arguments. 223 */ 224 Drupal.encodeURIComponent = function (item, uri) { 225 uri = uri || location.href; 226 item = encodeURIComponent(item).replace(/%2F/g, '/'); 227 return (uri.indexOf('?q=') != -1) ? item : item.replace(/%26/g, '%2526').replace(/%23/g, '%2523').replace(/\/\//g, '/%252F'); 228 }; 229 230 /** 231 * Get the text selection in a textarea. 232 */ 233 Drupal.getSelection = function (element) { 234 if (typeof(element.selectionStart) != 'number' && document.selection) { 235 // The current selection 236 var range1 = document.selection.createRange(); 237 var range2 = range1.duplicate(); 238 // Select all text. 239 range2.moveToElementText(element); 240 // Now move 'dummy' end point to end point of original range. 241 range2.setEndPoint('EndToEnd', range1); 242 // Now we can calculate start and end points. 243 var start = range2.text.length - range1.text.length; 244 var end = start + range1.text.length; 245 return { 'start': start, 'end': end }; 246 } 247 return { 'start': element.selectionStart, 'end': element.selectionEnd }; 248 }; 249 250 /** 251 * Build an error message from ahah response. 252 */ 253 Drupal.ahahError = function(xmlhttp, uri) { 254 if (xmlhttp.status == 200) { 255 if (jQuery.trim($(xmlhttp.responseText).text())) { 256 var message = Drupal.t("An error occurred. \n@uri\n@text", {'@uri': uri, '@text': xmlhttp.responseText }); 257 } 258 else { 259 var message = Drupal.t("An error occurred. \n@uri\n(no information available).", {'@uri': uri, '@text': xmlhttp.responseText }); 260 } 261 } 262 else { 263 var message = Drupal.t("An HTTP error @status occurred. \n@uri", {'@uri': uri, '@status': xmlhttp.status }); 264 } 265 return message; 266 } 267 268 // Global Killswitch on the <html> element 269 if (Drupal.jsEnabled) { 270 // Global Killswitch on the <html> element 271 $(document.documentElement).addClass('js'); 272 // 'js enabled' cookie 273 document.cookie = 'has_js=1; path=/'; 274 // Attach all behaviors. 275 $(document).ready(function() { 276 Drupal.attachBehaviors(this); 277 }); 278 } 279 280 /** 281 * The default themes. 282 */ 283 Drupal.theme.prototype = { 284 285 /** 286 * Formats text for emphasized display in a placeholder inside a sentence. 287 * 288 * @param str 289 * The text to format (plain-text). 290 * @return 291 * The formatted text (html). 292 */ 293 placeholder: function(str) { 294 return '<em>' + Drupal.checkPlain(str) + '</em>'; 295 } 296 };
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |