-
Notifications
You must be signed in to change notification settings - Fork 6
/
NewAttribute.js
150 lines (143 loc) · 4.83 KB
/
NewAttribute.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React from 'react'
import PropTypes from 'prop-types'
import { Component } from 'react'
import { MDCDialog } from '@material/dialog'
import AttPicker from '../pickers/AttPicker'
import { clone } from '../../utils/clone'
export default class NewAttributeDialog extends Component {
constructor(props) {
super(props)
this.defaultValue = 'newAttribute'
this.state = {
attribute: this.defaultValue,
canCreate: true
}
this.dialog
}
componentDidMount() {
this.dialog = new MDCDialog(this.refs.na)
this.dialog.listen('MDCDialog:closed', (event) => {
// NB default action needs to happen in all cases,
// do not add breaks.
switch (event.detail.action) {
case 'add_attribute':
this.props.add(this.state.attribute)
case 'add_from_picker':
case 'cancel':
default:
this.setState({attribute: this.defaultValue})
this.props.hide()
this.dialog.close()
}
})
this.dialog.listen('MDCDialog:opened', () => {
this.dialog.layout()
if (this.props.associatedAttributes.indexOf(this.state.attribute) !== -1) {
this.setState({canCreate: false})
}
})
}
componentDidUpdate() {
if (this.props.show) {
this.dialog.open()
}
}
cloneAttribute(type, attribute) {
const newAtt = clone(attribute)
newAtt.onElement = true
newAtt.mode = 'add'
newAtt._isNew = true
newAtt.clonedFrom = attribute.fromClass
return newAtt
}
setAttribute(val) {
if (val.length > 0) {
this.setState({
attribute: val,
canCreate: true
})
} else {
this.setState({
attribute: this.defaultValue
})
}
if (this.props.associatedAttributes.indexOf(val) !== -1) {
this.setState({canCreate: false})
}
// test the localname
let d = document.createElement('dummy')
try {
d.setAttribute(val, '')
} catch (e) {
this.setState({canCreate: false})
}
d = null
}
render() {
return (
<aside className="mdc-dialog"
ref="na"
role="alertdialog"
aria-modal="true">
<div className="mdc-dialog__container">
<div className="mdc-dialog__surface">
<h2 className="mdc-dialog__title">
Create new attribute
</h2>
<div className="mdc-dialog__content">
<div className="mdc-layout-grid__inner romajs-formrow">
<div className="mdc-layout-grid__cell--span-3">
<label>New</label>
<p className="mdc-text-field-helper-text mdc-text-field-helper-text--persistent">
Create an entirely new attribute.
</p>
</div>
<div className="mdc-layout-grid__cell--span-6">
<div className="mdc-text-field mdc-text-field--upgraded">
<input type="text" className="mdc-text-field__input"
onChange={(e) => this.setAttribute(e.target.value)} placeholder={this.state.attribute}/>
<div className="mdc-text-field__bottom-line" style={{transformOrigin: '145px center'}}/>
</div>
</div>
<div className="mdc-layout-grid__cell--span-2">
<button className="mdc-button mdc-dialog__button" data-mdc-dialog-action="add_attribute" disabled={!this.state.canCreate}>
Create
</button>
</div>
</div>
<div className="mdc-layout-grid__inner romajs-formrow">
<div className="mdc-layout-grid__cell--span-3">
<label>Copy</label>
<p className="mdc-text-field-helper-text mdc-text-field-helper-text--persistent">
Duplicate an existing attribute.
</p>
</div>
<div className="mdc-layout-grid__cell--span-8">
<AttPicker showAll items={this.props.items} pickerType={'attribute'} add={(t, a) => {
this.props.add(this.cloneAttribute(t, a))
this.props.hide()
this.dialog.close()
}} />
</div>
</div>
</div>
<footer className="mdc-dialog__actions">
<button ref="cancelBtn" type="button" className="mdc-button mdc-dialog__button" data-mdc-dialog-action="cancel">
Cancel
</button>
</footer>
</div>
</div>
<div className="mdc-dialog__scrim"/>
</aside>
)
}
}
NewAttributeDialog.propTypes = {
show: PropTypes.bool.isRequired,
items: PropTypes.array.isRequired,
associatedAttributes: PropTypes.array.isRequired,
add: PropTypes.func,
hide: PropTypes.func,
navigateToAttribute: PropTypes.func.isRequired
}