page.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. track()
  40. context
  41. page.replace = (path, state, skipDispatch, init) ->
  42. context = new Context(path, state or currentState)
  43. context.init = init
  44. currentState = context.state
  45. page.dispatch(context) unless skipDispatch
  46. context.replaceState()
  47. track() unless init or skipDispatch
  48. context
  49. page.dispatch = (context) ->
  50. i = 0
  51. next = ->
  52. fn(context, next) if fn = callbacks[i++]
  53. return
  54. next()
  55. return
  56. currentPath = ->
  57. location.pathname + location.search + location.hash
  58. class Context
  59. @initialPath: currentPath()
  60. @sessionId: Date.now()
  61. @stateId: 0
  62. @isInitialPopState: (state) ->
  63. state.path is @initialPath and @stateId is 1
  64. @isSameSession: (state) ->
  65. state.sessionId is @sessionId
  66. constructor: (@path = '/', @state = {}) ->
  67. @pathname = @path.replace /(?:\?([^#]*))?(?:#(.*))?$/, (_, query, hash) =>
  68. @query = query
  69. @hash = hash
  70. ''
  71. @state.id ?= @constructor.stateId++
  72. @state.sessionId ?= @constructor.sessionId
  73. @state.path = @path
  74. pushState: ->
  75. history.pushState @state, '', @path
  76. return
  77. replaceState: ->
  78. history.replaceState @state, '', @path
  79. return
  80. class Route
  81. constructor: (@path, options = {}) ->
  82. @keys = []
  83. @regexp = pathtoRegexp @path, @keys
  84. middleware: (fn) ->
  85. (context, next) =>
  86. if @match context.pathname, params = []
  87. context.params = params
  88. fn(context, next)
  89. else
  90. next()
  91. return
  92. match: (path, params) ->
  93. return unless matchData = @regexp.exec(path)
  94. for value, i in matchData[1..]
  95. value = decodeURIComponent value if typeof value is 'string'
  96. if key = @keys[i]
  97. params[key.name] = value
  98. else
  99. params.push value
  100. true
  101. pathtoRegexp = (path, keys) ->
  102. return path if path instanceof RegExp
  103. path = "(#{path.join '|'})" if path instanceof Array
  104. path = path
  105. .replace /\/\(/g, '(?:/'
  106. .replace /(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, (_, slash = '', format = '', key, capture, optional) ->
  107. keys.push name: key, optional: !!optional
  108. str = if optional then '' else slash
  109. str += '(?:'
  110. str += slash if optional
  111. str += format
  112. str += capture or if format then '([^/.]+?)' else '([^/]+?)'
  113. str += ')'
  114. str += optional if optional
  115. str
  116. .replace /([\/.])/g, '\\$1'
  117. .replace /\*/g, '(.*)'
  118. new RegExp "^#{path}$"
  119. onpopstate = (event) ->
  120. return if not event.state or Context.isInitialPopState(event.state)
  121. if Context.isSameSession(event.state)
  122. page.replace(event.state.path, event.state)
  123. else
  124. location.reload()
  125. return
  126. onclick = (event) ->
  127. return if event.which isnt 1 or event.metaKey or event.ctrlKey or event.shiftKey or event.defaultPrevented
  128. link = event.target
  129. link = link.parentElement while link and link.tagName isnt 'A'
  130. if link and not link.target and isSameOrigin(link.href)
  131. event.preventDefault()
  132. page.show link.pathname + link.search + link.hash
  133. return
  134. isSameOrigin = (url) ->
  135. url.indexOf("#{location.protocol}//#{location.hostname}") is 0
  136. track = ->
  137. ga?('send', 'pageview', location.pathname + location.search + location.hash)
  138. _gauges?.push(['track'])
  139. return