import type { VNode } from 'vue'; import type { ILocalizedText } from '../../common/localized-text'; import type { DropdownButtonItemArgs } from '../dropdown-button/dropdown-button-item'; import type { FormItemWrapperArgs, MarginType } from '../form/form-item-wrapper'; import { Prop, toNative } from 'vue-facing-decorator'; import { globalState } from '../../app/global-state'; import PowerduckState from '../../app/powerduck-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { Language } from '../../common/enums/language'; import LocalizedValueHelper from '../../common/localized-value-helper'; import { isNullOrEmpty } from '../../common/utils/is-null-or-empty'; import { LanguageUtils } from '../../common/utils/language-utils'; import { PortalUtils } from '../../common/utils/utils'; import Button from '../button/button'; import { ButtonLayout, ButtonSize } from '../button/button-layout'; import FlexContainer from '../form/flex-container'; import FlexFormItem from '../form/form-item-flex'; import FormItemWrapper from '../form/form-item-wrapper'; import Modal, { ModalSize } from '../modal/modal'; import ModalBody from '../modal/modal-body'; import ModalFooter from '../modal/modal-footer'; import { FileManagerDialog, FileManagerModalFileType } from '../modal/ts/file-manager-dialog'; import globalIcon from './img/global.png'; import './css/localized-string-input.css'; interface LocalizedUrlInputArgs extends FormItemWrapperArgs { value: ILocalizedText; changed: (e: ILocalizedText) => void; supportedLanguages?: Language[]; placeholder?: string; } @Component class LocalizedUrlInputComponent extends TsxComponent implements LocalizedUrlInputArgs { @Prop() label!: string | VNode; @Prop() labelButtons!: DropdownButtonItemArgs[]; @Prop() subtitle!: string; @Prop() value!: ILocalizedText; @Prop() changed: (e: ILocalizedText) => void; @Prop() supportedLanguages?: Language[]; @Prop() marginType?: MarginType; @Prop() maxWidth?: number; @Prop() placeholder!: string; @Prop() mandatory!: boolean; @Prop() wrap!: boolean; @Prop() showClearValueButton!: boolean; @Prop() cssClass: string; @Prop() hint: string; @Prop() appendIcon: string; @Prop() prependIcon: string; @Prop() appendClicked: () => void; @Prop() prependClicked: () => void; currentValue: ILocalizedText = null; modalWillShow: boolean = false; isModal: boolean = false; uuid: string = `lsw-${PortalUtils.randomString(10)}`; raiseChangeEvent(newValue: ILocalizedText): void { this.populateValidationDeclaration(); if (this.changed != null) { this.changed(newValue); } } isAllSame(): boolean { if (this.value != null) { const langList = LanguageUtils.getLanguageList(); const skValue = LocalizedValueHelper.getValue(this.value, Language.sk); for (let i = 0, len = langList.length; i < len; i++) { if (LocalizedValueHelper.getValue(this.value, langList[i].id) != skValue) { return false; } } } return true; } hasSomeValue(): boolean { return !isNullOrEmpty(LocalizedValueHelper.getValue(this.value, Language.sk)); } private getModalContext(): HTMLElement | null { return document.getElementById(this.uuid); } showInputModal(): void { this.modalWillShow = true; this.currentValue = JSON.parse(JSON.stringify(this.value || {})); this.$nextTick(() => { (this.$refs.dlgLocalizedString as typeof Modal.prototype).show({ onShown: () => { setTimeout(() => { const ctx = this.getModalContext(); let input = ctx?.querySelector(`.lsi-main-input[data-inv-lang="${PowerduckState.getCurrentLanguage()}"]`); if (input == null) { // Match `:visible` semantics roughly: ignore inputs not laid out. input = Array.from(ctx?.querySelectorAll('.lsi-main-input') ?? []) .find(el => el.offsetParent != null || el.getClientRects().length > 0) ?? null; } input?.focus(); }, 15); }, }); }); } updateUrl(lang?) { FileManagerDialog.show({ fileType: FileManagerModalFileType.All, callback: (data) => { this.handleModalValueChange(data.url, lang); }, }); } previewItem(url) { if (!url.match(/^[a-z]+:\/\//i)) { url = `http://${url}`; } globalState.open(url, '_blank'); } handleModalValueChange(newValue: string, lang?: LanguageUtils.LanguageListItem) { this.currentValue = JSON.parse(JSON.stringify(this.value || {})); const value = this.currentValue; const propName = lang?.id || 'sk'; value[propName] = newValue; this.getModalContext() ?.querySelectorAll('[data-inv-lang]') .forEach((el) => { const currentVal = el.value; if (currentVal == null || currentVal.length == 0) { const language: Language = el.getAttribute('data-inv-lang') as any; const lsValue = LocalizedValueHelper.getValue(value, language) || ''; el.setAttribute('placeholder', lsValue); } }); if (!this.isModal) { this.raiseChangeEvent(value); } } render(h) { return (
{this.renderInput(h)} {this.modalWillShow && this.renderModal(h)}
); } renderInput(h) { const allSame = this.isAllSame(); const hasValue = this.hasSomeValue(); if (allSame && !hasValue) { return this.renderInputPlaceholder(h); } else if (allSame) { return this.renderInputGlobalValue(h); } else { return this.renderInputMultipleValues(h); } } renderInputPlaceholder(h) { return (
this.showInputModal()}> {this.placeholder}