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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
util = require './util'
indexOf = util.indexOf
isInt = util.isInt
$ = jQuery
class SaveStateHandler
constructor: (tree_widget) ->
@tree_widget = tree_widget
saveState: ->
state = JSON.stringify(@getState())
if @tree_widget.options.onSetStateFromStorage
@tree_widget.options.onSetStateFromStorage(state)
else if @supportsLocalStorage()
localStorage.setItem(
@getCookieName(),
state
)
else if $.cookie
$.cookie.raw = true
$.cookie(
@getCookieName(),
state,
{path: '/'}
)
getStateFromStorage: ->
json_data = @_loadFromStorage()
if json_data
return @_parseState(json_data)
else
return null
_parseState: (json_data) ->
state = $.parseJSON(json_data)
# Check if selected_node is an int (instead of an array)
if state and state.selected_node and isInt(state.selected_node)
# Convert to array
state.selected_node = [state.selected_node]
return state
_loadFromStorage: ->
if @tree_widget.options.onGetStateFromStorage
return @tree_widget.options.onGetStateFromStorage()
else if @supportsLocalStorage()
return localStorage.getItem(
@getCookieName()
)
else if $.cookie
$.cookie.raw = true
return $.cookie(@getCookieName())
else
return null
getState: ->
getOpenNodeIds = =>
open_nodes = []
@tree_widget.tree.iterate((node) ->
if (
node.is_open and
node.id and
node.hasChildren()
)
open_nodes.push(node.id)
return true
)
return open_nodes
getSelectedNodeIds = =>
return (n.id for n in @tree_widget.getSelectedNodes())
return {
open_nodes: getOpenNodeIds(),
selected_node: getSelectedNodeIds()
}
# Set initial state
# Don't handle nodes that are loaded on demand
#
# result: must load on demand
setInitialState: (state) ->
if not state
return false
else
must_load_on_demand = @_openInitialNodes(state.open_nodes)
@_selectInitialNodes(state.selected_node)
return must_load_on_demand
_openInitialNodes: (node_ids) ->
must_load_on_demand = false
for node_id in node_ids
node = @tree_widget.getNodeById(node_id)
if node
if not node.load_on_demand
node.is_open = true
else
must_load_on_demand = true
return must_load_on_demand
_selectInitialNodes: (node_ids) ->
select_count = 0
for node_id in node_ids
node = @tree_widget.getNodeById(node_id)
if node
select_count += 1
@tree_widget.select_node_handler.addToSelection(node)
return select_count != 0
setInitialStateOnDemand: (state, cb_finished) ->
if state
@_setInitialStateOnDemand(state.open_nodes, state.selected_node, cb_finished)
else
cb_finished()
_setInitialStateOnDemand: (node_ids, selected_nodes, cb_finished) ->
loading_count = 0
openNodes = =>
new_nodes_ids = []
for node_id in node_ids
node = @tree_widget.getNodeById(node_id)
if not node
new_nodes_ids.push(node_id)
else
if not node.is_loading
if node.load_on_demand
loadAndOpenNode(node)
else
@tree_widget._openNode(node, false)
node_ids = new_nodes_ids
if @_selectInitialNodes(selected_nodes)
@tree_widget._refreshElements()
if loading_count == 0
cb_finished()
loadAndOpenNode = (node) =>
loading_count += 1
@tree_widget._openNode(
node,
false,
->
loading_count -= 1
openNodes()
)
openNodes()
getCookieName: ->
if typeof @tree_widget.options.saveState is 'string'
return @tree_widget.options.saveState
else
return 'tree'
supportsLocalStorage: ->
testSupport = ->
# Is local storage supported?
if not localStorage?
return false
else
# Check if it's possible to store an item. Safari does not allow this in private browsing mode.
try
key = '_storage_test'
sessionStorage.setItem(key, true)
sessionStorage.removeItem(key)
catch error
return false
return true
if not @_supportsLocalStorage?
@_supportsLocalStorage = testSupport()
return @_supportsLocalStorage
getNodeIdToBeSelected: ->
state = @getStateFromStorage()
if state and state.selected_node
return state.selected_node[0]
else
return null
module.exports = SaveStateHandler
|