Projet

Général

Profil

Paste
Télécharger (2,4 ko) Statistiques
| Branche: | Révision:

root / htmltest / misc / form.js @ 85ad3d82

1
(function ($) {
2

    
3
/**
4
 * Retrieves the summary for the first element.
5
 */
6
$.fn.drupalGetSummary = function () {
7
  var callback = this.data('summaryCallback');
8
  return (this[0] && callback) ? $.trim(callback(this[0])) : '';
9
};
10

    
11
/**
12
 * Sets the summary for all matched elements.
13
 *
14
 * @param callback
15
 *   Either a function that will be called each time the summary is
16
 *   retrieved or a string (which is returned each time).
17
 */
18
$.fn.drupalSetSummary = function (callback) {
19
  var self = this;
20

    
21
  // To facilitate things, the callback should always be a function. If it's
22
  // not, we wrap it into an anonymous function which just returns the value.
23
  if (typeof callback != 'function') {
24
    var val = callback;
25
    callback = function () { return val; };
26
  }
27

    
28
  return this
29
    .data('summaryCallback', callback)
30
    // To prevent duplicate events, the handlers are first removed and then
31
    // (re-)added.
32
    .unbind('formUpdated.summary')
33
    .bind('formUpdated.summary', function () {
34
      self.trigger('summaryUpdated');
35
    })
36
    // The actual summaryUpdated handler doesn't fire when the callback is
37
    // changed, so we have to do this manually.
38
    .trigger('summaryUpdated');
39
};
40

    
41
/**
42
 * Sends a 'formUpdated' event each time a form element is modified.
43
 */
44
Drupal.behaviors.formUpdated = {
45
  attach: function (context) {
46
    // These events are namespaced so that we can remove them later.
47
    var events = 'change.formUpdated click.formUpdated blur.formUpdated keyup.formUpdated';
48
    $(context)
49
      // Since context could be an input element itself, it's added back to
50
      // the jQuery object and filtered again.
51
      .find(':input').andSelf().filter(':input')
52
      // To prevent duplicate events, the handlers are first removed and then
53
      // (re-)added.
54
      .unbind(events).bind(events, function () {
55
        $(this).trigger('formUpdated');
56
      });
57
  }
58
};
59

    
60
/**
61
 * Prepopulate form fields with information from the visitor cookie.
62
 */
63
Drupal.behaviors.fillUserInfoFromCookie = {
64
  attach: function (context, settings) {
65
    $('form.user-info-from-cookie').once('user-info-from-cookie', function () {
66
      var formContext = this;
67
      $.each(['name', 'mail', 'homepage'], function () {
68
        var $element = $('[name=' + this + ']', formContext);
69
        var cookie = $.cookie('Drupal.visitor.' + this);
70
        if ($element.length && cookie) {
71
          $element.val(cookie);
72
        }
73
      });
74
    });
75
  }
76
};
77

    
78
})(jQuery);