PageSourceSearch

https://www.zamnesia.com/modules/gsnippetsreviews/js/jquery.star-rating.js?560f8f09da050aa5b6497d2740097f16

js zamnesia.com collected 2026-09-24 18:31:56 UTC 13,881 bytes, 391 lines download raw bytes

1/*
2 * jQuery Star Rating Plugin v3.14 - 2012-01-26 ###
3 * Home: http://www.fyneworks.com/jquery/star-rating/
4 * Code: http://code.google.com/p/jquery-star-rating-plugin/
5 *
6 * Dual licensed under the MIT and GPL licenses:
7 *   http://www.opensource.org/licenses/mit-license.php
8 *   http://www.gnu.org/licenses/gpl.html
9*/
10
11/*# AVOID COLLISIONS #*/
12;if(window.jQuery) (function($)
13{
14/*# AVOID COLLISIONS #*/
15	// IE6 Background Image Fix
16	if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) try { document.execCommand("BackgroundImageCache", false, true)} catch(e) { };
17	// Thanks to http://www.visualjquery.com/rating/rating_redux.html
18	
19	// plugin initialization
20	jQuery.fn.rating = function(options)
21	{
22		/*
23		 *  Default Settings ###
24		 *  eg.: You can override default control like this:
25		 *  $.fn.rating.options.cancel = 'Clear';
26		 */
27		var defaults = 
28		{
29			ratingField : null,
30			starGif : 'star.gif', // split the star into how many parts
31			split: 0, // split the star into how many parts
32			
33			// Width of star image in case the plugin can't work it out. This can happen if
34			// the jQuery.dimensions plugin is not available OR the image is hidden at installation
35			starWidth: 5,
36			
37			//NB.: These don't need to be pre-defined (can be undefined/null) so let's save some code!
38			half:     false, // just a shortcut to control.split = 2
39			readOnly: false  // disable rating plugin interaction/ values cannot be changed
40		};
41		
42		if(this.length==0) return this; // quick fail
43		
44		// Handle API methods
45		if(typeof arguments[0]=='string') {
46			// Perform API methods on individual elements
47			if(this.length>1) {
48				var args = arguments;
49				return this.each(function()
50					{
51						$.fn.rating.apply($(this), args);
52					}
53				);
54			};
55			// Invoke API method handler
56			jQuery.fn.rating[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
57			// Quick exit...
58			return this;
59		};
60		
61		// Initialize options for this call
62		jQuery.fn.rating.opts = $.extend(
63			defaults,
64			options
65		);
66		
67		// Allow multiple controls with the same name by making each call unique
68		jQuery.fn.rating.calls++;
69		
70		// loop through each matched element
71		this.not('.star-rating-applied').addClass('star-rating-applied').each(function()
72		{	
73			// Load control parameters / find context / etc
74			var control, input = jQuery(this);
75			var eid = (this.name || 'unnamed-rating').replace(/\[|\]/g, '_').replace(/^\_+|\_+$/g,'');
76			var context = jQuery(this.form || document.body);
77			
78			// FIX: http://code.google.com/p/jquery-star-rating-plugin/issues/detail?id=23
79			var raters = context.data('rating');
80			if(!raters || raters.call!=jQuery.fn.rating.calls) raters = { count:0, call:jQuery.fn.rating.calls };
81			var rater = raters[eid];
82			
83			// if rater is available, verify that the control still exists
84			if (rater) control = rater.data('rating');
85			
86			if (rater && control) {// save a byte!
87				// add star to control if rater is available and the same control still exists
88				control.count++;
89			}// save a byte!
90			else {
91				// create new control if first star or control element was removed/replaced
92				
93				// Initialize options for this rater
94				control = $.extend(
95					jQuery.fn.rating.opts,
96					(jQuery.metadata? input.metadata(): (jQuery.meta?input.data():null)) || {},
97					{ count:0, stars: [], inputs: [] }
98				);
99				
100				// increment number of rating controls
101				control.serial = raters.count++;
102				
103				// create rating element
104				rater = jQuery('<span class="star-rating-control"/>');
105				input.before(rater);
106				
107				// Mark element for initialization (once all stars are ready)
108				rater.addClass('rating-to-be-drawn');
109				
110				// Accept readOnly setting from 'disabled' property
111				if(input.attr('disabled') || input.hasClass('disabled')) control.readOnly = true;
112				
113				// Accept required setting from class property (class='required')
114				if(input.hasClass('required')) control.required = true;
115				
116				// Create 'cancel' button
117				rater.append(
118					control.cancel = jQuery('')
119					.mouseover(function(){
120						jQuery(this).rating('drain');
121						jQuery(this).addClass('star-rating-hover');
122						//$(this).rating('focus');
123					})
124					.mouseout(function(){
125						jQuery(this).rating('draw');
126						jQuery(this).removeClass('star-rating-hover');
127						//$(this).rating('blur');
128					})
129					.click(function(){
130						jQuery(this).rating('select');
131					})
132					.data('rating', control)
133				);
134			}; // first element of group
135			
136			// insert rating star
137			var star = jQuery('<div class="star-rating rater-'+ control.serial +'"><a title="' + (this.title || this.value) + '">&nbsp;</a></div>');
138			rater.append(star);
139			star.find('a').css({'background-image' : 'url(' + jQuery.fn.rating.opts.starGif + ')', 'background-repeat' : 'no-repeat', 'background-position' : '0 -1px'});
140			
141			// inherit attributes from input element
142			if (this.id) star.attr('id', this.id);
143			if (this.className) star.addClass(this.className);
144			
145			// Half-stars?
146			if (control.half) control.split = 2;
147			
148			// Prepare division control
149			if (typeof control.split=='number' && control.split>0){
150				var stw = (jQuery.fn.width ? star.width() : 0) || control.starWidth;
151				var spi = (control.count % control.split);
152				var spw = Math.floor(stw/control.split);
153				//fix very small width
154				if (spw < 8) {
155					var altstw = 17;
156					spw = Math.floor(altstw/control.split);
157				}
158				// restrict star's width and hide overflow (already in CSS)
159				// move the star left by using a negative margin
160				// this is work-around to IE's stupid box model (position:relative doesn't work)
161				star.width(spw).find('a').css({ 'margin-left':'-'+ (spi*spw) +'px' });		
162			}
163			
164			// readOnly?
165			if (control.readOnly) { //save a byte!
166				// Mark star as readOnly so user can customize display
167				star.addClass('star-rating-readonly');
168			}  //save a byte!
169			else { //save a byte!
170				// Enable hover css effects
171				star.addClass('star-rating-live')
172				// Attach mouse events
173				.mouseover(function(){
174					jQuery(this).rating('fill');
175					jQuery(this).rating('focus');
176				})
177				.mouseout(function(){
178					jQuery(this).rating('draw');
179					jQuery(this).rating('blur');
180				})
181				.click(function(){
182					jQuery(this).rating('select');
183				});
184			} //save a byte!
185			
186			// set current selection
187			if (this.checked)	control.current = star;
188			
189			// set current select for links
190			if (this.nodeName=="A"){
191			    if(jQuery(this).hasClass('selected'))
192			    	control.current = star;
193	   		};
194			
195			// hide input element
196			input.hide();
197		
198			// backward compatibility, form element to plugin
199			input.change(function(){
200				jQuery(this).rating('select');
201			});
202		
203			// attach reference to star to input element and vice-versa
204			star.data('rating.input', input.data('rating.star', star));
205			
206			// store control information in form (or body when form not available)
207			control.stars[control.stars.length] = star[0];
208			control.inputs[control.inputs.length] = input[0];
209			control.rater = raters[eid] = rater;
210			control.context = context;
211			
212			input.data('rating', control);
213			rater.data('rating', control);
214			star.data('rating', control);
215			context.data('rating', raters);
216		}); 
217		// each element
218		// Initialize ratings (first draw)
219		jQuery('.rating-to-be-drawn').rating('draw').removeClass('rating-to-be-drawn');
220		
221		return this; // don't break the chain...
222	};
223	// fn.rating
224	
225	/*
226	 * Core functionality and API ###
227	 */
228	jQuery.extend(jQuery.fn.rating,
229	{
230		// Used to append a unique serial number to internal control ID
231		// each time the plugin is invoked so same name controls can co-exist
232		calls: 0,
233		
234		focus: function()
235		{
236			var control = this.data('rating'); if(!control) return this;
237			if(!control.focus) return this; // quick fail if not required
238			// find data for event
239			var input = jQuery(this).data('rating.input') || jQuery( this.tagName=='INPUT' ? this : null );
240			// focus handler, as requested by focusdigital.co.uk
241			if(control.focus) control.focus.apply(input[0], [input.val(), jQuery('a', input.data('rating.star'))[0]]);
242		}, // $.fn.rating.focus
243		
244		blur: function()
245		{
246			var control = this.data('rating'); if(!control) return this;
247			if(!control.blur) return this; // quick fail if not required
248			// find data for event
249			var input = jQuery(this).data('rating.input') || jQuery( this.tagName=='INPUT' ? this : null );
250			// blur handler, as requested by focusdigital.co.uk
251			if(control.blur) control.blur.apply(input[0], [input.val(), jQuery('a', input.data('rating.star'))[0]]);
252		}, // $.fn.rating.blur
253		
254		fill: function()
255		{ 
256			// fill to the current mouse position.
257			var control = this.data('rating'); if(!control) return this;
258			// do not execute when control is in read-only mode
259			if(control.readOnly) return;
260			// Reset all stars and highlight them up to this element
261			this.rating('drain');
262			if (typeof $.fn.andSelf == 'function') {
263				this.prevAll().andSelf().filter('.rater-' + control.serial).addClass('star-rating-hover');
264			} else {
265				this.prevAll().addBack().filter('.rater-' + control.serial).addClass('star-rating-hover');
266			}
267		},// $.fn.rating.fill
268		
269		drain: function()
270		{ 
271			// drain all the stars.
272			var control = this.data('rating'); if(!control) return this;
273			// do not execute when control is in read-only mode
274			if(control.readOnly) return;
275			// Reset all stars
276			control.rater.children().filter('.rater-'+ control.serial).removeClass('star-rating-on').removeClass('star-rating-hover');
277		},// $.fn.rating.drain
278		
279		draw: function()
280		{ 
281			// set value and stars to reflect current selection
282			var control = this.data('rating'); if(!control) return this;
283			// Clear all stars
284			this.rating('drain');
285			// Set control value
286			if(control.current){
287				control.current.data('rating.input').attr('checked','checked');
288				if (typeof $.fn.andSelf == 'function') {
289					control.current.prevAll().andSelf().filter('.rater-'+ control.serial).addClass('star-rating-on');
290				} else {
291					control.current.prevAll().addBack().filter('.rater-'+ control.serial).addClass('star-rating-on');
292				}
293			}
294			else
295			 $(control.inputs).removeAttr('checked');
296			// Show/hide 'cancel' button
297//			control.cancel[control.readOnly || control.required?'hide':'show']();
298			// Add/remove read-only classes to remove hand pointer
299			this.siblings()[control.readOnly?'addClass':'removeClass']('star-rating-readonly');
300		},// $.fn.rating.draw
301
302		select: function(value,wantCallBack)
303		{ 
304			// select a value	
305			// ***** MODIFICATION *****
306			// Thanks to faivre.thomas - http://code.google.com/p/jquery-star-rating-plugin/issues/detail?id=27
307			//
308			// ***** LIST OF MODIFICATION *****
309			// ***** added Parameter wantCallBack : false if you don't want a callback. true or undefined if you want postback to be performed at the end of this method'
310			// ***** recursive calls to this method were like : ... .rating('select') it's now like .rating('select',undefined,wantCallBack); (parameters are set.)
311			// ***** line which is calling callback
312			// ***** /LIST OF MODIFICATION *****
313			
314			var control = this.data('rating'); if(!control) return this;
315			// do not execute when control is in read-only mode
316			if(control.readOnly) return;
317			// clear selection
318			control.current = null;
319			// programmatically (based on user input)
320			if (typeof value!='undefined'){
321				// select by index (0 based)
322				if (typeof value=='number')
323					return jQuery(control.stars[value]).rating('select',undefined,wantCallBack);
324				// select by literal value (must be passed as a string
325				if (typeof value=='string')
326					//return
327					$.each(control.stars, function(){
328						if(jQuery(this).data('rating.input').val()==value) jQuery(this).rating('select',undefined,wantCallBack);
329					});
330			}
331			else {
332				control.current = this[0].tagName=='INPUT' ? this.data('rating.star') : (this.is('.rater-'+ control.serial) ? this 
332: null);
333			}
334			// Update rating control state
335			this.data('rating', control);
336			// Update display
337			this.rating('draw');
338			// find data for event
339			var input = jQuery( control.current ? control.current.data('rating.input') : null );
340			// set note inf hidden field
341			if (jQuery.fn.rating.opts.ratingField != null) {
342				jQuery('#' + jQuery.fn.rating.opts.ratingField).val(input.val());
343			}
344			// click callback, as requested here: http://plugins.jquery.com/node/1655
345			if((wantCallBack ||wantCallBack == undefined) && control.callback) control.callback.apply(input[0], [input.val(), jQuery('a', control.current)[0]]);// callback event
346			//to ensure retro-compatibility, wantCallBack must be considered as true by default
347			// **** /MODIFICATION *****			
348		},// $.fn.rating.select
349
350		readOnly: function(toggle, disable)
351		{ 
352			// make the control read-only (still submits value)
353			var control = this.data('rating'); if(!control) return this;
354			// setread-only status
355			control.readOnly = toggle || toggle==undefined ? true : false;
356			// enable/disable control value submission
357			if(disable) jQuery(control.inputs).attr("disabled", "disabled");
358			else     			jQuery(control.inputs).removeAttr("disabled");
359			// Update rating control state
360			this.data('rating', control);
361			// Update display
362			this.rating('draw');
363		},// $.fn.rating.readOnly
364		
365		disable: function()
366		{
367			// make read-only and never submit value
368			this.rating('readOnly', true, true);
369		},// $.fn.rating.disable
370		
371		enable: function()
372		{ 
373			// make read/write and submit value
374			this.rating('readOnly', false, false);
375		}// $.fn.rating.select
376		
377	}); 
378	//fn.extends.rating
379
380	/*
381	 * Default implementation ###
382	 * The plugin will attach itself to file inputs
383	 * with the class 'multi' when the page loads
384	 */
385	$(function()
386	{
387//		$('input[type=radio].star').rating();
388	});
389/*# AVOID COLLISIONS #*/
390})(jQuery);
391/*# AVOID COLLISIONS #*/

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.