Projet

Général

Profil

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

root / htmltest / sites / all / modules / fivestar / js / fivestar.js @ a5572547

1
/**
2
 * Create a degradeable star rating interface out of a simple form structure.
3
 *
4
 * Originally based on the Star Rating jQuery plugin by Wil Stuckey:
5
 * http://sandbox.wilstuckey.com/jquery-ratings/
6
 */
7
(function($){ // Create local scope.
8

    
9
Drupal.behaviors.fivestar = {
10
  attach: function (context) {
11
    $('div.fivestar-form-item').once('fivestar', function() {
12
      var $this = $(this);
13
      var $container = $('<div class="fivestar-widget clearfix"></div>');
14
      var $select = $('select', $this);
15

    
16
      // Setup the cancel button
17
      var $cancel = $('option[value="0"]', $this);
18
      if ($cancel.length) {
19
        $('<div class="cancel"><a href="#0" title="' + $cancel.text() + '">' + $cancel.text() + '</a></div>')
20
          .appendTo($container);
21
      }
22

    
23
      // Setup the rating buttons
24
      var $options = $('option', $this).not('[value="-"], [value="0"]');
25
      var index = -1;
26
      $options.each(function(i, element) {
27
        var classes = 'star-' + (i+1);
28
        classes += (i + 1) % 2 == 0 ? ' even' : ' odd';
29
        classes += i == 0 ? ' star-first' : '';
30
        classes += i + 1 == $options.length ? ' star-last' : '';
31
        $('<div class="star"><a href="#' + element.value + '" title="' + element.text + '">' + element.text + '</a></div>')
32
          .addClass(classes)
33
          .appendTo($container);
34
        if (element.value == $select.val()) {
35
          index = i + 1;
36
        }
37
      });
38

    
39
      $container.find('.star:lt(' + index + ')').addClass('on');
40
      $container.addClass('fivestar-widget-' + ($options.length));
41
      $container.find('a')
42
        .bind('click', $this, Drupal.behaviors.fivestar.rate)
43
        .bind('mouseover', $this, Drupal.behaviors.fivestar.hover);
44

    
45
      $container.bind('mouseover mouseout', $this, Drupal.behaviors.fivestar.hover);
46

    
47
      // Attach the new widget and hide the existing widget.
48
      $select.after($container).css('display', 'none');
49
    });
50
  },
51
  rate: function(event) {
52
    var $this = $(this);
53
    var $widget = event.data;
54
    var value = this.hash.replace('#', '');
55
    $('select', $widget).val(value).change();
56
    var $this_star = $this.closest('.star');
57
    $this_star.prevAll('.star').andSelf().addClass('on');
58
    $this_star.nextAll('.star').removeClass('on');
59
    event.preventDefault();
60
  },
61
  hover: function(event) {
62
    var $this = $(this);
63
    var $widget = event.data;
64
    var $target = $(event.target);
65
    var $stars = $('.star', $this);
66

    
67
    if (event.type == 'mouseover') {
68
      var index = $stars.index($target.parent());
69
      $stars.each(function(i, element) {
70
        if (i <= index) {
71
          $(element).addClass('hover');
72
        } else {
73
          $(element).removeClass('hover');
74
        }
75
      });
76
    } else {
77
      $stars.removeClass('hover');
78
    }
79
  }
80
};
81

    
82
})(jQuery);