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.

428 lines
12 KiB

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