blob: 7cc941f8de4131e5b1e1d289e8a094534652deca [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 Krejci5a69fa32018-02-01 11:03:04 +01008import {TreeService} from './config.component';
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') {
63 this.modsService.createClose(node, reason);
64 }
65
66 creatingDialogSelect(node, index, source) {
67 this.modsService.create(this.activeSession, node, index);
68 this.sessionsService.storeData();
Radek Krejci5a69fa32018-02-01 11:03:04 +010069 if (('schemaChildren' in node) && node['schemaChildren'].length) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +010070 source.selectedIndex = 0;
71 }
72 }
73}
74
75@Component({
Radek Krejci6e772b22018-01-25 13:28:57 +010076 selector: 'tree-indent',
77 templateUrl: 'tree-indent.html',
78 styleUrls: ['./tree.component.scss']
79})
80export class TreeIndent implements OnInit {
81 @Input() node;
Radek Krejci7a244f02018-02-21 09:46:18 +010082 @Input() value;
Radek Krejci6e772b22018-01-25 13:28:57 +010083 @Input() indentation;
Radek Krejci7a244f02018-02-21 09:46:18 +010084 @Input() type;
Radek Krejci6e772b22018-01-25 13:28:57 +010085 activeSession: Session;
Radek Krejci7a244f02018-02-21 09:46:18 +010086 private timeout;
Radek Krejci6e772b22018-01-25 13:28:57 +010087
Radek Krejci9b41f5b2018-01-31 14:17:50 +010088 constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {}
Radek Krejci6e772b22018-01-25 13:28:57 +010089
90 ngOnInit(): void {
91 this.activeSession = this.sessionsService.getActiveSession();
92 }
93
Radek Krejci7a244f02018-02-21 09:46:18 +010094 getType():string {
95 if (this.type) {
96 return this.type;
97 } else {
98 if (this.node && ('new' in this.node)) {
99 return "new";
100 } else if (this.node && ('deleted' in this.node)) {
101 return "deleted";
102 } else {
103 return "current";
104 }
105 }
106 }
107
Radek Krejci6e772b22018-01-25 13:28:57 +0100108 showEditMenu(event) {
Radek Krejcicb890972018-01-26 10:12:45 +0100109 this.timeout = setTimeout(() => {
110 let menu = event.target.lastElementChild;
111 menu.style.visibility = "visible";
Radek Krejci6cdd21a2018-01-26 13:13:14 +0100112 menu.style.top = event.target.getBoundingClientRect().top + 'px';
113 menu.style.left = event.target.getBoundingClientRect().left + (event.target.getBoundingClientRect().width / 2) + 'px';
Radek Krejcicb890972018-01-26 10:12:45 +0100114 }, 300);
Radek Krejci6e772b22018-01-25 13:28:57 +0100115 }
Radek Krejcicb890972018-01-26 10:12:45 +0100116
Radek Krejci08ad0a82018-01-26 11:25:17 +0100117 hideEditMenu(menu) {
Radek Krejcicb890972018-01-26 10:12:45 +0100118 clearTimeout(this.timeout);
Radek Krejci08ad0a82018-01-26 11:25:17 +0100119 menu.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100120 }
Radek Krejcicb890972018-01-26 10:12:45 +0100121
Radek Krejci7a244f02018-02-21 09:46:18 +0100122 deleteSubtree(node, value = null) {
123 this.modsService.delete(this.activeSession, node, value);
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100124 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100125 }
Radek Krejci499d1ee2018-01-30 14:04:54 +0100126
Radek Krejci7a244f02018-02-21 09:46:18 +0100127 /* 0 - not deleted, 1 - deleted value, 2 - deleted all values */
128 isDeleted(): number {
129 if ('deleted' in this.node) {
130 if (typeof this.node['deleted'] === 'boolean') {
131 if (this.node['deleted']) {
132 return 2;
133 } else {
134 return 0;
135 }
136 } else if (this.value) {
137 if (this.node['deleted'].indexOf(this.value) != -1) {
138 return 1;
139 }
140 }
141 }
142 return 0;
143 }
144
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100145 openCreatingDialog(element, node, parent = false) {
146 if (parent) {
147 node = this.modsService.nodeParent(this.activeSession, node);
148 }
149 if (!('creatingChild' in node)) {
150 this.sessionsService.childrenSchemas(this.activeSession.key, node['info']['path'], node).then(result => {
151 this.modsService.createOpen(result, node);
152 });
153 } else if (element){
154 /* scroll to the existing element */
155 element.ownerDocument.getElementById(node['path'] + '_createChildDialog').scrollIntoView(false);
156 }
157 }
158
159 closeCreatingDialog(node, reason='abort') {
160 this.modsService.createClose(node, reason);
161 }
162
Radek Krejci7a244f02018-02-21 09:46:18 +0100163 cancelModification(node, value = null) {
164 if (value && node['deleted'].length > 1) {
165 let i = node['deleted'].indexOf(value);
166 node['deleted'].splice(i, 1);
167 } else {
168 this.modsService.cancelModification(this.activeSession, node, false);
169 }
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100170 this.sessionsService.storeData();
Radek Krejci499d1ee2018-01-30 14:04:54 +0100171 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100172}
173
Radek Krejcia1339602017-11-02 13:52:38 +0100174@Component({
175 selector: 'tree-view',
176 templateUrl: './tree.component.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +0100177 styleUrls: ['./tree.component.scss']
Radek Krejcia1339602017-11-02 13:52:38 +0100178})
179
180export class TreeView implements OnInit {
Radek Krejci6e772b22018-01-25 13:28:57 +0100181 @Input() node;
Radek Krejcia1339602017-11-02 13:52:38 +0100182 @Input() indentation;
Radek Krejcia1339602017-11-02 13:52:38 +0100183 activeSession: Session;
Radek Krejci5a69fa32018-02-01 11:03:04 +0100184
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100185 constructor(private modsService: ModificationsService,
186 private sessionsService: SessionsService,
Radek Krejci5a69fa32018-02-01 11:03:04 +0100187 private treeService: TreeService,
Radek Krejciad45e572018-02-21 10:54:54 +0100188 private schemasService: SchemasService,
189 private changeDetector: ChangeDetectorRef,
190 private router: Router) {}
Radek Krejcia1339602017-11-02 13:52:38 +0100191
192 ngOnInit(): void {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100193 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejcia1339602017-11-02 13:52:38 +0100194 }
195
Radek Krejci77f77202017-11-03 15:33:50 +0100196 inheritIndentation(node) {
197 let newIndent;
198 if (node['last']) {
199 newIndent = [true];
200 } else {
201 newIndent = [false];
Radek Krejcia1339602017-11-02 13:52:38 +0100202 }
Radek Krejci77f77202017-11-03 15:33:50 +0100203
204 if (!this.indentation) {
205 return newIndent;
206 } else {
207 return this.indentation.concat(newIndent);
208 }
Radek Krejcia1339602017-11-02 13:52:38 +0100209 }
210
Radek Krejci6e772b22018-01-25 13:28:57 +0100211 startEditing(node, target) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100212 if ((node['info']['key'] && !node['new']) || node['deleted']) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100213 return;
214 }
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100215 let parent = target.parentElement;
216
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100217 this.modsService.setEdit(this.activeSession, node, true)
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100218 this.changeDetector.detectChanges();
219
Radek Krejci6e772b22018-01-25 13:28:57 +0100220 parent.nextElementSibling.lastElementChild.focus();
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100221 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100222
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100223 checkValue(node, target, trusted = false) {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100224 let confirm = target.previousElementSibling;
Radek Krejci6e772b22018-01-25 13:28:57 +0100225 let cancel = confirm.previousElementSibling;
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100226
227 if (trusted) {
228 /* value is selected from valid options */
229 target.classList.remove("invalid");
230 confirm.style.visibility = "visible";
231 if ('value' in node) {
232 cancel.style.visibility = "visible";
233 }
234 return;
235 }
236
Radek Krejci6e772b22018-01-25 13:28:57 +0100237 let path: string;
238 if ('creatingChild' in node) {
239 path = node['creatingChild']['path'];
240 } else {
241 path = node['info']['path'];
242 }
243 this.sessionsService.checkValue(this.activeSession.key, path, target.value).subscribe(result => {
Radek Krejci4d3896c2018-01-08 17:10:43 +0100244 if (result['success']) {
245 target.classList.remove("invalid");
246 confirm.style.visibility = "visible";
Radek Krejci6e772b22018-01-25 13:28:57 +0100247 if ('value' in node) {
248 cancel.style.visibility = "visible";
249 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100250 } else {
251 target.classList.add("invalid");
252 confirm.style.visibility = "hidden";
Radek Krejci6e772b22018-01-25 13:28:57 +0100253 if (!('value' in node)) {
254 cancel.style.visibility = "hidden";
255 }
Radek Krejci4d3896c2018-01-08 17:10:43 +0100256 }
257 });
258 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100259
260 changeValueCancel(node) {
261 if ('value' in node) {
Radek Krejcid0ce4cf2018-02-09 14:44:34 +0100262 this.modsService.setEdit(this.activeSession, node, false);
Radek Krejci7a244f02018-02-21 09:46:18 +0100263 } else {
264 this.modsService.cancelModification(this.activeSession, node, false);
Radek Krejci6e772b22018-01-25 13:28:57 +0100265 }
Radek Krejci7a244f02018-02-21 09:46:18 +0100266 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100267 }
268
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100269 changeValue(node, target) {
270 let input;
271 if (target.classList.contains('value')) {
272 if (target.classList.contains('invalid')) {
273 return;
274 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100275 input = target;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100276 } else {
277 input = target.nextElementSibling;
278 }
279
Radek Krejci7a244f02018-02-21 09:46:18 +0100280 if (node['info']['type'] == 8) {
281 this.modsService.change(this.activeSession, node, [input.value]);
282 } else {
283 this.modsService.change(this.activeSession, node, input.value);
284 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100285 this.sessionsService.storeData();
286 }
287
Radek Krejci7a244f02018-02-21 09:46:18 +0100288 nodeValue(node, index:number = 0): string {
289 if ('value' in node) {
290 if (node['info']['type'] == 4) {
291 return node['value'];
292 } else if (node['info']['type'] == 8 && node['value'].length > index) {
293 return node['value'][index];
294 }
295 }
296 return null;
297 }
298
Radek Krejciad45e572018-02-21 10:54:54 +0100299 moduleName(node): string {
300 let at = node['info']['module'].indexOf('@');
301 if (at == -1) {
302 return node['info']['module'];
303 } else {
304 return node['info']['module'].substring(0, at);
305 }
306 }
307
308 showSchema(node) {
309 let schema = new Schema;
310 let at = node['info']['module'].indexOf('@');
311 if (at == -1) {
312 schema.name = node['info']['module'];
313 } else {
314 schema.name = node['info']['module'].substring(0, at);
315 schema.revision = node['info']['module'].substring(at + 1);
316 }
317 let key = node['info']['module'] + '.yang';
318
319 schema.name = this.moduleName(node);
320 this.schemasService.show(key, schema);
321 this.schemasService.changeActiveSchemaKey(key);
322 this.router.navigateByUrl( '/netopeer/yang' );
323 }
324
Radek Krejci6e772b22018-01-25 13:28:57 +0100325 newChildrenToShow(node) {
326 if ('newChildren' in node) {
327 return node['newChildren'];
328 } else {
329 return [];
330 }
331 }
Radek Krejcia1339602017-11-02 13:52:38 +0100332}