blob: ef04a3b038f1ae1483e95f8bbcd12077b67c49c8 [file] [log] [blame]
Radek Krejci6e772b22018-01-25 13:28:57 +01001import {Component, Directive, ElementRef, EventEmitter, Input, Output, OnInit, ChangeDetectorRef} from '@angular/core';
2import {Router} from '@angular/router';
Radek Krejcia1339602017-11-02 13:52:38 +01003
4import {Session} from './session';
Radek Krejci9b41f5b2018-01-31 14:17:50 +01005import {ModificationsService} from './modifications.service';
Radek Krejcia1339602017-11-02 13:52:38 +01006import {SessionsService} from './sessions.service';
Radek Krejci5a69fa32018-02-01 11:03:04 +01007import {TreeService} from './config.component';
Radek Krejcia1339602017-11-02 13:52:38 +01008
Radek Krejci6e772b22018-01-25 13:28:57 +01009@Directive({
10 selector: '[treeScrollTo]'
11})
12export class TreeScrollTo {
13 @Input() node;
14
15 constructor(private elRef:ElementRef) {}
16 ngAfterContentInit() {
17 if (!('new' in this.node)) {
Radek Krejci77088442018-01-26 11:32:12 +010018 let middle = this.elRef.nativeElement.getBoundingClientRect().top + window.pageYOffset - (window.innerHeight / 2);
19 window.scrollTo(0, middle);
Radek Krejci6e772b22018-01-25 13:28:57 +010020 }
21 }
22}
23
24@Directive({
25 selector: '[checkLeafValue]'
26})
27export class CheckLeafValue {
28 @Input() node;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010029 @Input() trusted = false;
Radek Krejci6e772b22018-01-25 13:28:57 +010030 @Output() onCheckValue = new EventEmitter();
31
32 constructor(private elRef:ElementRef) {}
Radek Krejci9b41f5b2018-01-31 14:17:50 +010033
Radek Krejci6e772b22018-01-25 13:28:57 +010034 ngAfterContentInit() {
35 console.log(this.node)
36 let node = this.node;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010037 let trusted = this.trusted;
Radek Krejci6e772b22018-01-25 13:28:57 +010038 let element = this.elRef.nativeElement;
39 element.value = node['value'];
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010040 this.onCheckValue.emit({node, element, trusted});
Radek Krejci6e772b22018-01-25 13:28:57 +010041 }
42}
43
44@Component({
Radek Krejci9b41f5b2018-01-31 14:17:50 +010045 selector: 'tree-create',
46 templateUrl: 'tree-create.html',
47 styleUrls: ['./tree.component.scss']
48})
49export class TreeCreate implements OnInit {
50 @Input() node;
51 @Input() indentation;
52 activeSession: Session;
53
54 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
55
56 ngOnInit(): void {
57 this.activeSession = this.sessionsService.getActiveSession();
58 }
59
60 closeCreatingDialog(node, reason='abort') {
61 this.modsService.createClose(node, reason);
62 }
63
64 creatingDialogSelect(node, index, source) {
65 this.modsService.create(this.activeSession, node, index);
66 this.sessionsService.storeData();
Radek Krejci5a69fa32018-02-01 11:03:04 +010067 if (('schemaChildren' in node) && node['schemaChildren'].length) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +010068 source.selectedIndex = 0;
69 }
70 }
71}
72
73@Component({
Radek Krejci6e772b22018-01-25 13:28:57 +010074 selector: 'tree-indent',
75 templateUrl: 'tree-indent.html',
76 styleUrls: ['./tree.component.scss']
77})
78export class TreeIndent implements OnInit {
79 @Input() node;
80 @Input() indentation;
81 @Input() type = "current";
Radek Krejci6e772b22018-01-25 13:28:57 +010082 activeSession: Session;
Radek Krejcicb890972018-01-26 10:12:45 +010083 timeout;
Radek Krejci6e772b22018-01-25 13:28:57 +010084
Radek Krejci9b41f5b2018-01-31 14:17:50 +010085 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
Radek Krejci6e772b22018-01-25 13:28:57 +010086
87 ngOnInit(): void {
88 this.activeSession = this.sessionsService.getActiveSession();
89 }
90
91 showEditMenu(event) {
Radek Krejcicb890972018-01-26 10:12:45 +010092 this.timeout = setTimeout(() => {
93 let menu = event.target.lastElementChild;
94 menu.style.visibility = "visible";
Radek Krejci6cdd21a2018-01-26 13:13:14 +010095 menu.style.top = event.target.getBoundingClientRect().top + 'px';
96 menu.style.left = event.target.getBoundingClientRect().left + (event.target.getBoundingClientRect().width / 2) + 'px';
Radek Krejcicb890972018-01-26 10:12:45 +010097 }, 300);
Radek Krejci6e772b22018-01-25 13:28:57 +010098 }
Radek Krejcicb890972018-01-26 10:12:45 +010099
Radek Krejci08ad0a82018-01-26 11:25:17 +0100100 hideEditMenu(menu) {
Radek Krejcicb890972018-01-26 10:12:45 +0100101 clearTimeout(this.timeout);
Radek Krejci08ad0a82018-01-26 11:25:17 +0100102 menu.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100103 }
Radek Krejcicb890972018-01-26 10:12:45 +0100104
Radek Krejci6e772b22018-01-25 13:28:57 +0100105 deleteSubtree(node) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100106 this.modsService.delete(this.activeSession, node);
107 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100108 }
Radek Krejci499d1ee2018-01-30 14:04:54 +0100109
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100110 openCreatingDialog(element, node, parent = false) {
111 if (parent) {
112 node = this.modsService.nodeParent(this.activeSession, node);
113 }
114 if (!('creatingChild' in node)) {
115 this.sessionsService.childrenSchemas(this.activeSession.key, node['info']['path'], node).then(result => {
116 this.modsService.createOpen(result, node);
117 });
118 } else if (element){
119 /* scroll to the existing element */
120 element.ownerDocument.getElementById(node['path'] + '_createChildDialog').scrollIntoView(false);
121 }
122 }
123
124 closeCreatingDialog(node, reason='abort') {
125 this.modsService.createClose(node, reason);
126 }
127
128 cancelModification(node) {
129 this.modsService.cancelModification(this.activeSession, node, false);
130 this.sessionsService.storeData();
Radek Krejci499d1ee2018-01-30 14:04:54 +0100131 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100132}
133
Radek Krejcia1339602017-11-02 13:52:38 +0100134@Component({
135 selector: 'tree-view',
136 templateUrl: './tree.component.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +0100137 styleUrls: ['./tree.component.scss']
Radek Krejcia1339602017-11-02 13:52:38 +0100138})
139
140export class TreeView implements OnInit {
Radek Krejci6e772b22018-01-25 13:28:57 +0100141 @Input() node;
Radek Krejcia1339602017-11-02 13:52:38 +0100142 @Input() indentation;
Radek Krejcia1339602017-11-02 13:52:38 +0100143 activeSession: Session;
Radek Krejci5a69fa32018-02-01 11:03:04 +0100144
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100145 constructor(private modsService: ModificationsService,
146 private sessionsService: SessionsService,
Radek Krejci5a69fa32018-02-01 11:03:04 +0100147 private treeService: TreeService,
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100148 private changeDetector: ChangeDetectorRef) {}
Radek Krejcia1339602017-11-02 13:52:38 +0100149
150 ngOnInit(): void {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100151 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejcia1339602017-11-02 13:52:38 +0100152 }
153
Radek Krejci77f77202017-11-03 15:33:50 +0100154 inheritIndentation(node) {
155 let newIndent;
156 if (node['last']) {
157 newIndent = [true];
158 } else {
159 newIndent = [false];
Radek Krejcia1339602017-11-02 13:52:38 +0100160 }
Radek Krejci77f77202017-11-03 15:33:50 +0100161
162 if (!this.indentation) {
163 return newIndent;
164 } else {
165 return this.indentation.concat(newIndent);
166 }
Radek Krejcia1339602017-11-02 13:52:38 +0100167 }
168
Radek Krejci6e772b22018-01-25 13:28:57 +0100169 startEditing(node, target) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100170 if ((node['info']['key'] && !node['new']) || node['deleted']) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100171 return;
172 }
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100173 let parent = target.parentElement;
174
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100175 this.modsService.setEdit(this.activeSession, node, true)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100176 this.changeDetector.detectChanges();
177
Radek Krejci6e772b22018-01-25 13:28:57 +0100178 parent.nextElementSibling.lastElementChild.focus();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100179 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100180
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100181 checkValue(node, target, trusted = false) {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100182 let confirm = target.previousElementSibling;
Radek Krejci6e772b22018-01-25 13:28:57 +0100183 let cancel = confirm.previousElementSibling;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100184
185 if (trusted) {
186 /* value is selected from valid options */
187 target.classList.remove("invalid");
188 confirm.style.visibility = "visible";
189 if ('value' in node) {
190 cancel.style.visibility = "visible";
191 }
192 return;
193 }
194
Radek Krejci6e772b22018-01-25 13:28:57 +0100195 let path: string;
196 if ('creatingChild' in node) {
197 path = node['creatingChild']['path'];
198 } else {
199 path = node['info']['path'];
200 }
201 this.sessionsService.checkValue(this.activeSession.key, path, target.value).subscribe(result => {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100202 if (result['success']) {
203 target.classList.remove("invalid");
204 confirm.style.visibility = "visible";
Radek Krejci6e772b22018-01-25 13:28:57 +0100205 if ('value' in node) {
206 cancel.style.visibility = "visible";
207 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100208 } else {
209 target.classList.add("invalid");
210 confirm.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100211 if (!('value' in node)) {
212 cancel.style.visibility = "hidden";
213 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100214 }
215 });
216 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100217
218 changeValueCancel(node) {
219 if ('value' in node) {
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100220 this.modsService.setEdit(this.activeSession, node, false);
Radek Krejci6e772b22018-01-25 13:28:57 +0100221 }
222 }
223
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100224 changeValue(node, target) {
225 let input;
226 if (target.classList.contains('value')) {
227 if (target.classList.contains('invalid')) {
228 return;
229 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100230 input = target;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100231 } else {
232 input = target.nextElementSibling;
233 }
234
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100235 this.modsService.change(this.activeSession, node, input.value);
Radek Krejci6e772b22018-01-25 13:28:57 +0100236 this.sessionsService.storeData();
237 }
238
Radek Krejci6e772b22018-01-25 13:28:57 +0100239 newChildrenToShow(node) {
240 if ('newChildren' in node) {
241 return node['newChildren'];
242 } else {
243 return [];
244 }
245 }
Radek Krejcia1339602017-11-02 13:52:38 +0100246}