1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
(function($){
|
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
|
|
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
|
|
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
|
|
56
|
$select.after($container).css('display', 'none');
|
57
|
|
58
|
|
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);
|