-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (81 loc) · 2.28 KB
/
index.js
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
'use strict';
// Dependencies
const Events = require('events');
const render = require('./lib/render');
const state = require('./lib/state');
/*
* Returns a flow-view instance
*
* example config
* config = {
* "templates": {
* "name": {
* "to": "#css",
* "css": ["/path/file.css"],
* "httpClient": "http_client",
* "html": "/path/file.html",
* "title": "Document title",
* "position:" "beforebegin|afterbegin|beforeend|afterend"
* "clear": false,
* "leaveKeys": true,
* "dontEscape": false,
* "events": {
* "listener": [
* {
* "on": "event",
* "sel": "#css",
* "elm": "elmName",
* "dontPrevent": true
* }
* ]
* }
* }
* },
* "states": {
* "stateA": [
* {
* "sel": "#css",
* "element": "elmName",
* "states": ["stateB"],
* "rm": ["className"],
* "add": ["className"],
* "toggle": ["className"]
* }
* ]
* }
* }
*/
module.exports = config => {
let self = new Events();
// create the config object
config = config || {};
self.config = {};
self.config.states = config.states || {};
self.config.templates = config.templates || {};
// caches
self.templates = {};
self.htmls = {};
// flow-view methods
self.state = state;
self.render = render;
self.addStates = states => {
Object.keys(states).forEach(stateName => {
self.config.states[stateName] = states[stateName];
});
};
self.addTemplates = templates => {
Object.keys(templates).forEach(templateName => {
self.config.templates[templateName] = templates[templateName]
});
};
self.close = () => {
window.close();
};
self.dom = key => {
let path = key.split(':');
// get find an element in the document
path[0] = document.querySelector(path[0]);
return path[0][path[1]] !== undefined ? path[0][path[1]] : path[0];
}
return self;
};