| 1 |
// $Id: form.js,v 1.11 2009/08/31 05:51:07 dries Exp $
|
| 2 |
(function ($) {
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Retrieves the summary for the first element.
|
| 6 |
*/
|
| 7 |
$.fn.getSummary = function () {
|
| 8 |
var callback = this.data('summaryCallback');
|
| 9 |
return (this[0] && callback) ? $.trim(callback(this[0])) : '';
|
| 10 |
};
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Sets the summary for all matched elements.
|
| 14 |
*
|
| 15 |
* @param callback
|
| 16 |
* Either a function that will be called each time the summary is
|
| 17 |
* retrieved or a string (which is returned each time).
|
| 18 |
*/
|
| 19 |
$.fn.setSummary = function (callback) {
|
| 20 |
var self = this;
|
| 21 |
|
| 22 |
// To facilitate things, the callback should always be a function. If it's
|
| 23 |
// not, we wrap it into an anonymous function which just returns the value.
|
| 24 |
if (typeof callback != 'function') {
|
| 25 |
var val = callback;
|
| 26 |
callback = function () { return val; };
|
| 27 |
}
|
| 28 |
|
| 29 |
return this
|
| 30 |
.data('summaryCallback', callback)
|
| 31 |
// To prevent duplicate events, the handlers are first removed and then
|
| 32 |
// (re-)added.
|
| 33 |
.unbind('formUpdated.summary')
|
| 34 |
.bind('formUpdated.summary', function () {
|
| 35 |
self.trigger('summaryUpdated');
|
| 36 |
})
|
| 37 |
// The actual summaryUpdated handler doesn't fire when the callback is
|
| 38 |
// changed, so we have to do this manually.
|
| 39 |
.trigger('summaryUpdated');
|
| 40 |
};
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Sends a 'formUpdated' event each time a form element is modified.
|
| 44 |
*/
|
| 45 |
Drupal.behaviors.formUpdated = {
|
| 46 |
attach: function (context) {
|
| 47 |
// These events are namespaced so that we can remove them later.
|
| 48 |
var events = 'change.formUpdated click.formUpdated blur.formUpdated keyup.formUpdated';
|
| 49 |
$(context)
|
| 50 |
// Since context could be an input element itself, it's added back to
|
| 51 |
// the jQuery object and filtered again.
|
| 52 |
.find(':input').andSelf().filter(':input')
|
| 53 |
// To prevent duplicate events, the handlers are first removed and then
|
| 54 |
// (re-)added.
|
| 55 |
.unbind(events).bind(events, function () {
|
| 56 |
$(this).trigger('formUpdated');
|
| 57 |
});
|
| 58 |
}
|
| 59 |
};
|
| 60 |
|
| 61 |
Drupal.behaviors.multiselectSelector = {
|
| 62 |
attach: function (context, settings) {
|
| 63 |
// Automatically selects the right radio button in a multiselect control.
|
| 64 |
$('.multiselect select', context).once('multiselect').change(function () {
|
| 65 |
$('.multiselect input:radio[value="' + this.id.substr(5) + '"]')
|
| 66 |
.attr('checked', true);
|
| 67 |
});
|
| 68 |
}
|
| 69 |
};
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Automatically display the guidelines of the selected text format.
|
| 73 |
*/
|
| 74 |
Drupal.behaviors.filterGuidelines = {
|
| 75 |
attach: function (context) {
|
| 76 |
$('.filter-guidelines', context).once('filter-guidelines')
|
| 77 |
.find('label').hide()
|
| 78 |
.parents('.filter-wrapper').find('select.filter-list')
|
| 79 |
.bind('change', function () {
|
| 80 |
$(this).parents('.filter-wrapper')
|
| 81 |
.find('.filter-guidelines-item').hide()
|
| 82 |
.siblings('#filter-guidelines-' + this.value).show();
|
| 83 |
})
|
| 84 |
.change();
|
| 85 |
}
|
| 86 |
};
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Prepopulate form fields with information from the visitor cookie.
|
| 90 |
*/
|
| 91 |
Drupal.behaviors.fillUserInfoFromCookie = {
|
| 92 |
attach: function (context, settings) {
|
| 93 |
$('form.user-info-from-cookie').once('user-info-from-cookie', function () {
|
| 94 |
var formContext = this;
|
| 95 |
$.each(['name', 'mail', 'homepage'], function () {
|
| 96 |
var $element = $('[name=' + this + ']', formContext);
|
| 97 |
var cookie = $.cookie('Drupal.visitor.' + this);
|
| 98 |
if ($element.length && cookie) {
|
| 99 |
$element.val(cookie);
|
| 100 |
}
|
| 101 |
});
|
| 102 |
});
|
| 103 |
}
|
| 104 |
};
|
| 105 |
|
| 106 |
})(jQuery);
|