-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompose-shell-param.js
More file actions
226 lines (204 loc) · 7.39 KB
/
compose-shell-param.js
File metadata and controls
226 lines (204 loc) · 7.39 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var template = require('./templates/param.hbs')
var BSON = require('./lib/bson')
var cssAnimEventTypes = require('./lib/anim-events')
xtag.register('compose-shell-param', {
lifecycle: {
created: function(){
this.params = xtag.queryChildren(this, 'compose-shell-param')
this.group = !!this.params.length
this.innerHTML = template({
before: this.getAttribute('before'),
group: this.group,
editable: this.editable,
value: this.parseValue(this.getAttribute('value')),
type: this.type,
content: this.textContent,
after: this.getAttribute('after'),
placeholder: this.placeholder
})
},
inserted: function(){
this.hide()
if (this.params.length > 0) {
var groupEl = this.querySelector('.params-group')
for (var param in this.params) {
// this.onShellSet.push(function(){
// this.params[param].shell = this.shell
// })
this.params[param].shell = this.shell
this.params[param].addEventListener('show', this.updateVisibility.bind(this))
this.params[param].addEventListener('hide', this.updateVisibility.bind(this))
groupEl.appendChild(this.params[param])
}
}
if (this.type)
this.shell.registerParam(this)// this.onShellSet = [function(){this.shell.registerParam(this)}].concat(this.onShellSet)
if (this.type === 'text' || (this.getAttribute('value') || this.required))
this.visible = true
if (this.getAttribute('focus'))
this.focusInput()
}
},
events: {
'focus:delegate(span[contenteditable])': function(event) {
var param = event.currentTarget
if (param.group) return
if (param.placeholder) {
if (this.textContent === param.placeholder && /placeholder/.test(this.className))
this.textContent = ''
}
param.setCursorAtEnd()
},
'blur:delegate(span[contenteditable])': function(event) {
var param = event.currentTarget
if (param.group) return
if (param.placeholder)
if (this.textContent === '') {
this.textContent = param.placeholder
if (!/placeholder/.test(this.className))
this.className += ' placeholder'
} else {
this.className = this.className.replace('placeholder', '')
}
},
'paste:delegate(span[contenteditable])': function(event){
var param = event.currentTarget
if (param.group) return
event.preventDefault()
if (event.clipboardData) {
var content = (event.originalEvent || event).clipboardData.getData('text/plain').replace(/^\s+|\s+$/g, '')
document.execCommand('insertText', false, content)
}
else if (window.clipboardData) {
var content = window.clipboardData.getData('Text').replace(/^\s+|\s+$/g, '')
document.selection.createRange().pasteHTML(content)
}
},
show: function(event){
if (this.hint)
this.showHint()
}
},
accessors: {
// params: { get: function(){ return xtag.queryChildren(this, 'compose-shell-param') } },
name: { get: function(){ return this.getAttribute('name') } },
type: { get: function(){ return this.getAttribute('type') } },
placeholder: { get: function(){ return this.getAttribute('placeholder') } },
hint: { get: function(){ return this.getAttribute('hint') } },
required: { get: function(){ return this.getAttribute('required') } },
focus: { get: function(){ return this.getAttribute('focus') } },
// group: { get: function() { return this.params.length > 0 } },
editable: { get: function() { return !this.group && this.type && this.type !== 'boolean' } },
optional: { get: function(){ return !!this.getAttribute('optional') } },
dependency: { get: function(){ return this.getAttribute('dependency') } },
customInput: { get: function() { return this.querySelector('span[contenteditable]') } },
parser: { get: function() { return this.getAttribute('parser') } },
visible: {
get: function(){ return !this.getAttribute('hidden') },
set: function(visible){
if (visible === true) {
this.removeAttribute('hidden')
xtag.fireEvent(this, 'show')
} else if (visible === false) {
this.setAttribute('hidden', true)
xtag.fireEvent(this, 'hide')
}
}
},
value: {
get: function(){
var val;
if (!this.visible)
return null // no value
if (this.type === 'boolean') {
val = 1
} else if (this.customInput) {
// ensure stuff by blurring.
this.customInput.blur()
val = this.customInput.textContent.replace(/[\u200B-\u200D\uFEFF]/g, '')
if (this.placeholder) {
if (/placeholder/.test(this.customInput.className) && val === this.placeholder)
val = ""
}
if (val && this.type === 'hash')
val = '{' + val + '}'
}
return this.serializeValue(val)
}
}
},
methods: {
toggle: function(){ this.visible = !this.visible },
show: function(){ this.visible = true },
hide: function(){ this.visible = false },
focusInput: function(){
if (this.customInput)
this.customInput.focus()
},
updateVisibility: function(){
this.visible = [].some.call(this.params, function(child){ return child.visible })
},
showHint: function(){
var hintEl = this.querySelector('.hint')
if (hintEl)
this.removeChild(hintEl)
hintEl = document.createElement('span')
hintEl.className = 'hint'
hintEl.textContent = this.hint
clearTimeout(this.hintTimeout)
this.appendChild(hintEl)
this.hintTimeout = setTimeout(function(){
hintEl.className += ' out'
hintEl.addEventListener(cssAnimEventTypes.end, function animEnd(event){
this.removeChild(hintEl)
hintEl.removeEventListener(cssAnimEventTypes.end, animEnd)
}.bind(this), false)
}.bind(this), 2000)
},
setCursorAtEnd: function(){
var text = this.customInput.firstChild
if(text)
this.setSelection(text.length, 0)
},
setSelection: function(start, length) {
var el = this.customInput
var range = document.createRange()
range.setStart(el.firstChild, start)
range.setEnd(el.firstChild, start + length)
var sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(range)
},
serializeValue: function(value){
if (!this.parser)
return value
if (this.parser === 'bson') {
try {
return JSON.stringify(BSON.bsonEval(value))
} catch (error) {
console.log(error)
xtag.fireEvent(this, 'error', {detail: {error: new Error('Unserializable value for ' + this.name)}})
return value
}
}
},
parseValue: function(value){
if (!this.parser)
return value
if (this.parser === 'bson' && value) {
try {
return stripWrapper(BSON.toBsonString(JSON.parse(value), {indentation: 0}))
} catch (error) {
console.log(error)
xtag.fireEvent(this, 'error', {detail: {error: new Error('Unparsable value for ' + this.name)}})
return value
}
}
}
}
})
function stripWrapper(queryString) {
var matches = queryString.match(/\{(.+)\}/)
if (matches)
return matches[1]
}