Issue #1089300 by nod_, mfer, JohnAlbin, ksenzee: Clean up drupal.js.

8.0.x
catch 2012-05-16 11:55:55 +09:00
parent ccde359a97
commit 777a7b9164
1 changed files with 45 additions and 43 deletions

View File

@ -3,7 +3,9 @@ var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} };
// Allow other JavaScript libraries to use $. // Allow other JavaScript libraries to use $.
jQuery.noConflict(); jQuery.noConflict();
(function ($) { // JavaScript should be made compatible with libraries other than jQuery by
// wrapping it in an anonymous closure.
(function ($, Drupal, window, document, undefined) {
"use strict"; "use strict";
@ -24,17 +26,15 @@ jQuery.noConflict();
* }; * };
* @endcode * @endcode
* *
* Drupal.attachBehaviors is added below to the jQuery ready event and so * Drupal.attachBehaviors is added below to the jQuery.ready event and therefore
* runs on initial page load. Developers implementing AHAH/Ajax in their * runs on initial page load. Developers implementing Ajax in their solutions
* solutions should also call this function after new page content has been * should also call this function after new page content has been loaded,
* loaded, feeding in an element to be processed, in order to attach all * feeding in an element to be processed, in order to attach all behaviors to
* behaviors to the new content. * the new content.
* *
* Behaviors should use * Behaviors should use
* @code * @code
* $(selector).once('behavior-name', function () { * var elements = $(context).find(selector).once('behavior-name');
* ...
* });
* @endcode * @endcode
* to ensure the behavior is attached only once to a given element. (Doing so * 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 * enables the reprocessing of given elements, which may be needed on occasion
@ -44,8 +44,8 @@ jQuery.noConflict();
* An element to attach behaviors to. If none is given, the document element * An element to attach behaviors to. If none is given, the document element
* is used. * is used.
* @param settings * @param settings
* An object containing settings for the current context. If none given, the * An object containing settings for the current context. If none is given,
* global Drupal.settings object is used. * the global Drupal.settings object is used.
*/ */
Drupal.attachBehaviors = function (context, settings) { Drupal.attachBehaviors = function (context, settings) {
context = context || document; context = context || document;
@ -115,18 +115,18 @@ Drupal.detachBehaviors = function (context, settings, trigger) {
/** /**
* Encode special characters in a plain-text string for display as HTML. * Encode special characters in a plain-text string for display as HTML.
* *
* @param str
* The string to be encoded.
* @return
* The encoded string.
* @ingroup sanitization * @ingroup sanitization
*/ */
Drupal.checkPlain = function (str) { Drupal.checkPlain = function (str) {
var character, regex, str = str.toString()
replace = { '&': '&amp;', '"': '&quot;', '<': '&lt;', '>': '&gt;' }; .replace(/&/g, '&amp;')
str = String(str); .replace(/"/g, '&quot;')
for (character in replace) { .replace(/</g, '&lt;')
if (replace.hasOwnProperty(character)) { .replace(/>/g, '&gt;');
regex = new RegExp(character, 'g');
str = str.replace(regex, replace[character]);
}
}
return str; return str;
}; };
@ -150,21 +150,23 @@ Drupal.checkPlain = function (str) {
Drupal.formatString = function(str, args) { Drupal.formatString = function(str, args) {
// Transform arguments before inserting them. // Transform arguments before inserting them.
for (var key in args) { for (var key in args) {
switch (key.charAt(0)) { if (args.hasOwnProperty(key)) {
// Escaped only. switch (key.charAt(0)) {
case '@': // Escaped only.
args[key] = Drupal.checkPlain(args[key]); case '@':
break; args[key] = Drupal.checkPlain(args[key]);
// Pass-through.
case '!':
break;
// Escaped and placeholder.
case '%':
default:
args[key] = Drupal.theme('placeholder', args[key]);
break; break;
// Pass-through.
case '!':
break;
// Escaped and placeholder.
case '%':
default:
args[key] = Drupal.theme('placeholder', args[key]);
break;
}
str = str.replace(key, args[key]);
} }
str = str.replace(key, args[key]);
} }
return str; return str;
}; };
@ -208,7 +210,7 @@ Drupal.t = function (str, args, options) {
*/ */
Drupal.url = function (path) { Drupal.url = function (path) {
return Drupal.settings.basePath + Drupal.settings.scriptPath + path; return Drupal.settings.basePath + Drupal.settings.scriptPath + path;
} };
/** /**
* Format a string containing a count of items. * Format a string containing a count of items.
@ -240,7 +242,7 @@ Drupal.url = function (path) {
* A translated string. * A translated string.
*/ */
Drupal.formatPlural = function (count, singular, plural, args, options) { Drupal.formatPlural = function (count, singular, plural, args, options) {
var args = args || {}; args = args || {};
args['@count'] = count; args['@count'] = count;
// Determine the index of the plural form. // Determine the index of the plural form.
var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1); var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1);
@ -311,26 +313,26 @@ Drupal.unfreezeHeight = function () {
* *
* For aesthetic reasons slashes are not escaped. * For aesthetic reasons slashes are not escaped.
*/ */
Drupal.encodePath = function (item, uri) { Drupal.encodePath = function (item) {
uri = uri || location.href; return window.encodeURIComponent(item).replace(/%2F/g, '/');
return encodeURIComponent(item).replace(/%2F/g, '/');
}; };
/** /**
* Get the text selection in a textarea. * Get the text selection in a textarea.
*/ */
Drupal.getSelection = function (element) { Drupal.getSelection = function (element) {
var range1, range2, start, end;
if (typeof element.selectionStart != 'number' && document.selection) { if (typeof element.selectionStart != 'number' && document.selection) {
// The current selection. // The current selection.
var range1 = document.selection.createRange(); range1 = document.selection.createRange();
var range2 = range1.duplicate(); range2 = range1.duplicate();
// Select all text. // Select all text.
range2.moveToElementText(element); range2.moveToElementText(element);
// Now move 'dummy' end point to end point of original range. // Now move 'dummy' end point to end point of original range.
range2.setEndPoint('EndToEnd', range1); range2.setEndPoint('EndToEnd', range1);
// Now we can calculate start and end points. // Now we can calculate start and end points.
var start = range2.text.length - range1.text.length; start = range2.text.length - range1.text.length;
var end = start + range1.text.length; end = start + range1.text.length;
return { 'start': start, 'end': end }; return { 'start': start, 'end': end };
} }
return { 'start': element.selectionStart, 'end': element.selectionEnd }; return { 'start': element.selectionStart, 'end': element.selectionEnd };
@ -405,4 +407,4 @@ Drupal.theme.prototype = {
} }
}; };
})(jQuery); })(jQuery, Drupal, this, this.document);