/* globals svgEditor */ import { jGraduate, jGraduateMethod } from './jgraduate/jQuery.jGraduate.js' import PaintBox from './PaintBox.js' import { t } from '../locale.js' const template = document.createElement('template') template.innerHTML = `
` /** * @class SeColorPicker */ export class SeColorPicker 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.$logo = this._shadowRoot.getElementById('logo') this.$label = this._shadowRoot.getElementById('label') this.$block = this._shadowRoot.getElementById('block') this.paintBox = null this.i18next = null this.$picker = this._shadowRoot.getElementById('picker') this.$color_picker = this._shadowRoot.getElementById('color_picker') this.imgPath = svgEditor.configObj.curConfig.imgPath } /** * @function init * @param {any} name * @returns {void} */ init (i18next) { this.i18next = i18next this.setAttribute('config-change_xxx_color', t('config.change_xxx_color')) } /** * @function observedAttributes * @returns {any} observed */ static get observedAttributes () { return ['label', 'src', 'type', 'config-change_xxx_color'] } /** * @function attributeChangedCallback * @param {string} name * @param {string} oldValue * @param {string} newValue * @returns {void} */ attributeChangedCallback (name, oldValue, newValue) { if (oldValue === newValue) return switch (name) { case 'src': this.$logo.setAttribute('src', this.imgPath + '/' + newValue) break case 'label': this.setAttribute('title', t(newValue)) break case 'type': this.$label.setAttribute('title', 'config.pick_paint_opavity') break case 'config-change_xxx_color': this.$label.setAttribute('title', newValue) break default: console.error(`unknown attribute: ${name}`) break } } /** * @function get * @returns {any} */ get label () { return this.$label.getAttribute('title') } /** * @function set * @returns {void} */ set label (value) { this.setAttribute('label', value) } /** * @function get * @returns {any} */ get type () { return this.getAttribute('type') } /** * @function set * @returns {void} */ set type (value) { this.setAttribute('type', value) } /** * @function get * @returns {any} */ get src () { return this.getAttribute('src') } /** * @function set * @returns {void} */ set src (value) { this.setAttribute('src', value) } // Wrap jGraduateMethod in a Promise jGraduateMethodAsync () { return new Promise((resolve, reject) => { jGraduateMethod( this.$color_picker, { images: { clientPath: './components/jgraduate/images/' }, paint: this.paintBox.paint, window: { pickerTitle: this.label }, newstop: 'inverse' }, (p) => resolve(p), () => reject(new Error('jGraduate cancelled')), this.i18next ) }) } async setJGraduateMethod () { try { const p = await this.jGraduateMethodAsync() const paint = new jGraduate.Paint(p) this.setPaint(paint) this.dispatchEvent(new CustomEvent('change', { detail: { paint } })) } catch (err) { // Handle rejection if needed } finally { this.$color_picker.style.display = 'none' } } /** * @param {PlainObject} svgCanvas * @param {PlainObject} selectedElement * @param {bool} apply * @returns {void} */ update (svgCanvas, selectedElement, apply) { const paint = this.paintBox.update(svgCanvas, selectedElement) // We check if the color picker popup is already open if (this.$color_picker.style.display === 'block') { // We recreate the color picker popup with the current color of the selected elements this.setJGraduateMethod() } if (paint && apply) { this.setPaint(paint) const changeEvent = new CustomEvent('change', { detail: { paint } }) this.dispatchEvent(changeEvent) } } /** * @param {PlainObject} paint * @returns {void} */ setPaint (paint) { this.paintBox.setPaint(paint) } /** * @function connectedCallback * @returns {void} */ connectedCallback () { this.paintBox = new PaintBox(this.$block, this.type) svgEditor.$click(this.$picker, () => { this.setJGraduateMethod() }) } } // Register customElements.define('se-colorpicker', SeColorPicker)