PageSourceSearch

https://www.admissionpremium.com/scripts/raty-fa-0.1.2/lib/jquery.raty-fa.js

js admissionpremium.com collected 2026-09-24 10:02:39 UTC 13,595 bytes, 452 lines download raw bytes

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