/*
*
* Copyright 2016 RIFT.IO Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* Created by onvelocity on 1/18/16.
*
* This class generates the form fields used to edit the CONFD JSON model.
*/
'use strict';
import _ from 'lodash'
import utils from '../libraries/utils'
import React from 'react'
import ClassNames from 'classnames'
import changeCase from 'change-case'
import toggle from '../libraries/ToggleElementHandler'
import Button from './Button'
import Property from '../libraries/model/DescriptorModelMetaProperty'
import ComposerAppActions from '../actions/ComposerAppActions'
import CatalogItemsActions from '../actions/CatalogItemsActions'
import DESCRIPTOR_MODEL_FIELDS from '../libraries/model/DescriptorModelFields'
import DescriptorModelFactory from '../libraries/model/DescriptorModelFactory'
import DescriptorModelMetaFactory from '../libraries/model/DescriptorModelMetaFactory'
import SelectionManager from '../libraries/SelectionManager'
import DeletionManager from '../libraries/DeletionManager'
import DescriptorModelIconFactory from '../libraries/model/IconFactory'
import getEventPath from '../libraries/getEventPath'
import CatalogDataStore from '../stores/CatalogDataStore'
import imgAdd from '../../../node_modules/open-iconic/svg/plus.svg'
import imgRemove from '../../../node_modules/open-iconic/svg/trash.svg'
import '../styles/EditDescriptorModelProperties.scss'
function getDescriptorMetaBasicForType(type) {
const basicPropertiesFilter = d => _.includes(DESCRIPTOR_MODEL_FIELDS[type], d.name);
return DescriptorModelMetaFactory.getModelMetaForType(type, basicPropertiesFilter) || {properties: []};
}
function getDescriptorMetaAdvancedForType(type) {
const advPropertiesFilter = d => !_.includes(DESCRIPTOR_MODEL_FIELDS[type], d.name);
return DescriptorModelMetaFactory.getModelMetaForType(type, advPropertiesFilter) || {properties: []};
}
function getTitle(model = {}) {
if (typeof model['short-name'] === 'string' && model['short-name']) {
return model['short-name'];
}
if (typeof model.name === 'string' && model.name) {
return model.name;
}
if (model.uiState && typeof model.uiState.displayName === 'string' && model.uiState.displayName) {
return model.uiState.displayName
}
if (typeof model.id === 'string') {
return model.id;
}
}
export default function EditDescriptorModelProperties(props) {
const container = props.container;
if (!(DescriptorModelFactory.isContainer(container))) {
return
}
function startEditing() {
DeletionManager.removeEventListeners();
}
function endEditing() {
DeletionManager.addEventListeners();
}
function onClickSelectItem(property, path, value, event) {
event.preventDefault();
const root = this.getRoot();
if (SelectionManager.select(value)) {
CatalogItemsActions.catalogItemMetaDataChanged(root.model);
}
}
function onFocusPropertyFormInputElement(property, path, value, event) {
event.preventDefault();
startEditing();
function removeIsFocusedClass(event) {
event.target.removeEventListener('blur', removeIsFocusedClass);
Array.from(document.querySelectorAll('.-is-focused')).forEach(d => d.classList.remove('-is-focused'));
}
removeIsFocusedClass(event);
const propertyWrapper = getEventPath(event).reduce((parent, element) => {
if (parent) {
return parent;
}
if (!element.classList) {
return false;
}
if (element.classList.contains('property')) {
return element;
}
}, false);
if (propertyWrapper) {
propertyWrapper.classList.add('-is-focused');
event.target.addEventListener('blur', removeIsFocusedClass);
}
}
function buildAddPropertyAction(container, property, path) {
function onClickAddProperty(property, path, event) {
event.preventDefault();
//SelectionManager.resume();
const create = Property.getContainerCreateMethod(property, this);
if (create) {
const model = null;
create(model, path, property);
} else {
const name = path.join('.');
const value = Property.createModelInstance(property);
utils.assignPathValue(this.model, name, value);
}
CatalogItemsActions.catalogItemDescriptorChanged(this.getRoot());
}
return (
);
}
function buildRemovePropertyAction(container, property, path) {
function onClickRemoveProperty(property, path, event) {
event.preventDefault();
const name = path.join('.');
const removeMethod = Property.getContainerMethod(property, this, 'remove');
if (removeMethod) {
removeMethod(utils.resolvePath(this.model, name));
} else {
utils.removePathValue(this.model, name);
}
CatalogItemsActions.catalogItemDescriptorChanged(this.getRoot());
}
return (