-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompose-shell.js
More file actions
122 lines (111 loc) · 3.69 KB
/
compose-shell.js
File metadata and controls
122 lines (111 loc) · 3.69 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
var template = require('./templates/shell.hbs')
xtag.register('compose-shell', {
lifecycle: {
created: function(){
var params = xtag.queryChildren(this, 'compose-shell-param')
var buttons = xtag.queryChildren(this, 'compose-shell-button')
var oldInputs = this.querySelectorAll('input,select,textarea')
// Render the initial template
this.innerHTML = template()
for (var i in this.attributes) {
var name = this.attributes[i].nodeName
if (name)
this.form.setAttribute(name, this.attributes[i].value)
}
[].forEach.call(oldInputs, function(input){
this.form.appendChild(input)
}.bind(this))
var paramsEl = this.querySelector('.params')
for (var i in params) {
params[i].shell = this
paramsEl.appendChild(params[i])
}
var buttonsEl = this.querySelector('.buttons')
for (var i in buttons) {
buttons[i].shell = this
buttonsEl.appendChild(buttons[i])
}
}
},
events: {
'submit:delegate(form)': function(event) {
console.log('form submit')
event.currentTarget.generateInputs()
},
'toggle:delegate(compose-shell-button)': function(event) {
var toggle = event.target.getAttribute('toggle')
var shell = event.currentTarget
var param = shell.params[toggle]
if (param) {
param.toggle()
param.focusInput()
}
},
'show:delegate(compose-shell-param)': function(event){
var shell = event.currentTarget
if (this.dependency && !shell.params[this.dependency].visible) {
xtag.fireEvent(shell, 'notify', {detail: {message: this.name + ' requires ' + this.dependency}})
shell.params[this.dependency].show()
}
if (shell.buttons && shell.buttons[this.name])
shell.buttons[this.name].enabled = true
},
'hide:delegate(compose-shell-param)': function(event){
var shell = event.currentTarget
if (this.requiredBy && !this.visible && shell.params[this.requiredBy].visible) {
shell.params[this.requiredBy].hide()
}
if (shell.buttons && shell.buttons[this.name])
shell.buttons[this.name].enabled = false
},
'keypress:keypass(13):delegate(compose-shell-param)': function(event){
event.preventDefault()
var submitEvent = new Event('submit', {bubbles: true, cancelable: true})
var form = event.currentTarget.form
form.dispatchEvent(submitEvent)
setTimeout(function(){
if (!submitEvent.defaultPrevented) {
form.submit()
}
}, 50)
},
notify: function(event){
if (this.onNotify)
this.onNotify(event.detail.message)
}
},
accessors: {
form: { get: function(){ return this.querySelector('form') } },
onNotify: {
get: function(){
var notify = this.getAttribute('on-notify')
return notify && eval(notify) || false
}
}
},
methods: {
registerParam: function(param){
this.params = this.params || {}
this.params[param.name] = param
if (param.dependency && this.params[param.dependency])
this.params[param.dependency].requiredBy = param.name
},
registerButton: function(button){
this.buttons = this.buttons || {}
this.buttons[button.toggle] = button
if (this.params[button.toggle].visible)
button.enabled = true
},
generateInputs: function() {
for (var name in this.params) {
if (this.params[name].value) {
var input = document.createElement('input')
input.type = 'hidden'
input.value = this.params[name].value
input.name = name
this.form.appendChild(input)
}
}
}
}
})