Browse code
misc: ???
Ed Langley authored on 21/02/2020 03:18:00
Showing 2 changed files
Showing 2 changed files
... | ... |
@@ -4,256 +4,256 @@ |
4 | 4 |
* See the COPYING document in the root of the source distribution for licensing information. |
5 | 5 |
*/ |
6 | 6 |
PresentationType = (function () { |
7 |
- var presentationTypes = {}; |
|
8 |
- |
|
9 |
- // TODO: think through selector better: right now, I think we assume that it |
|
10 |
- // is a class selector and things will break if it isn't we should, |
|
11 |
- // however, probably let it be more general. |
|
12 |
- function PresentationType(name, selector) { |
|
13 |
- if (this instanceof PresentationType) { |
|
14 |
- this.name = name; |
|
15 |
- if (selector === undefined) { |
|
16 |
- selector = '.'+this.name; |
|
17 |
- } |
|
18 |
- this.selector = selector; |
|
19 |
- this.receivers = []; |
|
20 |
- } else { |
|
21 |
- var result = presentationTypes[name]; |
|
22 |
- if (result === undefined) { |
|
23 |
- result = new PresentationType(name); |
|
24 |
- presentationTypes[result.name] = result; |
|
25 |
- } |
|
26 |
- return result; |
|
27 |
- } |
|
28 |
- } |
|
29 |
- |
|
30 |
- PresentationType.prototype = { |
|
31 |
- presentationsTypes: presentationTypes, |
|
32 |
- |
|
33 |
- isOfType(name) { |
|
34 |
- var type = presentationTypes[name]; |
|
35 |
- //console.log(name); |
|
36 |
- //console.log(type); |
|
37 |
- return type.isPrototypeOf(this); |
|
38 |
- }, |
|
39 |
- |
|
40 |
- addReceiver(receiver) { |
|
41 |
- this.receivers.push(receiver); |
|
42 |
- return this.receivers.length - 1; |
|
43 |
- }, |
|
44 |
- |
|
45 |
- removeReceiver(el) { |
|
46 |
- var elIdx = this.receivers.indexOf(el); |
|
47 |
- var result = elIdx !== -1; |
|
48 |
- if (result) { |
|
49 |
- this.receivers.splice(elIdx,1); |
|
50 |
- } |
|
51 |
- return result; |
|
52 |
- }, |
|
53 |
- |
|
54 |
- notifyReceivers(theEvent, self) { |
|
55 |
- if (self === undefined) { |
|
56 |
- self = this; |
|
57 |
- } |
|
58 |
- |
|
59 |
- var outerArgs = Array.prototype.slice.call(arguments); |
|
60 |
- outerArgs.unshift(theEvent, self, self.value); |
|
61 |
- |
|
62 |
- this.receivers.forEach((function (theReceiver) { |
|
63 |
- theReceiver.receive.apply(theReceiver, outerArgs); |
|
64 |
- })); |
|
65 |
- |
|
66 |
- var parent = Object.getPrototypeOf(this); |
|
67 |
- if (parent instanceof PresentationType && parent.receivers !== undefined && parent.receivers !== []) { |
|
68 |
- parent.notifyReceivers(theEvent, self); |
|
69 |
- } |
|
70 |
- }, |
|
71 |
- |
|
72 |
- selectAllTags() { |
|
73 |
- return Array.prototype.slice.call( |
|
74 |
- document.querySelectorAll(this.selector) |
|
75 |
- ); |
|
76 |
- }, |
|
77 |
- |
|
78 |
- selectAll() { |
|
79 |
- var allTags = this.selectAllTags(); |
|
80 |
- return Array.prototype.map.call(allTags, function (el) { |
|
81 |
- return el.ptObject; |
|
82 |
- }); |
|
83 |
- }, |
|
84 |
- |
|
85 |
- bindTags() { |
|
86 |
- var els = this.selectAllTags().filter(function (theEl) { |
|
87 |
- return theEl.ptObject === undefined; |
|
88 |
- }); |
|
89 |
- |
|
90 |
- els.forEach(this.bindEl, this); |
|
91 |
- |
|
92 |
- return this; |
|
93 |
- }, |
|
94 |
- |
|
95 |
- bindEl(theEl) { |
|
96 |
- var content = theEl.textContent.trim(); |
|
97 |
- var value = theEl.value; |
|
98 |
- |
|
99 |
- var bindTypes = theEl.dataset.bind; |
|
100 |
- if (bindTypes !== undefined) { |
|
101 |
- bindTypes = bindTypes.split(','); |
|
102 |
- } else { |
|
103 |
- bindTypes = ['click']; |
|
104 |
- } |
|
105 |
- |
|
106 |
- //console.log('bt',bindTypes); |
|
107 |
- |
|
108 |
- if (! theEl.hasAttribute('value') ) { |
|
109 |
- var tmpVal = theEl.dataset.value; |
|
110 |
- value = tmpVal === undefined ? content : theEl.dataset.value; |
|
111 |
- } |
|
112 |
- |
|
113 |
- return this.wrapTag(theEl, value, bindTypes, content); |
|
114 |
- }, |
|
115 |
- |
|
116 |
- wrapTag(tag, value, bindTypes, content) { |
|
117 |
- var result = Object.assign( |
|
118 |
- Object.create(this), { |
|
119 |
- tag: tag, |
|
120 |
- value: value, |
|
121 |
- display: content, |
|
122 |
- } |
|
123 |
- ); |
|
124 |
- tag.ptObject = result; |
|
125 |
- |
|
126 |
- this.classes.forEach(function (cls) { |
|
127 |
- tag.classList.add(cls); |
|
128 |
- }); |
|
129 |
- |
|
130 |
- bindTypes.forEach(function (event) { |
|
131 |
- //console.log(event); |
|
132 |
- tag.addEventListener(event, result, false); |
|
133 |
- }); |
|
134 |
- |
|
135 |
- if (result.validate()) { |
|
136 |
- return result; |
|
137 |
- } else { |
|
138 |
- this.doError(result); |
|
139 |
- } |
|
140 |
- }, |
|
141 |
- |
|
142 |
- get classes() { |
|
143 |
- var current = this, |
|
144 |
- classes = new Set(); |
|
145 |
- while (current instanceof PresentationType) { |
|
146 |
- if (current.name !== undefined) { |
|
147 |
- classes.add(current.name); |
|
148 |
- } |
|
149 |
- current = Object.getPrototypeOf(current); |
|
150 |
- } |
|
151 |
- |
|
152 |
- return classes; |
|
153 |
- }, |
|
154 |
- |
|
155 |
- extend(name, props) { |
|
156 |
- if (this.children === undefined) { |
|
157 |
- this.children = []; |
|
158 |
- } |
|
159 |
- |
|
160 |
- if (props === undefined) { |
|
161 |
- props = {}; |
|
162 |
- } |
|
163 |
- |
|
164 |
- props.name = name; |
|
165 |
- if (props.selector === undefined) { |
|
166 |
- props.selector = '.' + name; |
|
167 |
- } |
|
168 |
- props.receivers = []; |
|
169 |
- var newType = Object.assign(Object.create(this), props); |
|
170 |
- presentationTypes[name] = newType; |
|
171 |
- this.children.push(newType); |
|
172 |
- return newType; |
|
173 |
- }, |
|
174 |
- |
|
175 |
- makeTag(value, tagName, bindTypes, content) { |
|
176 |
- var newTag = document.createElement(tagName); |
|
177 |
- |
|
178 |
- this.classes.forEach(function (cls) { newTag.classList.add(cls); }); |
|
179 |
- |
|
180 |
- if (bindTypes === undefined) { |
|
181 |
- bindTypes = []; |
|
182 |
- } else if (!(bindTypes instanceof Array)) { |
|
183 |
- content = bindTypes; |
|
184 |
- bindTypes = []; |
|
185 |
- } |
|
186 |
- |
|
187 |
- if (content === undefined) { |
|
188 |
- content = value; |
|
189 |
- } |
|
190 |
- |
|
191 |
- newTag.textContent = content; |
|
192 |
- return this.wrapTag(newTag, value, bindTypes, content); |
|
193 |
- }, |
|
194 |
- |
|
195 |
- handleEvent(theEvent) { |
|
196 |
- //console.log('handling: ',theEvent); |
|
197 |
- var handler = this[theEvent.type]; |
|
198 |
- var result = true; |
|
199 |
- if (handler !== undefined) { |
|
200 |
- result = handler.bind(this)(theEvent); |
|
201 |
- } |
|
202 |
- this.notifyReceivers(theEvent); |
|
203 |
- |
|
204 |
- return result; |
|
205 |
- }, |
|
206 |
- |
|
207 |
- doError(object) { |
|
208 |
- return; |
|
209 |
- }, |
|
210 |
- |
|
211 |
- validate() { |
|
212 |
- return true; |
|
213 |
- }, |
|
214 |
- |
|
215 |
- toggleAll() { |
|
216 |
- var els = this.selectAllTags(); |
|
217 |
- |
|
218 |
- els.forEach(function (el) { |
|
219 |
- el.classList.toggle('activated'); |
|
220 |
- }); |
|
221 |
- }, |
|
222 |
- |
|
223 |
- deactivateAll() { |
|
224 |
- var els = this.selectAllTags(); |
|
225 |
- |
|
226 |
- els.forEach(function (el) { |
|
227 |
- el.classList.remove('activated'); |
|
228 |
- }); |
|
229 |
- }, |
|
230 |
- |
|
231 |
- activateAll() { |
|
232 |
- this.bindTags(); |
|
233 |
- var els = this.selectAllTags(); |
|
234 |
- |
|
235 |
- els.forEach(function (el) { |
|
236 |
- el.classList.add('activated'); |
|
237 |
- }); |
|
238 |
- |
|
239 |
- return els; |
|
240 |
- }, |
|
241 |
- |
|
242 |
- |
|
243 |
- }; |
|
244 |
- |
|
245 |
- var typeType = PresentationType('type'); |
|
246 |
- typeType.generateTags = function (tagName) { |
|
247 |
- if (tagName === undefined) { |
|
248 |
- tagName = 'span'; |
|
7 |
+ var presentationTypes = {}; |
|
8 |
+ |
|
9 |
+ // TODO: think through selector better: right now, I think we assume that it |
|
10 |
+ // is a class selector and things will break if it isn't we should, |
|
11 |
+ // however, probably let it be more general. |
|
12 |
+ function PresentationType(name, selector) { |
|
13 |
+ if (this instanceof PresentationType) { |
|
14 |
+ this.name = name; |
|
15 |
+ if (selector === undefined) { |
|
16 |
+ selector = '.'+this.name; |
|
17 |
+ } |
|
18 |
+ this.selector = selector; |
|
19 |
+ this.receivers = []; |
|
20 |
+ } else { |
|
21 |
+ var result = presentationTypes[name]; |
|
22 |
+ if (result === undefined) { |
|
23 |
+ result = new PresentationType(name); |
|
24 |
+ presentationTypes[result.name] = result; |
|
25 |
+ } |
|
26 |
+ return result; |
|
27 |
+ } |
|
249 | 28 |
} |
250 | 29 |
|
251 |
- return Object.keys(presentationTypes).map( |
|
252 |
- function (type) { |
|
253 |
- var tag = document.createElement(tagName); |
|
254 |
- return this.wrapTag(tag, type, ['click'], type); |
|
255 |
- }, this |
|
256 |
- ); |
|
257 |
- }; |
|
258 |
- return PresentationType; |
|
30 |
+ PresentationType.prototype = { |
|
31 |
+ presentationsTypes: presentationTypes, |
|
32 |
+ |
|
33 |
+ isOfType(name) { |
|
34 |
+ var type = presentationTypes[name]; |
|
35 |
+ //console.log(name); |
|
36 |
+ //console.log(type); |
|
37 |
+ return type.isPrototypeOf(this); |
|
38 |
+ }, |
|
39 |
+ |
|
40 |
+ addReceiver(receiver) { |
|
41 |
+ this.receivers.push(receiver); |
|
42 |
+ return this.receivers.length - 1; |
|
43 |
+ }, |
|
44 |
+ |
|
45 |
+ removeReceiver(el) { |
|
46 |
+ var elIdx = this.receivers.indexOf(el); |
|
47 |
+ var result = elIdx !== -1; |
|
48 |
+ if (result) { |
|
49 |
+ this.receivers.splice(elIdx,1); |
|
50 |
+ } |
|
51 |
+ return result; |
|
52 |
+ }, |
|
53 |
+ |
|
54 |
+ notifyReceivers(theEvent, self) { |
|
55 |
+ if (self === undefined) { |
|
56 |
+ self = this; |
|
57 |
+ } |
|
58 |
+ |
|
59 |
+ var outerArgs = Array.prototype.slice.call(arguments); |
|
60 |
+ outerArgs.unshift(theEvent, self, self.value); |
|
61 |
+ |
|
62 |
+ this.receivers.forEach((function (theReceiver) { |
|
63 |
+ theReceiver.receive.apply(theReceiver, outerArgs); |
|
64 |
+ })); |
|
65 |
+ |
|
66 |
+ var parent = Object.getPrototypeOf(this); |
|
67 |
+ if (parent instanceof PresentationType && parent.receivers !== undefined && parent.receivers !== []) { |
|
68 |
+ parent.notifyReceivers(theEvent, self); |
|
69 |
+ } |
|
70 |
+ }, |
|
71 |
+ |
|
72 |
+ selectAllTags() { |
|
73 |
+ return Array.prototype.slice.call( |
|
74 |
+ document.querySelectorAll(this.selector) |
|
75 |
+ ); |
|
76 |
+ }, |
|
77 |
+ |
|
78 |
+ selectAll() { |
|
79 |
+ var allTags = this.selectAllTags(); |
|
80 |
+ return Array.prototype.map.call(allTags, function (el) { |
|
81 |
+ return el.ptObject; |
|
82 |
+ }); |
|
83 |
+ }, |
|
84 |
+ |
|
85 |
+ bindTags() { |
|
86 |
+ var els = this.selectAllTags().filter(function (theEl) { |
|
87 |
+ return theEl.ptObject === undefined; |
|
88 |
+ }); |
|
89 |
+ |
|
90 |
+ els.forEach(this.bindEl, this); |
|
91 |
+ |
|
92 |
+ return this; |
|
93 |
+ }, |
|
94 |
+ |
|
95 |
+ bindEl(theEl) { |
|
96 |
+ var content = theEl.textContent.trim(); |
|
97 |
+ var value = theEl.value; |
|
98 |
+ |
|
99 |
+ var bindTypes = theEl.dataset.bind; |
|
100 |
+ if (bindTypes !== undefined) { |
|
101 |
+ bindTypes = bindTypes.split(','); |
|
102 |
+ } else { |
|
103 |
+ bindTypes = ['click']; |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ //console.log('bt',bindTypes); |
|
107 |
+ |
|
108 |
+ if (! theEl.hasAttribute('value') ) { |
|
109 |
+ var tmpVal = theEl.dataset.value; |
|
110 |
+ value = tmpVal === undefined ? content : theEl.dataset.value; |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ return this.wrapTag(theEl, value, bindTypes, content); |
|
114 |
+ }, |
|
115 |
+ |
|
116 |
+ wrapTag(tag, value, bindTypes, content) { |
|
117 |
+ var result = Object.assign( |
|
118 |
+ Object.create(this), { |
|
119 |
+ tag: tag, |
|
120 |
+ value: value, |
|
121 |
+ display: content, |
|
122 |
+ } |
|
123 |
+ ); |
|
124 |
+ tag.ptObject = result; |
|
125 |
+ |
|
126 |
+ this.classes.forEach(function (cls) { |
|
127 |
+ tag.classList.add(cls); |
|
128 |
+ }); |
|
129 |
+ |
|
130 |
+ bindTypes.forEach(function (event) { |
|
131 |
+ //console.log(event); |
|
132 |
+ tag.addEventListener(event, result, false); |
|
133 |
+ }); |
|
134 |
+ |
|
135 |
+ if (result.validate()) { |
|
136 |
+ return result; |
|
137 |
+ } else { |
|
138 |
+ this.doError(result); |
|
139 |
+ } |
|
140 |
+ }, |
|
141 |
+ |
|
142 |
+ get classes() { |
|
143 |
+ var current = this, |
|
144 |
+ classes = new Set(); |
|
145 |
+ while (current instanceof PresentationType) { |
|
146 |
+ if (current.name !== undefined) { |
|
147 |
+ classes.add(current.name); |
|
148 |
+ } |
|
149 |
+ current = Object.getPrototypeOf(current); |
|
150 |
+ } |
|
151 |
+ |
|
152 |
+ return classes; |
|
153 |
+ }, |
|
154 |
+ |
|
155 |
+ extend(name, props) { |
|
156 |
+ if (this.children === undefined) { |
|
157 |
+ this.children = []; |
|
158 |
+ } |
|
159 |
+ |
|
160 |
+ if (props === undefined) { |
|
161 |
+ props = {}; |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ props.name = name; |
|
165 |
+ if (props.selector === undefined) { |
|
166 |
+ props.selector = '.' + name; |
|
167 |
+ } |
|
168 |
+ props.receivers = []; |
|
169 |
+ var newType = Object.assign(Object.create(this), props); |
|
170 |
+ presentationTypes[name] = newType; |
|
171 |
+ this.children.push(newType); |
|
172 |
+ return newType; |
|
173 |
+ }, |
|
174 |
+ |
|
175 |
+ makeTag(value, tagName, bindTypes, content) { |
|
176 |
+ var newTag = document.createElement(tagName); |
|
177 |
+ |
|
178 |
+ this.classes.forEach(function (cls) { newTag.classList.add(cls); }); |
|
179 |
+ |
|
180 |
+ if (bindTypes === undefined) { |
|
181 |
+ bindTypes = []; |
|
182 |
+ } else if (!(bindTypes instanceof Array)) { |
|
183 |
+ content = bindTypes; |
|
184 |
+ bindTypes = []; |
|
185 |
+ } |
|
186 |
+ |
|
187 |
+ if (content === undefined) { |
|
188 |
+ content = value; |
|
189 |
+ } |
|
190 |
+ |
|
191 |
+ newTag.textContent = content; |
|
192 |
+ return this.wrapTag(newTag, value, bindTypes, content); |
|
193 |
+ }, |
|
194 |
+ |
|
195 |
+ handleEvent(theEvent) { |
|
196 |
+ //console.log('handling: ',theEvent); |
|
197 |
+ var handler = this[theEvent.type]; |
|
198 |
+ var result = true; |
|
199 |
+ if (handler !== undefined) { |
|
200 |
+ result = handler.bind(this)(theEvent); |
|
201 |
+ } |
|
202 |
+ this.notifyReceivers(theEvent); |
|
203 |
+ |
|
204 |
+ return result; |
|
205 |
+ }, |
|
206 |
+ |
|
207 |
+ doError(object) { |
|
208 |
+ return; |
|
209 |
+ }, |
|
210 |
+ |
|
211 |
+ validate() { |
|
212 |
+ return true; |
|
213 |
+ }, |
|
214 |
+ |
|
215 |
+ toggleAll() { |
|
216 |
+ var els = this.selectAllTags(); |
|
217 |
+ |
|
218 |
+ els.forEach(function (el) { |
|
219 |
+ el.classList.toggle('activated'); |
|
220 |
+ }); |
|
221 |
+ }, |
|
222 |
+ |
|
223 |
+ deactivateAll() { |
|
224 |
+ var els = this.selectAllTags(); |
|
225 |
+ |
|
226 |
+ els.forEach(function (el) { |
|
227 |
+ el.classList.remove('activated'); |
|
228 |
+ }); |
|
229 |
+ }, |
|
230 |
+ |
|
231 |
+ activateAll() { |
|
232 |
+ this.bindTags(); |
|
233 |
+ var els = this.selectAllTags(); |
|
234 |
+ |
|
235 |
+ els.forEach(function (el) { |
|
236 |
+ el.classList.add('activated'); |
|
237 |
+ }); |
|
238 |
+ |
|
239 |
+ return els; |
|
240 |
+ }, |
|
241 |
+ |
|
242 |
+ |
|
243 |
+ }; |
|
244 |
+ |
|
245 |
+ var typeType = PresentationType('type'); |
|
246 |
+ typeType.generateTags = function (tagName) { |
|
247 |
+ if (tagName === undefined) { |
|
248 |
+ tagName = 'span'; |
|
249 |
+ } |
|
250 |
+ |
|
251 |
+ return Object.keys(presentationTypes).map( |
|
252 |
+ function (type) { |
|
253 |
+ var tag = document.createElement(tagName); |
|
254 |
+ return this.wrapTag(tag, type, ['click'], type); |
|
255 |
+ }, this |
|
256 |
+ ); |
|
257 |
+ }; |
|
258 |
+ return PresentationType; |
|
259 | 259 |
})(); |