Permalink
Browse files

Code style change, change how the module is layed out in code

  • Loading branch information...
1 parent 0b96e0f commit 561758cbebc93751d5eb3377c417f5cba7b8c5f0 @bryanjswift committed Mar 8, 2013
Showing with 108 additions and 105 deletions.
  1. +108 −105 js/modules/carousel.js
View
@@ -1,123 +1,126 @@
-define(['underscore', 'modules/clazz', 'modules/addEvent', 'modules/data-attrs'], function(_, clazz, addEvent, data) {
- var root = false;
- var nav = document.querySelector('.carousel-nav');
- var items = [];
- var jumps = [];
- var jumpsEl = document.querySelector('.carousel-jumps');
- var nextEl = false;
- var previousEl = false;
- var count = items.count;
+define(
+ ['underscore', 'modules/clazz', 'modules/addEvent', 'modules/data-attrs'],
+ function(_, clazz, addEvent, data) {
+ var root = false;
+ var nav = document.querySelector('.carousel-nav');
+ var items = [];
+ var jumps = [];
+ var jumpsEl = document.querySelector('.carousel-jumps');
+ var nextEl = false;
+ var previousEl = false;
+ var count = items.count;
- var autoTimeout = 0;
- var currentIdx = 0;
- var isAuto = false;
+ var autoTimeout = 0;
+ var currentIdx = 0;
+ var isAuto = false;
- function auto() {
- handler('next', {});
- if (isAuto) {
- autoTimeout = _.delay(auto, 5000);
+ function auto() {
+ handler('next', {});
+ if (isAuto) {
+ autoTimeout = _.delay(auto, 5000);
+ }
}
- }
- function createButton(type) {
- var button = document.querySelector('.carousel-button-' + type);
- addEvent(button, 'click', _.bind(handler, button, type));
- return button;
- }
+ function createButton(type) {
+ var button = document.querySelector('.carousel-button-' + type);
+ addEvent(button, 'click', _.bind(handler, button, type));
+ return button;
+ }
- function handler(type, e) {
- var idx = currentIdx;
- if (type === 'next') {
- idx = idx + 1;
- } else if (type === 'previous') {
- idx = idx - 1;
+ function handler(type, e) {
+ var idx = currentIdx;
+ if (type === 'next') {
+ idx = idx + 1;
+ } else if (type === 'previous') {
+ idx = idx - 1;
+ }
+ if (idx === jumps.length) {
+ idx = 0;
+ } else if (idx < 0) {
+ idx = jumps.length - 1;
+ }
+ selectItem.apply(jumps[idx], [idx, e]);
}
- if (idx === jumps.length) {
- idx = 0;
- } else if (idx < 0) {
- idx = jumps.length - 1;
+
+ function initialize(el, opts) {
+ root = el;
+ opts = opts || {};
+ items = root.querySelectorAll('.carousel-item');
+ count = items.length;
+ jumps = opts.jumps || [];
+ if (count > 1) {
+ _.each(items, initItem);
+ initButtons();
+ selectItem.apply(jumps[0], [0, {}]);
+ addEvent(root, 'click', stopAuto);
+ if (isAuto) { autoTimeout = _.delay(auto, 5000); }
+ clazz.add(root, 'carousel-navved');
+ }
}
- selectItem.apply(jumps[idx], [idx, e]);
- }
- function initialize(el, opts) {
- root = el;
- opts = opts || {};
- items = root.querySelectorAll('.carousel-item');
- count = items.length;
- jumps = opts.jumps || [];
- if (count > 1) {
- _.each(items, initItem);
- initButtons();
- selectItem.apply(jumps[0], [0, {}]);
- addEvent(root, 'click', stopAuto);
- if (isAuto) { autoTimeout = _.delay(auto, 5000); }
- clazz.add(root, 'carousel-navved');
+ function initButtons() {
+ nextEl = createButton('next');
+ previousEl = createButton('previous');
}
- }
- function initButtons() {
- nextEl = createButton('next');
- previousEl = createButton('previous');
- }
+ function createJumpLink(i) {
+ var li = document.createElement('li');
+ var link = document.createElement('a');
+ link.href = '#item-' + i;
+ link.innerHTML = (i + 1) + '';
+ li.appendChild(link);
+ jumpsEl.appendChild(li);
+ jumps.push(link);
+ return link;
+ }
- function createJumpLink(i) {
- var li = document.createElement('li');
- var link = document.createElement('a');
- link.href = '#item-' + i;
- link.innerHTML = (i + 1) + '';
- li.appendChild(link);
- jumpsEl.appendChild(li);
- jumps.push(link);
- return link;
- }
+ function initItem(item, i) {
+ var link = jumps[i] || createJumpLink(i);
+ clazz.add(link, 'carousel-jump');
+ if (i === (count - 1)) { clazz.add(link, 'carousel-jump-last'); }
+ addEvent(link, 'click', _.bind(selectItem, link, i));
+ }
- function initItem(item, i) {
- var link = jumps[i] || createJumpLink(i);
- clazz.add(link, 'carousel-jump');
- if (i === (count - 1)) { clazz.add(link, 'carousel-jump-last'); }
- addEvent(link, 'click', _.bind(selectItem, link, i));
- }
+ function selectItem(idx, e) {
+ var prev = idx - 1;
+ var next = idx + 1;
+ if (prev < 0) { prev = count - 1; }
+ if (next >= count) { next = 0; }
+ if (e && e.preventDefault) { e.preventDefault(); }
+ e.returnValue = false;
- function selectItem(idx, e) {
- var prev = idx - 1;
- var next = idx + 1;
- if (prev < 0) { prev = count - 1; }
- if (next >= count) { next = 0; }
- if (e && e.preventDefault) { e.preventDefault(); }
- e.returnValue = false;
+ clearTimeout(autoTimeout);
+ if ('undefined' !== typeof e.type) {
+ // If they interact with it, stop auto rotating
+ isAuto = false;
+ }
- clearTimeout(autoTimeout);
- if ('undefined' !== typeof e.type) {
- // If they interact with it, stop auto rotating
- isAuto = false;
- }
+ _.each(items, function(item, i) {
+ if (idx > i) {
+ clazz.remove(item, 'carousel-active');
+ clazz.remove(item, 'carousel-next');
+ clazz.add(item, 'carousel-prev');
+ } else if (idx === i) {
+ clazz.add(item, 'carousel-active');
+ clazz.remove(item, 'carousel-next');
+ clazz.remove(item, 'carousel-prev');
+ } else {
+ clazz.remove(item, 'carousel-active');
+ clazz.add(item, 'carousel-next');
+ clazz.remove(item, 'carousel-prev');
+ }
+ });
- _.each(items, function(item, i) {
- if (idx > i) {
- clazz.remove(item, 'carousel-active');
- clazz.remove(item, 'carousel-next');
- clazz.add(item, 'carousel-prev');
- } else if (idx === i) {
- clazz.add(item, 'carousel-active');
- clazz.remove(item, 'carousel-next');
- clazz.remove(item, 'carousel-prev');
- } else {
- clazz.remove(item, 'carousel-active');
- clazz.add(item, 'carousel-next');
- clazz.remove(item, 'carousel-prev');
- }
- });
+ _.each(jumps, function(link) { clazz.remove(link, 'carousel-jump-active'); });
+ clazz.add(this, 'carousel-jump-active');
+ currentIdx = idx;
+ }
- _.each(jumps, function(link) { clazz.remove(link, 'carousel-jump-active'); });
- clazz.add(this, 'carousel-jump-active');
- currentIdx = idx;
- }
+ function stopAuto(e) {
+ clearTimeout(autoTimeout);
+ isAuto = false;
+ }
- function stopAuto(e) {
- clearTimeout(autoTimeout);
- isAuto = false;
+ return initialize;
}
-
- return initialize;
-});
+);

0 comments on commit 561758c

Please sign in to comment.