Projet

Général

Profil

Paste
Télécharger (3,14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / js / misc / autocomplete.js @ 87dbc3bf

1 87dbc3bf Benjamin Luce
(function ($) {
2
3
/**
4
 * Attaches the autocomplete behavior to all required fields.
5
 */
6
Drupal.behaviors.autocomplete = {
7
  attach: function (context, settings) {
8
    var acdb = [];
9
    $('input.autocomplete', context).once('autocomplete', function () {
10
      var uri = this.value;
11
      if (!acdb[uri]) {
12
        acdb[uri] = new Drupal.ACDB(uri);
13
      }
14
      var $input = $('#' + this.id.substr(0, this.id.length - 13))
15
        .attr('autocomplete', 'OFF')
16
        .attr('aria-autocomplete', 'list');
17
      $($input[0].form).submit(Drupal.autocompleteSubmit);
18
      $input
19
        .after($('<span class="element-invisible" aria-live="assertive"></span>')
20
          .attr('id', $input.attr('id') + '-autocomplete-aria-live')
21
        );
22
      $input.parent().parent().attr('role', 'application');
23
      new Drupal.jsAC($input, acdb[uri]);
24
    });
25
  }
26
};
27
28
/**
29
 * Highlights a suggestion.
30
 */
31
Drupal.jsAC.prototype.highlight = function (node) {
32
  if (this.selected) {
33
    $(this.selected).removeClass('active');
34
  }
35
  $(node).addClass('active');
36
  this.selected = node;
37
  $(this.ariaLive).html($(this.selected).html());
38
};
39
40
/**
41
 * Unhighlights a suggestion.
42
 */
43
Drupal.jsAC.prototype.unhighlight = function (node) {
44
  $(node).removeClass('active');
45
  this.selected = false;
46
  $(this.ariaLive).empty();
47
};
48
49
/**
50
 * Positions the suggestions popup and starts a search.
51
 */
52
Drupal.jsAC.prototype.populatePopup = function () {
53
  var $input = $(this.input);
54
  // Show popup.
55
  if (this.popup) {
56
    $(this.popup).remove();
57
  }
58
  this.selected = false;
59
  this.popup = $('<div class="dropdown"></div>')[0];
60
  this.popup.owner = this;
61
  $input.parent().after(this.popup);
62
63
  // Do search.
64
  this.db.owner = this;
65
  this.db.search(this.input.value);
66
};
67
68
/**
69
 * Fills the suggestion popup with any matches received.
70
 */
71
Drupal.jsAC.prototype.found = function (matches) {
72
  // If no value in the textfield, do not show the popup.
73
  if (!this.input.value.length) {
74
    return false;
75
  }
76
77
  // Prepare matches.
78
  var ul = $('<ul class="dropdown-menu"></ul>');
79
  var ac = this;
80
  ul.css({
81
    display: 'block',
82
    right: 0
83
  });
84
  for (var key in matches) {
85
    $('<li></li>')
86
      .html($('<a href="#"></a>').html(matches[key]).click(function (e) { e.preventDefault(); }))
87
      .mousedown(function () { ac.select(this); })
88
      .mouseover(function () { ac.highlight(this); })
89
      .mouseout(function () { ac.unhighlight(this); })
90
      .data('autocompleteValue', key)
91
      .appendTo(ul);
92
  }
93
94
  // Show popup with matches, if any.
95
  if (this.popup) {
96
    if (ul.children().length) {
97
      $(this.popup).empty().append(ul).show();
98
      $(this.ariaLive).html(Drupal.t('Autocomplete popup'));
99
    }
100
    else {
101
      $(this.popup).css({ visibility: 'hidden' });
102
      this.hidePopup();
103
    }
104
  }
105
};
106
107
Drupal.jsAC.prototype.setStatus = function (status) {
108
  var $throbber = $('.glyphicon-refresh', $('#' + this.input.id).parent());
109
110
  switch (status) {
111
    case 'begin':
112
      $throbber.addClass('glyphicon-spin');
113
      $(this.ariaLive).html(Drupal.t('Searching for matches...'));
114
      break;
115
    case 'cancel':
116
    case 'error':
117
    case 'found':
118
      $throbber.removeClass('glyphicon-spin');
119
      break;
120
  }
121
};
122
123
})(jQuery);