PageSourceSearch

https://lightnovelstranslations.com/wp-content/themes/light_novel/js/jquery.star-rating-svg.js

js lightnovelstranslations.com collected 2026-10-03 20:42:24 UTC 12,753 bytes, 341 lines download raw bytes

vendor: 4,412 bytes, lines 1-155
1/*
2 *  jQuery StarRatingSvg v1.2.0
3 *
4 *  http://github.com/nashio/star-rating-svg
5 *  Author: Ignacio Chavez
6 *  [email protected]
7 *  Licensed under MIT
8 */
9
10;(function ( $, window, document, undefined ) {
11
12  'use strict';
13
14  // Create the defaults once
15  var pluginName = 'starRating';
16  var noop = function(){};
17  var defaults = {
18    totalStars: 5,
19    useFullStars: false,
20    starShape: 'straight',
21    emptyColor: 'lightgray',
22    hoverColor: 'orange',
23    activeColor: 'gold',
24    ratedColor: 'crimson',
25    useGradient: true,
26    readOnly: false,
27    disableAfterRate: true,
28    baseUrl: false,
29    starGradient: {
30      start: '#FEF7CD',
31      end: '#FF9511'
32    },
33    strokeWidth: 4,
34    strokeColor: 'black',
35    initialRating: 0,
36    starSize: 40,
37    callback: noop,
38    onHover: noop,
39    onLeave: noop
40  };
41
42  // The actual plugin constructor
43  var Plugin = function( element, options ) {
44    var _rating;
45    var newRating;
46    var roundFn;
47
48    this.element = element;
49    this.$el = $(element);
50    this.settings = $.extend( {}, defaults, options );
51
52    // grab rating if defined on the element
53    _rating = this.$el.data('rating') || this.settings.initialRating;
54
55    // round to the nearest half
56    roundFn = this.settings.forceRoundUp ? Math.ceil : Math.round;
57    newRating = (roundFn( _rating * 2 ) / 2).toFixed(1);
58    this._state = {
59      rating: newRating
60    };
61
62    // create unique id for stars
63    this._uid = Math.floor( Math.random() * 999 );
64
65    // override gradient if not used
66    if( !options.starGradient && !this.settings.useGradient ){
67      this.settings.starGradient.start = this.settings.starGradient.end = this.settings.activeColor;
68    }
69
70    this._defaults = defaults;
71    this._name = pluginName;
72    this.init();
73  };
74
75  var methods = {
76    init: function () {
77      this.renderMarkup();
78      this.addListeners();
79      this.initRating();
80    },
81
82    addListeners: function(){
83      if( this.settings.readOnly ){ return; }
84      this.$stars.on('mouseover', this.hoverRating.bind(this));
85      this.$stars.on('mouseout', this.restoreState.bind(this));
86      this.$stars.on('click', this.handleRating.bind(this));
87    },
88
89    // apply styles to hovered stars
90    hoverRating: function(e){
91      var index = this.getIndex(e);
92      this.paintStars(index, 'hovered');
93      this.settings.onHover(index + 1, this._state.rating, this.$el);
94    },
95
96    // clicked on a rate, apply style and state
97    handleRating: function(e){
98      var index = this.getIndex(e);
99      var rating = index + 1;
100
101      this.applyRating(rating, this.$el);
102      this.executeCallback( rating, this.$el );
103
104      if(this.settings.disableAfterRate){
105        this.$stars.off();
106      }
107    },
108
109    applyRating: function(rating){
110      var index = rating - 1;
111      // paint selected and remove hovered color
112      this.paintStars(index, 'rated');
113      this._state.rating = index + 1;
114      this._state.rated = true;
115    },
116
117    restoreState: function(e){
118      var index = this.getIndex(e);
119      var rating = this._state.rating || -1;
120      // determine star color depending on manually rated
121      var colorType = this._state.rated ? 'rated' : 'active';
122      this.paintStars(rating - 1, colorType);
123      this.settings.onLeave(index + 1, this._state.rating, this.$el);
124    },
125
126    getIndex: function(e){
127      var $target = $(e.currentTarget);
128      var width = $target.width();
129      var side = $(e.target).attr('data-side');
130      var minRating = this.settings.minRating;
131
132      // hovered outside the star, calculate by pixel instead
133      side = (!side) ? this.getOffsetByPixel(e, $target, width) : side;
134      side = (this.settings.useFullStars) ? 'right' : side ;
135
136      // get index for half or whole star
137      var index = $target.index() - ((side === 'left') ? 0.5 : 0);
138
139      // pointer is way to the left, rating should be none
140      index = ( index < 0.5 && (e.offsetX < width / 4) ) ? -1 : index;
141
142      // force minimum rating
143      index = ( minRating && minRating <= this.settings.totalStars && index < minRating ) ? minRating - 1 : index;
144      return index;
145    },
146
147    getOffsetByPixel: function(e, $target, width){
148      var leftX = e.pageX - $target.offset().left;
149      return ( leftX <= (width / 2) && !this.settings.useFullStars) ? 'left' : 'right';
150    },
151
152    initRating: function(){
153      this.paintStars(this._state.rating - 1, 'active');
154    },
155
156    paintStars: function(endIndex, stateClass){
157      var $polygonLeft;
158      var $polygonRight;
159      var leftClass;
160      var rightClass;
161      var s = this.settings;
162
163      $.each(this.$stars, function(index, star){
164        $polygonLeft = $(star).find('[data-side="left"]');
165        $polygonRight = $(star).find('[data-side="right"]');
166        leftClass = rightClass = (index <= endIndex) ? stateClass : 'empty';
167
168        // has another half rating, add half star
169        leftClass = ( index - endIndex === 0.5 ) ? stateClass : leftClass;
170
171        $polygonLeft.attr('class', 'svg-'  + leftClass + '-' + this._uid);
172        $polygonRight.attr('class', 'svg-'  + rightClass + '-' + this._uid);
173
174        // get color for level
175        var ratedColorsIndex = endIndex >= 0 ? Math.ceil(endIndex) : 0;
176
177        var ratedColor;
178        if (s.ratedColors && s.ratedColors.length && s.ratedColors[ratedColorsIndex]) {
179          ratedColor = s.ratedColors[ratedColorsIndex];
180        } else {
181          ratedColor = this._defaults.ratedColor;
182        }
183
184        // only override colors in rated stars and when rated number is valid
185        if (stateClass === 'rated' && endIndex > -1) {
186          // limit to painting only to rated stars, and specific case for half star
187          if (index <= Math.ceil(endIndex) || (index < 1 && endIndex < 0)) {
188            $polygonLeft.attr('style', 'fill:'+ratedColor);
189          }
190          if (index <= endIndex) {
191            $polygonRight.attr('style', 'fill:'+ratedColor);
192          }
193        }
194      }.bind(this));
195    },
196
197    renderMarkup: function () {
198      var s = this.settings;
199      var baseUrl = s.baseUrl ? location.href.split('#')[0] : '';
200
201      // inject an svg manually to have control over attributes
202      var star = '<div class="jq-star" style="width:' + s.starSize+ 'px;  height:' + s.starSize + 'px;"><svg version="1.0" class="jq-star-svg" shape-rendering="geometricPrecision" xmlns="http://www.w3.org/2000/svg" ' + this.getSvgDimensions(s.starShape) +  ' stroke-width:' + s.strokeWidth + 'px;" xml:space="preserve"><style type="text/css">.svg-empty-' + this._uid + '{fill:url(' + baseUrl + '#' + this._uid + '_SVGID_1_)!important;}.svg-hovered-' + this._uid + '{fill:url(' + baseUrl + '#' + this._uid + '_SVGID_2_)!important;}.svg-active-' + this._uid + '{fill:url(' + baseUrl + '#' + this._uid + '_SVGID_3_)!important;}.svg-rated-' + this._uid + '{fill:' + s.ratedColor + '!important;}</style>' +
203
204      this.getLinearGradient(this._uid + '_SVGID_1_', s.emptyColor, s.emptyColor, s.starShape) +
205      this.getLinearGradient(this._uid + '_SVGID_2_', s.hoverColor, s.hoverColor, s.starShape) +
206      this.getLinearGradient(this._uid + '_SVGID_3_', s.starGradient.start, s.starGradient.end, s.starShape) +
207      this.getVectorPath(this._uid, {
208        starShape: s.starShape,
209        strokeWidth: s.strokeWidth,
210        strokeColor: s.strokeColor
211      } ) +
212      '</svg></div>';
213
214      // inject svg markup
215      var starsMarkup = '';
216      for( var i = 0; i < s.totalStars; i++){
217        starsMarkup += star;
218      }
219      this.$el.append(starsMarkup);
220      this.$stars = this.$el.find('.jq-star');
221    },
222
223    getVectorPath: function(id, attrs){
224      return (attrs.starShape === 'rounded') ?
225        this.getRoundedVectorPath(id, attrs) : this.getSpikeVectorPath(id, attrs);
226    },
227
228    getSpikeVectorPath: function(id, attrs){
229      return '<polygon data-side="center" class="svg-empty-' + id + '" points="281.1,129.8 364,55.7 255.5,46.8 214,-59 172.5,46.8 64,55.4 146.8,129.7 121.1,241 212.9,181.1 213.9,181 306.5,241 " style="fill: transparent; stroke: ' + attrs.strokeColor + ';" />' +
230        '<polygon data-side="left" class="svg-empty-' + id + '" points="281.1,129.8 364,55.7 255.5,46.8 214,-59 172.5,46.8 64,55.4 146.8,129.7 121.1,241 213.9,181.1 213.9,181 306.5,241 " style="stroke-opacity: 0;" />' +
231          '<polygon data-side="right" class="svg-empty-' + id + '" points="364,55.7 255.5,46.8 214,-59 213.9,181 306.5,241 281.1,129.8 " style="stroke-opacity: 0;" />';
232    },
233
234    getRoundedVectorPath: function(id, attrs){
235      var fullPoints = 'M520.9,336.5c-3.8-11.8-14.2-20.5-26.5-22.2l-140.9-20.5l-63-127.7 c-5.5-11.2-16.8-18.2-29.3-18.2c-12.5,0-23.8,7-29.3,18.2l-63,127.7L28,314.2C15.7,316,5.4,324.7,1.6,336.5S1,361.3,9.9,370 l102,99.4l-24,140.3c-2.1,12.3,2.9,24.6,13,32c5.7,4.2,12.4,6.2,19.2,6.2c5.2,0,10.5-1.2,15.2-3.8l126-66.3l126,66.2 c4.8,2.6,10,3.8,15.2,3.8c6.8,0,13.5-2.1,19.2-6.2c10.1-7.3,15.1-19.7,13-32l-24-140.3l102-99.4 C521.6,361.3,524.8,348.3,520.9,336.5z';
236
237      return '<path data-side="center" class="svg-empty-' + id + '" d="' + fullPoints + '" style="stroke: ' + attrs.strokeColor + '; fill: transparent; " /><path data-side="right" class="svg-empty-' + id + '" d="' + fullPoints + '" style="stroke-opacity: 0;" /><path data-side="left" class="svg-empty-' + id + '" d="M121,648c-7.3,0-14.1-2.2-19.8-6.4c-10.4-7.6-15.6-20.3-13.4-33l24-139.9l-101.6-99 c-9.1-8.9-12.4-22.4-8.6-34.5c3.9-12.1,14.6-21.1,27.2-23l140.4-20.4L232,164.6c5.7-11.6,17.3-18.8,30.2-16.8c0.6,0,1,0.4,1,1 v430.1c0,0.4-0.2,0.7-0.5,0.9l-126,66.3C132,646.6,126.6,648,121,648z" style="stroke: ' + attrs.strokeColor + '; stroke-opacity: 0;" />';
238    },
239
240    getSvgDimensions: function(starShape){
241      return (starShape === 'rounded') ? 'width="550px" height="500.2px" viewBox="0 146.8 550 500.2" style="enable-background:new 0 0 550 500.2;' : 'x="0px" y="0px" width="305px" height="305px" viewBox="60 -62 309 309" style="enable-background:new 64 -59 305 305;';
242    },
243
244    getLinearGradient: function(id, startColor, endColor, starShape){
245      var height = (starShape === 'rounded') ? 500 : 250;
246      return '<linearGradient id="' + id + '" gradientUnits="userSpaceOnUse" x1="0" y1="-50" x2="0" y2="' + height + '"><stop  offset="0" style="stop-color:' + startColor + '"/><stop  offset="1" style="stop-color:' + endColor + '"/> </linearGradient>';
247    },
248
249    executeCallback: function(rating, $el){
250      var callback = this.settings.callback;
251      callback(rating, $el);
252    }
253
254  };
255
256  var publicMethods = {
257
258    unload: function() {
259      var _name = 'plugin_' + pluginName;
260      var $el = $(this);
261      var $starSet = $el.data(_name).$stars;
262      $starSet.off();
263      $el.removeData(_name).remove();
264    },
265
266    setRating: function(rating, round) {
267      var _name = 'plugin_' + pluginName;
268      var $el = $(this);
269      var $plugin = $el.data(_name);
270      if( rating > $plugin.settings.totalStars || rating < 0 ) { return; }
271      if( round ){
272        rating = Math.round(rating);
273      }
274      $plugin.applyRating(rating);
275    },
276
277    getRating: function() {
278      var _name = 'plugin_' + pluginName;
279      var $el = $(this);
280      var $starSet = $el.data(_name);
281      return $starSet._state.rating;
282    },
283
284    resize: function(newSize) {
285      var _name = 'plugin_' + pluginName;
286      var $el = $(this);
287      var $starSet = $el.data(_name);
288      var $stars = $starSet.$stars;
289
290      if(newSize <= 1 || newSize > 200) {
291        console.error('star size out of bounds');
292        return;
293      }
294
295      $stars = Array.prototype.slice.call($stars);
296      $stars.forEach(function(star){
297        $(star).css({
298          'width': newSize + 'px',
299          'height': newSize + 'px'
300        });
301      });
302    },
303
304    setReadOnly: function(flag) {
305      var _name = 'plugin_' + pluginName;
306      var $el = $(this);
307      var $plugin = $el.data(_name);
308      if(flag === true){
309        $plugin.$stars.off('mouseover mouseout click');
310      } else {
311        $plugin.settings.readOnly = false;
312        $plugin.addListeners();
313      }
314    }
315
316  };
317
318
319  // Avoid Plugin.prototype conflicts
320  $.extend(Plugin.prototype, methods);
321
322  $.fn[ pluginName ] = function ( options ) {
323
324    // if options is a public method
325    if( !$.isPlainObject(options) ){
326      if( publicMethods.hasOwnProperty(options) ){
327        return publicMethods[options].apply(this, Array.prototype.slice.call(arguments, 1));
328      } else {
329        $.error('Method '+ options +' does not exist on ' + pluginName + '.js');
330      }
331    }
332
333    return this.each(function() {
334      // preventing against multiple instantiations
335      if ( !$.data( this, 'plugin_' + pluginName ) ) {
336        $.data( this, 'plugin_' + pluginName, new Plugin( this, options ) );
337      }
338    });
339  };
340
341})( jQuery, window, document );

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.