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.

409 lines
11 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * # Semantic - Rating
  3. * http://github.com/jlukic/semantic-ui/
  4. *
  5. *
  6. * Copyright 2014 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ($, window, document, undefined) {
  12. module.exports = function(parameters) {
  13. var _module = module;
  14. var
  15. $allModules = $(this),
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. query = arguments[0],
  20. methodInvoked = (typeof query == 'string'),
  21. queryArguments = [].slice.call(arguments, 1),
  22. returnedValue
  23. ;
  24. $allModules
  25. .each(function() {
  26. var
  27. settings = ( $.isPlainObject(parameters) )
  28. ? $.extend(true, {}, _module.exports.settings, parameters)
  29. : $.extend({}, _module.exports.settings),
  30. namespace = settings.namespace,
  31. className = settings.className,
  32. metadata = settings.metadata,
  33. selector = settings.selector,
  34. error = settings.error,
  35. eventNamespace = '.' + namespace,
  36. moduleNamespace = 'module-' + namespace,
  37. element = this,
  38. instance = $(this).data(moduleNamespace),
  39. $module = $(this),
  40. $icon = $module.find(selector.icon),
  41. module
  42. ;
  43. module = {
  44. initialize: function() {
  45. module.verbose('Initializing rating module', settings);
  46. if(settings.interactive) {
  47. module.enable();
  48. }
  49. else {
  50. module.disable();
  51. }
  52. if(settings.initialRating) {
  53. module.debug('Setting initial rating');
  54. module.setRating(settings.initialRating);
  55. }
  56. if( $module.data(metadata.rating) ) {
  57. module.debug('Rating found in metadata');
  58. module.setRating( $module.data(metadata.rating) );
  59. }
  60. module.instantiate();
  61. },
  62. instantiate: function() {
  63. module.verbose('Instantiating module', settings);
  64. instance = module;
  65. $module
  66. .data(moduleNamespace, module)
  67. ;
  68. },
  69. destroy: function() {
  70. module.verbose('Destroying previous instance', instance);
  71. $module
  72. .removeData(moduleNamespace)
  73. ;
  74. $icon
  75. .off(eventNamespace)
  76. ;
  77. },
  78. event: {
  79. mouseenter: function() {
  80. var
  81. $activeIcon = $(this)
  82. ;
  83. $activeIcon
  84. .nextAll()
  85. .removeClass(className.hover)
  86. ;
  87. $module
  88. .addClass(className.hover)
  89. ;
  90. $activeIcon
  91. .addClass(className.hover)
  92. .prevAll()
  93. .addClass(className.hover)
  94. ;
  95. },
  96. mouseleave: function() {
  97. $module
  98. .removeClass(className.hover)
  99. ;
  100. $icon
  101. .removeClass(className.hover)
  102. ;
  103. },
  104. click: function() {
  105. var
  106. $activeIcon = $(this),
  107. currentRating = module.getRating(),
  108. rating = $icon.index($activeIcon) + 1
  109. ;
  110. if(settings.clearable && currentRating == rating) {
  111. module.clearRating();
  112. }
  113. else {
  114. module.setRating( rating );
  115. }
  116. }
  117. },
  118. clearRating: function() {
  119. module.debug('Clearing current rating');
  120. module.setRating(0);
  121. },
  122. getRating: function() {
  123. var
  124. currentRating = $icon.filter('.' + className.active).size()
  125. ;
  126. module.verbose('Current rating retrieved', currentRating);
  127. return currentRating;
  128. },
  129. enable: function() {
  130. module.debug('Setting rating to interactive mode');
  131. $icon
  132. .on('mouseenter' + eventNamespace, module.event.mouseenter)
  133. .on('mouseleave' + eventNamespace, module.event.mouseleave)
  134. .on('click' + eventNamespace, module.event.click)
  135. ;
  136. $module
  137. .removeClass(className.disabled)
  138. ;
  139. },
  140. disable: function() {
  141. module.debug('Setting rating to read-only mode');
  142. $icon
  143. .off(eventNamespace)
  144. ;
  145. $module
  146. .addClass(className.disabled)
  147. ;
  148. },
  149. setRating: function(rating) {
  150. var
  151. ratingIndex = (rating - 1 >= 0)
  152. ? (rating - 1)
  153. : 0,
  154. $activeIcon = $icon.eq(ratingIndex)
  155. ;
  156. $module
  157. .removeClass(className.hover)
  158. ;
  159. $icon
  160. .removeClass(className.hover)
  161. .removeClass(className.active)
  162. ;
  163. if(rating > 0) {
  164. module.verbose('Setting current rating to', rating);
  165. $activeIcon
  166. .addClass(className.active)
  167. .prevAll()
  168. .addClass(className.active)
  169. ;
  170. }
  171. $.proxy(settings.onRate, element)(rating);
  172. },
  173. setting: function(name, value) {
  174. if( $.isPlainObject(name) ) {
  175. $.extend(true, settings, name);
  176. }
  177. else if(value !== undefined) {
  178. settings[name] = value;
  179. }
  180. else {
  181. return settings[name];
  182. }
  183. },
  184. internal: function(name, value) {
  185. if( $.isPlainObject(name) ) {
  186. $.extend(true, module, name);
  187. }
  188. else if(value !== undefined) {
  189. module[name] = value;
  190. }
  191. else {
  192. return module[name];
  193. }
  194. },
  195. debug: function() {
  196. if(settings.debug) {
  197. if(settings.performance) {
  198. module.performance.log(arguments);
  199. }
  200. else {
  201. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  202. module.debug.apply(console, arguments);
  203. }
  204. }
  205. },
  206. verbose: function() {
  207. if(settings.verbose && settings.debug) {
  208. if(settings.performance) {
  209. module.performance.log(arguments);
  210. }
  211. else {
  212. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  213. module.verbose.apply(console, arguments);
  214. }
  215. }
  216. },
  217. error: function() {
  218. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  219. module.error.apply(console, arguments);
  220. },
  221. performance: {
  222. log: function(message) {
  223. var
  224. currentTime,
  225. executionTime,
  226. previousTime
  227. ;
  228. if(settings.performance) {
  229. currentTime = new Date().getTime();
  230. previousTime = time || currentTime;
  231. executionTime = currentTime - previousTime;
  232. time = currentTime;
  233. performance.push({
  234. 'Element' : element,
  235. 'Name' : message[0],
  236. 'Arguments' : [].slice.call(message, 1) || '',
  237. 'Execution Time' : executionTime
  238. });
  239. }
  240. clearTimeout(module.performance.timer);
  241. module.performance.timer = setTimeout(module.performance.display, 100);
  242. },
  243. display: function() {
  244. var
  245. title = settings.name + ':',
  246. totalTime = 0
  247. ;
  248. time = false;
  249. clearTimeout(module.performance.timer);
  250. $.each(performance, function(index, data) {
  251. totalTime += data['Execution Time'];
  252. });
  253. title += ' ' + totalTime + 'ms';
  254. if(moduleSelector) {
  255. title += ' \'' + moduleSelector + '\'';
  256. }
  257. if($allModules.size() > 1) {
  258. title += ' ' + '(' + $allModules.size() + ')';
  259. }
  260. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  261. console.groupCollapsed(title);
  262. if(console.table) {
  263. console.table(performance);
  264. }
  265. else {
  266. $.each(performance, function(index, data) {
  267. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  268. });
  269. }
  270. console.groupEnd();
  271. }
  272. performance = [];
  273. }
  274. },
  275. invoke: function(query, passedArguments, context) {
  276. var
  277. object = instance,
  278. maxDepth,
  279. found,
  280. response
  281. ;
  282. passedArguments = passedArguments || queryArguments;
  283. context = element || context;
  284. if(typeof query == 'string' && object !== undefined) {
  285. query = query.split(/[\. ]/);
  286. maxDepth = query.length - 1;
  287. $.each(query, function(depth, value) {
  288. var camelCaseValue = (depth != maxDepth)
  289. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  290. : query
  291. ;
  292. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  293. object = object[camelCaseValue];
  294. }
  295. else if( object[camelCaseValue] !== undefined ) {
  296. found = object[camelCaseValue];
  297. return false;
  298. }
  299. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  300. object = object[value];
  301. }
  302. else if( object[value] !== undefined ) {
  303. found = object[value];
  304. return false;
  305. }
  306. else {
  307. return false;
  308. }
  309. });
  310. }
  311. if ( $.isFunction( found ) ) {
  312. response = found.apply(context, passedArguments);
  313. }
  314. else if(found !== undefined) {
  315. response = found;
  316. }
  317. if($.isArray(returnedValue)) {
  318. returnedValue.push(response);
  319. }
  320. else if(returnedValue !== undefined) {
  321. returnedValue = [returnedValue, response];
  322. }
  323. else if(response !== undefined) {
  324. returnedValue = response;
  325. }
  326. return found;
  327. }
  328. };
  329. if(methodInvoked) {
  330. if(instance === undefined) {
  331. module.initialize();
  332. }
  333. module.invoke(query);
  334. }
  335. else {
  336. if(instance !== undefined) {
  337. module.destroy();
  338. }
  339. module.initialize();
  340. }
  341. })
  342. ;
  343. return (returnedValue !== undefined)
  344. ? returnedValue
  345. : this
  346. ;
  347. };
  348. module.exports.settings = {
  349. name : 'Rating',
  350. namespace : 'rating',
  351. verbose : true,
  352. debug : false,
  353. performance : true,
  354. initialRating : 0,
  355. interactive : true,
  356. clearable : false,
  357. onRate : function(rating){},
  358. error : {
  359. method : 'The method you called is not defined'
  360. },
  361. metadata: {
  362. rating: 'rating'
  363. },
  364. className : {
  365. active : 'active',
  366. disabled : 'disabled',
  367. hover : 'hover',
  368. loading : 'loading'
  369. },
  370. selector : {
  371. icon : '.icon'
  372. }
  373. };
  374. })( require("jquery"), window , document );