blob: 2e3ad0955b856dbfcb2502835a94ddb537bff716 [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;
29 @Output() onCheckValue = new EventEmitter();
30
31 constructor(private elRef:ElementRef) {}
Radek Krejci9b41f5b2018-01-31 14:17:50 +010032
Radek Krejci6e772b22018-01-25 13:28:57 +010033 ngAfterContentInit() {
34 console.log(this.node)
35 let node = this.node;
36 let element = this.elRef.nativeElement;
37 element.value = node['value'];
38 this.onCheckValue.emit({node, element});
39 }
40}
41
42@Component({
Radek Krejci9b41f5b2018-01-31 14:17:50 +010043 selector: 'tree-create',
44 templateUrl: 'tree-create.html',
45 styleUrls: ['./tree.component.scss']
46})
47export class TreeCreate implements OnInit {
48 @Input() node;
49 @Input() indentation;
50 activeSession: Session;
51
52 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
53
54 ngOnInit(): void {
55 this.activeSession = this.sessionsService.getActiveSession();
56 }
57
58 closeCreatingDialog(node, reason='abort') {
59 this.modsService.createClose(node, reason);
60 }
61
62 creatingDialogSelect(node, index, source) {
63 this.modsService.create(this.activeSession, node, index);
64 this.sessionsService.storeData();
Radek Krejci5a69fa32018-02-01 11:03:04 +010065 if (('schemaChildren' in node) && node['schemaChildren'].length) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +010066 source.selectedIndex = 0;
67 }
68 }
69}
70
71@Component({
Radek Krejci6e772b22018-01-25 13:28:57 +010072 selector: 'tree-indent',
73 templateUrl: 'tree-indent.html',
74 styleUrls: ['./tree.component.scss']
75})
76export class TreeIndent implements OnInit {
77 @Input() node;
78 @Input() indentation;
79 @Input() type = "current";
Radek Krejci6e772b22018-01-25 13:28:57 +010080 activeSession: Session;
Radek Krejcicb890972018-01-26 10:12:45 +010081 timeout;
Radek Krejci6e772b22018-01-25 13:28:57 +010082
Radek Krejci9b41f5b2018-01-31 14:17:50 +010083 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
Radek Krejci6e772b22018-01-25 13:28:57 +010084
85 ngOnInit(): void {
86 this.activeSession = this.sessionsService.getActiveSession();
87 }
88
89 showEditMenu(event) {
Radek Krejcicb890972018-01-26 10:12:45 +010090 this.timeout = setTimeout(() => {
91 let menu = event.target.lastElementChild;
92 menu.style.visibility = "visible";
Radek Krejci6cdd21a2018-01-26 13:13:14 +010093 menu.style.top = event.target.getBoundingClientRect().top + 'px';
94 menu.style.left = event.target.getBoundingClientRect().left + (event.target.getBoundingClientRect().width / 2) + 'px';
Radek Krejcicb890972018-01-26 10:12:45 +010095 }, 300);
Radek Krejci6e772b22018-01-25 13:28:57 +010096 }
Radek Krejcicb890972018-01-26 10:12:45 +010097
Radek Krejci08ad0a82018-01-26 11:25:17 +010098 hideEditMenu(menu) {
Radek Krejcicb890972018-01-26 10:12:45 +010099 clearTimeout(this.timeout);
Radek Krejci08ad0a82018-01-26 11:25:17 +0100100 menu.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100101 }
Radek Krejcicb890972018-01-26 10:12:45 +0100102
Radek Krejci6e772b22018-01-25 13:28:57 +0100103 deleteSubtree(node) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100104 this.modsService.delete(this.activeSession, node);
105 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100106 }
Radek Krejci499d1ee2018-01-30 14:04:54 +0100107
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100108 openCreatingDialog(element, node, parent = false) {
109 if (parent) {
110 node = this.modsService.nodeParent(this.activeSession, node);
111 }
112 if (!('creatingChild' in node)) {
113 this.sessionsService.childrenSchemas(this.activeSession.key, node['info']['path'], node).then(result => {
114 this.modsService.createOpen(result, node);
115 });
116 } else if (element){
117 /* scroll to the existing element */
118 element.ownerDocument.getElementById(node['path'] + '_createChildDialog').scrollIntoView(false);
119 }
120 }
121
122 closeCreatingDialog(node, reason='abort') {
123 this.modsService.createClose(node, reason);
124 }
125
126 cancelModification(node) {
127 this.modsService.cancelModification(this.activeSession, node, false);
128 this.sessionsService.storeData();
Radek Krejci499d1ee2018-01-30 14:04:54 +0100129 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100130}
131
Radek Krejcia1339602017-11-02 13:52:38 +0100132@Component({
133 selector: 'tree-view',
134 templateUrl: './tree.component.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +0100135 styleUrls: ['./tree.component.scss']
Radek Krejcia1339602017-11-02 13:52:38 +0100136})
137
138export class TreeView implements OnInit {
Radek Krejci6e772b22018-01-25 13:28:57 +0100139 @Input() node;
Radek Krejcia1339602017-11-02 13:52:38 +0100140 @Input() indentation;
Radek Krejcia1339602017-11-02 13:52:38 +0100141 activeSession: Session;
Radek Krejci5a69fa32018-02-01 11:03:04 +0100142
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100143 constructor(private modsService: ModificationsService,
144 private sessionsService: SessionsService,
Radek Krejci5a69fa32018-02-01 11:03:04 +0100145 private treeService: TreeService,
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100146 private changeDetector: ChangeDetectorRef) {}
Radek Krejcia1339602017-11-02 13:52:38 +0100147
148 ngOnInit(): void {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100149 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejcia1339602017-11-02 13:52:38 +0100150 }
151
Radek Krejci77f77202017-11-03 15:33:50 +0100152 inheritIndentation(node) {
153 let newIndent;
154 if (node['last']) {
155 newIndent = [true];
156 } else {
157 newIndent = [false];
Radek Krejcia1339602017-11-02 13:52:38 +0100158 }
Radek Krejci77f77202017-11-03 15:33:50 +0100159
160 if (!this.indentation) {
161 return newIndent;
162 } else {
163 return this.indentation.concat(newIndent);
164 }
Radek Krejcia1339602017-11-02 13:52:38 +0100165 }
166
Radek Krejci6e772b22018-01-25 13:28:57 +0100167 startEditing(node, target) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100168 if ((node['info']['key'] && !node['new']) || node['deleted']) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100169 return;
170 }
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100171 let parent = target.parentElement;
172
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100173 this.modsService.setEdit(node, true)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100174 this.changeDetector.detectChanges();
175
Radek Krejci6e772b22018-01-25 13:28:57 +0100176 parent.nextElementSibling.lastElementChild.focus();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100177 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100178
Radek Krejci4d3896c2018-01-08 17:10:43 +0100179 checkValue(node, target) {
180 let confirm = target.previousElementSibling;
Radek Krejci6e772b22018-01-25 13:28:57 +0100181 let cancel = confirm.previousElementSibling;
182 let path: string;
183 if ('creatingChild' in node) {
184 path = node['creatingChild']['path'];
185 } else {
186 path = node['info']['path'];
187 }
188 this.sessionsService.checkValue(this.activeSession.key, path, target.value).subscribe(result => {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100189 if (result['success']) {
190 target.classList.remove("invalid");
191 confirm.style.visibility = "visible";
Radek Krejci6e772b22018-01-25 13:28:57 +0100192 if ('value' in node) {
193 cancel.style.visibility = "visible";
194 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100195 } else {
196 target.classList.add("invalid");
197 confirm.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100198 if (!('value' in node)) {
199 cancel.style.visibility = "hidden";
200 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100201 }
202 });
203 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100204
205 changeValueCancel(node) {
206 if ('value' in node) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100207 this.modsService.setEdit(node, false);
Radek Krejci6e772b22018-01-25 13:28:57 +0100208 }
209 }
210
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100211 changeValue(node, target) {
212 let input;
213 if (target.classList.contains('value')) {
214 if (target.classList.contains('invalid')) {
215 return;
216 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100217 input = target;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100218 } else {
219 input = target.nextElementSibling;
220 }
221
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100222 this.modsService.change(this.activeSession, node, input.value);
Radek Krejci6e772b22018-01-25 13:28:57 +0100223 this.sessionsService.storeData();
224 }
225
Radek Krejci6e772b22018-01-25 13:28:57 +0100226 newChildrenToShow(node) {
227 if ('newChildren' in node) {
228 return node['newChildren'];
229 } else {
230 return [];
231 }
232 }
Radek Krejcia1339602017-11-02 13:52:38 +0100233}