/
ResourceReactor.js
114 lines (113 loc) · 6.8 KB
/
ResourceReactor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from 'react';
import PropTypes from 'prop-types';
import {enableAuthentication} from '../../configs/general';
import ResourceStore from '../../stores/ResourceStore';
import {connectToStores} from 'fluxible-addons-react';
import Resource from '../resource/Resource';
import UserResource from '../resource/UserResource';
import PersonResource from '../resource/PersonResource';
import OrgResource from '../resource/OrgResource';
import CSVMappingResource from '../resource/CSVMappingResource';
import createProperty from '../../actions/createProperty';
import ReactDOM from 'react-dom';
import PrefixBasedInput from '../object/editor/individual/PrefixBasedInput';
class ResourceReactor extends React.Component {
constructor(props) {
super(props);
this.state = {
newPropURI: '',
newObjetValue: ''
};
}
//removes properties from an object
configMinus(config, props) {
let o = {};
for (let p in config) {
if (props.indexOf(p) === -1) {
o[p] = config[p];
}
}
return o;
}
handleNewProperty(e) {
let self = this;
if(this.state.newPropURI && this.state.newObjetValue){
this.context.executeAction(createProperty, {
dataset: self.props.ResourceStore.datasetURI,
resourceURI: self.props.ResourceStore.resourceURI,
propertyURI: this.state.newPropURI,
objectValue: this.state.newObjetValue
});
}
}
handleNewPropertyEdit(v) {
this.setState({newPropURI: v.trim()});
}
handleNewObjectValueEdit(v) {
this.setState({newObjetValue: v.trim()});
}
render() {
let datasetURI = this.props.ResourceStore.datasetURI;
let properties = this.props.ResourceStore.properties;
let resourceURI = this.props.ResourceStore.resourceURI;
let resourceType = this.props.ResourceStore.resourceType;
let title = this.props.ResourceStore.title;
let currentCategory = this.props.ResourceStore.currentCategory;
let propertyPath = this.props.ResourceStore.propertyPath;
let config = this.props.ResourceStore.config;
let error = this.props.ResourceStore.error;
let resourceReactor;
if (config && config.resourceReactor) {
switch (config.resourceReactor[0]) {
case 'Resource':
resourceReactor = <Resource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
break;
case 'UserResource':
resourceReactor = <UserResource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
break;
case 'PersonResource':
resourceReactor = <PersonResource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
break;
case 'OrgResource':
resourceReactor = <OrgResource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
break;
case 'CSVMappingResource':
resourceReactor = <CSVMappingResource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
break;
default:
resourceReactor = <Resource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
}
}else{
resourceReactor = <Resource enableAuthentication={enableAuthentication} datasetURI={datasetURI} properties={properties} resource={resourceURI} resourceType={resourceType} title={title} currentCategory={currentCategory} propertyPath={propertyPath} config={this.configMinus(config, ['resourceReactor'])} error={error}/>;
}
let newPropDIV = '';
if(!error && config && config.allowPropertyNew && !config.readOnly && (config.userIsCreator || config.userIsEditor)){
newPropDIV = <div className="ui fluid container ldr-padding-more"><div className="ui grid">
<div className="ui column"><div className="ui grey message form">
<PrefixBasedInput includeOnly={['ldrProperties','properties']} noFocus={true} spec={{value:''}} onDataEdit={this.handleNewPropertyEdit.bind(this)} placeholder="Enter the URI of the property. You can use common prefixes e.g. foaf:name"/>
<PrefixBasedInput noFocus={true} spec={{value:''}} onDataEdit={this.handleNewObjectValueEdit.bind(this)} placeholder="Value of the property" onEnterPress={this.handleNewProperty.bind(this)} allowActionByKey={true}/>
<button className="fluid ui primary icon button" onClick={this.handleNewProperty.bind(this)}><i className="icon square add"></i>Add Property/Value</button>
</div></div></div></div>;
}
let itemTypes = '';
if(Array.isArray(resourceType)){
itemTypes = resourceType.join(' ');
}else{
itemTypes = resourceType;
}
return (
<div ref="resourceReactor" itemScope itemType={itemTypes} itemID={resourceURI}>
{resourceReactor}
{newPropDIV}
</div>
);
}
}
ResourceReactor.contextTypes = {
executeAction: PropTypes.func.isRequired,
getUser: PropTypes.func
};
ResourceReactor = connectToStores(ResourceReactor, [ResourceStore], function(context, props) {
return {ResourceStore: context.getStore(ResourceStore).getState()};
});
export default ResourceReactor;