-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafter_dive.js
158 lines (156 loc) · 4.32 KB
/
after_dive.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import morphdom from 'morphdom'
export default {
init(baseElement, submitter, resolve, reject, isDev){
return {baseElement, submitter, resolve, reject, isDev, ...this}
},
getThing(obj, optional = false, doc = document){
let el
if(obj.id){
el = doc.getElementById(obj.id)
if(!el && !optional && this.isDev){console.log('could not find object with id: ', obj.id)}
} else if(obj.selector){
el = this.getSelector(obj.selector, doc)
if(!el && !optional && this.isDev){console.log('could not find object with selector: ', obj.selector)}
} else if(!optional && this.isDev) {
console.log('expected a node identifier (either a "selector" field or an "id" field)')
}
return el
},
getSelector(str, doc = document) {
if(str == "submitter") {
return this.submitter
} else if (str == "baseElement") {
return this.baseElement
} else {
return doc.querySelector(str)
}
},
log(obj){
console.log(obj)
},
reload(){
Turbo.cache.clear()
otty.goto(window.location.href, {reload: true})
// window.Turbo.visit(window.location.href, {action: 'replace'})
},
redirect(obj){
otty.goto(obj)
},
insert(obj){
let sel = this.getThing(obj)
if(!sel) { return }
let pos = obj['position']
let html = obj['html']
sel.insertAdjacentHTML(pos, html)
},
_morphOpts(opts) { //this is used externally
let perm = opts['permanent']
let ign = opts['ignore']
if(opts == null) {
return {}
}
if(ign != null || perm != null) {
let m_parse_p = (selector, from, to) => {
if(from.matches(selector) && to.matches(selector)) {
return false
}
return true
}
let m_parse_i = (selector, from, to) => {
if(from.matches(selector) || to.matches(selector)) {
return false
}
return true
}
let m_parse = (selectors, inner_parse, from, to) => {
if(!(Array.isArray(selectors))) {
selectors = [selectors]
}
for(let y = 0; y < selectors.length; y++) {
if(!inner_parse(selectors[y], from, to)){
return false
}
}
return true
}
opts['onBeforeElChildrenUpdated'] = (from, to) => {
if(! m_parse(ign, m_parse_i, from, to) ) {
return false
}
if(! m_parse(perm, m_parse_p, from, to) ) {
return false
}
return true
}
}
return opts
},
morph(opts) {
let s = this.getThing(opts)
if(!s) { return }
morphdom(s, opts['html'], this._morphOpts(opts))
},
remove(obj) {
let s = this.getThing(obj)
if(!s) { return }
s.parentNode.removeChild(s)
},
replace(obj){
//this method needs more scrutiny
//does not support baseElement or submitter selectors. Recommend using ids.
let sel, parser, tempdoc, orienter, childrenOnly
parser = new DOMParser();
tempdoc = parser.parseFromString(obj['html'], "text/html")
orienter = this.getThing(obj, false, tempdoc)
if(!orienter){return}
childrenOnly = obj['childrenOnly']
sel = this.getThing(obj)
if(!sel){return}
if(orienter == null) {
if(childrenOnly) {
sel.innerHTML = obj['html']
} else {
orienter = tempdoc.querySelector('body').children[0]
sel.replaceWith(orienter)
}
} else {
if(childrenOnly) {
sel.innerHTML = orienter.innerHTML
} else {
sel.replaceWith(orienter)
}
}
},
innerHtml(obj) {
let s = this.getThing(obj)
if(!s){return}
s.innerHTML = obj['html']
},
eval2(data) {
//anything weird? Just use this. You have access to anything in the hash.
//selector input is special and will automatically be set to the variable referenced.
//note it doesn't actually use eval for performance related reasons. Something about effects on minimization i believe.
//is this a security risk? We check we are connecting with ourselves above. So should be fine
let selector = getThing(data, true)
//continue being insane lol
let x = Function("data", "selector", 'baseElement', 'submitter', `"use strict"; ${data['code']};`)(data, selector, this.baseElement, this.submitter)
if(x == "break") { //lil bit of extra awesome
resolve(returning)
return "break"
}
},
setData(data){
let keys, x, key, obj, attrs, attr_keys, y, attr_key
keys = Object.keys(data)
for(x = 0; x < keys.length; x++) {
key = keys[x]
obj = this.getSelector(key)
attrs = data[key]
attr_keys = Object.keys(attrs)
for(y = 0; y < attr_keys.length; y++) {
attr_key = attr_keys[y]
obj.dataset[attr_key] = attrs[attr_key]
}
}
}
}