1
|
|
|
/** |
2
|
|
|
* [Siteapp] - multi-purpose frontend application |
3
|
|
|
* |
4
|
|
|
* Siteapp (Data) Storage Manager |
5
|
|
|
* |
6
|
|
|
* storage types: |
7
|
|
|
* - memory (default) |
8
|
|
|
* - cookie |
9
|
|
|
* - localStorage |
10
|
|
|
* - service |
11
|
|
|
* |
12
|
|
|
* @package [Siteapp] |
13
|
|
|
* @subpackage [Siteapp] storage |
14
|
|
|
* @author Björn Bartels <[email protected]> |
15
|
|
|
* @link https://gitlab.bjoernbartels.earth/groups/themes |
16
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
17
|
|
|
* @copyright copyright (c) 2016 Björn Bartels <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @namespace Siteapp |
20
|
|
|
* @module Siteapp.StorageManager |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
"use strict"; |
24
|
|
|
|
25
|
|
|
//import Siteapp from '../siteapp.core'; |
26
|
|
|
import {Exception} from '../sys/siteapp.exception'; |
27
|
|
|
import {StorageAdapter} from './siteapp.storageAdapter'; |
28
|
|
|
|
29
|
|
|
const Siteapp_StorageManager_VERSION = '0.0.1'; |
30
|
|
|
|
31
|
|
|
const Siteapp_StorageManager_DEFAULTS = { |
32
|
|
|
|
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
class StorageManagerException extends Exception { |
36
|
|
|
get name () { |
37
|
|
|
return "SiteappStorageManagerException"; |
38
|
|
|
} |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
class StorageManager { |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new instance of the storage manager. |
45
|
|
|
* @class |
46
|
|
|
* @name StorageManager |
47
|
|
|
* @param {Object} options - Overrides to the default plugin settings. |
48
|
|
|
*/ |
49
|
|
|
constructor (options) { |
50
|
|
|
|
51
|
|
|
this.options = $.extend({}, Siteapp_StorageManager_DEFAULTS, options); |
52
|
|
|
|
53
|
|
|
this._init(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Setup objects |
58
|
|
|
*/ |
59
|
|
|
_init () { |
60
|
|
|
this._version = Siteapp_StorageManager_VERSION; |
61
|
|
|
|
62
|
|
|
this._namespaces = {}; |
63
|
|
|
|
64
|
|
|
this._adapters = {}; |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve attached application. |
70
|
|
|
*/ |
71
|
|
|
get application () { |
72
|
|
|
return this._app; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Attach application object. |
77
|
|
|
*/ |
78
|
|
|
set application ( app ) { |
79
|
|
|
if (app instanceof Siteapp) { |
|
|
|
|
80
|
|
|
this._app = app; |
81
|
|
|
} else { |
82
|
|
|
throw new StorageManagerException('Invalid application object to attach'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve current version. |
88
|
|
|
*/ |
89
|
|
|
get version () { |
90
|
|
|
return this._version; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve list of available storage tyoes. |
95
|
|
|
*/ |
96
|
|
|
get types () { |
97
|
|
|
return [ |
98
|
|
|
'memory', |
99
|
|
|
'elementData', |
100
|
|
|
'sessionStorage', |
101
|
|
|
'cookie', |
102
|
|
|
'localStorage', |
103
|
|
|
'service' |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retrieve available storage adapters. |
109
|
|
|
*/ |
110
|
|
|
get namespaces () { |
111
|
|
|
return this._namespaces; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
setNamespaceAdapter ( _namespace, adapter ) { |
115
|
|
|
if ( !(adapter instanceof StorageAdapter)) { |
116
|
|
|
throw new StorageManagerException('Invalid storage adapter object to set'); |
117
|
|
|
} |
118
|
|
|
if (typeof this.namespaces[_namespace] == 'undefined') { |
119
|
|
|
this.namespaces[_namespace] = { |
120
|
|
|
data: {}, |
121
|
|
|
adapters: {} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
this.namespaces[_namespace].adapters[adapter.type] = adapter; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
export {StorageManager}; |
129
|
|
|
|
130
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.