Skip to content

Commit

Permalink
Draggable aside
Browse files Browse the repository at this point in the history
with dynamic keyframes
  • Loading branch information
pinaypunto committed Jul 9, 2013
1 parent 5a75c9e commit 1fc7d35
Showing 1 changed file with 55 additions and 13 deletions.
68 changes: 55 additions & 13 deletions src/modules/Lungo.Aside.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Lungo.Aside = do (lng = Lungo) ->

C = lng.Constants
_callback = undefined
CUSTOM_KF_SHOW = "__customKFShow__"
CUSTOM_KF_HIDE = "__customKFHide__"
_customAsideAnimation = undefined

###
Expand All @@ -19,18 +21,23 @@ Lungo.Aside = do (lng = Lungo) ->
@param {string} <aside> Id
###
show = (aside_id, fixed = false) ->
show = (aside_id, fixed = false, fromX = -1) ->
aside = lng.dom("##{aside_id}")
if aside.length
unless _alreadyOpen(aside_id)
unless fixed
# not fixed
fromX = 0
lng.Element.Cache.aside = aside
if lng.DEVICE is C.DEVICE.PHONE
aside.addClass(C.CLASS.SHOW)
aside_transition = aside.data(C.TRANSITION.ATTR) or "left"
lng.Element.Cache.section.data("aside-#{aside_transition}", "show")
unless fromX is null
customAnimation = _createCustomAnimation(fromX)
lng.Element.Cache.section.style "-webkit-animation-name", customAnimation
else
aside_transition = aside.data(C.TRANSITION.ATTR) or "left"
lng.Element.Cache.section.data("aside-#{aside_transition}", "show")
# aside_transition = aside.data(C.TRANSITION.ATTR) or "left"
# lng.Element.Cache.section.data("aside-#{aside_transition}", "show")
else
aside.addClass(C.CLASS.SHOW)
aside_section = lng.dom("[data-aside=#{aside_id}][data-children]")
Expand All @@ -53,13 +60,18 @@ Lungo.Aside = do (lng = Lungo) ->
@method hide
@param {function} Callback
###
hide = (callback) ->
if lng.Element.Cache.aside
hide = (callback, fromX=null) ->
if lng.Element.Cache.aside or fromX
_callback = callback
aside_transition = lng.Element.Cache.aside.data(C.TRANSITION.ATTR) or "left"
aside_transition = lng.Element.Cache.aside?.data(C.TRANSITION.ATTR) or "left"
if lng.DEVICE is C.DEVICE.PHONE
lng.Element.Cache.section.removeClass("aside").removeClass("aside-right")
lng.Element.Cache.section.data("aside-#{aside_transition}", "hide")
if fromX > 0
customAnimation = _createCustomAnimation(fromX, true)
lng.Element.Cache.section.removeClass("aside").removeClass("aside-right")
lng.Element.Cache.section.style "-webkit-animation-name", customAnimation
else
lng.Element.Cache.section.removeClass("aside").removeClass("aside-right")
lng.Element.Cache.section.data("aside-#{aside_transition}", "hide")
else
lng.dom(".aside").removeClass("aside").addClass("asideHidding")
lng.Element.Cache.aside = null
Expand All @@ -81,17 +93,20 @@ Lungo.Aside = do (lng = Lungo) ->
###
animationEnd = (event) ->
section = lng.dom(event.target)
aside_transition = lng.Element.Cache.aside.data(C.TRANSITION.ATTR) or "left"
aside_transition = lng.Element.Cache.aside?.data(C.TRANSITION.ATTR) or "left"
if section.data("aside-#{aside_transition}") is "hide"
lng.Element.Cache.aside.removeClass(C.CLASS.SHOW)
lng.Element.Cache.aside = null
section.removeAttr("data-aside-#{aside_transition}")
if _callback then _callback.call _callback
_callback = undefined
else
className = if aside_transition.indexOf("right") is -1 then "aside" else "aside-right"
if section.vendor("animation")[0] isnt CUSTOM_KF_HIDE
className = if aside_transition.indexOf("right") is -1 then "aside" else "aside-right"
section.removeAttr("style").removeAttr("data-aside-#{aside_transition}").addClass(className)

if _customAsideAnimation
section.removeAttr("style")
_customAsideAnimation.remove()
_customAsideAnimation = undefined

Expand All @@ -108,6 +123,7 @@ Lungo.Aside = do (lng = Lungo) ->
section = el.closest("section")
aside = lng.dom("aside#" + el.data("aside"))
section.swiping (gesture) ->
gesture.originalEvent.preventDefault()
unless section.hasClass("aside") or section.hasClass("aside-right")
xdiff = gesture.currentTouch.x - gesture.iniTouch.x
ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
Expand All @@ -122,11 +138,14 @@ Lungo.Aside = do (lng = Lungo) ->

section.swipe (gesture) ->
diff = gesture.currentTouch.x - gesture.iniTouch.x
diff = 256 if diff > 256
ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
section.attr "style", ""
if diff > MIN_XDIFF and started
show(aside.attr("id"), true, gesture.currentTouch.x)
else hide
show(aside.attr("id"), false, diff)
else
if started then hide(undefined, diff)
else hide()
started = false


Expand All @@ -139,6 +158,29 @@ Lungo.Aside = do (lng = Lungo) ->
_asideStylesheet = ->
if lng.Element.Cache.aside?.hasClass(C.CLASS.RIGHT) then "#{C.CLASS.RIGHT}" else " "

_createCustomAnimation = (x, forClose=false) ->
animationName = if forClose then CUSTOM_KF_HIDE else CUSTOM_KF_SHOW
_customAsideAnimation = document.createElement('style')
_customAsideAnimation.type = 'text/css'
unless forClose
rule = """
@-webkit-keyframes #{animationName} {
0% { -webkit-transform: translateX(#{x}px); }
60% { -webkit-transform: translateX(262px); }
100% { -webkit-transform: translateX(256px); }
}
"""
else
rule = """
@-webkit-keyframes #{animationName} {
0% { -webkit-transform: translateX(#{x}px); }
100% { -webkit-transform: translateX(0); }
}
"""
_customAsideAnimation.appendChild(document.createTextNode(rule))
document.getElementsByTagName("head")[0].appendChild(_customAsideAnimation)
return animationName


show : show
hide : hide
Expand Down

0 comments on commit 1fc7d35

Please sign in to comment.