SlideshowView.js
Summary
No overview generated for 'SlideshowView.js'
const TKSlideshowViewNumberOfElements = 'slideshowViewNumberOfElements';
const TKSlideshowViewElementAtIndex = 'slideshowViewElementAtIndex';
const TKSlideshowViewDidShowElementAtIndex = 'slideshowViewDidFocusElementAtIndex';
const TKSlideshowViewDidHideElementAtIndex = 'slideshowViewDidHideElementAtIndex';
const TKSlideshowViewCSSContainerClass = 'slideshow-view';
const TKSlideshowViewCSSElementClass = 'slideshow-view-element';
TKSlideshowView.synthetizes = ['dataSource',
'delegate',
'activeElementIndex',
'duration',
'fadeDuration',
'numberOfElements'];
function TKSlideshowView (element) {
this.callSuper();
this._activeElementIndex = null;
this._duration = 3000;
this._fadeDuration = 1000;
this._playing = false;
if (element) {
this.container = element;
} else {
this.container = document.createElement("div");
}
this.container.addClassName(TKSlideshowViewCSSContainerClass);
this.currentElement = null;
}
TKSlideshowView.prototype.init = function () {
if (!this.dataSource ||
!TKUtils.objectHasMethod(this.dataSource, TKSlideshowViewNumberOfElements) ||
!TKUtils.objectHasMethod(this.dataSource, TKSlideshowViewElementAtIndex)) {
return;
}
this.showElement();
};
TKSlideshowView.prototype.setActiveElementIndex = function (newActiveElementIndex) {
if (newActiveElementIndex >= 0 &&
newActiveElementIndex < this.numberOfElements &&
newActiveElementIndex != this._activeElementIndex) {
if (this._activeElementIndex === null) {
this._activeElementIndex = newActiveElementIndex;
return;
}
if (TKUtils.objectHasMethod(this.delegate, TKSlideshowViewDidHideElementAtIndex)) {
this.delegate[TKSlideshowViewDidHideElementAtIndex](this, this._activeElementIndex);
}
this._activeElementIndex = newActiveElementIndex;
this.showElement();
}
};
TKSlideshowView.prototype.getNumberOfElements = function () {
this._numberOfElements = this.dataSource[TKSlideshowViewNumberOfElements](this);
return this._numberOfElements;
};
TKSlideshowView.prototype.advance = function () {
if (this._playing) {
if (this.activeElementIndex < this.numberOfElements - 1) {
this.activeElementIndex++;
} else {
this.activeElementIndex = 0;
}
var _this = this;
setTimeout(function() {
_this.advance();
}, this._duration);
}
};
TKSlideshowView.prototype.play = function () {
if (!this._playing) {
this._playing = true;
var _this = this;
setTimeout(function() {
_this.advance();
}, this._duration);
}
};
TKSlideshowView.prototype.pause = function () {
this._playing = false;
};
TKSlideshowView.prototype.reset = function () {
this._playing = false;
this.currentElement = null;
};
TKSlideshowView.prototype.showElement = function () {
var oldElement = this.currentElement;
var el = this.dataSource[TKSlideshowViewElementAtIndex](this, this._activeElementIndex);
el.addClassName(TKSlideshowViewCSSElementClass);
el.style.webkitTransitionProperty = "opacity";
el.style.webkitTransitionDuration = this._fadeDuration + "ms";
el.style.opacity = 0;
if (oldElement) {
this.container.insertBefore(el, oldElement);
} else {
this.container.appendChild(el);
}
if (TKUtils.objectHasMethod(this.delegate, TKSlideshowViewDidShowElementAtIndex)) {
this.delegate[TKSlideshowViewDidShowElementAtIndex](this, this._activeElementIndex);
}
this.currentElement = el;
var _this = this;
setTimeout(function() {
el.style.opacity = 1;
if (oldElement) {
oldElement.style.pointerEvents = "none";
oldElement.style.opacity = 0;
setTimeout(function() {
if (_this.container && _this.container.hasChild(oldElement)) {
_this.container.removeChild(oldElement);
}
}, (_this.fadeDuration + 10));
}
}, 0);
};
TKClass(TKSlideshowView);
function TKSlideshowViewDataSourceHelper(data) {
this.data = data;
};
TKSlideshowViewDataSourceHelper.prototype.slideshowViewNumberOfElements = function(view) {
if (this.data) {
return this.data.length;
} else {
return 0;
}
};
TKSlideshowViewDataSourceHelper.prototype.slideshowViewElementAtIndex = function(view, index) {
if (!this.data || index >= this.data.length) {
return null;
}
var source = this.data[index];
var element = TKUtils.buildElement(source);
return element;
};
TKSlideshowView.buildSlideshowView = function(element, data) {
if (TKUtils.objectIsUndefined(data) || !data || data.type != "TKSlideshowView") {
return null;
}
var slideshowView = new TKSlideshowView(element);
if (!TKUtils.objectIsUndefined(data.elements)) {
slideshowView.dataSource = new TKSlideshowViewDataSourceHelper(data.elements);
}
TKSlideshowView.synthetizes.forEach(function(prop) {
if (prop != "dataSource" && prop != "delegate") {
if (!TKUtils.objectIsUndefined(data[prop])) {
slideshowView[prop] = data[prop];
}
}
});
return slideshowView;
};
Documentation generated by
JSDoc on Tue Sep 15 21:24:36 2009