-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
cmenuLayersDialog.js
157 lines (147 loc) · 4.5 KB
/
cmenuLayersDialog.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
/* globals svgEditor */
import cMenuLayersDialog from './cmenuLayersDialog.html'
const template = document.createElement('template')
template.innerHTML = cMenuLayersDialog
/**
* @class SeCMenuLayerDialog
*/
export class SeCMenuLayerDialog 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.source = ''
this._workarea = undefined
this.$sidePanels = document.getElementById('sidepanels')
this.$dialog = this._shadowRoot.querySelector('#cmenu_layers')
this.$duplicateLink = this._shadowRoot.querySelector('#se-dupe')
this.$deleteLink = this._shadowRoot.querySelector('#se-layer-delete')
this.$mergeDownLink = this._shadowRoot.querySelector('#se-merge-down')
this.$mergeAllLink = this._shadowRoot.querySelector('#se-merge-all')
}
/**
* @function init
* @param {any} name
* @returns {void}
*/
init (i18next) {
this.setAttribute('layers-dupe', i18next.t('layers.dupe'))
this.setAttribute('layers-del', i18next.t('layers.del'))
this.setAttribute('layers-merge_down', i18next.t('layers.merge_down'))
this.setAttribute('layers-merge_all', i18next.t('layers.merge_all'))
}
/**
* @function observedAttributes
* @returns {any} observed
*/
static get observedAttributes () {
return ['value', 'leftclick', 'layers-dupe', 'layers-del', 'layers-merge_down', 'layers-merge_all']
}
/**
* @function attributeChangedCallback
* @param {string} name
* @param {string} oldValue
* @param {string} newValue
* @returns {void}
*/
attributeChangedCallback (name, oldValue, newValue) {
if (oldValue === newValue) return
switch (name) {
case 'value':
this.source = newValue
if (newValue !== '' && newValue !== undefined) {
this._workarea = document.getElementById(this.source)
}
break
case 'layers-dupe':
this.$duplicateLink.textContent = newValue
break
case 'layers-del':
this.$deleteLink.textContent = newValue
break
case 'layers-merge_down':
this.$mergeDownLink.textContent = newValue
break
case 'layers-merge_all':
this.$mergeAllLink.textContent = newValue
break
default:
// super.attributeChangedCallback(name, oldValue, newValue);
break
}
}
/**
* @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 leftclick () {
return this.getAttribute('leftclick')
}
/**
* @function set
* @returns {void}
*/
set leftclick (value) {
this.setAttribute('leftclick', value)
}
/**
* @function connectedCallback
* @returns {void}
*/
connectedCallback () {
const current = this
const onMenuOpenHandler = (e) => {
e.preventDefault()
current.$dialog.style.top = e.pageY + 'px'
current.$dialog.style.left = e.pageX - 126 + 'px'
current.$dialog.style.display = 'block'
}
const onMenuCloseHandler = (e) => {
if (e.button !== 2) {
current.$dialog.style.display = 'none'
}
}
const onMenuClickHandler = (e, action, id) => {
const triggerEvent = new CustomEvent('change', {
detail: {
trigger: action,
source: id
}
})
this.dispatchEvent(triggerEvent)
current.$dialog.style.display = 'none'
}
if (this._workarea !== undefined) {
this._workarea.addEventListener('contextmenu', onMenuOpenHandler)
if (this.getAttribute('leftclick') === 'true') {
svgEditor.$click(this._workarea, onMenuOpenHandler)
}
this._workarea.addEventListener('mousedown', onMenuCloseHandler)
this.$sidePanels.addEventListener('mousedown', onMenuCloseHandler)
}
svgEditor.$click(this.$duplicateLink, (evt) => onMenuClickHandler(evt, 'dupe', this.source))
svgEditor.$click(this.$deleteLink, (evt) => onMenuClickHandler(evt, 'delete', this.source))
svgEditor.$click(this.$mergeDownLink, (evt) => onMenuClickHandler(evt, 'merge_down', this.source))
svgEditor.$click(this.$mergeAllLink, (evt) => onMenuClickHandler(evt, 'merge_all', this.source))
}
}
// Register
customElements.define('se-cmenu-layers', SeCMenuLayerDialog)