renaissance-spa
Version:
renaissance spa adpater
63 lines (54 loc) • 1.51 kB
text/coffeescript
# spa adapter
class SpaAdapter
constructor: () ->
this.meta =
'containerSelector': '.spa-container'
this.pages = {}
this
###
Get nodes by selector
{string} selector
{object} NodeList
###
# TODO: Should be moved to general utils module
_getNodes: (selector) ->
# try to use query selector all on slctr with attribute
if selector.match(/^.+\[.+\]$/)
return document.querySelectorAll(slctr)
# default - use standard selector statements
# node
if typeof selector == 'object'
return [selector]
# id
else if selector.charAt(0) == '#'
selector = selector.substring(1)
return [document.getElementById(selector)]
# class
else if selector.charAt(0) == '.'
selector = selector.substring(1)
return document.getElementsByClassName(selector)
# tag
else if typeof selector == 'string'
return document.getElementsByTagName(selector)
###
Register a spa container
{string} name
{string} slctr
{*}
###
register: (name, slctr) ->
this.pages[name] = slctr
###
Go to given page
{string} name
{*}
###
goto: (name) ->
spaContainers = Array.from(this._getNodes(this.meta.containerSelector))
container = this._getNodes(this.pages[name])[0]
spaContainers.forEach((el, idx) ->
cont = spaContainers[idx]
cont.style.display = 'none'
)
container.style.display = 'block'
module.exports = SpaAdapter