annotate misc/drupal.js @ 20:e3d20ebd63d1 tip

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