Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 1 | import {Component, Directive, ElementRef, EventEmitter, Input, Output, OnInit, ChangeDetectorRef} from '@angular/core'; |
| 2 | import {Router} from '@angular/router'; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 3 | |
| 4 | import {Session} from './session'; |
Radek Krejci | ad45e57 | 2018-02-21 10:54:54 +0100 | [diff] [blame] | 5 | import {Schema} from '../inventory/schema'; |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 6 | import {ModificationsService} from './modifications.service'; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 7 | import {SessionsService} from './sessions.service'; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 8 | import {TreeService} from './config.component'; |
Radek Krejci | ad45e57 | 2018-02-21 10:54:54 +0100 | [diff] [blame] | 9 | import {SchemasService} from '../yang/schemas.service'; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 10 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 11 | @Directive({ |
| 12 | selector: '[treeScrollTo]' |
| 13 | }) |
| 14 | export class TreeScrollTo { |
| 15 | @Input() node; |
| 16 | |
| 17 | constructor(private elRef:ElementRef) {} |
| 18 | ngAfterContentInit() { |
| 19 | if (!('new' in this.node)) { |
Radek Krejci | 7708844 | 2018-01-26 11:32:12 +0100 | [diff] [blame] | 20 | let middle = this.elRef.nativeElement.getBoundingClientRect().top + window.pageYOffset - (window.innerHeight / 2); |
| 21 | window.scrollTo(0, middle); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | @Directive({ |
| 27 | selector: '[checkLeafValue]' |
| 28 | }) |
| 29 | export class CheckLeafValue { |
| 30 | @Input() node; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 31 | @Input() trusted = false; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 32 | @Output() onCheckValue = new EventEmitter(); |
| 33 | |
| 34 | constructor(private elRef:ElementRef) {} |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 35 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 36 | ngAfterContentInit() { |
| 37 | console.log(this.node) |
| 38 | let node = this.node; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 39 | let trusted = this.trusted; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 40 | let element = this.elRef.nativeElement; |
| 41 | element.value = node['value']; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 42 | this.onCheckValue.emit({node, element, trusted}); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | @Component({ |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 47 | selector: 'tree-create', |
| 48 | templateUrl: 'tree-create.html', |
| 49 | styleUrls: ['./tree.component.scss'] |
| 50 | }) |
| 51 | export 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 Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 69 | if (('schemaChildren' in node) && node['schemaChildren'].length) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 70 | source.selectedIndex = 0; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Component({ |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 76 | selector: 'tree-indent', |
| 77 | templateUrl: 'tree-indent.html', |
| 78 | styleUrls: ['./tree.component.scss'] |
| 79 | }) |
| 80 | export class TreeIndent implements OnInit { |
| 81 | @Input() node; |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 82 | @Input() value; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 83 | @Input() indentation; |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 84 | @Input() type; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 85 | activeSession: Session; |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 86 | private timeout; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 87 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 88 | constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {} |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 89 | |
| 90 | ngOnInit(): void { |
| 91 | this.activeSession = this.sessionsService.getActiveSession(); |
| 92 | } |
| 93 | |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 94 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 108 | showEditMenu(event) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 109 | this.timeout = setTimeout(() => { |
| 110 | let menu = event.target.lastElementChild; |
| 111 | menu.style.visibility = "visible"; |
Radek Krejci | 6cdd21a | 2018-01-26 13:13:14 +0100 | [diff] [blame] | 112 | menu.style.top = event.target.getBoundingClientRect().top + 'px'; |
| 113 | menu.style.left = event.target.getBoundingClientRect().left + (event.target.getBoundingClientRect().width / 2) + 'px'; |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 114 | }, 300); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 115 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 116 | |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 117 | hideEditMenu(menu) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 118 | clearTimeout(this.timeout); |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 119 | menu.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 120 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 121 | |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 122 | deleteSubtree(node, value = null) { |
| 123 | this.modsService.delete(this.activeSession, node, value); |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 124 | this.sessionsService.storeData(); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 125 | } |
Radek Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 126 | |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 127 | /* 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 Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 145 | 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 Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 163 | 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 Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 170 | this.sessionsService.storeData(); |
Radek Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 171 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 174 | @Component({ |
| 175 | selector: 'tree-view', |
| 176 | templateUrl: './tree.component.html', |
Radek Krejci | cd1ebe1 | 2018-01-11 11:34:17 +0100 | [diff] [blame] | 177 | styleUrls: ['./tree.component.scss'] |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 178 | }) |
| 179 | |
| 180 | export class TreeView implements OnInit { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 181 | @Input() node; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 182 | @Input() indentation; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 183 | activeSession: Session; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 184 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 185 | constructor(private modsService: ModificationsService, |
| 186 | private sessionsService: SessionsService, |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 187 | private treeService: TreeService, |
Radek Krejci | ad45e57 | 2018-02-21 10:54:54 +0100 | [diff] [blame] | 188 | private schemasService: SchemasService, |
| 189 | private changeDetector: ChangeDetectorRef, |
| 190 | private router: Router) {} |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 191 | |
| 192 | ngOnInit(): void { |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 193 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 196 | inheritIndentation(node) { |
| 197 | let newIndent; |
| 198 | if (node['last']) { |
| 199 | newIndent = [true]; |
| 200 | } else { |
| 201 | newIndent = [false]; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 202 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 203 | |
| 204 | if (!this.indentation) { |
| 205 | return newIndent; |
| 206 | } else { |
| 207 | return this.indentation.concat(newIndent); |
| 208 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 211 | startEditing(node, target) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 212 | if ((node['info']['key'] && !node['new']) || node['deleted']) { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 213 | return; |
| 214 | } |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 215 | let parent = target.parentElement; |
| 216 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 217 | this.modsService.setEdit(this.activeSession, node, true) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 218 | this.changeDetector.detectChanges(); |
| 219 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 220 | parent.nextElementSibling.lastElementChild.focus(); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 221 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 222 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 223 | checkValue(node, target, trusted = false) { |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 224 | let confirm = target.previousElementSibling; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 225 | let cancel = confirm.previousElementSibling; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 226 | |
| 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 237 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 244 | if (result['success']) { |
| 245 | target.classList.remove("invalid"); |
| 246 | confirm.style.visibility = "visible"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 247 | if ('value' in node) { |
| 248 | cancel.style.visibility = "visible"; |
| 249 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 250 | } else { |
| 251 | target.classList.add("invalid"); |
| 252 | confirm.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 253 | if (!('value' in node)) { |
| 254 | cancel.style.visibility = "hidden"; |
| 255 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 256 | } |
| 257 | }); |
| 258 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 259 | |
| 260 | changeValueCancel(node) { |
| 261 | if ('value' in node) { |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame] | 262 | this.modsService.setEdit(this.activeSession, node, false); |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 263 | } else { |
| 264 | this.modsService.cancelModification(this.activeSession, node, false); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 265 | } |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 266 | this.sessionsService.storeData(); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 269 | changeValue(node, target) { |
| 270 | let input; |
| 271 | if (target.classList.contains('value')) { |
| 272 | if (target.classList.contains('invalid')) { |
| 273 | return; |
| 274 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 275 | input = target; |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 276 | } else { |
| 277 | input = target.nextElementSibling; |
| 278 | } |
| 279 | |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 280 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 285 | this.sessionsService.storeData(); |
| 286 | } |
| 287 | |
Radek Krejci | 7a244f0 | 2018-02-21 09:46:18 +0100 | [diff] [blame] | 288 | 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 Krejci | ad45e57 | 2018-02-21 10:54:54 +0100 | [diff] [blame] | 299 | 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 325 | newChildrenToShow(node) { |
| 326 | if ('newChildren' in node) { |
| 327 | return node['newChildren']; |
| 328 | } else { |
| 329 | return []; |
| 330 | } |
| 331 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 332 | } |