/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { LitElement, customElement, html, property, css } from 'lit-element/lit-element.js';
let KCEditableLabel = class KCEditableLabel extends LitElement {
    constructor() {
        super(...arguments);
        this.editing = false;
        this.singleline = false;
        this.label = '';
    }
    static get styles() {
        return [css `
            label, input {
                font-weight: inherit;
            }
            input {
                font-family: inherit;
                font-size: inherit;
                background: transparent;
                color: white;
                border: 1px solid grey;
                outline: none;
                padding: 2px;
                margin: -3px;
            }
            label {
                width: var(--editable-label-width, auto);
            }
            label.single-line {
                display: inline-block;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        `];
    }
    render() {
        return html `${this.editing ? this.inputEl : this.labelEl}`;
    }
    get inputEl() {
        return html `<input type="text" .value=${this.label} @keydown=${this._onInputKeyDown} @blur=${this._onInputBlur} />`;
    }
    get labelEl() {
        const additionalClass = this.singleline ? 'single-line' : '';
        return html `<label class="${additionalClass}" @click=${this._onLabelClick}>${this.label}</label>`;
    }
    get input() {
        return this.renderRoot.querySelector('input');
    }
    _onLabelClick() {
        this.editing = true;
        this.updateComplete.then(() => {
            const input = this.input;
            input.select();
        });
    }
    _onInputKeyDown(e) {
        if (e.keyCode === 27) {
            this.cancel();
        }
        if (e.keyCode === 13) {
            this.apply();
        }
    }
    _onInputBlur() {
        this.apply();
    }
    cancel() {
        this.editing = false;
    }
    apply() {
        const value = this.input.value;
        this.editing = false;
        if (value !== this.label) {
            this.label = value;
            this.dispatchEvent(new CustomEvent('change', { detail: value, bubbles: true, composed: true }));
        }
    }
};
__decorate([
    property({ type: Boolean })
], KCEditableLabel.prototype, "editing", void 0);
__decorate([
    property({ type: Boolean })
], KCEditableLabel.prototype, "singleline", void 0);
__decorate([
    property({ type: String })
], KCEditableLabel.prototype, "label", void 0);
KCEditableLabel = __decorate([
    customElement('kc-editable-label')
], KCEditableLabel);
export { KCEditableLabel };
//# sourceMappingURL=kc-editable-label.js.map