Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Monster
Manage
Activity
Members
Plan
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Javascript
Monster
Commits
068de5b0
Verified
Commit
068de5b0
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: new dom datasource
parent
bfec27fc
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
application/source/data/datasource/dom.mjs
+122
-0
122 additions, 0 deletions
application/source/data/datasource/dom.mjs
development/test/cases/data/datasource/dom.mjs
+18
-0
18 additions, 0 deletions
development/test/cases/data/datasource/dom.mjs
with
140 additions
and
0 deletions
application/source/data/datasource/dom.mjs
0 → 100644
+
122
−
0
View file @
068de5b0
/**
* Copyright schukai GmbH and contributors 2022. All Rights Reserved.
* Node module: @schukai/monster
* This file is licensed under the AGPLv3 License.
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
import
{
instanceSymbol
}
from
"
../../constants.mjs
"
;
import
{
isObject
}
from
"
../../types/is.mjs
"
;
import
{
Datasource
}
from
"
../datasource.mjs
"
;
export
{
DomStorage
};
/**
* The DomStorage is a class that stores data in memory.
*
* @license AGPLv3
* @copyright schukai GmbH
* @memberOf Monster.Data.Datasource
*/
class
DomStorage
extends
Datasource
{
/**
* @param {Object} [options] options contains definitions for the datasource.
*/
constructor
(
options
)
{
super
();
if
(
isObject
(
options
))
{
this
.
setOptions
(
options
);
}
}
/**
* This method is called by the `instanceof` operator.
* @returns {symbol}
*/
static
get
[
instanceSymbol
]()
{
return
Symbol
.
for
(
"
@schukai/monster/data/datasource/storage/dom-storage
"
);
}
/**
* @property {Object} defaults
* @property {Object} defaults.read
* @property {string} defaults.read.selector
* @property {Object} defaults.write
* @property {string} defaults.write.selector
*/
get
defaults
()
{
return
Object
.
assign
({},
super
.
defaults
,
{
read
:
{
selector
:
undefined
,
},
write
:
{
selector
:
undefined
,
}
});
}
/**
* @return {Promise}
* @throws {Error} The read selector is not defined
* @throws {Error} There are no storage element
*/
read
()
{
const
self
=
this
;
let
selector
=
self
.
getOption
(
"
read.selector
"
,
undefined
);
if
(
!
selector
)
{
throw
new
Error
(
"
The read selector is not defined
"
);
}
let
storage
=
document
.
querySelector
(
selector
);
if
(
!
storage
)
{
throw
new
Error
(
"
There are no storage element
"
);
}
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
let
data
=
JSON
.
parse
(
storage
.
innerHTML
);
self
.
set
(
data
)
resolve
(
data
);
}
catch
(
e
)
{
reject
(
e
);
}
;
})
}
/**
* @return {Promise}
* @throws {Error} The write selector is not defined
* @throws {Error} There are no storage element
*/
write
()
{
const
self
=
this
;
let
selector
=
self
.
getOption
(
"
write.selector
"
);
if
(
!
selector
)
{
throw
new
Error
(
"
The write selector is not defined
"
);
}
let
storage
=
document
.
querySelector
(
selector
);
if
(
!
storage
)
{
throw
new
Error
(
"
There are no storage element
"
);
}
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
storage
.
innerHTML
=
JSON
.
stringify
(
self
.
get
());
resolve
(
storage
);
}
catch
(
e
)
{
reject
(
e
);
}
})
}
}
This diff is collapsed.
Click to expand it.
development/test/cases/data/datasource/dom.mjs
0 → 100644
+
18
−
0
View file @
068de5b0
import
{
expect
}
from
"
chai
"
import
{
DomStorage
}
from
"
../../../../../application/source/data/datasource/dom.mjs
"
;
describe
(
'
ServeDomr
'
,
function
()
{
it
(
'
should init
'
,
function
()
{
const
dom
=
new
DomStorage
({
})
expect
(
dom
).
to
.
be
.
not
.
null
})
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment