update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / composer / src / src / components / model / EditDescriptorUtils.js
1 /*
2 *
3 * Copyright 2016-2017 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 import getEventPath from '../../libraries/getEventPath'
20 import DeletionManager from '../../libraries/DeletionManager'
21
22 function startEditing() {
23 DeletionManager.removeEventListeners();
24 }
25
26 function endEditing() {
27 DeletionManager.addEventListeners();
28 }
29
30 function onFocusPropertyFormInputElement(event) {
31
32 console.debug('property focus ', event.target.id);
33 event.preventDefault();
34 startEditing();
35
36 function removeIsFocusedClass(event) {
37 event.target.removeEventListener('blur', removeIsFocusedClass);
38 Array.from(document.querySelectorAll('.-is-focused')).forEach(d => d.classList.remove('-is-focused'));
39 }
40
41 removeIsFocusedClass(event);
42
43 const propertyWrapper = getEventPath(event).reduce((parent, element) => {
44 if (parent) {
45 return parent;
46 }
47 if (!element.classList) {
48 return false;
49 }
50 if (element.classList.contains('property')) {
51 return element;
52 }
53 }, false);
54
55 if (propertyWrapper) {
56 propertyWrapper.classList.add('-is-focused');
57 event.target.addEventListener('blur', removeIsFocusedClass);
58 }
59 }
60
61 function getTitle(model = {}) {
62 if (typeof model['short-name'] === 'string' && model['short-name']) {
63 return model['short-name'];
64 }
65 if (typeof model.name === 'string' && model.name) {
66 return model.name;
67 }
68 if (model.uiState && typeof model.uiState.displayName === 'string' && model.uiState.displayName) {
69 return model.uiState.displayName
70 }
71 if (typeof model.id === 'string') {
72 return model.id;
73 }
74 }
75
76 export {
77 startEditing,
78 endEditing,
79 onFocusPropertyFormInputElement,
80 getTitle
81 }