PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/jquery.rating.js

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