page.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ###
  2. * Based on github.com/visionmedia/page.js
  3. * Licensed under the MIT license
  4. * Copyright 2012 TJ Holowaychuk <tj@vision-media.ca>
  5. ###
  6. running = false
  7. currentState = null
  8. callbacks = []
  9. @page = (value, fn) ->
  10. if typeof value is 'function'
  11. page '*', value
  12. else if typeof fn is 'function'
  13. route = new Route(value)
  14. callbacks.push route.middleware(fn)
  15. else if typeof value is 'string'
  16. page.show(value, fn)
  17. else
  18. page.start(value)
  19. return
  20. page.start = (options = {}) ->
  21. unless running
  22. running = true
  23. addEventListener 'popstate', onpopstate
  24. addEventListener 'click', onclick
  25. page.replace currentPath(), null, null, true
  26. return
  27. page.stop = ->
  28. if running
  29. running = false
  30. removeEventListener 'click', onclick
  31. removeEventListener 'popstate', onpopstate
  32. return
  33. page.show = (path, state) ->
  34. return if path is currentState?.path
  35. context = new Context(path, state)
  36. currentState = context.state
  37. page.dispatch(context)
  38. context.pushState()
  39. ga?('send', 'pageview', location.pathname + location.search + location.hash)
  40. _gauges?.push(['track'])
  41. context
  42. page.replace = (path, state, skipDispatch, init) ->
  43. context = new Context(path, state or currentState)
  44. context.init = init
  45. currentState = context.state
  46. page.dispatch(context) unless skipDispatch
  47. context.replaceState()
  48. ga?('send', 'pageview', location.pathname + location.search + location.hash) unless init
  49. _gauges?.push(['track'])
  50. context
  51. page.dispatch = (context) ->
  52. i = 0
  53. next = ->
  54. fn(context, next) if fn = callbacks[i++]
  55. return
  56. next()
  57. return
  58. currentPath = ->
  59. location.pathname + location.search + location.hash
  60. class Context
  61. @initialPath: currentPath()
  62. @sessionId: Date.now()
  63. @stateId: 0
  64. @isInitialPopState: (state) ->
  65. state.path is @initialPath and @stateId is 1
  66. @isSameSession: (state) ->
  67. state.sessionId is @sessionId
  68. constructor: (@path = '/', @state = {}) ->
  69. @pathname = @path.replace /(?:\?([^#]*))?(?:#(.*))?$/, (_, query, hash) =>
  70. @query = query
  71. @hash = hash
  72. ''
  73. @state.id ?= @constructor.stateId++
  74. @state.sessionId ?= @constructor.sessionId
  75. @state.path = @path
  76. pushState: ->
  77. history.pushState @state, '', @path
  78. return
  79. replaceState: ->
  80. history.replaceState @state, '', @path
  81. return
  82. class Route
  83. constructor: (@path, options = {}) ->
  84. @keys = []
  85. @regexp = pathtoRegexp @path, @keys
  86. middleware: (fn) ->
  87. (context, next) =>
  88. if @match context.pathname, params = []
  89. context.params = params
  90. fn(context, next)
  91. else
  92. next()
  93. return
  94. match: (path, params) ->
  95. return unless matchData = @regexp.exec(path)
  96. for value, i in matchData[1..]
  97. value = decodeURIComponent value if typeof value is 'string'
  98. if key = @keys[i]
  99. params[key.name] = value
  100. else
  101. params.push value
  102. true
  103. pathtoRegexp = (path, keys) ->
  104. return path if path instanceof RegExp
  105. path = "(#{path.join '|'})" if path instanceof Array
  106. path = path
  107. .replace /\/\(/g, '(?:/'
  108. .replace /(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, (_, slash = '', format = '', key, capture, optional) ->
  109. keys.push name: key, optional: !!optional
  110. str = if optional then '' else slash
  111. str += '(?:'
  112. str += slash if optional
  113. str += format
  114. str += capture or if format then '([^/.]+?)' else '([^/]+?)'
  115. str += ')'
  116. str += optional if optional
  117. str
  118. .replace /([\/.])/g, '\\$1'
  119. .replace /\*/g, '(.*)'
  120. new RegExp "^#{path}$"
  121. onpopstate = (event) ->
  122. return if not event.state or Context.isInitialPopState(event.state)
  123. if Context.isSameSession(event.state)
  124. page.replace(event.state.path, event.state)
  125. else
  126. location.reload()
  127. return
  128. onclick = (event) ->
  129. return if event.which isnt 1 or event.metaKey or event.ctrlKey or event.shiftKey or event.defaultPrevented
  130. link = event.target
  131. link = link.parentElement while link and link.tagName isnt 'A'
  132. if link and not link.target and isSameOrigin(link.href)
  133. event.preventDefault()
  134. page.show link.pathname + link.search + link.hash
  135. return
  136. isSameOrigin = (url) ->
  137. url.indexOf("#{location.protocol}//#{location.hostname}") is 0