|
@@ -16,7 +16,8 @@ utils = {};
|
|
*
|
|
*
|
|
* @param object object the object to iterate through
|
|
* @param object object the object to iterate through
|
|
* @param function callback function to call on every item; the key is passed
|
|
* @param function callback function to call on every item; the key is passed
|
|
- * as first, the value as second parameter
|
|
|
|
|
|
+ * as first, the value as second parameter; the callback may return FALSE
|
|
|
|
+ * to stop the iteration
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
utils.forEach = function (object, callback) {
|
|
utils.forEach = function (object, callback) {
|
|
@@ -31,76 +32,55 @@ utils.forEach = function (object, callback) {
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Slides a element up (i.e. hide a element by changing its height to 0)
|
|
|
|
|
|
+ * Checks whether the client's browser is able to slide elements or not
|
|
*
|
|
*
|
|
- * @param HTMLElement element the element to slide up
|
|
|
|
- * @param function finishCallback function to call when the animation has
|
|
|
|
- * been finished (i.e. the element is hidden)
|
|
|
|
- * @param function startCallback function to call when the animation starts
|
|
|
|
- * @return void
|
|
|
|
|
|
+ * @return boolean TRUE when the browser supports sliding, FALSE otherwise
|
|
*/
|
|
*/
|
|
-utils.slideUp = function (element, finishCallback, startCallback) {
|
|
|
|
- utils.slideOut(element, {
|
|
|
|
- cssRule: 'height',
|
|
|
|
- cssRuleRef: 'clientHeight',
|
|
|
|
- cssClass: 'up',
|
|
|
|
- startCallback: startCallback,
|
|
|
|
- finishCallback: finishCallback
|
|
|
|
- });
|
|
|
|
|
|
+utils.canSlide = function () {
|
|
|
|
+ return (Modernizr.classlist && Modernizr.requestanimationframe && Modernizr.csstransitions);
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Slides a element down (i.e. show a hidden element)
|
|
|
|
|
|
+ * Slides a element up (i.e. hide a element by changing its height to 0px)
|
|
*
|
|
*
|
|
- * @param HTMLElement element the element to slide down
|
|
|
|
|
|
+ * @param HTMLElement element the element to slide up
|
|
* @param function finishCallback function to call when the animation has
|
|
* @param function finishCallback function to call when the animation has
|
|
- * been finished (i.e. the element is visible)
|
|
|
|
|
|
+ * been finished (i.e. the element is hidden)
|
|
* @param function startCallback function to call when the animation starts
|
|
* @param function startCallback function to call when the animation starts
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
-utils.slideDown = function (element, finishCallback, startCallback) {
|
|
|
|
- utils.slideIn(element, {
|
|
|
|
- cssRule: 'height',
|
|
|
|
- cssRuleRef: 'clientHeight',
|
|
|
|
- cssClass: 'up',
|
|
|
|
- startCallback: startCallback,
|
|
|
|
- finishCallback: finishCallback
|
|
|
|
- });
|
|
|
|
-};
|
|
|
|
|
|
+utils.slideUp = function (element, finishCallback, startCallback) {
|
|
|
|
+ if (!utils.canSlide()) {
|
|
|
|
+ if (startCallback) startCallback();
|
|
|
|
+ element.className += (element.className !== '') ? ' hidden' : 'hidden';
|
|
|
|
+ if (finishCallback) window.requestAnimationFrame(finishCallback);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
-/**
|
|
|
|
- * Slides a element out (i.e. hide the element)
|
|
|
|
- *
|
|
|
|
- * @param HTMLElement element the element to slide out
|
|
|
|
- * @param object options the settings of the sliding process
|
|
|
|
- * @return void
|
|
|
|
- */
|
|
|
|
-utils.slideOut = function (element, options) {
|
|
|
|
- element.style[options.cssRule] = element[options.cssRuleRef] + 'px';
|
|
|
|
|
|
+ element.style.height = element.clientHeight + 'px';
|
|
|
|
|
|
- var slideId = parseInt(element.dataset.slideId) || 0;
|
|
|
|
- element.dataset.slideId = ++slideId;
|
|
|
|
|
|
+ var slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
|
|
|
|
+ element.setAttribute('data-slide-id', ++slideId);
|
|
|
|
|
|
window.requestAnimationFrame(function () {
|
|
window.requestAnimationFrame(function () {
|
|
element.classList.add('slide');
|
|
element.classList.add('slide');
|
|
|
|
|
|
window.requestAnimationFrame(function () {
|
|
window.requestAnimationFrame(function () {
|
|
- element.classList.add(options.cssClass);
|
|
|
|
|
|
+ element.style.height = '0px';
|
|
|
|
|
|
- if (options.startCallback) {
|
|
|
|
- options.startCallback();
|
|
|
|
|
|
+ if (startCallback) {
|
|
|
|
+ startCallback();
|
|
}
|
|
}
|
|
|
|
|
|
window.setTimeout(function () {
|
|
window.setTimeout(function () {
|
|
- if (parseInt(element.dataset.slideId) !== slideId) return;
|
|
|
|
|
|
+ if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
|
|
|
|
|
|
element.classList.add('hidden');
|
|
element.classList.add('hidden');
|
|
element.classList.remove('slide');
|
|
element.classList.remove('slide');
|
|
- element.classList.remove(options.cssClass);
|
|
|
|
- element.style[options.cssRule] = null;
|
|
|
|
|
|
+ element.style.height = null;
|
|
|
|
|
|
- if (options.finishCallback) {
|
|
|
|
- window.requestAnimationFrame(options.finishCallback);
|
|
|
|
|
|
+ if (finishCallback) {
|
|
|
|
+ window.requestAnimationFrame(finishCallback);
|
|
}
|
|
}
|
|
}, 500);
|
|
}, 500);
|
|
});
|
|
});
|
|
@@ -108,42 +88,52 @@ utils.slideOut = function (element, options) {
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Slides a element in (i.e. make the element visible)
|
|
|
|
|
|
+ * Slides a element down (i.e. show a hidden element)
|
|
*
|
|
*
|
|
- * @param HTMLElement element the element to slide in
|
|
|
|
- * @param object options the settings of the sliding process
|
|
|
|
|
|
+ * @param HTMLElement element the element to slide down
|
|
|
|
+ * @param function finishCallback function to call when the animation has
|
|
|
|
+ * been finished (i.e. the element is visible)
|
|
|
|
+ * @param function startCallback function to call when the animation starts
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
-utils.slideIn = function (element, options) {
|
|
|
|
- var cssRuleOriginalValue = element[options.cssRuleRef] + 'px',
|
|
|
|
- slideId = parseInt(element.dataset.slideId) || 0;
|
|
|
|
|
|
+utils.slideDown = function (element, finishCallback, startCallback) {
|
|
|
|
+ if (!utils.canSlide()) {
|
|
|
|
+ if (startCallback) startCallback();
|
|
|
|
+ element.className = element.className.replace(/\bhidden\b */g, '');
|
|
|
|
+ if (finishCallback) window.requestAnimationFrame(finishCallback);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- element.dataset.slideId = ++slideId;
|
|
|
|
|
|
+ var cssRuleOriginalValue = element.clientHeight + 'px',
|
|
|
|
+ slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
|
|
|
|
|
|
- element.style[options.cssRule] = null;
|
|
|
|
- element.classList.remove('hidden', 'slide', options.cssClass);
|
|
|
|
- var cssRuleValue = element[options.cssRuleRef] + 'px';
|
|
|
|
|
|
+ element.setAttribute('data-slide-id', ++slideId);
|
|
|
|
|
|
- element.classList.add('slide');
|
|
|
|
|
|
+ element.style.height = null;
|
|
|
|
+ element.classList.remove('hidden');
|
|
|
|
+ element.classList.remove('slide');
|
|
|
|
+ var cssRuleValue = element.clientHeight + 'px';
|
|
|
|
+
|
|
|
|
+ element.style.height = cssRuleOriginalValue;
|
|
|
|
|
|
window.requestAnimationFrame(function () {
|
|
window.requestAnimationFrame(function () {
|
|
- element.style[options.cssRule] = cssRuleOriginalValue;
|
|
|
|
|
|
+ element.classList.add('slide');
|
|
|
|
|
|
window.requestAnimationFrame(function () {
|
|
window.requestAnimationFrame(function () {
|
|
- element.style[options.cssRule] = cssRuleValue;
|
|
|
|
|
|
+ element.style.height = cssRuleValue;
|
|
|
|
|
|
- if (options.startCallback) {
|
|
|
|
- options.startCallback();
|
|
|
|
|
|
+ if (startCallback) {
|
|
|
|
+ startCallback();
|
|
}
|
|
}
|
|
|
|
|
|
window.setTimeout(function () {
|
|
window.setTimeout(function () {
|
|
- if (parseInt(element.dataset.slideId) !== slideId) return;
|
|
|
|
|
|
+ if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
|
|
|
|
|
|
element.classList.remove('slide');
|
|
element.classList.remove('slide');
|
|
- element.style[options.cssRule] = null;
|
|
|
|
|
|
+ element.style.height = null;
|
|
|
|
|
|
- if (options.finishCallback) {
|
|
|
|
- window.requestAnimationFrame(options.finishCallback);
|
|
|
|
|
|
+ if (finishCallback) {
|
|
|
|
+ window.requestAnimationFrame(finishCallback);
|
|
}
|
|
}
|
|
}, 500);
|
|
}, 500);
|
|
});
|
|
});
|
|
@@ -151,11 +141,11 @@ utils.slideIn = function (element, options) {
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Check whether a element is visible or not
|
|
|
|
|
|
+ * Checks whether a element is visible or not
|
|
*
|
|
*
|
|
- * @param HTMLElement element the element to test
|
|
|
|
|
|
+ * @param HTMLElement element the element to check
|
|
* @return boolean TRUE when the element is visible, FALSE otherwise
|
|
* @return boolean TRUE when the element is visible, FALSE otherwise
|
|
*/
|
|
*/
|
|
utils.isElementVisible = function (element) {
|
|
utils.isElementVisible = function (element) {
|
|
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
|
|
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
|
|
-};
|
|
|
|
|
|
+};
|