PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BARN/Server/barn/public/javascripts/jquery/ui/jquery.rating.js

http://xbat-devel.googlecode.com/
JavaScript | 240 lines | 108 code | 37 blank | 95 comment | 32 complexity | 9573d03d315f03ddfdc0a6032c80f3ea MD5 | raw file
Possible License(s): AGPL-1.0, AGPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, BSD-2-Clause, LGPL-2.0
  1. /*
  2. ### jQuery Star Rating Plugin v2.61 - 2009-01-23 ###
  3. * http://www.fyneworks.com/ - diego@fyneworks.com
  4. * Dual licensed under the MIT and GPL licenses:
  5. * http://www.opensource.org/licenses/mit-license.php
  6. * http://www.gnu.org/licenses/gpl.html
  7. ###
  8. Project: http://plugins.jquery.com/project/MultipleFriendlyStarRating
  9. Website: http://www.fyneworks.com/jquery/star-rating/
  10. *//*
  11. Based on http://www.phpletter.com/Demo/Jquery-Star-Rating-Plugin/
  12. Original comments:
  13. This is hacked version of star rating created by <a href="http://php.scripts.psu.edu/rja171/widgets/rating.php">Ritesh Agrawal</a>
  14. It thansform a set of radio type input elements to star rating type and remain the radio element name and value,
  15. so could be integrated with your form. It acts as a normal radio button.
  16. modified by : Logan Cai (cailongqun[at]yahoo.com.cn)
  17. */
  18. /*# AVOID COLLISIONS #*/
  19. ;if(window.jQuery) (function($){
  20. /*# AVOID COLLISIONS #*/
  21. // IE6 Background Image Fix
  22. if ($.browser.msie) try { document.execCommand("BackgroundImageCache", false, true)} catch(e) { }
  23. // Thanks to http://www.visualjquery.com/rating/rating_redux.html
  24. // default settings
  25. $.rating = {
  26. cancel: 'Cancel Rating', // advisory title for the 'cancel' link
  27. cancelValue: '', // value to submit when user click the 'cancel' link
  28. split: 0, // split the star into how many parts?
  29. // Width of star image in case the plugin can't work it out. This can happen if
  30. // the jQuery.dimensions plugin is not available OR the image is hidden at installation
  31. starWidth: 16,
  32. //NB.: These don't need to be defined (can be undefined/null) so let's save some code!
  33. //half: false, // just a shortcut to settings.split = 2
  34. //required: false, // disables the 'cancel' button so user can only select one of the specified values
  35. //readOnly: false, // disable rating plugin interaction/ values cannot be changed
  36. //focus: function(){}, // executed when stars are focused
  37. //blur: function(){}, // executed when stars are focused
  38. //callback: function(){}, // executed when a star is clicked
  39. // required properties:
  40. initialized: {},
  41. groups: {},// allows multiple star ratings on one page
  42. event: {// plugin event handlers
  43. fill: function(n, el, settings, state){ // fill to the current mouse position.
  44. this.drain(n);
  45. $(el).prevAll('.star_group_' + n).andSelf().addClass('star_' + (state || 'hover'));
  46. // focus handler, as requested by focusdigital.co.uk
  47. var lnk = $(el).children('a'); val = lnk.text();
  48. if(settings.focus) settings.focus.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
  49. },
  50. drain: function(n, el, settings) { // drain all the stars.
  51. $.rating.groups[n].valueElem.siblings('.star_group_'+n).removeClass('star_on').removeClass('star_hover');
  52. },
  53. reset: function(n, el, settings){ // Reset the stars to the default index.
  54. if(!$($.rating.groups[n].current).is('.cancel'))
  55. $($.rating.groups[n].current).prevAll('.star_group_'+n).andSelf().addClass('star_on');
  56. // blur handler, as requested by focusdigital.co.uk
  57. var lnk = $(el).children('a'); val = lnk.text();
  58. if(settings.blur) settings.blur.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
  59. },
  60. click: function(n, el, settings){ // Selected a star or cancelled
  61. $.rating.groups[n].current = el;
  62. var lnk = $(el).children('a'); val = lnk.text();
  63. // Set value
  64. $.rating.groups[n].valueElem.val(val);
  65. // Update display
  66. $.rating.event.drain(n, el, settings);
  67. $.rating.event.reset(n, el, settings);
  68. // click callback, as requested here: http://plugins.jquery.com/node/1655
  69. if(settings.callback) settings.callback.apply($.rating.groups[n].valueElem[0], [val, lnk[0]]);
  70. }
  71. }// plugin events
  72. };
  73. $.fn.rating = function(instanceSettings){
  74. if(this.length==0) return this; // quick fail
  75. instanceSettings = $.extend(
  76. {}/* new object */,
  77. $.rating/* global settings */,
  78. instanceSettings || {} /* just-in-time settings */
  79. );
  80. // loop through each matched element
  81. this.each(function(i){
  82. var settings = $.extend(
  83. {}/* new object */,
  84. instanceSettings || {} /* current call settings */,
  85. ($.metadata? $(this).metadata(): ($.meta?$(this).data():null)) || {} /* metadata settings */
  86. );
  87. // Generate internal control ID
  88. // - ignore square brackets in element names
  89. var n = (this.name || 'unnamed-rating').replace(/\[|\]+/g, "_");
  90. // Grouping
  91. if(!$.rating.groups[n]){
  92. $.rating.groups[n] = {count: 0};
  93. }
  94. i = $.rating.groups[n].count; $.rating.groups[n].count++;
  95. // Accept readOnly setting from 'disabled' property
  96. $.rating.groups[n].readOnly = $.rating.groups[n].readOnly || settings.readOnly || $(this).attr('disabled');
  97. // Things to do with the first element...
  98. if(i == 0){
  99. // Create value element (disabled if readOnly)
  100. $.rating.groups[n].valueElem = $('<input type="hidden" name="' + n + '" value=""' + (settings.readOnly ? ' disabled="disabled"' : '') + '/>');
  101. // Insert value element into form
  102. $(this).before($.rating.groups[n].valueElem);
  103. if($.rating.groups[n].readOnly || settings.required){
  104. // DO NOT display 'cancel' button
  105. }
  106. else{
  107. // Display 'cancel' button
  108. $(this).before(
  109. $('<div class="cancel"><a title="' + settings.cancel + '">' + settings.cancelValue + '</a></div>')
  110. .mouseover(function(){ $.rating.event.drain(n, this, settings); $(this).addClass('star_on'); })
  111. .mouseout(function(){ $.rating.event.reset(n, this, settings); $(this).removeClass('star_on'); })
  112. .click(function(){ $.rating.event.click(n, this, settings); })
  113. );
  114. }
  115. }; // if (i == 0) (first element)
  116. // insert rating option right after preview element
  117. eStar = $('<div class="star"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
  118. $(this).after(eStar);
  119. // Half-stars?
  120. if(settings.half) settings.split = 2;
  121. // Prepare division settings
  122. if(typeof settings.split=='number' && settings.split>0){
  123. var stw = ($.fn.width ? $(eStar).width() : 0) || settings.starWidth;
  124. var spi = (i % settings.split), spw = Math.floor(stw/settings.split);
  125. $(eStar)
  126. // restrict star's width and hide overflow (already in CSS)
  127. .width(spw)
  128. // move the star left by using a negative margin
  129. // this is work-around to IE's stupid box model (position:relative doesn't work)
  130. .find('a').css({ 'margin-left':'-'+ (spi*spw) +'px' })
  131. };
  132. // Remember group name so controls within the same container don't get mixed up
  133. $(eStar).addClass('star_group_'+n);
  134. // readOnly?
  135. if($.rating.groups[n].readOnly){
  136. // Mark star as readOnly so user can customize display
  137. $(eStar).addClass('star_readonly');
  138. }
  139. else{
  140. $(eStar)
  141. // Enable hover css effects
  142. .addClass('star_live')
  143. // Attach mouse events
  144. .mouseover(function(){ $.rating.event.drain(n, this, settings); $.rating.event.fill(n, this, settings, 'hover'); })
  145. .mouseout(function(){ $.rating.event.drain(n, this, settings); $.rating.event.reset(n, this, settings); })
  146. .click(function(){ $.rating.event.click(n, this, settings); });
  147. };
  148. ////if(window.console) console.log(['###', n, this.checked, $.rating.groups[n].initial]);
  149. if(this.checked) $.rating.groups[n].current = eStar;
  150. //remove this checkbox
  151. $(this).remove();
  152. // reset display if last element
  153. if(i + 1 == this.length) $.rating.event.reset(n, this, settings);
  154. }); // each element
  155. // initialize groups...
  156. /*
  157. This is some seriously shitty code.
  158. What the original appeared to do was to loop through the groups to initialize this stars.
  159. Only problem was that it was trying to initialize every group, every time this is called...
  160. furthermore, it was not keeping track of groups that had already been initialized!
  161. So 2 additions were required to make this code work:
  162. 1)
  163. "names" in the _ratings partial need to be unique, thus they are given a timestamp.
  164. This is because the ratings() jQuery function groups the inputs that need to be processed by this name.
  165. If a rating is rendered again, say by ajax opening and closing of a pane, elements are *appended* to the group since they have the same name.
  166. There may be another way of dealing with this for memories sake - like checking for an exisiting group, but short of rewriting this, I cant be bothered.
  167. In any case, the way that groups are managed needs some very serious work.
  168. 2)
  169. Once we have initialized a group, we mark it as initialized and do not visit it again.
  170. again, this could be obviated by an improved group management function
  171. */
  172. // Why the fuck is this being called for every single star element???
  173. // I have no fucking clue since it appears we are outside the loop.
  174. // What the would appear to mean is that we do not need this routine above --> this.each(function(i){
  175. for(n in $.rating.groups){
  176. // Have we initialized this group already? If so, on to the next element
  177. if($.rating.initialized[n]) continue;
  178. // WHY?? why did you have to write it like this?
  179. (function(c, v, n){ if(!c) return;
  180. $.rating.event.fill(n, c, instanceSettings || {}, 'on');
  181. $(v).val($(c).children('a').text());
  182. })
  183. ($.rating.groups[n].current, $.rating.groups[n].valueElem, n);
  184. };
  185. // THIS ASSUMES A 5 STAR RATING!!!!
  186. if($.rating.groups[n].count % 5 == 0){
  187. $.rating.initialized[n] = true;
  188. }
  189. return this; // don't break the chain...
  190. };
  191. /*
  192. ### Default implementation ###
  193. The plugin will attach itself to file inputs
  194. with the class 'multi' when the page loads
  195. */
  196. $(function(){ $('input[type=radio].star').rating(); });
  197. /*# AVOID COLLISIONS #*/
  198. })(jQuery);
  199. /*# AVOID COLLISIONS #*/