1/*! 2 * jQuery Raty FA - A Star Rating Plugin with Font Awesome 3 * 4 * Licensed under The MIT License 5 * 6 * @author : Jacob Overgaard 7 * @doc : http://jacob87.github.io/raty-fa/ 8 * @version : 0.1.1 9 * 10 jedicpe10 edit <i> to <em> 11 */ 12'use strict'; 13 14;(function($) { 15 16 var methods = { 17 init: function(settings) { 18 return this.each(function() { 19 methods.destroy.call(this); 20 21 this.opt = $.extend(true, {}, $.fn.raty.defaults, settings); 22 23 var that = $(this), 24 inits = ['number', 'readOnly', 'score', 'scoreName']; 25 26 methods._callback.call(this, inits); 27 28 if (this.opt.precision) { 29 methods._adjustPrecision.call(this); 30 } 31 32 this.opt.number = methods._between(this.opt.number, 0, this.opt.numberMax); 33 34 this.stars = methods._createStars.call(this); 35 this.score = methods._createScore.call(this); 36 37 methods._apply.call(this, this.opt.score); 38 39 if (this.opt.cancel) { 40 this.cancel = methods._createCancel.call(this); 41 } 42 43 if (this.opt.width) { 44 that.css('width', this.opt.width); 45 } 46 47 if (this.opt.readOnly) { 48 methods._lock.call(this); 49 } else { 50 that.css('cursor', 'pointer'); 51 methods._binds.call(this); 52 } 53 54 methods._target.call(this, this.opt.score); 55 56 that.data({ 'settings': this.opt, 'raty': true }); 57 }); 58 }, _adjustPrecision: function() { 59 this.opt.targetType = 'score'; 60 this.opt.half = true; 61 }, _apply: function(score) { 62 if (typeof score !== 'undefined' && score >= 0) { 63 score = methods._between(score, 0, this.opt.number); 64 this.score.val(score); 65 } 66 67 methods._fill.call(this, score); 68 69 if (score) { 70 methods._roundStars.call(this, score); 71 } 72 }, _between: function(value, min, max) { 73 return Math.min(Math.max(parseFloat(value), min), max); 74 }, _binds: function() { 75 if (this.cancel) { 76 methods._bindCancel.call(this); 77 } 78 79 methods._bindClick.call(this); 80 methods._bindOut.call(this); 81 methods._bindOver.call(this); 82 }, _bindCancel: function() { 83 methods._bindClickCancel.call(this); 84 methods._bindOutCancel.call(this); 85 methods._bindOverCancel.call(this); 86 }, _bindClick: function() { 87 var self = this, 88 that = $(self); 89 90 self.stars.on('click.raty', function(evt) { 91 self.score.val((self.opt.half || self.opt.precision) ? that.data('score') : $(this).data('score')); 92 93 if (self.opt.click) { 94 self.opt.click.call(self, parseFloat(self.score.val()), evt); 95 } 96 }); 97 }, _bindClickCancel: function() { 98 var self = this; 99 100 self.cancel.on('click.raty', function(evt) { 101 self.score.removeAttr('value'); 102 103 if (self.opt.click) { 104 self.opt.click.call(self, null, evt); 105 } 106 }); 107 }, _bindOut: function() { 108 var self = this; 109 110 $(this).on('mouseleave.raty', function(evt) { 111 var score = parseFloat(self.score.val()) || undefined; 112 113 methods._apply.call(self, score); 114 methods._target.call(self, score, evt); 115 116 if (self.opt.mouseout) { 117 self.opt.mouseout.call(self, score, evt); 118 } 119 }); 120 }, _bindOutCancel: function() { 121 var self = this; 122 123 self.cancel.on('mouseleave.raty', function(evt) { 124 $(this).attr('class', self.opt.cancelOff); 125 126 if (self.opt.mouseout) { 127 self.opt.mouseout.call(self, self.score.val() || null, evt); 128 } 129 }); 130 }, _bindOverCancel: function() { 131 var self = this; 132 133 self.cancel.on('mouseover.raty', function(evt) { 134 $(this).attr('class', self.opt.cancelOn); 135 136 self.stars.attr('class', self.opt.starOff); 137 138 methods._target.call(self, null, evt); 139 140 if (self.opt.mouseover) { 141 self.opt.mouseover.call(self, null); 142 } 143 }); 144 }, _bindOver: function() { 145 var self = this, 146 that = $(self), 147 action = self.opt.half ? 'mousemove.raty' : 'mouseover.raty'; 148 149 self.stars.on(action, function(evt) { 150 var score = parseInt($(this).data('score'), 10); 151 152 if (self.opt.half) { 153 var position = parseFloat((evt.pageX - $(this).offset().left) / (self.opt.size ? self.opt.size : parseInt(that.css('font-size')))
153), 154 plus = (position > .5) ? 1 : .5; 155 156 score = score - 1 + plus; 157 158 methods._fill.call(self, score); 159 160 if (self.opt.precision) { 161 score = score - plus + position; 162 } 163 164 methods._roundStars.call(self, score); 165 166 that.data('score', score); 167 } else { 168 methods._fill.call(self, score); 169 } 170 171 methods._target.call(self, score, evt); 172 173 if (self.opt.mouseover) { 174 self.opt.mouseover.call(self, score, evt); 175 } 176 }); 177 }, _callback: function(options) { 178 for (var i in options) { 179 if (typeof this.opt[options[i]] === 'function') { 180 this.opt[options[i]] = this.opt[options[i]].call(this); 181 } 182 } 183 }, _createCancel: function() { 184 var that = $(this), 185 icon = this.opt.cancelOff, 186 cancel = $('<em />', { 'class': icon, title: this.opt.cancelHint }); 187 188 if (this.opt.cancelPlace == 'left') { 189 that.prepend(' ').prepend(cancel); 190 } else { 191 that.append(' ').append(cancel); 192 } 193 194 return cancel; 195 }, _createScore: function() { 196 return $('<input />', { type: 'hidden', name: this.opt.scoreName }).appendTo(this); 197 }, _createStars: function() { 198 var that = $(this); 199 console.log("ok create"); 200 for (var i = 1; i <= this.opt.number; i++) { 201 var title = methods._getHint.call(this, i), 202 icon = (this.opt.score && this.opt.score >= i) ? 'starOn' : 'starOff'; 203 204 icon = this.opt[icon]; 205 206 $('<em />', { 'class' : icon, title: title, 'data-score': i }).appendTo(this); 207 208 if (this.opt.space) { 209 that.append((i < this.opt.number) ? ' ' : ''); 210 } 211 } 212 213 return that.children('em'); 214 }, _error: function(message) { 215 $(this).html(message); 216 217 $.error(message); 218 }, _fill: function(score) { 219 var self = this, 220 hash = 0; 221 222 for (var i = 1; i <= self.stars.length; i++) { 223 var star = self.stars.eq(i - 1), 224 select = self.opt.single ? (i == score) : (i <= score); 225 226 if (self.opt.iconRange && self.opt.iconRange.length > hash) { 227 var irange = self.opt.iconRange[hash], 228 on = irange.on || self.opt.starOn, 229 off = irange.off || self.opt.starOff, 230 icon = select ? on : off; 231 232 if (i <= irange.range) { 233 star.attr('class', icon); 234 } 235 236 if (i == irange.range) { 237 hash++; 238 } 239 } else { 240 var icon = select ? 'starOn' : 'starOff'; 241 242 star.attr('class', this.opt[icon]); 243 } 244 } 245 }, _getHint: function(score) { 246 var hint = this.opt.hints[score - 1]; 247 return (hint === '') ? '' : (hint || score); 248 }, _lock: function() { 249 var score = parseInt(this.score.val(), 10), // TODO: 3.1 >> [['1'], ['2'], ['3', '.1', '.2']] 250 hint = score ? methods._getHint.call(this, score) : this.opt.noRatedMsg; 251 252 $(this).data('readonly', true).css('cursor', '').attr('title', hint); 253 254 this.score.attr('readonly', 'readonly'); 255 this.stars.attr('title', hint); 256 257 if (this.cancel) { 258 this.cancel.hide(); 259 } 260 }, _roundStars: function(score) { 261 var rest = (score - Math.floor(score)).toFixed(2); 262 263 if (rest > this.opt.round.down) { 264 var icon = 'starOn'; // Up: [x.76 .. x.99] 265 266 if (this.opt.halfShow && rest < this.opt.round.up) { // Half: [x.26 .. x.75] 267 icon = 'starHalf'; 268 } else if (rest < this.opt.round.full) { // Down: [x.00 .. x.5] 269 icon = 'starOff'; 270 } 271 272 this.stars.eq(Math.ceil(score) - 1).attr('class', this.opt[icon]); 273 } // Full down: [x.00 .. x.25] 274 }, _target: function(score, evt) { 275 if (this.opt.target) { 276 var target = $(this.opt.target); 277 278 if (target.length === 0) { 279 methods._error.call(this, 'Target selector invalid or missing!'); 280 } 281 282 if (this.opt.targetFormat.indexOf('{score}') < 0) { 283 methods._error.call(this, 'Template "{score}" missing!'); 284 } 285 286 var mouseover = evt && evt.type == 'mouseover'; 287 288 if (score === undefined) { 289 score = this.opt.targetText; 290 } else if (score === null) { 291 score = mouseover ? this.opt.cancelHint : this.opt.targetText; 292 } else { 293 if (this.opt.targetType == 'hint') { 294 score = methods._getHint.call(this, Math.ceil(score)); 295 } else if (this.opt.precision) { 296 score = parseFloat(score).toFixed(1); 297 } 298 299 if (!mouseover && !this.opt.targetKeep) { 300 score = this.opt.targetText; 301 } 302 } 303 304 if (score) { 305 score = this.opt.targetFormat.toString().replace('{score}', score); 306 } 307 308 if (target.is(':input')) { 309 target.val(score); 310 } else { 311 target.html(score); 312 } 313 } 314 }, _unlock: function() { 315 $(this).data('readonly', false).css('cursor', 'pointer').removeAttr('title'); 316 317 this.score.removeAttr('readonly', 'readonly'); 318 319 for (var i = 0; i < this.opt.number; i++) { 320 this.stars.eq(i).attr('title', methods._getHint.call(this, i + 1)); 321 } 322 323 if (this.cancel) { 324 this.cancel.css('display', ''); 325 } 326 }, cancel: function(click) { 327 return this.each(function() { 328 if ($(this).data('readonly') !== true) { 329 methods[click ? 'click' : 'score'].call(this, null); 330 this.score.removeAttr('value'); 331 } 332 }); 333 }, click: function(score) { 334 return $(this).each(function() { 335 if ($(this).data('readonly') !== true) { 336 methods._apply.call(this, score); 337 338 if (!this.opt.click) { 339 methods._error.call(this, 'You must add the "click: function(score, evt) { }" callback.'); 340 } 341 342 343 this.opt.click.call(this, score, $.Event('click')); 344 345 methods._target.call(this, score); 346 } 347 }); 348 }, destroy: function() { 349 return $(this).each(function() { 350 var that = $(this), 351 raw = that.data('raw'); 352 353 if (raw) { 354 that.off('.raty').empty().css({ cursor: raw.style.cursor, width: raw.style.width }).removeData('readonly'); 355 } else { 356 that.data('raw', that.clone()[0]); 357 } 358 }); 359 }, getScore: function() { 360 var score = [], 361 value ; 362 363 $(this).each(function() { 364 value = this.score.val(); 365 366 score.push(value ? parseFloat(value) : undefined); 367 }); 368 369 return (score.length > 1) ? score : score[0]; 370 }, readOnly: function(readonly) { 371 return this.each(function() { 372 var that = $(this); 373 374 if (that.data('readonly') !== readonly) { 375 if (readonly) { 376 that.off('.raty').children('em').off('.raty'); 377 378 methods._lock.call(this); 379 } else { 380 methods._binds.call(this); 381 methods._unlock.call(this); 382 } 383 384 that.data('readonly', readonly); 385 } 386 }); 387 }, reload: function() { 388 return methods.set.call(this, {}); 389 }, score: function() { 390 return arguments.length ? methods.setScore.apply(this, arguments) : methods.getScore.call(this); 391 }, set: function(settings) { 392 return this.each(function() { 393 var that = $(this), 394 actual = that.data('settings'), 395 news = $.extend({}, actual, settings); 396 397 that.raty(news); 398 }); 399 }, setScore: function(score) { 400 return $(this).each(function() { 401 if ($(this).data('readonly') !== true) { 402 methods._apply.call(this, score); 403 methods._target.call(this, score); 404 } 405 }); 406 } 407 }; 408 409 $.fn.raty = function(method) { 410 if (methods[method]) { 411 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 412 } else if (typeof method === 'object' || !method) { 413 return methods.init.apply(this, arguments); 414 } else { 415 $.error('Method ' + method + ' does not exist!'); 416 } 417 }; 418 419 $.fn.raty.defaults = { 420 cancel : false, 421 cancelHint : 'Cancel this rating!', 422 cancelOff : 'fa fa-fw fa-minus-square', 423 cancelOn : 'fa fa-fw fa-check-square', 424 cancelPlace : 'right', 425 click : undefined, 426 half : false, 427 halfShow : true, 428 hints : ['1', '2', '3', '4', '5'], 429 iconRange : undefined, 430 mouseout : undefined, 431 mouseover : undefined, 432 noRatedMsg : 'Not rated yet!', 433 number : 5, 434 numberMax : 20, 435 precision : false, 436 readOnly : false, 437 round : { down: .25, full: .6, up: .76 }, 438 score : undefined, 439 scoreName : 'score', 440 single : false, 441 size : null, 442 space : true, 443 starHalf : 'fa fa-fw fa-star-half-o', 444 starOff : 'fa fa-fw fa-star-o', 445 starOn : 'fa fa-fw fa-star', 446 target : undefined, 447 targetFormat : '{score}', 448 targetKeep : false, 449 targetText : '', 450 targetType : 'hint', 451 width : false 452 }; 453 454})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.