diff options
Diffstat (limited to 'ecomp-portal-FE/client/bower_components/jqTree/lib/save_state_handler.js')
-rw-r--r-- | ecomp-portal-FE/client/bower_components/jqTree/lib/save_state_handler.js | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/ecomp-portal-FE/client/bower_components/jqTree/lib/save_state_handler.js b/ecomp-portal-FE/client/bower_components/jqTree/lib/save_state_handler.js new file mode 100644 index 00000000..1e137a7d --- /dev/null +++ b/ecomp-portal-FE/client/bower_components/jqTree/lib/save_state_handler.js @@ -0,0 +1,238 @@ +var $, SaveStateHandler, indexOf, isInt, util; + +util = require('./util'); + +indexOf = util.indexOf; + +isInt = util.isInt; + +$ = jQuery; + +SaveStateHandler = (function() { + function SaveStateHandler(tree_widget) { + this.tree_widget = tree_widget; + } + + SaveStateHandler.prototype.saveState = function() { + var state; + state = JSON.stringify(this.getState()); + if (this.tree_widget.options.onSetStateFromStorage) { + return this.tree_widget.options.onSetStateFromStorage(state); + } else if (this.supportsLocalStorage()) { + return localStorage.setItem(this.getCookieName(), state); + } else if ($.cookie) { + $.cookie.raw = true; + return $.cookie(this.getCookieName(), state, { + path: '/' + }); + } + }; + + SaveStateHandler.prototype.getStateFromStorage = function() { + var json_data; + json_data = this._loadFromStorage(); + if (json_data) { + return this._parseState(json_data); + } else { + return null; + } + }; + + SaveStateHandler.prototype._parseState = function(json_data) { + var state; + state = $.parseJSON(json_data); + if (state && state.selected_node && isInt(state.selected_node)) { + state.selected_node = [state.selected_node]; + } + return state; + }; + + SaveStateHandler.prototype._loadFromStorage = function() { + if (this.tree_widget.options.onGetStateFromStorage) { + return this.tree_widget.options.onGetStateFromStorage(); + } else if (this.supportsLocalStorage()) { + return localStorage.getItem(this.getCookieName()); + } else if ($.cookie) { + $.cookie.raw = true; + return $.cookie(this.getCookieName()); + } else { + return null; + } + }; + + SaveStateHandler.prototype.getState = function() { + var getOpenNodeIds, getSelectedNodeIds; + getOpenNodeIds = (function(_this) { + return function() { + var open_nodes; + open_nodes = []; + _this.tree_widget.tree.iterate(function(node) { + if (node.is_open && node.id && node.hasChildren()) { + open_nodes.push(node.id); + } + return true; + }); + return open_nodes; + }; + })(this); + getSelectedNodeIds = (function(_this) { + return function() { + var n; + return (function() { + var i, len, ref, results; + ref = this.tree_widget.getSelectedNodes(); + results = []; + for (i = 0, len = ref.length; i < len; i++) { + n = ref[i]; + results.push(n.id); + } + return results; + }).call(_this); + }; + })(this); + return { + open_nodes: getOpenNodeIds(), + selected_node: getSelectedNodeIds() + }; + }; + + SaveStateHandler.prototype.setInitialState = function(state) { + var must_load_on_demand; + if (!state) { + return false; + } else { + must_load_on_demand = this._openInitialNodes(state.open_nodes); + this._selectInitialNodes(state.selected_node); + return must_load_on_demand; + } + }; + + SaveStateHandler.prototype._openInitialNodes = function(node_ids) { + var i, len, must_load_on_demand, node, node_id; + must_load_on_demand = false; + for (i = 0, len = node_ids.length; i < len; i++) { + node_id = node_ids[i]; + node = this.tree_widget.getNodeById(node_id); + if (node) { + if (!node.load_on_demand) { + node.is_open = true; + } else { + must_load_on_demand = true; + } + } + } + return must_load_on_demand; + }; + + SaveStateHandler.prototype._selectInitialNodes = function(node_ids) { + var i, len, node, node_id, select_count; + select_count = 0; + for (i = 0, len = node_ids.length; i < len; i++) { + node_id = node_ids[i]; + node = this.tree_widget.getNodeById(node_id); + if (node) { + select_count += 1; + this.tree_widget.select_node_handler.addToSelection(node); + } + } + return select_count !== 0; + }; + + SaveStateHandler.prototype.setInitialStateOnDemand = function(state, cb_finished) { + if (state) { + return this._setInitialStateOnDemand(state.open_nodes, state.selected_node, cb_finished); + } else { + return cb_finished(); + } + }; + + SaveStateHandler.prototype._setInitialStateOnDemand = function(node_ids, selected_nodes, cb_finished) { + var loadAndOpenNode, loading_count, openNodes; + loading_count = 0; + openNodes = (function(_this) { + return function() { + var i, len, new_nodes_ids, node, node_id; + new_nodes_ids = []; + for (i = 0, len = node_ids.length; i < len; i++) { + node_id = node_ids[i]; + node = _this.tree_widget.getNodeById(node_id); + if (!node) { + new_nodes_ids.push(node_id); + } else { + if (!node.is_loading) { + if (node.load_on_demand) { + loadAndOpenNode(node); + } else { + _this.tree_widget._openNode(node, false); + } + } + } + } + node_ids = new_nodes_ids; + if (_this._selectInitialNodes(selected_nodes)) { + _this.tree_widget._refreshElements(); + } + if (loading_count === 0) { + return cb_finished(); + } + }; + })(this); + loadAndOpenNode = (function(_this) { + return function(node) { + loading_count += 1; + return _this.tree_widget._openNode(node, false, function() { + loading_count -= 1; + return openNodes(); + }); + }; + })(this); + return openNodes(); + }; + + SaveStateHandler.prototype.getCookieName = function() { + if (typeof this.tree_widget.options.saveState === 'string') { + return this.tree_widget.options.saveState; + } else { + return 'tree'; + } + }; + + SaveStateHandler.prototype.supportsLocalStorage = function() { + var testSupport; + testSupport = function() { + var error, key; + if (typeof localStorage === "undefined" || localStorage === null) { + return false; + } else { + try { + key = '_storage_test'; + sessionStorage.setItem(key, true); + sessionStorage.removeItem(key); + } catch (error1) { + error = error1; + return false; + } + return true; + } + }; + if (this._supportsLocalStorage == null) { + this._supportsLocalStorage = testSupport(); + } + return this._supportsLocalStorage; + }; + + SaveStateHandler.prototype.getNodeIdToBeSelected = function() { + var state; + state = this.getStateFromStorage(); + if (state && state.selected_node) { + return state.selected_node[0]; + } else { + return null; + } + }; + + return SaveStateHandler; + +})(); + +module.exports = SaveStateHandler; |