2009-01-06 13:16:09 +00:00
|
|
|
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} };
|
2005-05-24 06:00:22 +00:00
|
|
|
|
2009-02-18 13:46:55 +00:00
|
|
|
// Allow other JavaScript libraries to use $.
|
|
|
|
jQuery.noConflict();
|
|
|
|
|
2012-05-16 02:55:55 +00:00
|
|
|
// JavaScript should be made compatible with libraries other than jQuery by
|
|
|
|
// wrapping it in an anonymous closure.
|
|
|
|
(function ($, Drupal, window, document, undefined) {
|
2009-02-18 13:46:55 +00:00
|
|
|
|
2012-05-08 02:57:33 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-08-24 11:38:13 +00:00
|
|
|
/**
|
|
|
|
* Custom error type thrown after attach/detach if one or more behaviors failed.
|
|
|
|
*
|
|
|
|
* @param list
|
|
|
|
* An array of errors thrown during attach/detach.
|
|
|
|
* @param event
|
|
|
|
* A string containing either 'attach' or 'detach'.
|
|
|
|
*/
|
|
|
|
function DrupalBehaviorError(list, event) {
|
|
|
|
this.name = 'DrupalBehaviorError';
|
|
|
|
this.event = event || 'attach';
|
|
|
|
this.list = list;
|
|
|
|
// Makes the list of errors readable.
|
|
|
|
var messageList = [];
|
|
|
|
messageList.push(this.event);
|
|
|
|
for (var i = 0, il = this.list.length; i < il; i++) {
|
|
|
|
messageList.push(this.list[i].behavior + ': ' + this.list[i].error.message);
|
|
|
|
}
|
|
|
|
this.message = messageList.join(' ; ');
|
|
|
|
}
|
|
|
|
DrupalBehaviorError.prototype = new Error();
|
|
|
|
|
2007-07-01 15:37:10 +00:00
|
|
|
/**
|
|
|
|
* Attach all registered behaviors to a page element.
|
|
|
|
*
|
|
|
|
* Behaviors are event-triggered actions that attach to page elements, enhancing
|
2011-02-19 00:09:11 +00:00
|
|
|
* default non-JavaScript UIs. Behaviors are registered in the Drupal.behaviors
|
2008-10-29 10:01:28 +00:00
|
|
|
* object using the method 'attach' and optionally also 'detach' as follows:
|
2007-07-01 15:37:10 +00:00
|
|
|
* @code
|
2008-10-29 10:01:28 +00:00
|
|
|
* Drupal.behaviors.behaviorName = {
|
2009-11-03 05:34:37 +00:00
|
|
|
* attach: function (context, settings) {
|
2008-10-29 10:01:28 +00:00
|
|
|
* ...
|
|
|
|
* },
|
2009-11-03 05:34:37 +00:00
|
|
|
* detach: function (context, settings, trigger) {
|
2008-10-29 10:01:28 +00:00
|
|
|
* ...
|
|
|
|
* }
|
2007-07-01 15:37:10 +00:00
|
|
|
* };
|
|
|
|
* @endcode
|
|
|
|
*
|
2012-05-16 02:55:55 +00:00
|
|
|
* Drupal.attachBehaviors is added below to the jQuery.ready event and therefore
|
|
|
|
* runs on initial page load. Developers implementing Ajax in their solutions
|
|
|
|
* should also call this function after new page content has been loaded,
|
|
|
|
* feeding in an element to be processed, in order to attach all behaviors to
|
|
|
|
* the new content.
|
2007-07-01 15:37:10 +00:00
|
|
|
*
|
2009-08-31 05:51:08 +00:00
|
|
|
* Behaviors should use
|
|
|
|
* @code
|
2012-05-16 02:55:55 +00:00
|
|
|
* var elements = $(context).find(selector).once('behavior-name');
|
2009-08-31 05:51:08 +00:00
|
|
|
* @endcode
|
|
|
|
* to ensure the behavior is attached only once to a given element. (Doing so
|
|
|
|
* enables the reprocessing of given elements, which may be needed on occasion
|
|
|
|
* despite the ability to limit behavior attachment to a particular element.)
|
2007-07-01 15:37:10 +00:00
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* An element to attach behaviors to. If none is given, the document element
|
|
|
|
* is used.
|
2009-03-13 23:15:09 +00:00
|
|
|
* @param settings
|
2012-05-16 02:55:55 +00:00
|
|
|
* An object containing settings for the current context. If none is given,
|
|
|
|
* the global Drupal.settings object is used.
|
2007-07-01 15:37:10 +00:00
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.attachBehaviors = function (context, settings) {
|
2007-07-01 15:37:10 +00:00
|
|
|
context = context || document;
|
2009-03-13 23:15:09 +00:00
|
|
|
settings = settings || Drupal.settings;
|
2012-08-24 11:38:13 +00:00
|
|
|
var i, errors = [], behaviors = Drupal.behaviors;
|
2008-05-19 19:42:18 +00:00
|
|
|
// Execute all of them.
|
2012-03-21 05:40:42 +00:00
|
|
|
for (i in behaviors) {
|
2012-04-10 07:53:10 +00:00
|
|
|
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') {
|
2012-08-24 11:38:13 +00:00
|
|
|
// Don't stop the execution of behaviors in case of an error.
|
|
|
|
try {
|
|
|
|
behaviors[i].attach(context, settings);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
errors.push({ behavior: i, error: e });
|
|
|
|
}
|
2008-10-29 10:01:28 +00:00
|
|
|
}
|
2012-03-21 05:40:42 +00:00
|
|
|
}
|
2012-08-24 11:38:13 +00:00
|
|
|
// Once all behaviors have been processed, inform the user about errors.
|
|
|
|
if (errors.length) {
|
|
|
|
throw new DrupalBehaviorError(errors, 'attach');
|
|
|
|
}
|
2008-10-29 10:01:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach registered behaviors from a page element.
|
|
|
|
*
|
2011-02-19 00:09:11 +00:00
|
|
|
* Developers implementing AHAH/Ajax in their solutions should call this
|
2008-10-29 10:01:28 +00:00
|
|
|
* function before page content is about to be removed, feeding in an element
|
|
|
|
* to be processed, in order to allow special behaviors to detach from the
|
|
|
|
* content.
|
|
|
|
*
|
|
|
|
* Such implementations should look for the class name that was added in their
|
|
|
|
* corresponding Drupal.behaviors.behaviorName.attach implementation, i.e.
|
|
|
|
* behaviorName-processed, to ensure the behavior is detached only from
|
|
|
|
* previously processed elements.
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* An element to detach behaviors from. If none is given, the document element
|
|
|
|
* is used.
|
2009-11-03 05:34:37 +00:00
|
|
|
* @param settings
|
|
|
|
* An object containing settings for the current context. If none given, the
|
|
|
|
* global Drupal.settings object is used.
|
|
|
|
* @param trigger
|
|
|
|
* A string containing what's causing the behaviors to be detached. The
|
|
|
|
* possible triggers are:
|
|
|
|
* - unload: (default) The context element is being removed from the DOM.
|
|
|
|
* - move: The element is about to be moved within the DOM (for example,
|
|
|
|
* during a tabledrag row swap). After the move is completed,
|
|
|
|
* Drupal.attachBehaviors() is called, so that the behavior can undo
|
|
|
|
* whatever it did in response to the move. Many behaviors won't need to
|
|
|
|
* do anything simply in response to the element being moved, but because
|
|
|
|
* IFRAME elements reload their "src" when being moved within the DOM,
|
|
|
|
* behaviors bound to IFRAME elements (like WYSIWYG editors) may need to
|
|
|
|
* take some action.
|
2011-02-19 00:09:11 +00:00
|
|
|
* - serialize: When an Ajax form is submitted, this is called with the
|
2009-11-03 05:34:37 +00:00
|
|
|
* form as the context. This provides every behavior within the form an
|
|
|
|
* opportunity to ensure that the field elements have correct content
|
|
|
|
* in them before the form is serialized. The canonical use-case is so
|
|
|
|
* that WYSIWYG editors can update the hidden textarea to which they are
|
|
|
|
* bound.
|
2008-10-29 10:01:28 +00:00
|
|
|
*
|
|
|
|
* @see Drupal.attachBehaviors
|
|
|
|
*/
|
2009-11-03 05:34:37 +00:00
|
|
|
Drupal.detachBehaviors = function (context, settings, trigger) {
|
2008-10-29 10:01:28 +00:00
|
|
|
context = context || document;
|
2009-03-13 23:15:09 +00:00
|
|
|
settings = settings || Drupal.settings;
|
2009-11-03 05:34:37 +00:00
|
|
|
trigger = trigger || 'unload';
|
2012-08-24 11:38:13 +00:00
|
|
|
var i, errors = [], behaviors = Drupal.behaviors;
|
2008-10-29 10:01:28 +00:00
|
|
|
// Execute all of them.
|
2012-03-21 05:40:42 +00:00
|
|
|
for (i in behaviors) {
|
2012-04-10 07:53:10 +00:00
|
|
|
if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function' ) {
|
2012-08-24 11:38:13 +00:00
|
|
|
// Don't stop the execution of behaviors in case of an error.
|
|
|
|
try {
|
|
|
|
behaviors[i].detach(context, settings, trigger);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
errors.push({ behavior: i, error: e });
|
|
|
|
}
|
2008-10-29 10:01:28 +00:00
|
|
|
}
|
2012-03-21 05:40:42 +00:00
|
|
|
}
|
2012-08-24 11:38:13 +00:00
|
|
|
// Once all behaviors have been processed, inform the user about errors.
|
|
|
|
if (errors.length) {
|
|
|
|
throw new DrupalBehaviorError(errors, 'detach:' + trigger);
|
|
|
|
}
|
2007-07-01 15:37:10 +00:00
|
|
|
};
|
|
|
|
|
2012-06-27 20:41:36 +00:00
|
|
|
/**
|
|
|
|
* Helper to test document width for mobile configurations.
|
|
|
|
* @todo Temporary solution for the mobile initiative.
|
|
|
|
*/
|
|
|
|
Drupal.checkWidthBreakpoint = function (width) {
|
|
|
|
width = width || Drupal.settings.widthBreakpoint || 640;
|
|
|
|
return (document.documentElement.clientWidth > width);
|
|
|
|
};
|
|
|
|
|
2007-06-08 12:51:59 +00:00
|
|
|
/**
|
|
|
|
* Encode special characters in a plain-text string for display as HTML.
|
2011-08-07 15:01:39 +00:00
|
|
|
*
|
2012-05-16 02:55:55 +00:00
|
|
|
* @param str
|
|
|
|
* The string to be encoded.
|
|
|
|
* @return
|
|
|
|
* The encoded string.
|
2011-08-07 15:01:39 +00:00
|
|
|
* @ingroup sanitization
|
2007-06-08 12:51:59 +00:00
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.checkPlain = function (str) {
|
2012-05-16 02:55:55 +00:00
|
|
|
str = str.toString()
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
2007-06-08 12:51:59 +00:00
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2011-08-07 15:01:39 +00:00
|
|
|
/**
|
|
|
|
* Replace placeholders with sanitized values in a string.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* A string with placeholders.
|
|
|
|
* @param args
|
|
|
|
* An object of replacements pairs to make. Incidences of any key in this
|
|
|
|
* array are replaced with the corresponding value. Based on the first
|
|
|
|
* character of the key, the value is escaped and/or themed:
|
|
|
|
* - !variable: inserted as is
|
|
|
|
* - @variable: escape plain text to HTML (Drupal.checkPlain)
|
|
|
|
* - %variable: escape text and theme as a placeholder for user-submitted
|
|
|
|
* content (checkPlain + Drupal.theme('placeholder'))
|
|
|
|
*
|
|
|
|
* @see Drupal.t()
|
|
|
|
* @ingroup sanitization
|
|
|
|
*/
|
|
|
|
Drupal.formatString = function(str, args) {
|
|
|
|
// Transform arguments before inserting them.
|
|
|
|
for (var key in args) {
|
2012-05-16 02:55:55 +00:00
|
|
|
if (args.hasOwnProperty(key)) {
|
|
|
|
switch (key.charAt(0)) {
|
|
|
|
// Escaped only.
|
|
|
|
case '@':
|
|
|
|
args[key] = Drupal.checkPlain(args[key]);
|
2011-08-07 15:01:39 +00:00
|
|
|
break;
|
2012-05-16 02:55:55 +00:00
|
|
|
// Pass-through.
|
|
|
|
case '!':
|
|
|
|
break;
|
|
|
|
// Escaped and placeholder.
|
|
|
|
default:
|
|
|
|
args[key] = Drupal.theme('placeholder', args[key]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str = str.replace(key, args[key]);
|
2011-08-07 15:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
2012-01-21 19:01:59 +00:00
|
|
|
};
|
2011-08-07 15:01:39 +00:00
|
|
|
|
2007-06-08 12:51:59 +00:00
|
|
|
/**
|
|
|
|
* Translate strings to the page language or a given language.
|
|
|
|
*
|
|
|
|
* See the documentation of the server-side t() function for further details.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* A string containing the English string to translate.
|
|
|
|
* @param args
|
|
|
|
* An object of replacements pairs to make after translation. Incidences
|
|
|
|
* of any key in this array are replaced with the corresponding value.
|
2011-08-07 15:01:39 +00:00
|
|
|
* See Drupal.formatString().
|
2011-10-26 07:38:48 +00:00
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* - 'context' (defaults to the empty context): The context the source string
|
|
|
|
* belongs to.
|
|
|
|
*
|
2007-06-08 12:51:59 +00:00
|
|
|
* @return
|
|
|
|
* The translated string.
|
|
|
|
*/
|
2011-10-26 07:38:48 +00:00
|
|
|
Drupal.t = function (str, args, options) {
|
|
|
|
options = options || {};
|
|
|
|
options.context = options.context || '';
|
|
|
|
|
2007-06-08 12:51:59 +00:00
|
|
|
// Fetch the localized version of the string.
|
2011-10-26 07:38:48 +00:00
|
|
|
if (Drupal.locale.strings && Drupal.locale.strings[options.context] && Drupal.locale.strings[options.context][str]) {
|
|
|
|
str = Drupal.locale.strings[options.context][str];
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args) {
|
2011-08-07 15:01:39 +00:00
|
|
|
str = Drupal.formatString(str, args);
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2012-04-29 15:16:27 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL to a Drupal page.
|
|
|
|
*/
|
|
|
|
Drupal.url = function (path) {
|
|
|
|
return Drupal.settings.basePath + Drupal.settings.scriptPath + path;
|
2012-05-16 02:55:55 +00:00
|
|
|
};
|
2012-04-29 15:16:27 +00:00
|
|
|
|
2007-06-08 12:51:59 +00:00
|
|
|
/**
|
|
|
|
* Format a string containing a count of items.
|
|
|
|
*
|
|
|
|
* This function ensures that the string is pluralized correctly. Since Drupal.t() is
|
|
|
|
* called by this function, make sure not to pass already-localized strings to it.
|
|
|
|
*
|
|
|
|
* See the documentation of the server-side format_plural() function for further details.
|
|
|
|
*
|
|
|
|
* @param count
|
|
|
|
* The item count to display.
|
|
|
|
* @param singular
|
|
|
|
* The string for the singular case. Please make sure it is clear this is
|
|
|
|
* singular, to ease translation (e.g. use "1 new comment" instead of "1 new").
|
|
|
|
* Do not use @count in the singular string.
|
|
|
|
* @param plural
|
|
|
|
* The string for the plural case. Please make sure it is clear this is plural,
|
|
|
|
* to ease translation. Use @count in place of the item count, as in "@count
|
|
|
|
* new comments".
|
|
|
|
* @param args
|
|
|
|
* An object of replacements pairs to make after translation. Incidences
|
|
|
|
* of any key in this array are replaced with the corresponding value.
|
2011-08-07 15:01:39 +00:00
|
|
|
* See Drupal.formatString().
|
2007-06-08 12:51:59 +00:00
|
|
|
* Note that you do not need to include @count in this array.
|
|
|
|
* This replacement is done automatically for the plural case.
|
2011-10-26 07:38:48 +00:00
|
|
|
* @param options
|
|
|
|
* The options to pass to the Drupal.t() function.
|
2007-06-08 12:51:59 +00:00
|
|
|
* @return
|
|
|
|
* A translated string.
|
|
|
|
*/
|
2011-10-26 07:38:48 +00:00
|
|
|
Drupal.formatPlural = function (count, singular, plural, args, options) {
|
2012-05-16 02:55:55 +00:00
|
|
|
args = args || {};
|
2007-06-08 12:51:59 +00:00
|
|
|
args['@count'] = count;
|
|
|
|
// Determine the index of the plural form.
|
2012-06-23 16:50:41 +00:00
|
|
|
var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] === 1) ? 0 : 1);
|
2007-06-08 12:51:59 +00:00
|
|
|
|
2012-06-23 16:50:41 +00:00
|
|
|
if (index === 0) {
|
2011-10-26 07:38:48 +00:00
|
|
|
return Drupal.t(singular, args, options);
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
2012-06-23 16:50:41 +00:00
|
|
|
else if (index === 1) {
|
2011-10-26 07:38:48 +00:00
|
|
|
return Drupal.t(plural, args, options);
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-04-26 19:18:46 +00:00
|
|
|
args['@count[' + index + ']'] = args['@count'];
|
2007-06-08 12:51:59 +00:00
|
|
|
delete args['@count'];
|
2011-10-26 07:38:48 +00:00
|
|
|
return Drupal.t(plural.replace('@count', '@count[' + index + ']'), args, options);
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the themed representation of a Drupal object.
|
|
|
|
*
|
|
|
|
* All requests for themed output must go through this function. It examines
|
|
|
|
* the request and routes it to the appropriate theme function. If the current
|
|
|
|
* theme does not provide an override function, the generic theme function is
|
|
|
|
* called.
|
|
|
|
*
|
2010-11-22 04:33:02 +00:00
|
|
|
* For example, to retrieve the HTML for text that should be emphasized and
|
|
|
|
* displayed as a placeholder inside a sentence, call
|
|
|
|
* Drupal.theme('placeholder', text).
|
2007-06-08 12:51:59 +00:00
|
|
|
*
|
|
|
|
* @param func
|
|
|
|
* The name of the theme function to call.
|
|
|
|
* @param ...
|
|
|
|
* Additional arguments to pass along to the theme function.
|
|
|
|
* @return
|
|
|
|
* Any data the theme function returns. This could be a plain HTML string,
|
|
|
|
* but also a complex object.
|
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.theme = function (func) {
|
2010-11-13 07:34:46 +00:00
|
|
|
var args = Array.prototype.slice.apply(arguments, [1]);
|
2012-08-19 11:55:16 +00:00
|
|
|
if (func in Drupal.theme) {
|
|
|
|
return Drupal.theme[func].apply(this, args);
|
|
|
|
}
|
2007-06-08 12:51:59 +00:00
|
|
|
};
|
|
|
|
|
2006-08-31 23:31:25 +00:00
|
|
|
/**
|
|
|
|
* Freeze the current body height (as minimum height). Used to prevent
|
|
|
|
* unnecessary upwards scrolling when doing DOM manipulations.
|
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.freezeHeight = function () {
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.unfreezeHeight();
|
2009-04-26 19:18:46 +00:00
|
|
|
$('<div id="freeze-height"></div>').css({
|
2006-08-31 23:31:25 +00:00
|
|
|
position: 'absolute',
|
|
|
|
top: '0px',
|
|
|
|
left: '0px',
|
|
|
|
width: '1px',
|
|
|
|
height: $('body').css('height')
|
2009-04-26 19:18:46 +00:00
|
|
|
}).appendTo('body');
|
2006-10-14 02:39:48 +00:00
|
|
|
};
|
2006-08-31 23:31:25 +00:00
|
|
|
|
|
|
|
/**
|
2008-10-12 00:29:09 +00:00
|
|
|
* Unfreeze the body height.
|
2006-08-31 23:31:25 +00:00
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.unfreezeHeight = function () {
|
2006-08-31 23:31:25 +00:00
|
|
|
$('#freeze-height').remove();
|
2006-10-14 02:39:48 +00:00
|
|
|
};
|
2006-02-05 19:04:58 +00:00
|
|
|
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
/**
|
2010-01-29 22:40:41 +00:00
|
|
|
* Encodes a Drupal path for use in a URL.
|
|
|
|
*
|
|
|
|
* For aesthetic reasons slashes are not escaped.
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
*/
|
2012-05-16 02:55:55 +00:00
|
|
|
Drupal.encodePath = function (item) {
|
|
|
|
return window.encodeURIComponent(item).replace(/%2F/g, '/');
|
2006-10-14 02:39:48 +00:00
|
|
|
};
|
2006-08-31 23:31:25 +00:00
|
|
|
|
2007-04-09 13:58:03 +00:00
|
|
|
/**
|
|
|
|
* Get the text selection in a textarea.
|
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
Drupal.getSelection = function (element) {
|
2012-05-16 02:55:55 +00:00
|
|
|
var range1, range2, start, end;
|
2012-06-23 16:50:41 +00:00
|
|
|
if (typeof element.selectionStart !== 'number' && document.selection) {
|
2008-10-12 00:29:09 +00:00
|
|
|
// The current selection.
|
2012-05-16 02:55:55 +00:00
|
|
|
range1 = document.selection.createRange();
|
|
|
|
range2 = range1.duplicate();
|
2007-04-09 13:58:03 +00:00
|
|
|
// Select all text.
|
|
|
|
range2.moveToElementText(element);
|
|
|
|
// Now move 'dummy' end point to end point of original range.
|
|
|
|
range2.setEndPoint('EndToEnd', range1);
|
|
|
|
// Now we can calculate start and end points.
|
2012-05-16 02:55:55 +00:00
|
|
|
start = range2.text.length - range1.text.length;
|
|
|
|
end = start + range1.text.length;
|
2007-04-09 13:58:03 +00:00
|
|
|
return { 'start': start, 'end': end };
|
|
|
|
}
|
|
|
|
return { 'start': element.selectionStart, 'end': element.selectionEnd };
|
2007-06-01 09:05:45 +00:00
|
|
|
};
|
2007-04-09 13:58:03 +00:00
|
|
|
|
2008-01-04 11:53:21 +00:00
|
|
|
/**
|
2011-02-19 00:09:11 +00:00
|
|
|
* Build an error message from an Ajax response.
|
2008-01-04 11:53:21 +00:00
|
|
|
*/
|
2009-08-17 07:12:16 +00:00
|
|
|
Drupal.ajaxError = function (xmlhttp, uri) {
|
2009-12-14 23:57:39 +00:00
|
|
|
var statusCode, statusText, pathText, responseText, readyStateText, message;
|
|
|
|
if (xmlhttp.status) {
|
|
|
|
statusCode = "\n" + Drupal.t("An AJAX HTTP error occurred.") + "\n" + Drupal.t("HTTP Result Code: !status", {'!status': xmlhttp.status});
|
2008-01-04 11:53:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-12-14 23:57:39 +00:00
|
|
|
statusCode = "\n" + Drupal.t("An AJAX HTTP request terminated abnormally.");
|
2008-01-04 11:53:21 +00:00
|
|
|
}
|
2009-12-14 23:57:39 +00:00
|
|
|
statusCode += "\n" + Drupal.t("Debugging information follows.");
|
|
|
|
pathText = "\n" + Drupal.t("Path: !uri", {'!uri': uri} );
|
2011-01-01 04:11:39 +00:00
|
|
|
statusText = '';
|
2012-06-23 16:50:41 +00:00
|
|
|
// In some cases, when statusCode === 0, xmlhttp.statusText may not be defined.
|
2011-01-01 04:11:39 +00:00
|
|
|
// Unfortunately, testing for it with typeof, etc, doesn't seem to catch that
|
|
|
|
// and the test causes an exception. So we need to catch the exception here.
|
|
|
|
try {
|
|
|
|
statusText = "\n" + Drupal.t("StatusText: !statusText", {'!statusText': $.trim(xmlhttp.statusText)});
|
|
|
|
}
|
|
|
|
catch (e) {}
|
|
|
|
|
|
|
|
responseText = '';
|
|
|
|
// Again, we don't have a way to know for sure whether accessing
|
|
|
|
// xmlhttp.responseText is going to throw an exception. So we'll catch it.
|
|
|
|
try {
|
|
|
|
responseText = "\n" + Drupal.t("ResponseText: !responseText", {'!responseText': $.trim(xmlhttp.responseText) } );
|
|
|
|
} catch (e) {}
|
|
|
|
|
2009-12-14 23:57:39 +00:00
|
|
|
// Make the responseText more readable by stripping HTML tags and newlines.
|
|
|
|
responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,"");
|
|
|
|
responseText = responseText.replace(/[\n]+\s+/g,"\n");
|
|
|
|
|
|
|
|
// We don't need readyState except for status == 0.
|
2012-06-23 16:50:41 +00:00
|
|
|
readyStateText = xmlhttp.status === 0 ? ("\n" + Drupal.t("ReadyState: !readyState", {'!readyState': xmlhttp.readyState})) : "";
|
2009-12-14 23:57:39 +00:00
|
|
|
|
|
|
|
message = statusCode + pathText + statusText + responseText + readyStateText;
|
|
|
|
return message;
|
2009-02-18 13:46:55 +00:00
|
|
|
};
|
2008-01-04 11:53:21 +00:00
|
|
|
|
2009-04-26 00:59:24 +00:00
|
|
|
// Class indicating that JS is enabled; used for styling purpose.
|
|
|
|
$('html').addClass('js');
|
|
|
|
|
2010-07-28 01:38:28 +00:00
|
|
|
//Attach all behaviors.
|
2009-04-27 20:19:38 +00:00
|
|
|
$(function () {
|
2009-04-26 00:59:24 +00:00
|
|
|
Drupal.attachBehaviors(document, Drupal.settings);
|
|
|
|
});
|
2007-06-08 12:51:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default themes.
|
|
|
|
*/
|
2012-08-19 11:55:16 +00:00
|
|
|
$.extend(Drupal.theme, {
|
2007-06-08 12:51:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats text for emphasized display in a placeholder inside a sentence.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* The text to format (plain-text).
|
|
|
|
* @return
|
|
|
|
* The formatted text (html).
|
|
|
|
*/
|
2009-04-27 20:19:38 +00:00
|
|
|
placeholder: function (str) {
|
2010-11-22 04:33:02 +00:00
|
|
|
return '<em class="placeholder">' + Drupal.checkPlain(str) + '</em>';
|
2007-06-08 12:51:59 +00:00
|
|
|
}
|
2012-08-19 11:55:16 +00:00
|
|
|
});
|
2009-02-18 13:46:55 +00:00
|
|
|
|
2012-05-16 02:55:55 +00:00
|
|
|
})(jQuery, Drupal, this, this.document);
|