Skip to content

Instantly share code, notes, and snippets.

@mattlo
Created January 24, 2013 18:20
Show Gist options
  • Save mattlo/4626007 to your computer and use it in GitHub Desktop.
Save mattlo/4626007 to your computer and use it in GitHub Desktop.
/**
* @class IndexController
* @abstract
* Used primarily for index tracking on slideshows and slideshow-like components
*/
NS('GT.src').IndexController = Class.extend(function () {
'use strict';
return {
/**
* @constructor
*/
init: function () {
// default is on 0
this.index = 0;
this.lastIndex = 0;
this.maxIndex = 0;
this.isInfinite = false;
},
/**
* Proceeds to next index value going forward, invokes `setIndex`
* Does nothing if conditions aren't met
* @return {undefined}
*/
next: function () {
if (this.index < this.maxIndex) {
this.setIndex(this.index + 1);
} else if (this.isInfinite === true) {
this.setIndex(0);
}
},
/**
* Proceeds to next index value going backwards, invokes `setIndex`
* Does nothing if conditions aren't met
* @return {undefined}
*/
back: function () {
if (this.index > 0) {
this.setIndex(this.index - 1);
} else if (this.isInfinite === true) {
this.setIndex(this.maxIndex);
}
},
/**
* Sets index, defines previous index, invokes `action`
*/
setIndex: function (index) {
// set a last index for point of references in `action`
this.lastIndex = this.index;
// defined new index
this.index = index;
// invoke action which may have animation
this.action();
},
/**
* Return current index
* @return {number}
*/
getIndex: function () {
return this.index;
},
/**
* Stub method, overridable method
*/
action: function () {},
/**
* Set max index
* @param {number} number
* @return {undefined}
*/
setMaxIndex: function (number) {
this.maxIndex = number;
},
/**
* Set max index
* @param {number} number
* @return {undefined}
*/
getMaxIndex: function (number) {
return this.maxIndex;
},
/**
* Enables/Disables infinite next/back methods
* @param {boolean} isInfinite
* @return {undefined}
*/
setInfinity: function (isInfinite) {
this.isInfinite = isInfinite;
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment