-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
svgSourceDialog.js
218 lines (204 loc) · 5.56 KB
/
svgSourceDialog.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
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
/* globals svgEditor */
import svgSourceDialogHTML from './svgSourceDialog.html'
const template = document.createElement('template')
template.innerHTML = svgSourceDialogHTML
/**
* @class SeSvgSourceEditorDialog
*/
export class SeSvgSourceEditorDialog extends HTMLElement {
/**
* @function constructor
*/
constructor () {
super()
// create the shadowDom and insert the template
this._shadowRoot = this.attachShadow({ mode: 'open' })
this._shadowRoot.append(template.content.cloneNode(true))
this.$dialog = this._shadowRoot.querySelector('#svg_source_editor')
this.$copyBtn = this._shadowRoot.querySelector('#copy_save_done')
this.$saveBtn = this._shadowRoot.querySelector('#tool_source_save')
this.$cancelBtn = this._shadowRoot.querySelector('#tool_source_cancel')
this.$sourceTxt = this._shadowRoot.querySelector('#svg_source_textarea')
this.$copySec = this._shadowRoot.querySelector('#save_output_btns')
this.$applySec = this._shadowRoot.querySelector('#tool_source_back')
this.$toggleDynamic = this._shadowRoot.querySelector('#tool_source_dynamic')
this.$toggleDynamic.checked = svgEditor.configObj.curConfig.dynamicOutput
}
/**
* @function init
* @param {any} name
* @returns {void}
*/
init (i18next) {
this.setAttribute('tools-source_save', i18next.t('tools.source_save'))
this.setAttribute('common-cancel', i18next.t('common.cancel'))
this.setAttribute('notification-source_dialog_note', i18next.t('notification.source_dialog_note'))
this.setAttribute('config-done', i18next.t('config.done'))
}
/**
* @function observedAttributes
* @returns {any} observed
*/
static get observedAttributes () {
return ['dialog', 'value', 'applysec', 'copysec', 'tools-source_save', 'common-cancel', 'notification-source_dialog_note', 'config-done']
}
/**
* @function attributeChangedCallback
* @param {string} name
* @param {string} oldValue
* @param {string} newValue
* @returns {void}
*/
attributeChangedCallback (name, oldValue, newValue) {
if (oldValue === newValue) return
let node
switch (name) {
case 'dialog':
if (newValue === 'open') {
this.$sourceTxt.focus()
this.$dialog.open()
} else {
this.$dialog.close()
this.$sourceTxt.blur()
}
break
case 'applysec':
if (newValue === 'false') {
this.$applySec.style.display = 'none'
} else {
this.$applySec.style.display = 'block'
}
break
case 'copysec':
if (newValue === 'false') {
this.$copySec.style.display = 'none'
} else {
this.$copySec.style.display = 'block'
}
break
case 'value':
this.$sourceTxt.value = newValue
break
case 'tools-source_save':
this.$saveBtn.textContent = newValue
break
case 'common-cancel':
this.$cancelBtn.textContent = newValue
break
case 'notification-source_dialog_note':
node = this._shadowRoot.querySelector('#copy_save_note')
node.textContent = newValue
break
case 'config-done':
this.$copyBtn.textContent = newValue
break
default:
super.attributeChangedCallback(name, oldValue, newValue)
break
}
}
/**
* @function get
* @returns {any}
*/
get dialog () {
return this.getAttribute('dialog')
}
/**
* @function set
* @returns {void}
*/
set dialog (value) {
this.setAttribute('dialog', value)
}
/**
* @function get
* @returns {any}
*/
get value () {
return this.getAttribute('value')
}
/**
* @function set
* @returns {void}
*/
set value (value) {
this.setAttribute('value', value)
}
/**
* @function get
* @returns {any}
*/
get applysec () {
return this.getAttribute('applysec')
}
/**
* @function set
* @returns {void}
*/
set applysec (value) {
this.setAttribute('applysec', value)
}
/**
* @function get
* @returns {any}
*/
get copysec () {
return this.getAttribute('copysec')
}
/**
* @function set
* @returns {void}
*/
set copysec (value) {
this.setAttribute('copysec', value)
}
/**
* @function connectedCallback
* @returns {void}
*/
connectedCallback () {
const onCancelHandler = () => {
const closeEvent = new CustomEvent('change', {
detail: {
dialog: 'closed'
}
})
this.dispatchEvent(closeEvent)
}
const onCopyHandler = () => {
const closeEvent = new CustomEvent('change', {
detail: {
copy: 'click',
value: this.$sourceTxt.value
}
})
this.dispatchEvent(closeEvent)
}
const onSaveHandler = () => {
const closeEvent = new CustomEvent('change', {
detail: {
value: this.$sourceTxt.value,
dialog: 'close'
}
})
this.dispatchEvent(closeEvent)
}
const onToggleDynamicHandler = () => {
const closeEvent = new CustomEvent('change', {
detail: {
dynamic: this.$toggleDynamic.checked,
dialog: 'dynamic'
}
})
this.dispatchEvent(closeEvent)
}
svgEditor.$click(this.$copyBtn, onCopyHandler)
svgEditor.$click(this.$saveBtn, onSaveHandler)
svgEditor.$click(this.$cancelBtn, onCancelHandler)
svgEditor.$click(this.$toggleDynamic, onToggleDynamicHandler)
this.$dialog.addEventListener('close', onCancelHandler)
}
}
// Register
customElements.define('se-svg-source-editor-dialog', SeSvgSourceEditorDialog)