wallace 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.js ADDED
@@ -0,0 +1,64 @@
1
+ import {doc, isStr, makeEl} from './helpers'
2
+ import {Wrapper} from './wrapper'
3
+
4
+ /**
5
+ * Creates and mounts a component onto an element.
6
+ *
7
+ * @param {unsure} elementOrId Either a string representing an id, or an element.
8
+ * @param {class} cls The class of Component to create
9
+ * @param {object} props The props to pass to the component (optional)
10
+ * @param {object} parent The parent component (optional)
11
+ */
12
+ export function mount(elementOrId, cls, props, parent) {
13
+ const component = createComponent(cls, parent, props)
14
+ const nodeToReplace = getElement(elementOrId)
15
+ nodeToReplace.parentNode.replaceChild(component.e, nodeToReplace)
16
+ return component
17
+ }
18
+
19
+ /**
20
+ * returns a Wrapper around an element.
21
+ *
22
+ * @param {unsure} elementOrId Either a string representing an id, or an element.
23
+ */
24
+ export function wrap(elementOrId) {
25
+ return new Wrapper(getElement(elementOrId))
26
+ }
27
+
28
+
29
+ function getElement(elementOrId) {
30
+ return isStr(elementOrId) ? document.getElementById(elementOrId) : elementOrId
31
+ }
32
+
33
+
34
+ /**
35
+ * Creates a component and initialises it.
36
+ *
37
+ * @param {class} cls The class of Component to create
38
+ * @param {object} parent The parent component (optional)
39
+ * @param {object} props The props to pass to the component (optional)
40
+ */
41
+ export function createComponent(cls, parent, props) {
42
+ const component = buildComponent(cls, parent)
43
+ component.props = props
44
+ component.init()
45
+ component.update()
46
+ return component
47
+ }
48
+
49
+ /**
50
+ * Builds a component.
51
+ */
52
+ export function buildComponent(cls, parent) {
53
+ const component = new cls(parent)
54
+ component.__bv(component, cls.prototype)
55
+ return component
56
+ }
57
+
58
+
59
+ /**
60
+ * Creates a wrapper of type tag e.g. h('div')
61
+ */
62
+ export function h(tag) {
63
+ return new Wrapper(doc.createElement(tag))
64
+ }
package/src/wrapper.js ADDED
@@ -0,0 +1,152 @@
1
+ import {doc} from './helpers'
2
+
3
+
4
+ /**
5
+ * A wrapper around a DOM element.
6
+ * All transformative methods return this (except transitions as they return promises)
7
+ * This means those methods can be chained.
8
+ */
9
+ export function Wrapper(element) {
10
+ this.e = element
11
+ this._pool = undefined
12
+ }
13
+
14
+ Wrapper.prototype = {
15
+ /**
16
+ * Get element as 'e' from item, else return text node.
17
+ */
18
+ __ge: function(item) {
19
+ return item.e || doc.createTextNode(item)
20
+ },
21
+ /**
22
+ * Gets an attribute from the element. Cannot be chained.
23
+ */
24
+ getAtt: function(name) {
25
+ return this.e[name]
26
+ },
27
+ /**
28
+ * Gets the element's value. Cannot be chained.
29
+ */
30
+ getValue: function() {
31
+ return this.e.value
32
+ },
33
+ isChecked: function() {
34
+ return this.e.checked
35
+ },
36
+ /* Every method below must return 'this' so it can be chained */
37
+ append: function(item) {
38
+ this.e.appendChild(this.__ge(item))
39
+ return this
40
+ },
41
+ att: function(name, value) {
42
+ this.e.setAttribute(name, value)
43
+ return this
44
+ },
45
+ atts: function(atts) {
46
+ for (let name in atts) {
47
+ this.att(name, atts[name])
48
+ }
49
+ return this
50
+ },
51
+ pool: function(pool) {
52
+ this._pool = pool
53
+ return this
54
+ },
55
+ clear: function() {
56
+ this.e.innerHTML = ''
57
+ this.e.textContent = ''
58
+ this.e.value = ''
59
+ return this
60
+ },
61
+ checked: function(value) {
62
+ this.e.checked = !!value
63
+ return this
64
+ },
65
+ child: function(wrapper) {
66
+ this.e.innerHTML = ''
67
+ this.e.appendChild(wrapper.e)
68
+ return this
69
+ },
70
+ css: function(style) {
71
+ this.e.className = style
72
+ return this
73
+ },
74
+ cssAdd: function(style) {
75
+ this.e.classList.add(style)
76
+ return this
77
+ },
78
+ cssRemove: function(style) {
79
+ this.e.classList.remove(style)
80
+ return this
81
+ },
82
+ cssToggle: function(style) {
83
+ this.e.classList.toggle(style)
84
+ return this
85
+ },
86
+ disabled: function(disabled) {
87
+ this.e.disabled = disabled
88
+ return this
89
+ },
90
+ href: function(value) {
91
+ return this.att('href', value)
92
+ },
93
+ html: function(html) {
94
+ this.e.innerHTML = html
95
+ return this
96
+ },
97
+ id: function(value) {
98
+ return this.att('id', value)
99
+ },
100
+ /*
101
+ * Set inner as individual item or array. Not optimised.
102
+ */
103
+ inner: function(items) {
104
+ if (!Array.isArray(items)) {
105
+ items = [items]
106
+ }
107
+ const e = this.e
108
+ e.innerHTML = ''
109
+ for (var i=0, il=items.length; i<il; i++) {
110
+ e.appendChild(this.__ge(items[i]))
111
+ }
112
+ return this
113
+ },
114
+ /*
115
+ * Set items from pool.
116
+ */
117
+ items: function(items, parent) {
118
+ this._pool.patch(this.e, items, parent)
119
+ return this
120
+ },
121
+ on: function(event, callback) {
122
+ this.e.addEventListener(event, e => callback(this, e))
123
+ return this
124
+ },
125
+ replace: function(el) {
126
+ this.e.parentNode.replaceChild(el, this.e)
127
+ return this
128
+ },
129
+ src: function(value) {
130
+ return this.att('src', value)
131
+ },
132
+ style: function(name, value) {
133
+ this.e.style[name] = value
134
+ return this
135
+ },
136
+ swap: function(key, parent) {
137
+ this.child(this._pool.getOne(key, parent))
138
+ return this
139
+ },
140
+ text: function(value) {
141
+ this.e.textContent = value
142
+ return this
143
+ },
144
+ visible: function(visible) {
145
+ this.e.classList.toggle('hidden', !visible)
146
+ return this
147
+ },
148
+ value: function(value) {
149
+ this.e.value = value
150
+ return this
151
+ }
152
+ }