-
Notifications
You must be signed in to change notification settings - Fork 0
/
extRating.js
78 lines (59 loc) · 2.45 KB
/
extRating.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$(
$.fn.extRating = function(options) {
var defaults = {
activeStarPath: '/assets/extRating/activeStar.png',
inactiveStarPath: '/assets/extRating/inactiveStar.png',
numberOfStars: 5
};
options = $.extend(defaults, options);
var current_rating = parseInt($(this).val());
var $input = $(this);
var $parent = $(this).parent();
var container_id = $input.attr("id") + "_extStar"
var $container = $('<span>', {
id: container_id
}).appendTo($parent);
$input.hide();
for(var star_index = 1; star_index <= options.numberOfStars; star_index++) {
var $star = $('<span>', { class: 'extStar' }).appendTo($container);
var $active_star_img = $('<img>', {
class: 'extActiveStarImage',
src: options.activeStarPath,
alt: star_index
}).appendTo($star);
var $inactive_star_img = $('<img>', {
class: 'extInactiveStarImage',
src: options.inactiveStarPath,
alt: star_index
}).appendTo($star);
$.each([$active_star_img, $inactive_star_img], function() {
this.click(function() {
var last_star_index = parseInt($(this).attr('alt'));
$input.val(last_star_index);
});
this.mousemove(function() {
var last_star_index = parseInt($(this).attr('alt'));
fillStars(container_id, last_star_index)
});
this.mouseleave(function() {
var last_star_index = parseInt($input.val());
fillStars(container_id, last_star_index)
});
});
}
fillStars(container_id, current_rating);
function fillStars(container_id, current_star_index) {
for(var i = 1; i <= options.numberOfStars; i++) {
var $active = $("#" + container_id + " .extActiveStarImage[alt='" + i + "']");
var $inactive = $("#" + container_id + " .extInactiveStarImage[alt='" + i + "']");
if(current_star_index >= i) {
$active.show();
$inactive.hide();
} else {
$active.hide();
$inactive.show();
}
}
}
}
);