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] core |
||
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.AdapterManager |
||
21 | */ |
||
22 | /** global: Siteapp */ |
||
23 | /** global: Siteapp_AdapterManager_VERSION */ |
||
24 | /** global: Siteapp_AdapterManager_DEFAULTS */ |
||
25 | "use strict"; |
||
26 | |||
27 | //import Siteapp from '../siteapp.core'; |
||
28 | import {Exception} from '../sys/siteapp.exception'; |
||
29 | |||
30 | const Siteapp_AdapterManager_VERSION = '0.0.1'; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
31 | |||
32 | const Siteapp_AdapterManager_DEFAULTS = { |
||
0 ignored issues
–
show
|
|||
33 | |||
34 | }; |
||
35 | |||
36 | class AdapterManagerException extends Exception { |
||
37 | get name () { |
||
38 | return "SiteappAdapterManagerException"; |
||
39 | } |
||
40 | }; |
||
41 | |||
42 | const AdapterManager = class AdapterManager { |
||
43 | |||
44 | /** |
||
45 | * Create a new instance of the storage manager. |
||
46 | * @class |
||
47 | * @name AdapterManager |
||
48 | * @param {Object} options - Overrides to the default plugin settings. |
||
49 | */ |
||
50 | constructor (options) { |
||
51 | |||
52 | this.options = $.extend({}, Siteapp_AdapterManager_DEFAULTS, options); |
||
53 | |||
54 | this._init(); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Setup objects |
||
59 | */ |
||
60 | _init () { |
||
61 | this._version = Siteapp_AdapterManager_VERSION; |
||
62 | |||
63 | this._adapters = {}; |
||
64 | |||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Registera an adapter. |
||
69 | * |
||
70 | * @function |
||
71 | * @param {object} adapter |
||
72 | * @param {string} name |
||
73 | */ |
||
74 | register ( adapter, name ) { |
||
75 | var namespace = Siteapp.sys.hyphenate(Siteapp.sys.functionName(adapter)); |
||
0 ignored issues
–
show
|
|||
76 | if ("" == name) { |
||
77 | name = Siteapp.sys.functionName(adapter); |
||
78 | } |
||
79 | if (this.isRegistered(name)) { |
||
80 | console.warn(`An adapter with the name "${name}" has already been registered.`) |
||
81 | } else { |
||
82 | this._adapters[name] = adapter; |
||
83 | } |
||
84 | return this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Unregisters an adapter. |
||
89 | * |
||
90 | * @function |
||
91 | * @param {object} adapter |
||
0 ignored issues
–
show
|
|||
92 | * |
||
93 | */ |
||
94 | get ( name ) { |
||
95 | |||
96 | if (!this.isRegistered(name)) { |
||
97 | console.warn(`An adapter with the name "${name}" has not been registered.`) |
||
0 ignored issues
–
show
|
|||
98 | } else { |
||
99 | return this.adapters[name]; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Unregisters an adapter. |
||
105 | * |
||
106 | * @function |
||
107 | * @param {object} adapter |
||
108 | * |
||
109 | */ |
||
110 | unregister ( adapter ) { |
||
111 | if ( (typeof adapter != 'string')) { |
||
112 | adapter = Siteapp.sys.functionName(adapter); |
||
0 ignored issues
–
show
|
|||
113 | } |
||
114 | if (this.isRegistered(name)) { |
||
0 ignored issues
–
show
The variable
name seems to be never declared. If this is a global, consider adding a /** global: name */ comment.
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. ![]() |
|||
115 | this._adapters[name] = undefined; |
||
116 | } |
||
117 | return this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Checks is an adapter is registered by name. |
||
122 | * |
||
123 | * @function |
||
124 | * @param {string} name |
||
125 | * @returns {boolean} |
||
126 | */ |
||
127 | isRegistered ( name ) { |
||
128 | |||
129 | return (typeof this._adapters[name] != 'undefined'); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Retrieves an adapter is registered by name. |
||
134 | * |
||
135 | * @function |
||
136 | * @param {string} name |
||
137 | * @returns {object} |
||
138 | */ |
||
139 | getAdapterByName ( name ) { |
||
140 | if (this.isRegistered(name)) { |
||
141 | return this._adapters[name]; |
||
142 | } |
||
143 | return null; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Retrive registered adapters |
||
148 | * |
||
149 | * @var [{object}] adapters |
||
150 | */ |
||
151 | get adapters () { |
||
152 | return this._adapters; |
||
153 | } |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Retrieve attached application. |
||
158 | */ |
||
159 | get application () { |
||
160 | return this._app; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Attach application object. |
||
165 | */ |
||
166 | set application ( app ) { |
||
167 | if (app instanceof Siteapp) { |
||
168 | this._app = app; |
||
169 | } else { |
||
170 | throw new AdapterManagerException('Invalid application object to attach'); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Retrieve current version. |
||
176 | */ |
||
177 | get version () { |
||
178 | return this._version; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | export default AdapterManager; |
||
183 | |||
184 |