Commit ac484c27 authored by webchick's avatar webchick

#125030 by kkaefer, quicksketch, dvessel, Steven, and John Resig: Allow...

#125030 by kkaefer, quicksketch, dvessel, Steven, and John Resig: Allow compatibility with other JavaScript libraries.
parent b6025a88
......@@ -2334,6 +2334,8 @@ function drupal_clear_css_cache() {
* directly in the page. This can, for example, be useful to tell the user that
* a new message arrived, by opening a pop up, alert box etc. This should only
* be used for JavaScript which cannot be placed and executed from a file.
* When adding inline code, make sure that you are not relying on $ being jQuery.
* Wrap your code in (function($) { ... })(jQuery); or use jQuery instead of $.
*
* - Add settings ('setting'):
* Adds a setting to Drupal's global storage of JavaScript settings. Per-page
......@@ -2344,8 +2346,8 @@ function drupal_clear_css_cache() {
* @code
* drupal_add_js('misc/collapse.js');
* drupal_add_js('misc/collapse.js', 'file');
* drupal_add_js('$(document).ready(function(){alert("Hello!");});', 'inline');
* drupal_add_js('$(document).ready(function(){alert("Hello!");});',
* drupal_add_js('jQuery(document).ready(function(){alert("Hello!");});', 'inline');
* drupal_add_js('jQuery(document).ready(function(){alert("Hello!");});',
* array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
* );
* @endcode
......
......@@ -731,7 +731,7 @@ function install_tasks($profile, $task) {
drupal_add_js('
// Global Killswitch
if (Drupal.jsEnabled) {
$(document).ready(function() {
jQuery(document).ready(function() {
Drupal.cleanURLsInstallCheck();
});
}', 'inline');
......
// $Id$
(function($) {
/**
* Provides AJAX-like page updating via AHAH (Asynchronous HTML and HTTP).
......@@ -138,7 +139,7 @@ Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {
else if (this.progress.type == 'throbber') {
this.progress.element = $('<div class="ahah-progress ahah-progress-throbber"><div class="throbber">&nbsp;</div></div>');
if (this.progress.message) {
$('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>')
$('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>');
}
$(this.element).after(this.progress.element);
}
......@@ -225,3 +226,5 @@ Drupal.ahah.prototype.error = function (response, uri) {
// Re-enable the element.
$(this.element).removeClass('progess-disabled').attr('disabled', false);
};
})(jQuery);
// $Id$
(function($) {
/**
* Attaches the autocomplete behavior to all required fields.
......@@ -298,3 +299,5 @@ Drupal.ACDB.prototype.cancel = function() {
if (this.timer) clearTimeout(this.timer);
this.searchString = '';
};
})(jQuery);
// $Id$
(function($) {
/**
* Attaches the batch behavior to progress bars.
......@@ -38,3 +39,5 @@ Drupal.behaviors.batch = {
});
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Toggle the visibility of a fieldset using smooth animations
......@@ -77,3 +78,5 @@ Drupal.behaviors.collapse = {
});
}
};
})(jQuery);
......@@ -2,6 +2,19 @@
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} };
// Allow other JavaScript libraries to use $.
jQuery.noConflict();
// Indicate when other scripts use $ with out wrapping their code.
if ($ === undefined) {
$ = function() {
alert("Please wrap your JavaScript code in (function($) { ... })(jQuery); to be compatible. See http://docs.jquery.com/Using_jQuery_with_Other_Libraries.");
};
}
(function($) {
/**
* Set the variable that indicates if JavaScript behaviors should be applied.
*/
......@@ -42,8 +55,8 @@ Drupal.jsEnabled = document.getElementsByTagName && document.createElement && do
Drupal.attachBehaviors = function(context) {
context = context || document;
// Execute all of them.
jQuery.each(Drupal.behaviors, function() {
if (jQuery.isFunction(this.attach)) {
$.each(Drupal.behaviors, function() {
if ($.isFunction(this.attach)) {
this.attach(context);
}
});
......@@ -71,8 +84,8 @@ Drupal.attachBehaviors = function(context) {
Drupal.detachBehaviors = function(context) {
context = context || document;
// Execute all of them.
jQuery.each(Drupal.behaviors, function() {
if (jQuery.isFunction(this.detach)) {
$.each(Drupal.behaviors, function() {
if ($.isFunction(this.detach)) {
this.detach(context);
}
});
......@@ -286,7 +299,7 @@ Drupal.getSelection = function (element) {
*/
Drupal.ahahError = function(xmlhttp, uri) {
if (xmlhttp.status == 200) {
if (jQuery.trim(xmlhttp.responseText)) {
if ($.trim(xmlhttp.responseText)) {
var message = Drupal.t("An error occurred. \n@uri\n@text", {'@uri': uri, '@text': xmlhttp.responseText });
}
else {
......@@ -296,8 +309,8 @@ Drupal.ahahError = function(xmlhttp, uri) {
else {
var message = Drupal.t("An HTTP error @status occurred. \n@uri", {'@uri': uri, '@status': xmlhttp.status });
}
return message.replace(/\n/g, '<br />');;
}
return message.replace(/\n/g, '<br />');
};
// Global Killswitch on the <html> element.
if (Drupal.jsEnabled) {
......@@ -328,3 +341,5 @@ Drupal.theme.prototype = {
return '<em>' + Drupal.checkPlain(str) + '</em>';
}
};
})(jQuery);
// $Id$
// Farbtastic 1.2
(function($) {
jQuery.fn.farbtastic = function (callback) {
$.farbtastic = function (callback) {
$.farbtastic(this, callback);
return this;
};
jQuery.farbtastic = function (container, callback) {
$.farbtastic = function (container, callback) {
var container = $(container).get(0);
return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
return container.farbtastic || (container.farbtastic = new $._farbtastic(container, callback));
};
jQuery._farbtastic = function (container, callback) {
$._farbtastic = function (container, callback) {
// Store farbtastic object
var fb = this;
......@@ -266,4 +267,6 @@ jQuery._farbtastic = function (container, callback) {
if (callback) {
fb.linkTo(callback);
}
};
\ No newline at end of file
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.multiselectSelector = {
attach: function(context) {
......@@ -10,3 +11,5 @@ Drupal.behaviors.multiselectSelector = {
});
}
};
})(jQuery);
// $Id$
(function($) {
/**
* A progressbar object. Initialized with the given id. Must be inserted into
......@@ -105,3 +106,5 @@ Drupal.progressBar.prototype.displayError = function (string) {
this.errorCallback(this);
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Drag and drop table rows with field manipulation.
......@@ -321,7 +322,9 @@ Drupal.tableDrag.prototype.makeDraggable = function(item) {
var groupHeight = 0;
nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
if (nextGroup) {
$(nextGroup.group).each(function () {groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight});
$(nextGroup.group).each(function () {
groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
});
nextGroupRow = $(nextGroup.group).filter(':last').get(0);
self.rowObject.swap('after', nextGroupRow);
// No need to check for indentation, 0 is the only valid one.
......@@ -957,7 +960,7 @@ Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow
}
return {'min':minIndent, 'max':maxIndent};
}
};
/**
* Indent a row within the legal bounds of the table.
......@@ -1021,7 +1024,7 @@ Drupal.tableDrag.prototype.row.prototype.findSiblings = function(rowSettings) {
// Either add immediately if this is a flat table, or check to ensure
// that this row has the same level of indentation.
if (this.indentEnabled) {
var checkRowIndentation = $('.indentation', checkRow).length
var checkRowIndentation = $('.indentation', checkRow).length;
}
if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) {
......@@ -1096,3 +1099,5 @@ Drupal.theme.prototype.tableDragIndentation = function () {
Drupal.theme.prototype.tableDragChangedWarning = function () {
return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("Changes made in this table will not be saved until the form is submitted.") + '</div>';
};
})(jQuery);
\ No newline at end of file
// $Id$
(function($) {
Drupal.tableHeaderDoScroll = function() {
if (typeof(Drupal.tableHeaderOnScroll)=='function') {
......@@ -9,7 +10,7 @@ Drupal.tableHeaderDoScroll = function() {
Drupal.behaviors.tableHeader = {
attach: function(context) {
// This breaks in anything less than IE 7. Prevent it from running.
if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) {
if ($.browser.msie && parseInt($.browser.version, 10) < 7) {
return;
}
......@@ -113,3 +114,5 @@ Drupal.behaviors.tableHeader = {
$(window).resize(resize);
}
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.tableSelect = {
attach: function(context) {
......@@ -82,8 +83,10 @@ Drupal.tableSelectRange = function(from, to, state) {
}
}
// A faster alternative to doing $(i).filter(to).length.
else if (jQuery.filter(to, [i]).r.length) {
else if ($.filter(to, [i]).r.length) {
break;
}
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Auto-attach for teaser behavior.
......@@ -96,3 +97,5 @@ Drupal.behaviors.teaser = {
});
}
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.textarea = {
attach: function(context) {
......@@ -36,3 +37,5 @@ Drupal.behaviors.textarea = {
});
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Set the client's system time zone as default values of form fields.
......@@ -57,8 +58,10 @@ Drupal.behaviors.setTimezone = {
if (data) {
$(element).val(data);
}
},
}
});
});
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Move a block in the blocks table from one region to another via select list.
......@@ -95,3 +96,5 @@ Drupal.behaviors.blockDrag = {
};
}
};
})(jQuery);
......@@ -415,7 +415,7 @@ function _book_parent_select($book_link) {
function _book_add_form_elements(&$form, $node) {
// Need this for AJAX.
$form['#cache'] = TRUE;
drupal_add_js("if (Drupal.jsEnabled) { $(document).ready(function() { $('#edit-book-pick-book').css('display', 'none'); }); }", 'inline');
drupal_add_js("if (Drupal.jsEnabled) { jQuery(function() { jQuery('#edit-book-pick-book').css('display', 'none'); }); }", 'inline');
$form['book'] = array(
'#type' => 'fieldset',
......
// $Id$
(function($) {
Drupal.behaviors.color = {
attach: function(context) {
......@@ -251,3 +252,5 @@ Drupal.behaviors.color = {
preview();
}
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.comment = {
attach: function(context) {
......@@ -35,3 +36,5 @@ Drupal.comment.getCookie = function(name) {
return returnValue;
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.contentTypes = {
attach: function() {
if ($('#edit-type').val() == $('#edit-name').val().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/_+/g, '_') || $('#edit-type').val() == '') {
......@@ -22,3 +25,5 @@ Drupal.behaviors.contentTypes = {
}
}
};
})(jQuery);
// $Id$
(function($) {
Drupal.behaviors.openid = {
attach: function(context) {
......@@ -38,3 +39,5 @@ Drupal.behaviors.openid = {
});
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Add functionality to the profile drag and drop table.
......@@ -54,3 +55,5 @@ Drupal.behaviors.profileDrag = {
};
}
};
})(jQuery);
\ No newline at end of file
// $Id$
(function($) {
/**
* Add the cool table collapsing on the testing overview page.
......@@ -76,7 +77,7 @@ Drupal.behaviors.simpleTestSelectAll = {
});
}
$(groupCheckbox).attr('checked', (checkedTests == testCheckboxes.length));
}
};
// Have the single-test checkboxes follow the group checkbox.
groupCheckbox.change(function() {
......@@ -99,3 +100,5 @@ Drupal.behaviors.simpleTestSelectAll = {
});
}
};
})(jQuery);
......@@ -445,7 +445,7 @@ class JavaScriptTestCase extends DrupalWebTestCase {
* Test adding inline scripts.
*/
function testAddInline() {
$inline = '$(document).ready(function(){});';
$inline = 'jQuery(function(){});';
$javascript = drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$this->assertTrue(array_key_exists('misc/jquery.js', $javascript), t('jQuery is added when inline scripts are added.'));
$data = end($javascript);
......@@ -456,7 +456,7 @@ class JavaScriptTestCase extends DrupalWebTestCase {
* Test drupal_get_js() with a footer scope.
*/
function testFooterHTML() {
$inline = '$(document).ready(function(){});';
$inline = 'jQuery(function(){});';
drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$javascript = drupal_get_js('footer');
$this->assertTrue(strpos($javascript, $inline) > 0, t('Rendered JavaScript footer returns the inline code.'));
......
// $Id$
(function($) {
/**
* Internal function to check using Ajax if clean URLs can be enabled on the
......@@ -80,7 +81,7 @@ Drupal.behaviors.copyFieldValue = {
for (var sourceId in Drupal.settings.copyFieldValue) {
// Get the list of target fields.
targetIds = Drupal.settings.copyFieldValue[sourceId];
if (!$('#'+ sourceId + '.copy-field-values-processed').size(), context) {
if (!$('#'+ sourceId + '.copy-field-values-processed', context).size()) {
// Add the behavior to update target fields on blur of the primary field.
sourceField = $('#' + sourceId);
sourceField.bind('blur', function() {
......@@ -131,4 +132,6 @@ Drupal.behaviors.poweredByPreview = {
$('img.powered-by-preview').attr('src', path);
});
}
};
\ No newline at end of file
};
})(jQuery);
\ No newline at end of file
// $Id$
(function($) {
/**
* Move a block in the blocks table from one region to another via select list.
......@@ -36,3 +37,5 @@ Drupal.behaviors.termDrag = {
};
}
};
})(jQuery);
// $Id$
(function($) {
/**
* Attach handlers to evaluate the strength of any password fields and to check
......@@ -76,7 +77,7 @@ Drupal.behaviors.password = {
else {
confirmResult.css({ visibility: "hidden" });
}
}
};
// Monitor keyup and blur events.
// Blur must be used because a mouse paste does not trigger keyup.
......@@ -171,3 +172,5 @@ Drupal.behaviors.userSettings = {
});
}
};
})(jQuery);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment