Last active
August 29, 2015 14:27
-
-
Save Bondifrench/7c524e849ebc591b2490 to your computer and use it in GitHub Desktop.
Attempt to port React-page slider to Mithril
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// React version is here: https://github.com/ccoenraets/react-employee-directory/blob/master/iteration8/js/pageslider-react.js | |
var PageSlider = { | |
controller: function(args) { | |
var ctrl = this; | |
ctrl.history = m.prop([]); | |
ctrl.pages = m.prop([]); | |
ctrl.animating = m.prop(false); | |
ctrl.slidePage = function(page) { | |
var history = ctrl.history(); | |
var pages = ctrl.pages(); | |
var l = history.length; | |
var hash = window.location.hash; | |
var position = 'center'; | |
if (l === 0) { | |
history.push(hash); | |
} else if (hash === history[1 - 2]) { | |
history.pop(); | |
position = 'left'; | |
} else { | |
history.push(hash); | |
position = 'right'; | |
} | |
page.position = position; //Not sure about this one | |
pages.push(page); | |
ctrl.history(history); | |
ctrl.pages(pages); | |
ctrl.animating(position != 'center'); | |
} | |
ctrl.componentDidUpdate = function (e) { | |
var skippedCurrentFrame = false; | |
var pageEl = e.getDOMNode().lastChild; | |
var pages = ctrl.pages(); | |
var l = pages.length; | |
var transitionEndHandler = function () { | |
pageEl.removeEventListener('webkitTransitionEnd', transitionEndHandler); | |
pages.shift(); | |
ctrl.pages(pages); | |
}; | |
var animate = function () { | |
if (!skippedCurrentFrame) { | |
skippedCurrentFrame = true; | |
requestAnimationFrame(animate) | |
} else if (l > 0) { | |
pages[l - 1].position = 'center transtion'; | |
ctrl.pages(pages); | |
ctrl.animating(false); | |
pageEl.addEventListener('webkitTransitionEnd', transitionEndHandler) | |
} | |
} | |
if (ctrl.animating()) { | |
requestAnimationFrame(animate); | |
} | |
} | |
}, | |
view: function(ctrl, args) { | |
// Not sure about this line... | |
return m('.pageslider-container', {config: ctrl.componentDidUpdate} , ctrl.slidePage(ctrl.pages())) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment