Projet

Général

Profil

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

root / drupal7 / sites / all / modules / fivestar / js / fivestar.js @ 3461d8cb

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
      // Allow other modules to modify the widget.
59
      Drupal.attachBehaviors($this);
60
    });
61
  },
62
  rate: function(event) {
63
    var $this = $(this);
64
    var $widget = event.data;
65
    var value = this.hash.replace('#', '');
66
    $('select', $widget).val(value).change();
67
    var $this_star = (value == 0) ? $this.parent().parent().find('.star') : $this.closest('.star');
68
    $this_star.prevAll('.star').andSelf().addClass('on');
69
    $this_star.nextAll('.star').removeClass('on');
70
    if(value==0){
71
      $this_star.removeClass('on');
72
    }
73

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

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

    
97
})(jQuery);