-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathViewStateManager.js
More file actions
101 lines (89 loc) · 3.29 KB
/
Copy pathViewStateManager.js
File metadata and controls
101 lines (89 loc) · 3.29 KB
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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/**
* ViewStateManager is a singleton for views to park their global viwe state. The state is saved
* with project data but the View or View Factory is responsible for restoring the view state
* when the view is created.
*
* Views should implement `getViewState()` so that the view state can be saved and that data is cached
* for later use.
*
* Views or View Factories are responsible for restoring the view state when the view of that file is created
* by recalling the cached state. Views determine what data is store in the view state and how to restore it.
* @module view/ViewStateManager
*/
define(function (require, exports, module) {
var _ = require("thirdparty/lodash");
/**
* The view state cache.
* @type {Object.<string,*>}
* @private
*/
var _viewStateCache = {};
/**
* resets the view state cache
*/
function reset() {
_viewStateCache = {};
}
/**
* Sets the view state for the specified file
* @private
* @param {!File} file - the file to record the view state for
* @param {?*} viewState - any data that the view needs to restore the view state.
*/
function _setViewState(file, viewState) {
_viewStateCache[file.fullPath] = viewState;
}
/**
* Updates the view state for the specified view
* @param {!{!getFile:function():File, getViewState:function():*}} view - the to save state
* @param {?*} viewState - any data that the view needs to restore the view state.
*/
function updateViewState(view) {
if (view.getViewState) {
_setViewState(view.getFile(), view.getViewState());
}
}
/**
* gets the view state for the specified file
* @param {!File} file - the file to record the view state for
* @return {?*} whatever data that was saved earlier with a call setViewState
*/
function getViewState(file) {
return _viewStateCache[file.fullPath];
}
/**
* adds an array of view states
* @param {!object.<string, *>} viewStates - View State object to append to the current set of view states
*/
function addViewStates(viewStates) {
_viewStateCache = _.extend(_viewStateCache, viewStates);
}
/*
* Public API
*/
exports.reset = reset;
exports.updateViewState = updateViewState;
exports.getViewState = getViewState;
exports.addViewStates = addViewStates;
});