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 | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 5 | import {ModificationsService} from './modifications.service'; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 6 | import {SessionsService} from './sessions.service'; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 7 | import {TreeService} from './config.component'; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 8 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 9 | @Directive({ |
| 10 | selector: '[treeScrollTo]' |
| 11 | }) |
| 12 | export class TreeScrollTo { |
| 13 | @Input() node; |
| 14 | |
| 15 | constructor(private elRef:ElementRef) {} |
| 16 | ngAfterContentInit() { |
| 17 | if (!('new' in this.node)) { |
Radek Krejci | 7708844 | 2018-01-26 11:32:12 +0100 | [diff] [blame] | 18 | let middle = this.elRef.nativeElement.getBoundingClientRect().top + window.pageYOffset - (window.innerHeight / 2); |
| 19 | window.scrollTo(0, middle); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | @Directive({ |
| 25 | selector: '[checkLeafValue]' |
| 26 | }) |
| 27 | export class CheckLeafValue { |
| 28 | @Input() node; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 29 | @Input() trusted = false; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 30 | @Output() onCheckValue = new EventEmitter(); |
| 31 | |
| 32 | constructor(private elRef:ElementRef) {} |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 33 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 34 | ngAfterContentInit() { |
| 35 | console.log(this.node) |
| 36 | let node = this.node; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 37 | let trusted = this.trusted; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 38 | let element = this.elRef.nativeElement; |
| 39 | element.value = node['value']; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 40 | this.onCheckValue.emit({node, element, trusted}); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | @Component({ |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 45 | selector: 'tree-create', |
| 46 | templateUrl: 'tree-create.html', |
| 47 | styleUrls: ['./tree.component.scss'] |
| 48 | }) |
| 49 | export 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 Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 67 | if (('schemaChildren' in node) && node['schemaChildren'].length) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 68 | source.selectedIndex = 0; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Component({ |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 74 | selector: 'tree-indent', |
| 75 | templateUrl: 'tree-indent.html', |
| 76 | styleUrls: ['./tree.component.scss'] |
| 77 | }) |
| 78 | export class TreeIndent implements OnInit { |
| 79 | @Input() node; |
| 80 | @Input() indentation; |
| 81 | @Input() type = "current"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 82 | activeSession: Session; |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 83 | timeout; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 84 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 85 | constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {} |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 86 | |
| 87 | ngOnInit(): void { |
| 88 | this.activeSession = this.sessionsService.getActiveSession(); |
| 89 | } |
| 90 | |
| 91 | showEditMenu(event) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 92 | this.timeout = setTimeout(() => { |
| 93 | let menu = event.target.lastElementChild; |
| 94 | menu.style.visibility = "visible"; |
Radek Krejci | 6cdd21a | 2018-01-26 13:13:14 +0100 | [diff] [blame] | 95 | menu.style.top = event.target.getBoundingClientRect().top + 'px'; |
| 96 | 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] | 97 | }, 300); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 98 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 99 | |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 100 | hideEditMenu(menu) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 101 | clearTimeout(this.timeout); |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 102 | menu.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 103 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 104 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 105 | deleteSubtree(node) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 106 | this.modsService.delete(this.activeSession, node); |
| 107 | this.sessionsService.storeData(); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 108 | } |
Radek Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 109 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 110 | 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 Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 131 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 134 | @Component({ |
| 135 | selector: 'tree-view', |
| 136 | templateUrl: './tree.component.html', |
Radek Krejci | cd1ebe1 | 2018-01-11 11:34:17 +0100 | [diff] [blame] | 137 | styleUrls: ['./tree.component.scss'] |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 138 | }) |
| 139 | |
| 140 | export class TreeView implements OnInit { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 141 | @Input() node; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 142 | @Input() indentation; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 143 | activeSession: Session; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 144 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 145 | constructor(private modsService: ModificationsService, |
| 146 | private sessionsService: SessionsService, |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 147 | private treeService: TreeService, |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 148 | private changeDetector: ChangeDetectorRef) {} |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 149 | |
| 150 | ngOnInit(): void { |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 151 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 154 | inheritIndentation(node) { |
| 155 | let newIndent; |
| 156 | if (node['last']) { |
| 157 | newIndent = [true]; |
| 158 | } else { |
| 159 | newIndent = [false]; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 160 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 161 | |
| 162 | if (!this.indentation) { |
| 163 | return newIndent; |
| 164 | } else { |
| 165 | return this.indentation.concat(newIndent); |
| 166 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 169 | startEditing(node, target) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 170 | if ((node['info']['key'] && !node['new']) || node['deleted']) { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 171 | return; |
| 172 | } |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 173 | let parent = target.parentElement; |
| 174 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 175 | this.modsService.setEdit(this.activeSession, node, true) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 176 | this.changeDetector.detectChanges(); |
| 177 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 178 | parent.nextElementSibling.lastElementChild.focus(); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 179 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 180 | |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 181 | checkValue(node, target, trusted = false) { |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 182 | let confirm = target.previousElementSibling; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 183 | let cancel = confirm.previousElementSibling; |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 184 | |
| 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 Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 195 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 202 | if (result['success']) { |
| 203 | target.classList.remove("invalid"); |
| 204 | confirm.style.visibility = "visible"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 205 | if ('value' in node) { |
| 206 | cancel.style.visibility = "visible"; |
| 207 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 208 | } else { |
| 209 | target.classList.add("invalid"); |
| 210 | confirm.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 211 | if (!('value' in node)) { |
| 212 | cancel.style.visibility = "hidden"; |
| 213 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 214 | } |
| 215 | }); |
| 216 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 217 | |
| 218 | changeValueCancel(node) { |
| 219 | if ('value' in node) { |
Radek Krejci | d0ce4cf | 2018-02-09 14:44:34 +0100 | [diff] [blame^] | 220 | this.modsService.setEdit(this.activeSession, node, false); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 224 | changeValue(node, target) { |
| 225 | let input; |
| 226 | if (target.classList.contains('value')) { |
| 227 | if (target.classList.contains('invalid')) { |
| 228 | return; |
| 229 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 230 | input = target; |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 231 | } else { |
| 232 | input = target.nextElementSibling; |
| 233 | } |
| 234 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 235 | this.modsService.change(this.activeSession, node, input.value); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 236 | this.sessionsService.storeData(); |
| 237 | } |
| 238 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 239 | newChildrenToShow(node) { |
| 240 | if ('newChildren' in node) { |
| 241 | return node['newChildren']; |
| 242 | } else { |
| 243 | return []; |
| 244 | } |
| 245 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 246 | } |