forked from ClickerMonkey/SemanticUI-Angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm-rating.js
108 lines (87 loc) · 2.5 KB
/
sm-rating.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(function(app)
{
app
.factory('SemanticRatingLink', ['SemanticUI', SemanticRatingLink])
.directive('smRatingBind', ['SemanticUI', SemanticRatingBind])
.directive('smRating', ['SemanticRatingLink', SemanticRating])
;
var BEHAVIORS = {
smRatingSet: 'set rating',
smRatingDisable: 'disable',
smRatingEnable: 'enable',
smRatingClear: 'clear rating'
};
angular.forEach( BEHAVIORS, function(method, directive)
{
app.directive( directive, ['SemanticUI', function(SemanticUI)
{
return SemanticUI.createBehavior( directive, 'rating', method );
}]);
});
function SemanticRatingBind(SemanticUI)
{
return SemanticUI.createBind( 'smRatingBind', 'rating' );
}
function SemanticRating(SemanticRatingLink)
{
return {
restrict: 'E',
replace: true,
scope: {
/* Required */
model: '=',
total: '=',
/* Optional */
type: '@',
disabled: '=',
settings: '=',
onInit: '=',
/* Events */
onRate: '='
},
template: '<div class="ui rating {{ type }}" data-rating="{{ model }}" data-max-rating="{{ total }}"></div>',
link: SemanticRatingLink
};
}
function SemanticRatingLink(SemanticUI)
{
return function(scope, element, attributes)
{
element.ready(function()
{
var settings = scope.settings || {};
SemanticUI.linkSettings( scope, element, attributes, 'rating', true );
SemanticUI.triggerChange( scope, 'model', element, true );
if ( attributes.disabled )
{
var disabledWatcher = SemanticUI.watcher( scope, 'disabled',
function(updated) {
element.rating( updated ? 'disable' : 'enable' );
}
);
}
var valueWatcher = SemanticUI.watcher( scope, 'model',
function(updated) {
element.rating( 'set rating', updated );
}
);
SemanticUI.onEvent( settings, 'onRate',
function(value) {
valueWatcher.set( value );
}
);
SemanticUI.linkEvents( scope, settings, $.fn.rating.settings, {
onRate: 'onRate'
});
element.rating( settings );
if ( scope.disabled )
{
element.rating( 'disable' );
}
if ( angular.isFunction( scope.onInit ) ) {
scope.onInit( element );
}
});
};
}
})( angular.module('semantic-ui-rating', ['semantic-ui-core']) );