3
* http://github.com/jlukic/semantic-ui/
6
* Copyright 2014 Contributors
7
* Released under the MIT license
8
* http://opensource.org/licenses/MIT
12
;(function ($, window, document, undefined) {
14
$.fn.rating = function(parameters) {
16
$allModules = $(this),
17
moduleSelector = $allModules.selector || '',
19
time = new Date().getTime(),
23
methodInvoked = (typeof query == 'string'),
24
queryArguments = [].slice.call(arguments, 1),
30
settings = ( $.isPlainObject(parameters) )
31
? $.extend(true, {}, $.fn.rating.settings, parameters)
32
: $.extend({}, $.fn.rating.settings),
34
namespace = settings.namespace,
35
className = settings.className,
36
metadata = settings.metadata,
37
selector = settings.selector,
38
error = settings.error,
40
eventNamespace = '.' + namespace,
41
moduleNamespace = 'module-' + namespace,
44
instance = $(this).data(moduleNamespace),
47
$icon = $module.find(selector.icon),
54
initialize: function() {
55
module.verbose('Initializing rating module', settings);
57
if(settings.interactive) {
64
if(settings.initialRating) {
65
module.debug('Setting initial rating');
66
module.setRating(settings.initialRating);
68
if( $module.data(metadata.rating) ) {
69
module.debug('Rating found in metadata');
70
module.setRating( $module.data(metadata.rating) );
75
instantiate: function() {
76
module.verbose('Instantiating module', settings);
79
.data(moduleNamespace, module)
84
module.verbose('Destroying previous instance', instance);
86
.removeData(moduleNamespace)
94
mouseenter: function() {
100
.removeClass(className.hover)
103
.addClass(className.hover)
106
.addClass(className.hover)
108
.addClass(className.hover)
111
mouseleave: function() {
113
.removeClass(className.hover)
116
.removeClass(className.hover)
121
$activeIcon = $(this),
122
currentRating = module.getRating(),
123
rating = $icon.index($activeIcon) + 1
125
if(settings.clearable && currentRating == rating) {
126
module.clearRating();
129
module.setRating( rating );
134
clearRating: function() {
135
module.debug('Clearing current rating');
139
getRating: function() {
141
currentRating = $icon.filter('.' + className.active).size()
143
module.verbose('Current rating retrieved', currentRating);
144
return currentRating;
148
module.debug('Setting rating to interactive mode');
150
.on('mouseenter' + eventNamespace, module.event.mouseenter)
151
.on('mouseleave' + eventNamespace, module.event.mouseleave)
152
.on('click' + eventNamespace, module.event.click)
155
.removeClass(className.disabled)
159
disable: function() {
160
module.debug('Setting rating to read-only mode');
165
.addClass(className.disabled)
169
setRating: function(rating) {
171
ratingIndex = (rating - 1 >= 0)
174
$activeIcon = $icon.eq(ratingIndex)
177
.removeClass(className.hover)
180
.removeClass(className.hover)
181
.removeClass(className.active)
184
module.verbose('Setting current rating to', rating);
186
.addClass(className.active)
188
.addClass(className.active)
191
$.proxy(settings.onRate, element)(rating);
194
setting: function(name, value) {
195
if( $.isPlainObject(name) ) {
196
$.extend(true, settings, name);
198
else if(value !== undefined) {
199
settings[name] = value;
202
return settings[name];
205
internal: function(name, value) {
206
if( $.isPlainObject(name) ) {
207
$.extend(true, module, name);
209
else if(value !== undefined) {
210
module[name] = value;
218
if(settings.performance) {
219
module.performance.log(arguments);
222
module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
223
module.debug.apply(console, arguments);
227
verbose: function() {
228
if(settings.verbose && settings.debug) {
229
if(settings.performance) {
230
module.performance.log(arguments);
233
module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
234
module.verbose.apply(console, arguments);
239
module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
240
module.error.apply(console, arguments);
243
log: function(message) {
249
if(settings.performance) {
250
currentTime = new Date().getTime();
251
previousTime = time || currentTime;
252
executionTime = currentTime - previousTime;
257
'Arguments' : [].slice.call(message, 1) || '',
258
'Execution Time' : executionTime
261
clearTimeout(module.performance.timer);
262
module.performance.timer = setTimeout(module.performance.display, 100);
264
display: function() {
266
title = settings.name + ':',
270
clearTimeout(module.performance.timer);
271
$.each(performance, function(index, data) {
272
totalTime += data['Execution Time'];
274
title += ' ' + totalTime + 'ms';
276
title += ' \'' + moduleSelector + '\'';
278
if($allModules.size() > 1) {
279
title += ' ' + '(' + $allModules.size() + ')';
281
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
282
console.groupCollapsed(title);
284
console.table(performance);
287
$.each(performance, function(index, data) {
288
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
296
invoke: function(query, passedArguments, context) {
303
passedArguments = passedArguments || queryArguments;
304
context = element || context;
305
if(typeof query == 'string' && object !== undefined) {
306
query = query.split(/[\. ]/);
307
maxDepth = query.length - 1;
308
$.each(query, function(depth, value) {
309
var camelCaseValue = (depth != maxDepth)
310
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
313
if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
314
object = object[camelCaseValue];
316
else if( object[camelCaseValue] !== undefined ) {
317
found = object[camelCaseValue];
320
else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
321
object = object[value];
323
else if( object[value] !== undefined ) {
324
found = object[value];
332
if ( $.isFunction( found ) ) {
333
response = found.apply(context, passedArguments);
335
else if(found !== undefined) {
338
if($.isArray(returnedValue)) {
339
returnedValue.push(response);
341
else if(returnedValue !== undefined) {
342
returnedValue = [returnedValue, response];
344
else if(response !== undefined) {
345
returnedValue = response;
351
if(instance === undefined) {
354
module.invoke(query);
357
if(instance !== undefined) {
365
return (returnedValue !== undefined)
371
$.fn.rating.settings = {
374
namespace : 'rating',
384
onRate : function(rating){},
387
method : 'The method you called is not defined'
396
disabled : 'disabled',
407
})( jQuery, window , document );