forked from dobtco/starrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starrr.coffee
86 lines (63 loc) · 2.42 KB
/
starrr.coffee
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
78
79
80
81
82
83
84
85
86
(($, window) ->
# Define the plugin class
window.Starrr = class Starrr
defaults:
rating: undefined
numStars: 5
emptyStarClass: 'fa fa-star-o'
fullStarClass: 'fa fa-star'
change: (e, value) ->
constructor: ($el, options) ->
@options = $.extend({}, @defaults, options)
@$el = $el
for i, _ of @defaults
@options[i] = @$el.data(i.toLowerCase()) if @$el.data(i.toLowerCase())?
if @$el.data('connected-input')
@$connectedInput = $("[name=\"#{@$el.data('connected-input')}\"]")
@options.rating = if @$connectedInput.val() then parseInt(@$connectedInput.val(), 10) else undefined
@createStars()
@syncRating()
return if @$connectedInput && @$connectedInput.is(':disabled')
@$el.on 'mouseover.starrr', 'i', (e) =>
@syncRating(@getStars().index(e.currentTarget) + 1)
@$el.on 'mouseout.starrr', =>
@syncRating()
@$el.on 'click.starrr', 'i', (e) =>
@setRating(@getStars().index(e.currentTarget) + 1)
@$el.on 'starrr:change', @options.change
if @$connectedInput?
@$el.on 'starrr:change', (e, value) =>
@$connectedInput.val(value)
@$connectedInput.trigger('focusout') # trigger change
getStars: ->
@$el.find('i')
createStars: ->
@$el.append("""<i class='#{@options.emptyStarClass}'></i>""") for [[email protected]]
setRating: (rating) ->
rating = undefined if @options.rating == rating
@options.rating = rating
@syncRating()
@$el.trigger('starrr:change', rating)
getRating: ->
@options.rating
syncRating: (rating) ->
rating ||= @options.rating
if rating
for i in [0..rating - 1]
@getStars().eq(i).removeClass(@options.emptyStarClass).addClass(@options.fullStarClass)
if rating && rating < @options.numStars
for i in [rating..(@options.numStars - 1)]
@getStars().eq(i).removeClass(@options.fullStarClass).addClass(@options.emptyStarClass)
if !rating
@getStars().removeClass(@options.fullStarClass).addClass(@options.emptyStarClass)
# Define the plugin
$.fn.extend starrr: (option, args...) ->
@each ->
data = $(@).data('starrr')
if !data
$(@).data 'starrr', (data = new Starrr($(@), option))
if typeof option == 'string'
data[option].apply(data, args)
) window.jQuery, window
$ ->
$(".starrr").starrr()