annotate misc/form.js @ 13:134d4b2e75f6

updated quicktabs and google analytics modules
author danieleb <danielebarchiesi@me.com>
date Tue, 29 Oct 2013 13:48:59 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 (function ($) {
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * Retrieves the summary for the first element.
danielebarchiesi@0 5 */
danielebarchiesi@0 6 $.fn.drupalGetSummary = function () {
danielebarchiesi@0 7 var callback = this.data('summaryCallback');
danielebarchiesi@0 8 return (this[0] && callback) ? $.trim(callback(this[0])) : '';
danielebarchiesi@0 9 };
danielebarchiesi@0 10
danielebarchiesi@0 11 /**
danielebarchiesi@0 12 * Sets the summary for all matched elements.
danielebarchiesi@0 13 *
danielebarchiesi@0 14 * @param callback
danielebarchiesi@0 15 * Either a function that will be called each time the summary is
danielebarchiesi@0 16 * retrieved or a string (which is returned each time).
danielebarchiesi@0 17 */
danielebarchiesi@0 18 $.fn.drupalSetSummary = function (callback) {
danielebarchiesi@0 19 var self = this;
danielebarchiesi@0 20
danielebarchiesi@0 21 // To facilitate things, the callback should always be a function. If it's
danielebarchiesi@0 22 // not, we wrap it into an anonymous function which just returns the value.
danielebarchiesi@0 23 if (typeof callback != 'function') {
danielebarchiesi@0 24 var val = callback;
danielebarchiesi@0 25 callback = function () { return val; };
danielebarchiesi@0 26 }
danielebarchiesi@0 27
danielebarchiesi@0 28 return this
danielebarchiesi@0 29 .data('summaryCallback', callback)
danielebarchiesi@0 30 // To prevent duplicate events, the handlers are first removed and then
danielebarchiesi@0 31 // (re-)added.
danielebarchiesi@0 32 .unbind('formUpdated.summary')
danielebarchiesi@0 33 .bind('formUpdated.summary', function () {
danielebarchiesi@0 34 self.trigger('summaryUpdated');
danielebarchiesi@0 35 })
danielebarchiesi@0 36 // The actual summaryUpdated handler doesn't fire when the callback is
danielebarchiesi@0 37 // changed, so we have to do this manually.
danielebarchiesi@0 38 .trigger('summaryUpdated');
danielebarchiesi@0 39 };
danielebarchiesi@0 40
danielebarchiesi@0 41 /**
danielebarchiesi@0 42 * Sends a 'formUpdated' event each time a form element is modified.
danielebarchiesi@0 43 */
danielebarchiesi@0 44 Drupal.behaviors.formUpdated = {
danielebarchiesi@0 45 attach: function (context) {
danielebarchiesi@0 46 // These events are namespaced so that we can remove them later.
danielebarchiesi@0 47 var events = 'change.formUpdated click.formUpdated blur.formUpdated keyup.formUpdated';
danielebarchiesi@0 48 $(context)
danielebarchiesi@0 49 // Since context could be an input element itself, it's added back to
danielebarchiesi@0 50 // the jQuery object and filtered again.
danielebarchiesi@0 51 .find(':input').andSelf().filter(':input')
danielebarchiesi@0 52 // To prevent duplicate events, the handlers are first removed and then
danielebarchiesi@0 53 // (re-)added.
danielebarchiesi@0 54 .unbind(events).bind(events, function () {
danielebarchiesi@0 55 $(this).trigger('formUpdated');
danielebarchiesi@0 56 });
danielebarchiesi@0 57 }
danielebarchiesi@0 58 };
danielebarchiesi@0 59
danielebarchiesi@0 60 /**
danielebarchiesi@0 61 * Prepopulate form fields with information from the visitor cookie.
danielebarchiesi@0 62 */
danielebarchiesi@0 63 Drupal.behaviors.fillUserInfoFromCookie = {
danielebarchiesi@0 64 attach: function (context, settings) {
danielebarchiesi@0 65 $('form.user-info-from-cookie').once('user-info-from-cookie', function () {
danielebarchiesi@0 66 var formContext = this;
danielebarchiesi@0 67 $.each(['name', 'mail', 'homepage'], function () {
danielebarchiesi@0 68 var $element = $('[name=' + this + ']', formContext);
danielebarchiesi@0 69 var cookie = $.cookie('Drupal.visitor.' + this);
danielebarchiesi@0 70 if ($element.length && cookie) {
danielebarchiesi@0 71 $element.val(cookie);
danielebarchiesi@0 72 }
danielebarchiesi@0 73 });
danielebarchiesi@0 74 });
danielebarchiesi@0 75 }
danielebarchiesi@0 76 };
danielebarchiesi@0 77
danielebarchiesi@0 78 })(jQuery);