forked from replit-archive/repl.it
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.coffee
66 lines (62 loc) · 1.98 KB
/
hash.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Extension module.
# Responsible for generating cross-browser hash change events.
$ = jQuery
HASH_SEPARATOR = ':'
popStateSupported = 'onpopstate' of window
hashchangeSupported = 'onhashchange' of window
window_loaded = false
hash_states = ['','']
initial_hash = window.location.hash.slice 1
$.extend REPLIT,
HASH_SEPARATOR: HASH_SEPARATOR
setHash: (index, target) ->
cb = ->
return if hash_states[index]? is target
if not target?
target = index
window.location.hash = target if target isnt ''
else
# We want to set the location hash without changing the states.
# Save the old state.
_oldstate = hash_states[index]
hash_states[index] = target
# Construct the new hash.
hash = hash_states.join(':')
hash = '' if hash is ':'
# Restore the original state so that hashchange event can be triggered
# by the hash_check function.
hash_states[index] = _oldstate
window.location.hash = hash
$(window).trigger 'hashchange'
if not window_loaded
$(window).bind (-> setTimeout((-> REPLIT.setHash index, target), 0))
else
cb()
$(window).bind 'load', ->
window_loaded = true
REPLIT.setHash initial_hash
hash_check = ->
hash = window.location.hash.slice 1
hash_1 = hash.split(':')[0]
hash_2 = hash.split(':')[1]
if hash_states[0] isnt hash_1
hash_states[0] = hash_1
REPLIT.$this.trigger 'hashchange:0', [hash_1]
if hash_states[1] isnt hash_2
hash_states[1] = hash_2
REPLIT.$this.trigger 'hashchange:1', [hash_2]
return true
if popStateSupported
$(window).bind 'popstate', hash_check
$(window).trigger 'popstate'
else if hashchangeSupported
$(window).bind 'hashchange', hash_check
$(window).trigger 'hashchange'
else
lastHash = null
checkHash = ->
hash = window.location.hash.slice 1
if hash isnt lastHash
lastHash = hash
hash_check()
setInterval checkHash, 250