Projet

Général

Profil

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

root / drupal7 / sites / all / modules / fivestar / js / fivestar.js @ 4853591f

1
/**
2
 * @file
3
 *
4
 * Fivestar JavaScript behaviors integration.
5
 */
6

    
7
/**
8
 * Create a degradeable star rating interface out of a simple form structure.
9
 *
10
 * Originally based on the Star Rating jQuery plugin by Wil Stuckey:
11
 * http://sandbox.wilstuckey.com/jquery-ratings/
12
 */
13
(function($){ // Create local scope.
14

    
15
Drupal.behaviors.fivestar = {
16
  attach: function (context) {
17
    $(context).find('div.fivestar-form-item').once('fivestar', function() {
18
      var $this = $(this);
19
      var $container = $('<div class="fivestar-widget clearfix"></div>');
20
      var $select = $('select', $this);
21

    
22
      // Setup the cancel button
23
      var $cancel = $('option[value="0"]', $this);
24
      if ($cancel.length) {
25
        $('<div class="cancel"><a href="#0" title="' + $cancel.text() + '">' + $cancel.text() + '</a></div>')
26
          .appendTo($container);
27
      }
28

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

    
45
      if (index != -1) {
46
        $container.find('.star').slice(0, index).addClass('on');
47
      }
48
      $container.addClass('fivestar-widget-' + ($options.length));
49
      $container.find('a')
50
        .bind('click', $this, Drupal.behaviors.fivestar.rate)
51
        .bind('mouseover', $this, Drupal.behaviors.fivestar.hover);
52

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

    
55
      // Attach the new widget and hide the existing widget.
56
      $select.after($container).css('display', 'none');
57
    });
58
  },
59
  rate: function(event) {
60
    var $this = $(this);
61
    var $widget = event.data;
62
    var value = this.hash.replace('#', '');
63
    $('select', $widget).val(value).change();
64
    var $this_star = (value == 0) ? $this.parent().parent().find('.star') : $this.closest('.star');
65
    $this_star.prevAll('.star').andSelf().addClass('on');
66
    $this_star.nextAll('.star').removeClass('on');
67
    if(value==0){
68
      $this_star.removeClass('on');
69
    }
70

    
71
    event.preventDefault();
72
  },
73
  hover: function(event) {
74
    var $this = $(this);
75
    var $widget = event.data;
76
    var $target = $(event.target);
77
    var $stars = $('.star', $this);
78

    
79
    if (event.type == 'mouseover') {
80
      var index = $stars.index($target.parent());
81
      $stars.each(function(i, element) {
82
        if (i <= index) {
83
          $(element).addClass('hover');
84
        } else {
85
          $(element).removeClass('hover');
86
        }
87
      });
88
    } else {
89
      $stars.removeClass('hover');
90
    }
91
  }
92
};
93

    
94
})(jQuery);