/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { ChangeDetectorRef, Component, OnInit } from '@angular/core'; import { SettingType, SettingsItem, isNullOrUndefined, isObject, isOptionalArrayOf, isOptionalOf, isSettingsItem, isString } from '@kangaroo-and-rabbit/kar-cw'; import { SettingsService } from 'base/service'; export interface SettingsItem222 { // Displayed Title title?: string; // Description of the parameter description?: string; // If true the parameter is applied directly to the backend directApply?: boolean; // Parameter key to SET/GET or the sub-menu values?: SettingsItem[]; // Local new Parameters: newValues?: object; // local state: if undefined => no change, otherwise the number of wrong values. state?: number; // values get from the server. serverValues?: object; } export function isSettingsItem222(data: any): data is SettingsItem222 { if (isNullOrUndefined(data)) { return false; } if (!isObject(data)) { return false; } if (!isOptionalOf(data.title, isString)) { return false; } if (!isOptionalOf(data.description, isString)) { return false; } if (!isOptionalOf(data.key, isString)) { return false; } if (!isOptionalArrayOf(data.values, isSettingsItem)) { return false; } return true; } @Component({ selector: 'app-settings', templateUrl: './settings.html', styleUrls: ['./settings.less'], }) export class SettingsScene implements OnInit { page = ''; constructor( private settingService: SettingsService, private cdr: ChangeDetectorRef, ) { } menu: SettingsItem222[] = [ { title: 'Authentication:', description: 'Manage the right of access to the web-services', values: undefined, }, ]; // this permit to clear the input menu... configureEditInput() { const root = this.menu[0]; root.values = [ { type: SettingType.BOOLEAN, title: 'Enable Sign-in', description: 'Enable User to sign-in (only authorize administrators).', key: 'SIGN_IN_ENABLE', value: root.serverValues["SIGN_IN_ENABLE"], }, { type: SettingType.LINE, }, { type: SettingType.BOOLEAN, title: 'Enable Sign-up', description: 'Enable unregister user to sign-up (register) on this web-site.', key: 'SIGN_UP_ENABLE', value: root.serverValues["SIGN_UP_ENABLE"], }, { type: SettingType.STRING, title: 'Sign-up e-mail filter', description: 'Specify the e-mail filtering to sign-up (regex).', key: 'SIGN_UP_FILTER', value: root.serverValues["SIGN_UP_FILTER"], }, { type: SettingType.BOOLEAN, title: 'e-mail validation required', description: 'Force-user to validate his e-mail to access on service.', key: 'EMAIL_VALIDATION_REQUIRED', value: root.serverValues["EMAIL_VALIDATION_REQUIRED"], }, ], this.menu[0].state = undefined; this.menu[0].newValues = {}; } ngOnInit() { this.settingService .gets(['SIGN_IN_ENABLE', 'SIGN_UP_ENABLE', 'SIGN_UP_FILTER', 'EMAIL_VALIDATION_REQUIRED']) .then((response: object) => { console.log(`??? get full response: ${JSON.stringify(response, null, 4)}`); this.menu[0].serverValues = response; this.configureEditInput(); }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); } onState(value: number, elem: SettingsItem222) { console.log(`changeState : ${JSON.stringify(value, null, 2)}`); elem.state = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } onDeltaValues(value: any, elem: SettingsItem222) { console.log(`onDeltaValues : ${JSON.stringify(value, null, 2)}`); elem.newValues = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } /* changeValueElement(newValue: string, elem: SettingsItem222, parent: SettingsItem222): void { if (elem.value === newValue) { elem.newValue = undefined; this.updateSendNeeded(parent); return; } elem.newValue = newValue; this.updateSendNeeded(parent); } updateSendNeeded(parent: SettingsItem222) { if (isNullOrUndefined(parent.value)) { return; } if (!isArrayOf(parent.value, isSettingsItem222)) { console.log('Error Can not convert type setting menu'); return; } let needUpdate = false; for (let elem of parent.value) { if (!isUndefined(elem.newValue)) { needUpdate = true; break; } } parent.newValue = needUpdate; } onUpdate(parent: SettingsItem222) {} */ onUpdate(elem: SettingsItem222) { const self = this; this.settingService.sets(elem.newValues) .then((result: object) => { console.log(`data is updated: ${JSON.stringify(result, null, 2)}`); let keys = Object.keys(result); keys.forEach(key => { console.log(` KEY='${key}'`); const values = self.menu[0].values; for (let iii = 0; iii < values.length; iii++) { if (values[iii].key === key) { console.log(` Find=${JSON.stringify(values[iii], null, 2)}`); values[iii].value = values[iii].newValue; values[iii].newValue = undefined; return; } } }); self.cdr.detectChanges(); }) .catch((error: { fail: object, done: object }) => { let keys = Object.keys(error.done); keys.forEach(key => { const values = self.menu[0].values; for (let iii = 0; iii < values.length; iii++) { if (values[iii].key === key) { values[iii].value = values[iii].newValue; values[iii].newValue = undefined; return; } } }); }); } }