Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ClassificatorValue
- {
- public static readonly STATUS_ENABLED = 'enabled';
- id: number|null = null;
- key: string = '';
- title: string = '';
- parent: string|null = null;
- translations: Translations = {};
- status: string = ClassificatorValue.STATUS_ENABLED;
- constructor(id: number | null, key: string, title: string, parent: string|null, translations: {}, status: string)
- {
- this.id = id;
- this.key = key;
- this.title = title;
- this.parent = parent;
- this.translations = translations;
- this.status = status;
- }
- }
- interface Translations {
- [locale: string]: {[fields: string]: string};
- }
- class RowWrapper
- {
- $row: JQuery;
- $titleInput: JQuery;
- $keyInput: JQuery;
- $idInput: JQuery;
- $parentInput: JQuery;
- constructor($row: JQuery<HTMLElement>)
- {
- this.$row = $row;
- this.$titleInput = this.get$element('.title_inp');
- this.$keyInput = this.get$element('.key_inp');
- this.$idInput = this.get$element('.id_inp');
- this.$parentInput = this.get$element('.parent_inp');
- }
- private get$element(selector: string): JQuery
- {
- return this.$row.find(selector);
- }
- public setTitleInput(title: string): void
- {
- this.$titleInput.val(title)
- }
- public getTitleInput(): string
- {
- return <string>this.$titleInput.val();
- }
- public setKey(key: string): void
- {
- this.$keyInput.val(key);
- }
- public getKey(): string
- {
- return <string>this.$keyInput.val();
- }
- public setId(id: number|null): void
- {
- this.$keyInput.val(String(id));
- }
- public getId(): number|null
- {
- let val = this.$idInput.val();
- if (val === '') {
- return null;
- }
- return <number>val;
- }
- public hasParentInput(): boolean
- {
- return this.$parentInput.length > 0;
- }
- public setParentInput(parent: string|null): void
- {
- if (!this.hasParentInput()) {
- return;
- }
- this.$parentInput.val(String(parent));
- }
- public getParentInput(): string|null
- {
- if (!this.hasParentInput()) {
- return null;
- }
- let val = this.$parentInput.val();
- if (val === '') {
- return null;
- }
- return String(val);
- }
- public setTitleInputReadOnly(): void
- {
- this.$titleInput.prop('readonly', true);
- }
- private get$TranslationElement(locale: string, field: string): JQuery
- {
- let selector = `.trans_field[data-locale='${locale}'][data-field='${field}']`;
- let $translationElement = this.$row.find(selector);
- return $translationElement;
- }
- public setTranslationFieldValue(locale: string, field: string, value: string): void
- {
- let $translationElement = this.get$TranslationElement(locale, field);
- if ($translationElement.length === 0) {
- return;
- }
- $translationElement.val(value);
- }
- }
- export interface ClassificatorEditOptions {
- rowTemplateSelector: string;
- rowsContainerSelector: string;
- savedClassificatorValues: ClassificatorValue[];
- isEditMode: boolean;
- isTitleEditDisabled: boolean;
- }
- export default class UniversalClassificatorEdit
- {
- private $rowTemplate: JQuery;
- private $rowContainer: JQuery;
- private savedClassificatorValues: ClassificatorValue[];
- private isEditMode: boolean;
- private isTitleEditDisabled: boolean;
- private rowNumberGenerator: IterableIterator<number>;
- constructor(options: ClassificatorEditOptions)
- {
- this.$rowTemplate = $(options.rowTemplateSelector);
- this.$rowContainer = $(options.rowsContainerSelector);
- this.savedClassificatorValues = options.savedClassificatorValues;
- this.isEditMode = options.isEditMode;
- this.isTitleEditDisabled = options.isTitleEditDisabled;
- console.log(this.savedClassificatorValues);
- this.rowNumberGenerator = function* () {
- let row = 0;
- while (true) {
- yield row++;
- }
- }();
- this.addSavedValuesToRowContainer();
- }
- private addSavedValuesToRowContainer(): void
- {
- for(const classificatorValue of this.savedClassificatorValues) {
- this.addRow(classificatorValue);
- }
- }
- private make$row()
- {
- let rowTemplateHtml = this.$rowTemplate.html();
- let rowNumber = this.rowNumberGenerator.next();
- let newRowHtml = rowTemplateHtml.split('__num__').join(rowNumber.toString());
- let $row = $(newRowHtml);
- return $row;
- }
- private addRow(classificatorValue: ClassificatorValue): void
- {
- let $row = this.make$row();
- let rowWrapper = new RowWrapper($row);
- rowWrapper.setTitleInput(classificatorValue.title);
- rowWrapper.setKey(classificatorValue.key);
- rowWrapper.setId(classificatorValue.id);
- rowWrapper.setParentInput(classificatorValue.parent);
- let translations = classificatorValue.translations;
- if( (typeof translations === 'object') && (translations !== null) ) {
- for (let locale in translations) {
- let fields = translations[locale];
- for (let field in fields) {
- let fieldValue = fields[field];
- rowWrapper.setTranslationFieldValue(locale, field, fieldValue);
- }
- }
- }
- let isNewValue = classificatorValue.id === null;
- if (isNewValue || this.isTitleEditDisabled) {
- rowWrapper.setTitleInputReadOnly();
- }
- this.$rowContainer.append($row);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement