You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.9 KiB

  1. /* ******************************
  2. Star Review
  3. Author: Jack Lukic
  4. Notes: First Commit Sep 04, 2012
  5. Simple rating module
  6. ****************************** */
  7. ;(function ($, window, document, undefined) {
  8. $.fn.starReview = function(parameters) {
  9. var
  10. settings = $.extend(true, {}, $.fn.starReview.settings, parameters),
  11. // hoist arguments
  12. moduleArguments = arguments || false
  13. ;
  14. $(this)
  15. .each(function() {
  16. var
  17. $module = $(this),
  18. $star = $module.find(settings.selector.star),
  19. className = settings.className,
  20. namespace = settings.namespace,
  21. instance = $module.data('module'),
  22. module
  23. ;
  24. module = {
  25. settings: settings,
  26. initialize: function() {
  27. if(settings.rateable) {
  28. // expandable with states
  29. if($.fn.state !== undefined) {
  30. $module
  31. .state()
  32. ;
  33. $star
  34. .state()
  35. ;
  36. }
  37. $star
  38. .bind('mouseenter.' + namespace, module.event.mouseenter)
  39. .bind('mouseleave.' + namespace, module.event.mouseleave)
  40. .bind('click.' + namespace, module.event.click)
  41. ;
  42. }
  43. $module
  44. .addClass(className.initialize)
  45. .data('module', module)
  46. ;
  47. },
  48. setRating: function(rating) {
  49. var
  50. $activeStar = $star.eq(rating - 1)
  51. ;
  52. $module
  53. .removeClass(className.hover)
  54. ;
  55. $star
  56. .removeClass(className.hover)
  57. ;
  58. $activeStar
  59. .nextAll()
  60. .removeClass(className.active)
  61. ;
  62. $activeStar
  63. .addClass(className.active)
  64. .prevAll()
  65. .addClass(className.active)
  66. ;
  67. $.proxy(settings.onRate, $module)();
  68. },
  69. event: {
  70. mouseenter: function() {
  71. var
  72. $activeStar = $(this)
  73. ;
  74. $activeStar
  75. .nextAll()
  76. .removeClass(className.hover)
  77. ;
  78. $module
  79. .addClass(className.hover)
  80. ;
  81. $activeStar
  82. .addClass(className.hover)
  83. .prevAll()
  84. .addClass(className.hover)
  85. ;
  86. },
  87. mouseleave: function() {
  88. $star
  89. .removeClass(className.hover)
  90. ;
  91. },
  92. click: function() {
  93. var
  94. $activeStar = $(this)
  95. ;
  96. module.setRating( $star.index($activeStar) + 1);
  97. }
  98. },
  99. // handle error logging
  100. error: function(errorMessage) {
  101. console.warn(settings.moduleName + ': ' + errorMessage);
  102. },
  103. // allows for dot notation method calls
  104. invoke: function(methodName, context, methodArguments) {
  105. var
  106. method
  107. ;
  108. methodArguments = methodArguments || Array.prototype.slice.call( arguments, 2 );
  109. if(typeof methodName == 'string' && instance !== undefined) {
  110. methodName = methodName.split('.');
  111. $.each(methodName, function(index, name) {
  112. if( $.isPlainObject( instance[name] ) ) {
  113. instance = instance[name];
  114. return true;
  115. }
  116. else if( $.isFunction( instance[name] ) ) {
  117. method = instance[name];
  118. return true;
  119. }
  120. module.error(settings.errors.method);
  121. return false;
  122. });
  123. }
  124. return ( $.isFunction( method ) )
  125. ? method.apply(context, methodArguments)
  126. : false
  127. ;
  128. }
  129. };
  130. if(instance !== undefined && moduleArguments) {
  131. // simpler than invoke realizing to invoke itself (and losing scope due prototype.call()
  132. if(moduleArguments[0] == 'invoke') {
  133. moduleArguments = Array.prototype.slice.call( moduleArguments, 1 );
  134. }
  135. return module.invoke(moduleArguments[0], this, Array.prototype.slice.call( moduleArguments, 1 ) );
  136. }
  137. // initializing
  138. module.initialize();
  139. })
  140. ;
  141. return this;
  142. };
  143. $.fn.starReview.settings = {
  144. moduleName : 'Star Module',
  145. namespace : 'star',
  146. rateable : true,
  147. onRate : function(){},
  148. className : {
  149. initialize : 'initialize',
  150. loading : 'loading',
  151. active : 'active',
  152. hover : 'hover',
  153. down : 'down'
  154. },
  155. selector : {
  156. star : 'i'
  157. }
  158. };
  159. })( jQuery, window , document );