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.

325 lines
9.6 KiB

  1. /* ******************************
  2. Semantic Module: Carousel
  3. Author: Jack Lukic
  4. Notes: First Commit May 28, 2013
  5. A carousel alternates between
  6. several pieces of content in sequence.
  7. ****************************** */
  8. ;(function ( $, window, document, undefined ) {
  9. $.fn.carousel = function(parameters) {
  10. var
  11. $allModules = $(this),
  12. settings = $.extend(true, {}, $.fn.carousel.settings, parameters),
  13. eventNamespace = '.' + settings.namespace,
  14. moduleNamespace = 'module-' + settings.namespace,
  15. moduleSelector = $allModules.selector || '',
  16. time = new Date().getTime(),
  17. performance = [],
  18. query = arguments[0],
  19. methodInvoked = (typeof query == 'string'),
  20. queryArguments = [].slice.call(arguments, 1),
  21. invokedResponse
  22. ;
  23. $allModules
  24. .each(function() {
  25. var
  26. $module = $(this),
  27. $arrows = $(settings.selector.arrows),
  28. $leftArrow = $(settings.selector.leftArrow),
  29. $rightArrow = $(settings.selector.rightArrow),
  30. $content = $(settings.selector.content),
  31. $navigation = $(settings.selector.navigation),
  32. $navItem = $(settings.selector.navItem),
  33. selector = $module.selector || '',
  34. element = this,
  35. instance = $module.data('module-' + settings.namespace),
  36. className = settings.className,
  37. namespace = settings.namespace,
  38. errors = settings.errors,
  39. module
  40. ;
  41. module = {
  42. initialize: function() {
  43. module.openingAnimation();
  44. module.marquee.autoAdvance();
  45. $leftArrow
  46. .on('click', module.marquee.left)
  47. ;
  48. $rightArrow
  49. .on('click', module.marquee.right)
  50. ;
  51. $navItem
  52. .on('click', module.marquee.change)
  53. ;
  54. },
  55. destroy: function() {
  56. module.verbose('Destroying previous module for', $module);
  57. $module
  58. .off(namespace)
  59. ;
  60. },
  61. left: function() {
  62. var
  63. $activeContent = $content.filter('.' + className.active),
  64. currentIndex = $content.index($activeContent),
  65. imageCount = $content.size(),
  66. newIndex = (currentIndex - 1 != -1)
  67. ? (currentIndex - 1)
  68. : (imageCount - 1)
  69. ;
  70. $navItem
  71. .eq(newIndex)
  72. .trigger('click')
  73. ;
  74. },
  75. right: function() {
  76. var
  77. $activeContent = $content.filter('.' + className.active),
  78. currentIndex = $content.index($activeContent),
  79. imageCount = $content.size(),
  80. newIndex = (currentIndex + 1 != imageCount)
  81. ? (currentIndex + 1)
  82. : 0
  83. ;
  84. $navItem
  85. .eq(newIndex)
  86. .trigger('click')
  87. ;
  88. },
  89. change: function() {
  90. var
  91. $selected = $(this),
  92. selectedIndex = $navItem.index($selected),
  93. $selectedImage = $content.eq(selectedIndex)
  94. ;
  95. module.marquee.autoAdvance();
  96. $selected
  97. .addClass('active')
  98. .siblings()
  99. .removeClass('active')
  100. ;
  101. $selectedImage
  102. .addClass('active animated fadeIn')
  103. .siblings('.' + className.active)
  104. .removeClass('animated fadeIn scaleIn')
  105. .animate({
  106. opacity: 0
  107. }, 500, function(){
  108. $(this)
  109. .removeClass('active')
  110. .removeAttr('style')
  111. ;
  112. })
  113. ;
  114. },
  115. autoAdvance: function() {
  116. clearInterval(module.timer);
  117. module.timer = setInterval(module.marquee.right, settings.duration);
  118. },
  119. setting: function(name, value) {
  120. if(value !== undefined) {
  121. if( $.isPlainObject(name) ) {
  122. module.verbose('Modifying settings object', name, value);
  123. $.extend(true, settings, name);
  124. }
  125. else {
  126. module.verbose('Modifying setting', name, value);
  127. settings[name] = value;
  128. }
  129. }
  130. else {
  131. return settings[name];
  132. }
  133. },
  134. internal: function(name, value) {
  135. if(value !== undefined) {
  136. if( $.isPlainObject(name) ) {
  137. module.verbose('Modifying internal property', name, value);
  138. $.extend(true, module, name);
  139. }
  140. else {
  141. module.verbose('Changing internal method to', value);
  142. module[name] = value;
  143. }
  144. }
  145. else {
  146. return module[name];
  147. }
  148. },
  149. debug: function() {
  150. if(settings.debug) {
  151. if(settings.performance) {
  152. module.performance.log(arguments);
  153. }
  154. else {
  155. module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  156. }
  157. }
  158. },
  159. verbose: function() {
  160. if(settings.verbose && settings.debug) {
  161. if(settings.performance) {
  162. module.performance.log(arguments);
  163. }
  164. else {
  165. module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  166. }
  167. }
  168. },
  169. error: function() {
  170. module.error = Function.prototype.bind.call(console.log, console, settings.moduleName + ':');
  171. },
  172. performance: {
  173. log: function(message) {
  174. var
  175. currentTime,
  176. executionTime,
  177. previousTime
  178. ;
  179. if(settings.performance) {
  180. currentTime = new Date().getTime();
  181. previousTime = time || currentTime,
  182. executionTime = currentTime - previousTime;
  183. time = currentTime;
  184. performance.push({
  185. 'Element' : element,
  186. 'Name' : message[0],
  187. 'Arguments' : message[1] || 'None',
  188. 'Execution Time' : executionTime
  189. });
  190. clearTimeout(module.performance.timer);
  191. module.performance.timer = setTimeout(module.performance.display, 100);
  192. }
  193. },
  194. display: function() {
  195. var
  196. title = settings.moduleName,
  197. caption = settings.moduleName + ': ' + moduleSelector + '(' + $allModules.size() + ' elements)',
  198. totalExecutionTime = 0
  199. ;
  200. if(moduleSelector) {
  201. title += ' Performance (' + moduleSelector + ')';
  202. }
  203. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  204. console.groupCollapsed(title);
  205. if(console.table) {
  206. $.each(performance, function(index, data) {
  207. totalExecutionTime += data['Execution Time'];
  208. });
  209. console.table(performance);
  210. }
  211. else {
  212. $.each(performance, function(index, data) {
  213. totalExecutionTime += data['Execution Time'];
  214. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  215. });
  216. }
  217. console.log('Total Execution Time:', totalExecutionTime +'ms');
  218. console.groupEnd();
  219. performance = [];
  220. time = false;
  221. }
  222. }
  223. },
  224. invoke: function(query, passedArguments, context) {
  225. var
  226. maxDepth,
  227. found
  228. ;
  229. passedArguments = passedArguments || queryArguments;
  230. context = element || context;
  231. if(typeof query == 'string' && instance !== undefined) {
  232. query = query.split('.');
  233. maxDepth = query.length - 1;
  234. $.each(query, function(depth, value) {
  235. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  236. instance = instance[value];
  237. return true;
  238. }
  239. else if( instance[value] !== undefined ) {
  240. found = instance[value];
  241. return true;
  242. }
  243. module.error(errors.method);
  244. return false;
  245. });
  246. }
  247. if ( $.isFunction( found ) ) {
  248. instance.verbose('Executing invoked function', found);
  249. return found.apply(context, passedArguments);
  250. }
  251. return found || false;
  252. }
  253. };
  254. if(methodInvoked) {
  255. if(instance === undefined) {
  256. module.initialize();
  257. }
  258. invokedResponse = module.invoke(query);
  259. }
  260. else {
  261. if(instance !== undefined) {
  262. module.destroy();
  263. }
  264. module.initialize();
  265. }
  266. })
  267. ;
  268. return (invokedResponse)
  269. ? invokedResponse
  270. : this
  271. ;
  272. };
  273. $.fn.carousel.settings = {
  274. moduleName : 'Carousel Module',
  275. namespace : 'carousel',
  276. verbose : true,
  277. debug : true,
  278. performance : true,
  279. // delegated event context
  280. duration: 5000,
  281. errors : {
  282. method : 'The method you called is not defined.'
  283. },
  284. selector : {
  285. arrows : '.arrow',
  286. leftArrow : '.left.arrow',
  287. rightArrow : '.right.arrow',
  288. content : '.content',
  289. navigation : '.navigation',
  290. navItem : '.navigation .icon'
  291. },
  292. className : {
  293. active : 'active'
  294. }
  295. };
  296. })( jQuery, window , document );