Skip to content

Commit

Permalink
Trying to start all
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmert committed May 16, 2014
1 parent 4a0e5b5 commit b8069b7
Show file tree
Hide file tree
Showing 21 changed files with 1,940 additions and 212 deletions.
34 changes: 25 additions & 9 deletions coffee/affix.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Affix extends MaxmertkitHelpers
spy: @el.getAttribute( 'data-spy' ) or _name

# Number; in px, vertical offset from the top
offset: @el.getAttribute( 'data-offset' ) or 5
offset: @el.getAttribute( 'data-offset' ) or -25

# Events
beforeactive: ->
Expand Down Expand Up @@ -51,7 +51,7 @@ class Affix extends MaxmertkitHelpers

destroy: ->
_deactivate.call @
@el.dataset["data-kit-#{@_name}"] = null
@el.dataset["kitAffix"] = null
super

start: ->
Expand Down Expand Up @@ -133,10 +133,17 @@ _deactivate = ->


_setPosition = ->
if @container.getBoundingClientRect().top - @options.offset <= document.body.scrollTop
if @container.getBoundingClientRect().top + @_outerHeight(@scroller) - @options.offset - @_outerHeight() >= document.body.scrollTop
containerTop = @container.offsetTop

if containerTop - @options.offset <= document.body.scrollTop
if containerTop + @_outerHeight(@container) - @options.offset - @_outerHeight() >= document.body.scrollTop
@el.style.width = @el.offsetWidth
@el.style.position = 'fixed'
top = @options.offset
try
style = @el.currentStyle or getComputedStyle(@el)
if style?
if style.marginTop? and style.marginTop isnt '' then top += parseInt(style.marginTop)
@el.style.top = "#{@options.offset}px"
@el.style.bottom = 'auto'
else
Expand All @@ -153,19 +160,28 @@ _setPosition = ->
window['Affix'] = Affix
window['mkitAffix'] = ( options ) ->
result = null

if not @dataset? then @dataset = {}

unless @dataset['data-kit-affix']
unless @dataset['kitAffix']
result = new Affix @, options
@dataset['data-kit-affix'] = result
@dataset['kitAffix'] = result

else
if typeof options is 'object'
@dataset['data-kit-affix']._setOptions options
@dataset['kitAffix']._setOptions options
else
if typeof options is "string" and options.charAt(0) isnt "_"
@dataset['data-kit-affix'][options]
@dataset['kitAffix'][options]

result = @dataset['data-kit-affix']
result = @dataset['kitAffix']

return result


# if $? and jQuery?
# $.fn[_name] = window['mkitAffix']
# $(window).on 'load', ->
# $('[data-spy="affix"]').each ->
# $btn = $(@)
# $btn.affix($btn.data())
7 changes: 7 additions & 0 deletions coffee/button.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ window['mkitButton'] = ( options ) ->

return result

# if $? and jQuery?
# $.fn.button = window['mkitButton']
# $(window).on 'load', ->
# $('[data-target="button"]').each ->
# $btn = $(@)
# $btn.button($btn.data())


# class Button extends MaxmertkitHelpers
#
Expand Down
10 changes: 5 additions & 5 deletions coffee/maxmertkit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ class MaxmertkitHelpers

_outerWidth: (el) ->
el = el or @el
height = el.offsetWidth
width = el.offsetWidth
style = el.currentStyle or getComputedStyle(el)
height += parseInt(style.marginLeft) + parseInt(style.marginRight)
height
width += parseInt(style.marginLeft) + parseInt(style.marginRight)
width

_outerHeight: (el) ->
el = el or @el
Expand All @@ -225,8 +225,8 @@ class MaxmertkitHelpers
# Return Document if there is not any parents with any style (usually if element is not deep in DOM)
while parent? and parent = parent.parentNode
try
style = getComputedStyle parent

style = getComputedStyle(parent)
return parent if not style?

if /(relative|fixed)/.test(style['position'])
Expand Down
119 changes: 111 additions & 8 deletions coffee/scrollspy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Scrollspy extends MaxmertkitHelpers
_name: _name
_instances: _instances
started: no
active: -1

constructor: ( @el, @options ) ->

Expand Down Expand Up @@ -45,11 +46,23 @@ class Scrollspy extends MaxmertkitHelpers

# Get scrolling container with items inside
@target = document.querySelector @options.target
@scroller = @_getScrollContainer @target

@spy = _spy.bind(@)

@_setOptions @options

super @el, @options

# Set global event
@reactor.registerEvent "initialize.#{_name}"
@reactor.registerEvent "start.#{_name}"
@reactor.registerEvent "stop.#{_name}"

@reactor.dispatchEvent "initialize.#{_name}"

@start()

destroy: ->
_deactivate.call @
@el.dataset["data-kit-#{@_name}"] = null
Expand All @@ -63,39 +76,129 @@ class Scrollspy extends MaxmertkitHelpers
switch key
when 'target'
# TODO: Reset events here
@target = document.querySelector @options.target
@_removeEventListener @scroller, 'scroll', @spy
@target = document.querySelector value
@scroller = @_getScrollContainer @target
@_addEventListener @scroller, 'scroll', @spy

when 'elements'
@elements = @refresh()
@refresh()

@options[key] = value
if typeof value is 'function' then @[key] = value

start: ->
if not @started
_beforeactivate.call @

stop: ->
if @started
_beforedeactivate.call @

refresh: ->
elements = @el.querySelectorAll(@options.elements)
@elements = []
for el in elements
targetEl = @target.querySelector(el.getAttribute( @options.elementsAttr ))
@elements.push
element: el
target: targetEl
height: targetEl.offsetHeight
top: targetEl.offsetTop



# ===============
# PRIVATE METHODS
_activateItem = ( itemNumber ) ->
for el in @elements
@_removeClass '_active_', el.element
@_removeClass '_active_', el.element.parent

_deactivate = ->
@_addClass '_active_', @elements[itemNumber].element
@_addClass '_active_', @elements[itemNumber].element.parent


_deactivateItem = ( itemNumber ) ->
@_removeClass '_active_', @elements[itemNumber].element
@_removeClass '_active_', @elements[itemNumber].element.parent


_spy = ( event ) ->
i = 0
while i < @elements.length
if (@elements[i].top <= (event.currentTarget.scrollTop or event.currentTarget.scrollY) + @options.offset <= @elements[i].top + @elements[i].height )
if not @_hasClass '_active_', @elements[i].element
_activateItem.call @, i
else
if @_hasClass('_active_', @elements[i].element) and (event.currentTarget.scrollTop or event.currentTarget.scrollY) + @options.offset < @elements[i].top + @elements[i].height
_deactivateItem.call @, i
i++

_beforeactivate = ->
if @beforeactive?
try
deferred = @beforeactive.call @el
deferred
.done =>
_activate.call @

.fail =>
@failactive?.call @el

catch
_activate.call @

else
_activate.call @

_activate = ->
@_addEventListener @scroller, 'scroll', @spy
@onactive?.call @el
@reactor.dispatchEvent "start.#{_name}"
@started = yes

_beforedeactivate = ->
if @beforedeactive?
try
deferred = @beforedeactive.call @el
deferred
.done =>
_deactivate.call @

.fail =>
@faildeactive?.call @el

catch
_deactivate.call @

else
_deactivate.call @

_deactivate = ->
@_removeEventListener @scroller, 'scroll', @spy
@reactor.dispatchEvent "stop.#{_name}"
@ondeactive?.call @el
@started = no


window['Scrollspy'] = Scrollspy
window['mkitScrollspy'] = ( options ) ->
result = null
if not @dataset? then @dataset = {}

unless @dataset['data-kit-scrollspy']
unless @dataset['kitScrollspy']
result = new Scrollspy @, options
@dataset['data-kit-scrollspy'] = result
@dataset['kitScrollspy'] = result

else
if typeof options is 'object'
@dataset['data-kit-scrollspy']._setOptions options
@dataset['kitScrollspy']._setOptions options
else
if typeof options is "string" and options.charAt(0) isnt "_"
@dataset['data-kit-scrollspy'][options]
@dataset['kitScrollspy'][options]

result = @dataset['data-kit-scrollspy']
result = @dataset['kitScrollspy']

return result

Expand Down
Loading

0 comments on commit b8069b7

Please sign in to comment.