Guest User

Untitled

a guest
Nov 18th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.23 KB | None | 0 0
  1. import { ChangeDetectorRef, Component, Input, OnInit, ViewChild, NgZone } from '@angular/core';
  2. import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  3. import { Store } from '@ngrx/store';
  4. import * as uuid from 'node-uuid';
  5.  
  6. import {
  7. IClients, INameValidation, language, maxParameterName, minParameterName
  8. } from '../../../../app.config';
  9. import { UIModalComponent } from '../../../shared/semantic/modal/modal.component';
  10. import { IAppState } from '../../../../store';
  11. import { Utilities } from '../../../../services/utilities/form.utility';
  12. import {
  13. PARAMETER_UPDATE, PARAMETER_LOCKED_VALUE_ADD, PARAMETER_LOCKED_VALUE_DELETE, PARAMETER_LOCKED_VALUE_UPDATE,
  14. PROJECT_PARAMETERS_CREATE, PARAMETER_ENABLE_LOCKED_VALUE
  15. } from '../../../../store/projects/projects.actions';
  16. import { IParametersDropdown, ParametersDropdown } from '../data/service/parameters-config';
  17. import { MODAL_SHOW } from '../../../../store/layout/layout.actions';
  18. import { IParameters } from '../../../../models/models';
  19. import { ApiService } from '../../../../services/application/api.service';
  20. import { SuiTabset } from '../../../shared/semantic/sui-tab/components/tabset';
  21. import { Parser } from '../../../../services/utilities/parser.utility';
  22. import { ICategory } from '../../../../models/category/category.model';
  23. import { Observable } from 'rxjs/Observable';
  24. import { IProject } from '../../../../models/project/project.model';
  25.  
  26.  
  27. import { ITableStructure } from '../../../../models/ui/table.model';
  28. import { UIParameterCategoriesTableComponent } from '../../../shared/parameter-categories/parameter-categories-table';
  29. import { FilterUtilities } from '../../../../services/utilities/filter.utility';
  30. import { Tab } from '../../../shared/semantic/sui-tab/classes/tab';
  31.  
  32.  
  33. @Component({
  34. selector: 'be-project-parameters-modal',
  35. templateUrl: './project-parameters-modal.component.html',
  36. styleUrls: ['project-parameters-modal.component.css']
  37. })
  38.  
  39. export class ParametersModalProjectComponent implements OnInit {
  40.  
  41. table: ITableStructure;
  42.  
  43. @Input() activeClients: Array<{}> = [];
  44. @Input() projectGuid: string;
  45. @Input() editMode = false;
  46. @Input() loading = false;
  47. @Input() errors: {};
  48. @Input() activeProjectCategories: Array<ICategory> = [];
  49. @Input() projectParameters: IParameters[];
  50.  
  51.  
  52. lockedValueForEdit: string;
  53. lockedValues = false;
  54.  
  55. parametersForm: FormGroup;
  56. listOfValuesForm: FormGroup;
  57.  
  58. paramValues: IParametersDropdown = ParametersDropdown; // dropdown values
  59. selectedProject$: Observable<IProject>;
  60. allParameters: string[] = [];
  61. categoriesForms: FormArray;
  62. parameterLockedValuesArray: Array<{ editing: boolean; value: string }> = [];
  63. parameterLockedValuesArrayLowerCase: string[] = [];
  64. editingModeLockedValue = false;
  65. errorMessage: string; // error message for locked values
  66. parameterGuid: string;
  67. oldValue: string;
  68. parameterEditValidation: INameValidation = {
  69. listOfNames: [],
  70. editName: ''
  71. };
  72. lockedValueValidation: INameValidation = {
  73. listOfNames: [],
  74. editName: ''
  75. };
  76. valuesSearch: string;
  77. selectAllCategories: boolean; // variable for controlling select all categories
  78. valueToRemove: string;
  79. @ViewChild('modal') modal: UIModalComponent;
  80. @ViewChild('tabSet') tabSet: SuiTabset;
  81. @ViewChild('lockedValueInput') lockedValueInput: any;
  82. @ViewChild('categoriesTable') categoriesTable: UIParameterCategoriesTableComponent;
  83. @ViewChild('deleteLockedValueModal') deleteLockedValueModal;
  84. @ViewChild('tabLV') lockedListOfValuesTab;
  85.  
  86. constructor(private fb: FormBuilder,
  87. private store: Store<IAppState>,
  88. private apiService: ApiService,
  89. public parser: Parser,
  90. private zone: NgZone,
  91. private cd: ChangeDetectorRef) {
  92.  
  93. this.table = {
  94. columns: [
  95. {name: 'BIMeye'},
  96. {name: 'Revit'},
  97. {name: 'ArchiCAD'},
  98. {name: 'Tekla'},
  99. ],
  100. tableClass: 'ui small fixed celled table'
  101. };
  102.  
  103. // project parameters create form validation
  104. this.parametersForm = this.fb.group({
  105. name: new FormControl('', Validators.compose([
  106. Utilities.requiredValidator(language.nameLength.name_required),
  107. Utilities.minLengthValidator(minParameterName, language.nameLength.too_short),
  108. Utilities.maxLengthValidator(maxParameterName, language.nameLength.too_long),
  109. Utilities.uniqueName(this.parameterEditValidation, language.nameExists)
  110. ])),
  111. guid: new FormControl(uuid.v4(), Validators.compose([
  112. Validators.required
  113. ])),
  114. customTooltip: new FormControl(''),
  115. settings: this.fb.group({
  116. canCopy: new FormControl(this.paramValues.canCopy[0].value, Validators.required),
  117. dataType: new FormControl(this.paramValues.dataType[0].value, Validators.required),
  118. parameterType: new FormControl(this.paramValues.parameterType[0].value, Validators.required),
  119. inUse: new FormControl(true, Validators.required),
  120. readOnly: new FormControl(this.paramValues.readOnly[0].value, Validators.required)
  121. }),
  122. categories: new FormArray([]),
  123. clients: new FormArray([])
  124. });
  125.  
  126. this.listOfValuesForm = this.fb.group({
  127. addValue: new FormControl('', Validators.compose([
  128. Utilities.requiredValidator(language.nameLength.name_required),
  129. Utilities.minLengthValidator(minParameterName, language.nameLength.too_short),
  130. Utilities.maxLengthValidator(maxParameterName, language.nameLength.too_long),
  131. Utilities.uniqueName(this.lockedValueValidation, language.nameExists)
  132. ])),
  133. enabled: new FormControl(''),
  134. lockedValuesSearch: new FormControl(''),
  135. updateValue: new FormControl('', Validators.compose([
  136. Utilities.requiredValidator(language.nameLength.name_required),
  137. Utilities.minLengthValidator(minParameterName, language.nameLength.too_short),
  138. Utilities.maxLengthValidator(maxParameterName, language.nameLength.too_long),
  139. Utilities.uniqueName(this.lockedValueValidation, language.nameExists)
  140. ]))
  141. });
  142.  
  143. // locked values search
  144. this.listOfValuesForm
  145. .get('lockedValuesSearch')
  146. .valueChanges
  147. .subscribe((value: string) => {
  148. this.valuesSearch = value;
  149. this.zone.run(() => this.cd.markForCheck());
  150. });
  151.  
  152. this.selectedProject$ = this.store.select('projects').pluck('selectedProject');
  153.  
  154. this.selectedProject$.subscribe(
  155. (project: IProject) => {
  156. if (project && project.hasOwnProperty('parameters')) {
  157. project.parameters.items.forEach((parameter) => {
  158. this.allParameters.push(parameter.name);
  159. }
  160. );
  161. }
  162. }
  163. );
  164. }
  165.  
  166. ngOnInit(): void {
  167. // get all active clients and parse them as key => value
  168. this.initializeClients();
  169. this.categoriesForms = this.parametersForm.get('categories') as FormArray;
  170. }
  171.  
  172.  
  173. /** * Create parameters
  174. */
  175. createOrUpdateParameter(): void {
  176.  
  177. if (this.parametersForm.valid) {
  178. this.errors = undefined;
  179. this.store.dispatch({
  180. type: !this.editMode ? PROJECT_PARAMETERS_CREATE : PARAMETER_UPDATE,
  181. payload: !this.editMode ? {
  182. parameter: this.parametersForm.value,
  183. projectGuid: this.projectGuid
  184. } : Object.assign({}, {
  185. project: this.projectGuid
  186. }, this.parametersForm.value)
  187. });
  188. }
  189. }
  190.  
  191. clearTabs(): void {
  192. this.tabSet.tabs.forEach((tab: Tab) => {
  193. tab.isActive = false;
  194. this.zone.run(() => this.cd.markForCheck());
  195. });
  196. }
  197.  
  198. show(editMode: boolean, parameterGuid?: string, firstTabActive: boolean = true): void {
  199.  
  200. this.clearTabs();
  201.  
  202. if (firstTabActive && this.tabSet.tabs.length > 0) {
  203. this.tabSet.activeTab = this.tabSet.tabs[0];
  204. this.zone.run(() => this.cd.markForCheck());
  205. }
  206.  
  207. this.editMode = editMode;
  208. this.resetCategories();
  209. this.selectedProject$ = this.store.select('projects').pluck('selectedProject');
  210. this.parameterEditValidation.editName = '';
  211. this.lockedValueValidation.listOfNames = [];
  212. this.lockedValueValidation.editName = '';
  213.  
  214.  
  215. this.selectedProject$.subscribe(
  216. (project: IProject) => {
  217. if (project && project.hasOwnProperty('parameters')) {
  218. project.parameters.items.forEach((parameter) => {
  219. this.parameterEditValidation.listOfNames.push(parameter.name);
  220. if (parameter.guid === parameterGuid) {
  221. this.parameterEditValidation.editName = parameter.name;
  222. }
  223. }
  224. );
  225. }
  226. }
  227. );
  228.  
  229. if (this.editMode && parameterGuid) {
  230. this.parameterGuid = parameterGuid;
  231.  
  232. this.apiService
  233. .getByModule(`project/${this.projectGuid}/parameter/${parameterGuid}`, 'data')
  234. .first()
  235. .subscribe((responseParameter: IParameters) => {
  236. responseParameter.categories.forEach(categories => {
  237. this.categoriesForms.push(this.fb.group({
  238. name: new FormControl(categories.name),
  239. guid: new FormControl(categories.guid)
  240. })
  241. );
  242. });
  243.  
  244. this.parametersForm.patchValue(responseParameter);
  245.  
  246. if (this.projectParameters) {
  247. this.projectParameters.forEach((parameter: IParameters) => {
  248. if (parameter.guid === this.parameterGuid && this.editMode && parameter.hasOwnProperty('listOfValues')) {
  249. this.lockedValues = parameter.listOfValues.enabled;
  250. this.parameterLockedValuesArray = parameter.listOfValues.values.map((val) => {
  251. this.lockedValueValidation.listOfNames.push(val);
  252.  
  253. return {
  254. editing: false,
  255. value: val
  256. };
  257. });
  258. this.listOfValuesForm.get('enabled').setValue(this.lockedValues);
  259. // disable or enable values input depends on is enabled true or false
  260. !this.lockedValues ? this.listOfValuesForm.get('addValue').disable() : this.listOfValuesForm.get('addValue').enable();
  261. }
  262. });
  263. }
  264. // Get locked values for parameter
  265. /*
  266. this.selectedProject$.subscribe((project: IProject) => {
  267. if (project && project.parameters !== undefined) {
  268. project.parameters.items.forEach((parameter: IParameters) => {
  269. if (parameter.guid === this.parameterGuid) {
  270. this.lockedValues = parameter.listOfValues.enabled;
  271. this.zone.run(() => this.cd.markForCheck());
  272. if (parameter.hasOwnProperty('listOfValues')) {
  273. this.parameterLockedValuesArray = parameter.listOfValues.values.map(val => ({
  274. editing: false,
  275. value: val
  276. }));
  277. this.parameterLockedValuesArray.forEach((lockedValue) => {
  278. this.lockedValueValidation.listOfNames.push(lockedValue.value);
  279. });
  280. this.zone.run(() => this.cd.markForCheck());
  281.  
  282. if (this.editMode) {
  283. this.listOfValuesForm.get('enabled').setValue(this.lockedValues);
  284. // disable or enable values input depends on is enabled true or false
  285. !this.lockedValues ? this.listOfValuesForm.get('addValue').disable() : this.listOfValuesForm.get('addValue').enable();
  286. this.zone.run(() => this.cd.markForCheck());
  287. }
  288. }
  289. }
  290. });
  291. }
  292. });
  293. */
  294.  
  295. this.showParameterModal();
  296. });
  297. return;
  298. }
  299.  
  300.  
  301. this.parametersForm.get('guid').setValue(uuid.v4());
  302. this.parametersForm.get('customTooltip').setValue('');
  303. this.resetToDefaultFormSettings();
  304. this.errors = undefined;
  305. this.showParameterModal();
  306. }
  307.  
  308. hide(): void {
  309. this.modal.hide();
  310. }
  311.  
  312. /**
  313. * Showing input box
  314. * when double click on value
  315. * @param value
  316. * */
  317. inlineEdit(value): void {
  318. this.lockedValueValidation.editName = value;
  319. this.oldValue = value;
  320. this.parameterLockedValuesArray.forEach((param) => {
  321. param.editing = param.value === value;
  322. });
  323. this.listOfValuesForm.get('updateValue').setValue(value);
  324. }
  325.  
  326. showDeleteValueModal(value: string): void {
  327. this.createOrUpdateParameter();
  328. this.deleteLockedValueModal.show();
  329. this.valueToRemove = value;
  330. }
  331.  
  332. onHideDeleteValueModal(): void {
  333. this.show(true, this.parameterGuid, false);
  334. this.setLockedValuesTabActive();
  335. }
  336.  
  337. /**
  338. * check if value is unique and valid
  339. * if value is valid and unique it would be updated
  340. * in parameter locked values array
  341. * else it will display error
  342. * @param value
  343. */
  344. updateLockedValue(value: string): void {
  345. value = this.listOfValuesForm.get('updateValue').value.trim();
  346. this.store.dispatch({
  347. type: PARAMETER_LOCKED_VALUE_UPDATE,
  348. payload: {
  349. projectGuid: this.projectGuid,
  350. parameterGuid: this.parameterGuid,
  351. oldValue: this.oldValue,
  352. newValue: value
  353. }
  354. });
  355. }
  356.  
  357. cancelEditLockedValue(value): void {
  358. this.parameterLockedValuesArray.map((item) => {
  359. if (item.value === value) {
  360. item.editing = false;
  361. }
  362. });
  363. }
  364.  
  365. // Trim whitespaces before and after entered value and dispatch action
  366. addLockedValues(): void {
  367. const value: string = this.listOfValuesForm.get('addValue').value.trim();
  368.  
  369. this.store.dispatch({
  370. type: PARAMETER_LOCKED_VALUE_ADD,
  371. payload: {
  372. projectGuid: this.projectGuid,
  373. parameterGuid: this.parameterGuid,
  374. value: value
  375. }
  376. });
  377.  
  378. this.lockedValuesInputClear();
  379. }
  380.  
  381.  
  382. /**
  383. * Remove locked value from parameter
  384. * @param value
  385. */
  386. removeLockedValue(): void {
  387. this.store.dispatch({
  388. type: PARAMETER_LOCKED_VALUE_DELETE,
  389. payload: {
  390. projectGuid: this.projectGuid,
  391. parameterGuid: this.parameterGuid,
  392. value: this.valueToRemove
  393. }
  394. });
  395. this.deleteLockedValueModal.hide();
  396. }
  397.  
  398.  
  399. onProjectParametersModalHide(): void {
  400. this.setLockedValuesTabInactive();
  401. this.parametersForm.reset();
  402. this.errors = undefined;
  403. this.editingModeLockedValue = false;
  404. this.lockedValuesInputClear();
  405.  
  406. // reset search on categories on modal hide
  407. this.categoriesTable.search = undefined;
  408. this.categoriesTable.resetSearch();
  409. this.selectAllCategories = false;
  410. }
  411.  
  412. // reset search input and input for creating locked values
  413. lockedValuesInputClear(): void {
  414. this.listOfValuesForm.get('addValue').reset();
  415. this.listOfValuesForm.get('lockedValuesSearch').setValue('');
  416. }
  417.  
  418. /**
  419. * Receive value from locked
  420. * values checkbox and enable/disable
  421. * values on parameter
  422. * @params status {boolean}
  423. */
  424. onCheckboxLockedValues(status: boolean): void {
  425.  
  426. this.store.dispatch({
  427. type: PARAMETER_ENABLE_LOCKED_VALUE,
  428. payload: {
  429. parameterGuid: this.parameterGuid,
  430. projectGuid: this.projectGuid,
  431. enabled: status
  432. }
  433. });
  434.  
  435. this.lockedValuesInputClear();
  436. }
  437.  
  438. private showParameterModal(): void {
  439. this.store.dispatch({
  440. type: MODAL_SHOW
  441. });
  442.  
  443. this.modal.show();
  444. }
  445.  
  446. setLockedValuesTabInactive(): void {
  447. if (this.lockedListOfValuesTab !== undefined && this.tabSet.tabs.length > 0) {
  448. this.tabSet.tabs.forEach((tab: Tab) => {
  449. if (tab.id === this.lockedListOfValuesTab.id) {
  450. tab.isActive = false;
  451. this.zone.run(() => this.cd.markForCheck());
  452. }
  453. });
  454. }
  455. };
  456.  
  457. setLockedValuesTabActive(): void {
  458. if (this.tabSet) {
  459. this.tabSet.tabs.forEach((tab: Tab) => {
  460. tab.isActive = false;
  461. this.zone.run(() => this.cd.markForCheck());
  462. if (tab.id === this.lockedListOfValuesTab.id) {
  463. tab.isActive = true;
  464. this.zone.run(() => this.cd.markForCheck());
  465. }
  466. });
  467. }
  468. }
  469.  
  470. private resetToDefaultFormSettings(): void {
  471. this.parametersForm.get('settings').get('canCopy').setValue(this.paramValues.canCopy[0].value);
  472. this.parametersForm.get('settings').get('dataType').setValue(this.paramValues.dataType[0].value);
  473. this.parametersForm.get('settings').get('parameterType').setValue(this.paramValues.parameterType[0].value);
  474. this.parametersForm.get('settings').get('inUse').setValue(true);
  475. this.parametersForm.get('settings').get('readOnly').setValue(this.paramValues.readOnly[0].value);
  476. this.parametersForm.get('categories').setValue([]);
  477. this.initializeClients();
  478. }
  479.  
  480. private initializeClients(): void {
  481. const controls: FormArray = this.parametersForm.get('clients') as FormArray;
  482.  
  483. if (controls.length) {
  484. for (let i = 0; i < controls.length; i++) {
  485. controls.at(i).patchValue({
  486. client: this.activeClients[i]['_id'],
  487. code: this.activeClients[i]['code'],
  488. identifier: '',
  489. nameInModel: '',
  490. syncToBimeye: this.paramValues.syncToBimeye[1].value,
  491. syncFromBimeye: this.paramValues.syncFromBimeye[1].value
  492. });
  493. }
  494. } else {
  495. this.activeClients
  496. .forEach((client: IClients, index: number) => (this.parametersForm.get('clients') as FormArray).push(this.fb.group({
  497. client: new FormControl(client['_id']),
  498. code: new FormControl(client['code']),
  499. identifier: new FormControl(''),
  500. nameInModel: new FormControl(''),
  501. syncToBimeye: new FormControl(this.paramValues.syncToBimeye[1].value, Validators.required),
  502. syncFromBimeye: new FormControl(this.paramValues.syncFromBimeye[1].value, Validators.required)
  503. })));
  504. }
  505. }
  506.  
  507. /**
  508. * On category click
  509. * @param event
  510. */
  511. onCategoryClick(event: { name: string, guid: string, isChecked: boolean }) {
  512.  
  513. if (event.isChecked) {
  514. this.categoriesForms.push(this.fb.group({
  515. name: new FormControl(event.name),
  516. guid: new FormControl(event.guid)
  517. })
  518. );
  519. } else {
  520. let i = 0;
  521.  
  522. this.categoriesForms.controls.forEach((ctrl: FormControl) => {
  523. if (ctrl.value.name === event.name) {
  524. this.categoriesForms.removeAt(i);
  525. return;
  526. }
  527.  
  528. i++;
  529. });
  530. }
  531. }
  532.  
  533. /**
  534. * Check is category active. This is used for update window.
  535. * @param categoryGuid
  536. * @returns {boolean}
  537. */
  538. isCategoryActive(categoryGuid: string): boolean {
  539. let checked = false;
  540.  
  541. this.parametersForm.get('categories').value.forEach(checkedCategory => {
  542. if (checkedCategory.guid === categoryGuid) {
  543. checked = true;
  544. return;
  545. }
  546. });
  547.  
  548. return checked;
  549. }
  550.  
  551. /**
  552. * On category click
  553. * @param event
  554. */
  555. onSelectAllCategories(event: { isChecked: boolean, search: string, columns: Array<string> }) {
  556. this.selectAllCategories = true;
  557. const searchedCategories: Array<{}> = FilterUtilities.multiColumnNestedFilter(this.activeProjectCategories, event.search, event.columns);
  558.  
  559. if (event.isChecked) {
  560. this.resetCategories();
  561. searchedCategories.forEach((category: ICategory) => {
  562. this.categoriesForms.push(this.fb.group({
  563. name: new FormControl(category.name),
  564. guid: new FormControl(category['guid'])
  565. }));
  566. });
  567. } else {
  568. this.resetCategories();
  569. }
  570. }
  571.  
  572. /**
  573. * Function which deselect all categories
  574. */
  575. private resetCategories() {
  576. const len = this.categoriesForms.controls.length;
  577. if (len >= 1) {
  578. for (let i = len; i >= 0; i--) {
  579. this.categoriesForms.removeAt(i);
  580. }
  581. }
  582. }
  583. }
Add Comment
Please, Sign In to add comment