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; |
| 29 | @Output() onCheckValue = new EventEmitter(); |
| 30 | |
| 31 | constructor(private elRef:ElementRef) {} |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 32 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 33 | 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 Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 43 | selector: 'tree-create', |
| 44 | templateUrl: 'tree-create.html', |
| 45 | styleUrls: ['./tree.component.scss'] |
| 46 | }) |
| 47 | export 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 Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 65 | if (('schemaChildren' in node) && node['schemaChildren'].length) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 66 | source.selectedIndex = 0; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Component({ |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 72 | selector: 'tree-indent', |
| 73 | templateUrl: 'tree-indent.html', |
| 74 | styleUrls: ['./tree.component.scss'] |
| 75 | }) |
| 76 | export class TreeIndent implements OnInit { |
| 77 | @Input() node; |
| 78 | @Input() indentation; |
| 79 | @Input() type = "current"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 80 | activeSession: Session; |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 81 | timeout; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 82 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 83 | constructor(private modsService: ModificationsService, private sessionsService: SessionsService) {} |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 84 | |
| 85 | ngOnInit(): void { |
| 86 | this.activeSession = this.sessionsService.getActiveSession(); |
| 87 | } |
| 88 | |
| 89 | showEditMenu(event) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 90 | this.timeout = setTimeout(() => { |
| 91 | let menu = event.target.lastElementChild; |
| 92 | menu.style.visibility = "visible"; |
Radek Krejci | 6cdd21a | 2018-01-26 13:13:14 +0100 | [diff] [blame] | 93 | menu.style.top = event.target.getBoundingClientRect().top + 'px'; |
| 94 | 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] | 95 | }, 300); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 96 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 97 | |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 98 | hideEditMenu(menu) { |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 99 | clearTimeout(this.timeout); |
Radek Krejci | 08ad0a8 | 2018-01-26 11:25:17 +0100 | [diff] [blame] | 100 | menu.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 101 | } |
Radek Krejci | cb89097 | 2018-01-26 10:12:45 +0100 | [diff] [blame] | 102 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 103 | deleteSubtree(node) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 104 | this.modsService.delete(this.activeSession, node); |
| 105 | this.sessionsService.storeData(); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 106 | } |
Radek Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 107 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 108 | 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 Krejci | 499d1ee | 2018-01-30 14:04:54 +0100 | [diff] [blame] | 129 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 132 | @Component({ |
| 133 | selector: 'tree-view', |
| 134 | templateUrl: './tree.component.html', |
Radek Krejci | cd1ebe1 | 2018-01-11 11:34:17 +0100 | [diff] [blame] | 135 | styleUrls: ['./tree.component.scss'] |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 136 | }) |
| 137 | |
| 138 | export class TreeView implements OnInit { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 139 | @Input() node; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 140 | @Input() indentation; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 141 | activeSession: Session; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 142 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 143 | constructor(private modsService: ModificationsService, |
| 144 | private sessionsService: SessionsService, |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 145 | private treeService: TreeService, |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 146 | private changeDetector: ChangeDetectorRef) {} |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 147 | |
| 148 | ngOnInit(): void { |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 149 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 152 | inheritIndentation(node) { |
| 153 | let newIndent; |
| 154 | if (node['last']) { |
| 155 | newIndent = [true]; |
| 156 | } else { |
| 157 | newIndent = [false]; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 158 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 159 | |
| 160 | if (!this.indentation) { |
| 161 | return newIndent; |
| 162 | } else { |
| 163 | return this.indentation.concat(newIndent); |
| 164 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 167 | startEditing(node, target) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 168 | if ((node['info']['key'] && !node['new']) || node['deleted']) { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 169 | return; |
| 170 | } |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 171 | let parent = target.parentElement; |
| 172 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 173 | this.modsService.setEdit(node, true) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 174 | this.changeDetector.detectChanges(); |
| 175 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 176 | parent.nextElementSibling.lastElementChild.focus(); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 177 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 178 | |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 179 | checkValue(node, target) { |
| 180 | let confirm = target.previousElementSibling; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 181 | 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 Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 189 | if (result['success']) { |
| 190 | target.classList.remove("invalid"); |
| 191 | confirm.style.visibility = "visible"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 192 | if ('value' in node) { |
| 193 | cancel.style.visibility = "visible"; |
| 194 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 195 | } else { |
| 196 | target.classList.add("invalid"); |
| 197 | confirm.style.visibility = "hidden"; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 198 | if (!('value' in node)) { |
| 199 | cancel.style.visibility = "hidden"; |
| 200 | } |
Radek Krejci | 4d3896c | 2018-01-08 17:10:43 +0100 | [diff] [blame] | 201 | } |
| 202 | }); |
| 203 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 204 | |
| 205 | changeValueCancel(node) { |
| 206 | if ('value' in node) { |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 207 | this.modsService.setEdit(node, false); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 211 | changeValue(node, target) { |
| 212 | let input; |
| 213 | if (target.classList.contains('value')) { |
| 214 | if (target.classList.contains('invalid')) { |
| 215 | return; |
| 216 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 217 | input = target; |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 218 | } else { |
| 219 | input = target.nextElementSibling; |
| 220 | } |
| 221 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 222 | this.modsService.change(this.activeSession, node, input.value); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 223 | this.sessionsService.storeData(); |
| 224 | } |
| 225 | |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 226 | newChildrenToShow(node) { |
| 227 | if ('newChildren' in node) { |
| 228 | return node['newChildren']; |
| 229 | } else { |
| 230 | return []; |
| 231 | } |
| 232 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 233 | } |