blob: 2810095e6cc894f59106ffba4524db4305f0e103 [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 Krejciad45e572018-02-21 10:54:54 +01005import {Schema} from '../inventory/schema';
Radek Krejci9b41f5b2018-01-31 14:17:50 +01006import {ModificationsService} from './modifications.service';
Radek Krejcia1339602017-11-02 13:52:38 +01007import {SessionsService} from './sessions.service';
Radek Krejci30ce1592018-03-01 14:44:14 +01008import {TreeService} from './tree.service';
Radek Krejciad45e572018-02-21 10:54:54 +01009import {SchemasService} from '../yang/schemas.service';
Radek Krejcia1339602017-11-02 13:52:38 +010010
Radek Krejci6e772b22018-01-25 13:28:57 +010011@Directive({
12 selector: '[treeScrollTo]'
13})
14export class TreeScrollTo {
15 @Input() node;
16
17 constructor(private elRef:ElementRef) {}
18 ngAfterContentInit() {
19 if (!('new' in this.node)) {
Radek Krejci77088442018-01-26 11:32:12 +010020 let middle = this.elRef.nativeElement.getBoundingClientRect().top + window.pageYOffset - (window.innerHeight / 2);
21 window.scrollTo(0, middle);
Radek Krejci6e772b22018-01-25 13:28:57 +010022 }
23 }
24}
25
26@Directive({
27 selector: '[checkLeafValue]'
28})
29export class CheckLeafValue {
30 @Input() node;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010031 @Input() trusted = false;
Radek Krejci6e772b22018-01-25 13:28:57 +010032 @Output() onCheckValue = new EventEmitter();
33
34 constructor(private elRef:ElementRef) {}
Radek Krejci9b41f5b2018-01-31 14:17:50 +010035
Radek Krejci6e772b22018-01-25 13:28:57 +010036 ngAfterContentInit() {
37 console.log(this.node)
38 let node = this.node;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010039 let trusted = this.trusted;
Radek Krejci6e772b22018-01-25 13:28:57 +010040 let element = this.elRef.nativeElement;
41 element.value = node['value'];
Radek Krejcid0ce4cf2018-02-09 14:44:34 +010042 this.onCheckValue.emit({node, element, trusted});
Radek Krejci6e772b22018-01-25 13:28:57 +010043 }
44}
45
46@Component({
Radek Krejci9b41f5b2018-01-31 14:17:50 +010047 selector: 'tree-create',
48 templateUrl: 'tree-create.html',
49 styleUrls: ['./tree.component.scss']
50})
51export class TreeCreate implements OnInit {
52 @Input() node;
53 @Input() indentation;
54 activeSession: Session;
55
56 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
57
58 ngOnInit(): void {
59 this.activeSession = this.sessionsService.getActiveSession();
60 }
61
62 closeCreatingDialog(node, reason='abort') {
Radek Krejci30ce1592018-03-01 14:44:14 +010063 this.modsService.createClose(this.activeSession, node, reason);
Radek Krejci9b41f5b2018-01-31 14:17:50 +010064 }
65
66 creatingDialogSelect(node, index, source) {
Radek Krejci30ce1592018-03-01 14:44:14 +010067 console.log(node)
Radek Krejci9b41f5b2018-01-31 14:17:50 +010068 this.modsService.create(this.activeSession, node, index);
69 this.sessionsService.storeData();
Radek Krejci5a69fa32018-02-01 11:03:04 +010070 if (('schemaChildren' in node) && node['schemaChildren'].length) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +010071 source.selectedIndex = 0;
72 }
73 }
74}
75
76@Component({
Radek Krejci6e772b22018-01-25 13:28:57 +010077 selector: 'tree-indent',
78 templateUrl: 'tree-indent.html',
79 styleUrls: ['./tree.component.scss']
80})
81export class TreeIndent implements OnInit {
82 @Input() node;
Radek Krejci7a244f02018-02-21 09:46:18 +010083 @Input() value;
Radek Krejci6e772b22018-01-25 13:28:57 +010084 @Input() indentation;
Radek Krejci7a244f02018-02-21 09:46:18 +010085 @Input() type;
Radek Krejci6e772b22018-01-25 13:28:57 +010086 activeSession: Session;
Radek Krejci7a244f02018-02-21 09:46:18 +010087 private timeout;
Radek Krejci6e772b22018-01-25 13:28:57 +010088
Radek Krejci30ce1592018-03-01 14:44:14 +010089 constructor(private treeService: TreeService,
90 private modsService: ModificationsService,
91 private sessionsService: SessionsService) {}
Radek Krejci6e772b22018-01-25 13:28:57 +010092
93 ngOnInit(): void {
94 this.activeSession = this.sessionsService.getActiveSession();
95 }
96
Radek Krejci7a244f02018-02-21 09:46:18 +010097 getType():string {
98 if (this.type) {
99 return this.type;
100 } else {
101 if (this.node && ('new' in this.node)) {
102 return "new";
103 } else if (this.node && ('deleted' in this.node)) {
104 return "deleted";
105 } else {
106 return "current";
107 }
108 }
109 }
110
Radek Krejci6e772b22018-01-25 13:28:57 +0100111 showEditMenu(event) {
Radek Krejcicb890972018-01-26 10:12:45 +0100112 this.timeout = setTimeout(() => {
113 let menu = event.target.lastElementChild;
114 menu.style.visibility = "visible";
Radek Krejci6cdd21a2018-01-26 13:13:14 +0100115 menu.style.top = event.target.getBoundingClientRect().top + 'px';
116 menu.style.left = event.target.getBoundingClientRect().left + (event.target.getBoundingClientRect().width / 2) + 'px';
Radek Krejcicb890972018-01-26 10:12:45 +0100117 }, 300);
Radek Krejci6e772b22018-01-25 13:28:57 +0100118 }
Radek Krejcicb890972018-01-26 10:12:45 +0100119
Radek Krejci08ad0a82018-01-26 11:25:17 +0100120 hideEditMenu(menu) {
Radek Krejcicb890972018-01-26 10:12:45 +0100121 clearTimeout(this.timeout);
Radek Krejci08ad0a82018-01-26 11:25:17 +0100122 menu.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100123 }
Radek Krejcicb890972018-01-26 10:12:45 +0100124
Radek Krejci30ce1592018-03-01 14:44:14 +0100125 deleteSubtree(node) {
126 let rmlist = [];
127 if (node['info']['type'] == 8) {
128 rmlist = this.treeService.nodesToShow(this.activeSession, node);
129 } else {
130 rmlist.push(node);
131 }
132 for (let item of rmlist) {
133 this.modsService.delete(this.activeSession, item);
134 }
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100135 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100136 }
Radek Krejci499d1ee2018-01-30 14:04:54 +0100137
Radek Krejci30ce1592018-03-01 14:44:14 +0100138 deleteInstance(node) {
139 this.modsService.delete(this.activeSession, node);
140 this.sessionsService.storeData();
Radek Krejci7a244f02018-02-21 09:46:18 +0100141 }
142
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100143 openCreatingDialog(element, node, parent = false) {
144 if (parent) {
Radek Krejci30ce1592018-03-01 14:44:14 +0100145 node = this.treeService.nodeParent(this.activeSession, node);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100146 }
147 if (!('creatingChild' in node)) {
148 this.sessionsService.childrenSchemas(this.activeSession.key, node['info']['path'], node).then(result => {
Radek Krejci30ce1592018-03-01 14:44:14 +0100149 this.modsService.createOpen(this.activeSession, result, node);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100150 });
151 } else if (element){
152 /* scroll to the existing element */
153 element.ownerDocument.getElementById(node['path'] + '_createChildDialog').scrollIntoView(false);
154 }
155 }
156
157 closeCreatingDialog(node, reason='abort') {
Radek Krejci30ce1592018-03-01 14:44:14 +0100158 this.modsService.createClose(this.activeSession, node, reason);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100159 }
160
Radek Krejci7a244f02018-02-21 09:46:18 +0100161 cancelModification(node, value = null) {
162 if (value && node['deleted'].length > 1) {
163 let i = node['deleted'].indexOf(value);
164 node['deleted'].splice(i, 1);
165 } else {
166 this.modsService.cancelModification(this.activeSession, node, false);
167 }
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100168 this.sessionsService.storeData();
Radek Krejci499d1ee2018-01-30 14:04:54 +0100169 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100170}
171
Radek Krejcia1339602017-11-02 13:52:38 +0100172@Component({
Radek Krejci30ce1592018-03-01 14:44:14 +0100173 selector: 'tree-node',
174 templateUrl: './tree-node.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +0100175 styleUrls: ['./tree.component.scss']
Radek Krejcia1339602017-11-02 13:52:38 +0100176})
177
Radek Krejci30ce1592018-03-01 14:44:14 +0100178export class TreeNode {
Radek Krejci6e772b22018-01-25 13:28:57 +0100179 @Input() node;
Radek Krejcia1339602017-11-02 13:52:38 +0100180 @Input() indentation;
Radek Krejci30ce1592018-03-01 14:44:14 +0100181 @Input() activeSession: Session;
Radek Krejci5a69fa32018-02-01 11:03:04 +0100182
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100183 constructor(private modsService: ModificationsService,
184 private sessionsService: SessionsService,
Radek Krejci5a69fa32018-02-01 11:03:04 +0100185 private treeService: TreeService,
Radek Krejciad45e572018-02-21 10:54:54 +0100186 private schemasService: SchemasService,
187 private changeDetector: ChangeDetectorRef,
188 private router: Router) {}
Radek Krejcia1339602017-11-02 13:52:38 +0100189
Radek Krejci77f77202017-11-03 15:33:50 +0100190 inheritIndentation(node) {
191 let newIndent;
192 if (node['last']) {
193 newIndent = [true];
194 } else {
195 newIndent = [false];
Radek Krejcia1339602017-11-02 13:52:38 +0100196 }
Radek Krejci77f77202017-11-03 15:33:50 +0100197
198 if (!this.indentation) {
199 return newIndent;
200 } else {
201 return this.indentation.concat(newIndent);
202 }
Radek Krejcia1339602017-11-02 13:52:38 +0100203 }
204
Radek Krejci6e772b22018-01-25 13:28:57 +0100205 startEditing(node, target) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100206 if ((node['info']['key'] && !node['new']) || node['deleted']) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100207 return;
208 }
Radek Krejcibf449e02018-02-23 11:39:00 +0100209
210 let container = target.parentElement.parentElement;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100211
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100212 this.modsService.setEdit(this.activeSession, node, true)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100213 this.changeDetector.detectChanges();
214
Radek Krejcibf449e02018-02-23 11:39:00 +0100215 container.nextElementSibling.lastElementChild.focus();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100216 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100217
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100218 checkValue(node, target, trusted = false) {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100219 let confirm = target.previousElementSibling;
Radek Krejci6e772b22018-01-25 13:28:57 +0100220 let cancel = confirm.previousElementSibling;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100221
222 if (trusted) {
223 /* value is selected from valid options */
224 target.classList.remove("invalid");
225 confirm.style.visibility = "visible";
226 if ('value' in node) {
227 cancel.style.visibility = "visible";
228 }
229 return;
230 }
231
Radek Krejci6e772b22018-01-25 13:28:57 +0100232 let path: string;
233 if ('creatingChild' in node) {
234 path = node['creatingChild']['path'];
235 } else {
236 path = node['info']['path'];
237 }
238 this.sessionsService.checkValue(this.activeSession.key, path, target.value).subscribe(result => {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100239 if (result['success']) {
240 target.classList.remove("invalid");
241 confirm.style.visibility = "visible";
Radek Krejci6e772b22018-01-25 13:28:57 +0100242 if ('value' in node) {
243 cancel.style.visibility = "visible";
244 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100245 } else {
246 target.classList.add("invalid");
247 confirm.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100248 if (!('value' in node)) {
249 cancel.style.visibility = "hidden";
250 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100251 }
252 });
253 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100254
255 changeValueCancel(node) {
256 if ('value' in node) {
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100257 this.modsService.setEdit(this.activeSession, node, false);
Radek Krejci7a244f02018-02-21 09:46:18 +0100258 } else {
259 this.modsService.cancelModification(this.activeSession, node, false);
Radek Krejci6e772b22018-01-25 13:28:57 +0100260 }
Radek Krejci7a244f02018-02-21 09:46:18 +0100261 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100262 }
263
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100264 changeValue(node, target) {
265 let input;
Radek Krejci30ce1592018-03-01 14:44:14 +0100266 if (target.classList.contains('value_inline')) {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100267 if (target.classList.contains('invalid')) {
268 return;
269 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100270 input = target;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100271 } else {
272 input = target.nextElementSibling;
273 }
274
Radek Krejci7a244f02018-02-21 09:46:18 +0100275 if (node['info']['type'] == 8) {
Radek Krejci30ce1592018-03-01 14:44:14 +0100276 this.modsService.change(this.activeSession, node, input.value);
Radek Krejci7a244f02018-02-21 09:46:18 +0100277 } else {
278 this.modsService.change(this.activeSession, node, input.value);
279 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100280 this.sessionsService.storeData();
281 }
282
Radek Krejci7a244f02018-02-21 09:46:18 +0100283 nodeValue(node, index:number = 0): string {
284 if ('value' in node) {
285 if (node['info']['type'] == 4) {
286 return node['value'];
287 } else if (node['info']['type'] == 8 && node['value'].length > index) {
288 return node['value'][index];
289 }
290 }
291 return null;
292 }
293
Radek Krejciad45e572018-02-21 10:54:54 +0100294 moduleName(node): string {
295 let at = node['info']['module'].indexOf('@');
296 if (at == -1) {
297 return node['info']['module'];
298 } else {
299 return node['info']['module'].substring(0, at);
300 }
301 }
302
303 showSchema(node) {
304 let schema = new Schema;
305 let at = node['info']['module'].indexOf('@');
306 if (at == -1) {
307 schema.name = node['info']['module'];
308 } else {
309 schema.name = node['info']['module'].substring(0, at);
310 schema.revision = node['info']['module'].substring(at + 1);
311 }
312 let key = node['info']['module'] + '.yang';
313
314 schema.name = this.moduleName(node);
315 this.schemasService.show(key, schema);
316 this.schemasService.changeActiveSchemaKey(key);
317 this.router.navigateByUrl( '/netopeer/yang' );
318 }
319
Radek Krejci6e772b22018-01-25 13:28:57 +0100320 newChildrenToShow(node) {
321 if ('newChildren' in node) {
322 return node['newChildren'];
323 } else {
324 return [];
325 }
326 }
Radek Krejcia1339602017-11-02 13:52:38 +0100327}
Radek Krejci30ce1592018-03-01 14:44:14 +0100328
329@Component({
330 selector: 'tree-view',
331 templateUrl: './tree.component.html',
332 styleUrls: ['./tree.component.scss']
333})
334
335export class TreeView implements OnInit {
336 @Input() node;
337 @Input() indentation;
338 activeSession: Session;
339
340 constructor(private sessionsService: SessionsService,
341 private treeService: TreeService) {}
342
343 ngOnInit(): void {
344 this.activeSession = this.sessionsService.getActiveSession();
345 }
346}
347