Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. class ClassificatorValue
  2. {
  3. public static readonly STATUS_ENABLED = 'enabled';
  4.  
  5. id: number|null = null;
  6. key: string = '';
  7. title: string = '';
  8. parent: string|null = null;
  9. translations: Translations = {};
  10. status: string = ClassificatorValue.STATUS_ENABLED;
  11.  
  12. constructor(id: number | null, key: string, title: string, parent: string|null, translations: {}, status: string)
  13. {
  14. this.id = id;
  15. this.key = key;
  16. this.title = title;
  17. this.parent = parent;
  18. this.translations = translations;
  19. this.status = status;
  20. }
  21. }
  22.  
  23. interface Translations {
  24. [locale: string]: {[fields: string]: string};
  25. }
  26.  
  27. class RowWrapper
  28. {
  29. $row: JQuery;
  30. $titleInput: JQuery;
  31. $keyInput: JQuery;
  32. $idInput: JQuery;
  33. $parentInput: JQuery;
  34.  
  35.  
  36. constructor($row: JQuery<HTMLElement>)
  37. {
  38. this.$row = $row;
  39. this.$titleInput = this.get$element('.title_inp');
  40. this.$keyInput = this.get$element('.key_inp');
  41. this.$idInput = this.get$element('.id_inp');
  42. this.$parentInput = this.get$element('.parent_inp');
  43. }
  44.  
  45. private get$element(selector: string): JQuery
  46. {
  47. return this.$row.find(selector);
  48. }
  49.  
  50. public setTitleInput(title: string): void
  51. {
  52. this.$titleInput.val(title)
  53. }
  54.  
  55. public getTitleInput(): string
  56. {
  57. return <string>this.$titleInput.val();
  58. }
  59.  
  60. public setKey(key: string): void
  61. {
  62. this.$keyInput.val(key);
  63. }
  64.  
  65. public getKey(): string
  66. {
  67. return <string>this.$keyInput.val();
  68. }
  69.  
  70. public setId(id: number|null): void
  71. {
  72. this.$keyInput.val(String(id));
  73. }
  74.  
  75. public getId(): number|null
  76. {
  77. let val = this.$idInput.val();
  78. if (val === '') {
  79. return null;
  80. }
  81.  
  82. return <number>val;
  83. }
  84.  
  85. public hasParentInput(): boolean
  86. {
  87. return this.$parentInput.length > 0;
  88. }
  89.  
  90. public setParentInput(parent: string|null): void
  91. {
  92. if (!this.hasParentInput()) {
  93. return;
  94. }
  95.  
  96. this.$parentInput.val(String(parent));
  97. }
  98.  
  99. public getParentInput(): string|null
  100. {
  101. if (!this.hasParentInput()) {
  102. return null;
  103. }
  104.  
  105. let val = this.$parentInput.val();
  106. if (val === '') {
  107. return null;
  108. }
  109.  
  110. return String(val);
  111. }
  112.  
  113. public setTitleInputReadOnly(): void
  114. {
  115. this.$titleInput.prop('readonly', true);
  116. }
  117.  
  118. private get$TranslationElement(locale: string, field: string): JQuery
  119. {
  120. let selector = `.trans_field[data-locale='${locale}'][data-field='${field}']`;
  121. let $translationElement = this.$row.find(selector);
  122.  
  123. return $translationElement;
  124. }
  125.  
  126. public setTranslationFieldValue(locale: string, field: string, value: string): void
  127. {
  128. let $translationElement = this.get$TranslationElement(locale, field);
  129. if ($translationElement.length === 0) {
  130. return;
  131. }
  132. $translationElement.val(value);
  133. }
  134. }
  135.  
  136. export interface ClassificatorEditOptions {
  137. rowTemplateSelector: string;
  138. rowsContainerSelector: string;
  139. savedClassificatorValues: ClassificatorValue[];
  140. isEditMode: boolean;
  141. isTitleEditDisabled: boolean;
  142. }
  143.  
  144.  
  145. export default class UniversalClassificatorEdit
  146. {
  147. private $rowTemplate: JQuery;
  148. private $rowContainer: JQuery;
  149. private savedClassificatorValues: ClassificatorValue[];
  150. private isEditMode: boolean;
  151. private isTitleEditDisabled: boolean;
  152. private rowNumberGenerator: IterableIterator<number>;
  153.  
  154. constructor(options: ClassificatorEditOptions)
  155. {
  156. this.$rowTemplate = $(options.rowTemplateSelector);
  157. this.$rowContainer = $(options.rowsContainerSelector);
  158. this.savedClassificatorValues = options.savedClassificatorValues;
  159. this.isEditMode = options.isEditMode;
  160. this.isTitleEditDisabled = options.isTitleEditDisabled;
  161.  
  162. console.log(this.savedClassificatorValues);
  163.  
  164. this.rowNumberGenerator = function* () {
  165. let row = 0;
  166. while (true) {
  167. yield row++;
  168. }
  169. }();
  170.  
  171. this.addSavedValuesToRowContainer();
  172. }
  173.  
  174. private addSavedValuesToRowContainer(): void
  175. {
  176. for(const classificatorValue of this.savedClassificatorValues) {
  177. this.addRow(classificatorValue);
  178. }
  179. }
  180.  
  181. private make$row()
  182. {
  183. let rowTemplateHtml = this.$rowTemplate.html();
  184. let rowNumber = this.rowNumberGenerator.next();
  185. let newRowHtml = rowTemplateHtml.split('__num__').join(rowNumber.toString());
  186. let $row = $(newRowHtml);
  187.  
  188. return $row;
  189. }
  190.  
  191. private addRow(classificatorValue: ClassificatorValue): void
  192. {
  193. let $row = this.make$row();
  194. let rowWrapper = new RowWrapper($row);
  195.  
  196. rowWrapper.setTitleInput(classificatorValue.title);
  197. rowWrapper.setKey(classificatorValue.key);
  198. rowWrapper.setId(classificatorValue.id);
  199. rowWrapper.setParentInput(classificatorValue.parent);
  200.  
  201. let translations = classificatorValue.translations;
  202.  
  203. if( (typeof translations === 'object') && (translations !== null) ) {
  204. for (let locale in translations) {
  205. let fields = translations[locale];
  206. for (let field in fields) {
  207. let fieldValue = fields[field];
  208. rowWrapper.setTranslationFieldValue(locale, field, fieldValue);
  209. }
  210. }
  211. }
  212.  
  213. let isNewValue = classificatorValue.id === null;
  214.  
  215. if (isNewValue || this.isTitleEditDisabled) {
  216. rowWrapper.setTitleInputReadOnly();
  217. }
  218.  
  219. this.$rowContainer.append($row);
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement