refactoting tag manager

This commit is contained in:
or-else
2018-03-28 11:04:45 -07:00
parent ed268053f3
commit 515e27b489
2 changed files with 80 additions and 60 deletions
+2 -3
View File
@@ -744,6 +744,7 @@ table.permission-editor td.checkbox {
.panel-form { .panel-form {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
padding: 0;
} }
.panel-form-row { .panel-form-row {
@@ -1574,12 +1575,10 @@ ul.tabbar li.active > a:after {
border-bottom: 1px solid #ccc; border-bottom: 1px solid #ccc;
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);
transition: all 0.1s ease-in-out; transition: all 0.1s ease-in-out;
width: 100%;
max-height: 10rem; max-height: 10rem;
min-height: 4rem; min-height: 4rem;
overflow-y: scroll; overflow-y: scroll;
margin: 0.25rem 0rem 0.25rem 0rem; margin: 0.25rem 0 0.25rem 0;
padding: 0 0 0.5rem 0.5rem;
outline: none; outline: none;
color: #666; color: #666;
align-items: center; align-items: center;
+63 -42
View File
@@ -1449,14 +1449,6 @@
this.props.onSubmit(null, vcard(this.state.fullName, img)); this.props.onSubmit(null, vcard(this.state.fullName, img));
}, },
handleShowTagManager: function() {
this.setState({showTagManager: true});
},
handleHideTagManager: function() {
this.setState({showTagManager: false});
},
handleTagsUpdated: function(tags) { handleTagsUpdated: function(tags) {
this.setState({showTagManager: false}); this.setState({showTagManager: false});
// Check if tags have actually changed. // Check if tags have actually changed.
@@ -1466,6 +1458,10 @@
this.props.onUpdateTags(tags); this.props.onUpdateTags(tags);
}, },
handleHideTagManager: function() {
// do nothing. Needed to enable Cancel button in tag manager.
},
render: function() { render: function() {
var tags = []; var tags = [];
this.state.tags.map(function(tag) { this.state.tags.map(function(tag) {
@@ -1515,20 +1511,12 @@
</div> </div>
</div> </div>
<div className="hr" /> <div className="hr" />
{this.state.showTagManager ?
<TagManager <TagManager
title="Tags (user discovery)"
activated={false}
tags={this.state.tags} tags={this.state.tags}
onSubmit={this.handleTagsUpdated} onSubmit={this.handleTagsUpdated}
onCancel={this.handleHideTagManager} /> onCancel={this.handleHideTagManager} />
:
<div className="panel-form-column">
<div><label className="small">Tags (user discovery):</label></div>
<a href="#" className="flat-button" onClick={this.handleShowTagManager}>
<i className="material-icons">edit</i> Manage tags
</a>
<span>{tags}</span>
</div>
}
</div> </div>
); );
} }
@@ -1539,26 +1527,15 @@
var TagManager = React.createClass({ var TagManager = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
tags: this.wrapTags(this.props.tags), tags: this.props.tags,
tagInput: '', tagInput: '',
activated: this.props.activated,
noTagsMessage: 'Add some tags' noTagsMessage: 'Add some tags'
}; };
}, },
wrapTags: function(tags) { handleShowTagManager: function() {
var wrapped = []; this.setState({activated: true});
tags.map(function(tag) {
wrapped.push({user: tag});
});
return wrapped;
},
unwrapTags: function(tags) {
var unwrapped = [];
tags.map(function(tag) {
unwrapped.push(tag.user);
});
return unwrapped;
}, },
handleTagInput: function(text) { handleTagInput: function(text) {
@@ -1579,47 +1556,81 @@
tag = tag.trim(); tag = tag.trim();
if (tag.length > 0) { if (tag.length > 0) {
var tags = this.state.tags.slice(0); var tags = this.state.tags.slice(0);
tags.push({user: tag}); tags.push(tag);
this.setState({tags: tags, tagInput: ''}); this.setState({tags: tags, tagInput: ''});
} }
if (this.props.onTagsChanged) {
this.props.onTagsChanged(tags);
}
}, },
handleRemoveTag: function(tag, index) { handleRemoveTag: function(tag, index) {
var tags = this.state.tags.slice(0); var tags = this.state.tags.slice(0);
tags.splice(index, 1); tags.splice(index, 1);
this.setState({tags: tags}); this.setState({tags: tags});
if (this.props.onTagsChanged) {
this.props.onTagsChanged(tags);
}
}, },
handleSubmit: function() { handleSubmit: function() {
var tags = this.unwrapTags(this.state.tags); var tags = this.state.tags.slice(0);
var inp = this.state.tagInput.trim(); var inp = this.state.tagInput.trim();
if (inp.length > 0) { if (inp.length > 0) {
tags.push(inp); tags.push(inp);
} }
this.props.onSubmit(tags); this.props.onSubmit(tags);
this.setState({activated: false, tags: tags});
}, },
handleCancel: function() { handleCancel: function() {
this.setState({activated: false, tags: this.props.tags});
this.props.onCancel(); this.props.onCancel();
}, },
render: function() { render: function() {
var tags = [];
if (this.state.activated) {
this.state.tags.map(function(tag) {
tags.push({user: tag});
});
} else {
this.state.tags.map(function(tag) {
tags.push(<span className="badge" key={tags.length}>{tag}</span>);
});
if (tags.length == 0) {
tags = <i>No tags defined. Add some.</i>;
}
}
return ( return (
<div id="tag-manager"> <div className="panel-form-column">
<div className="panel-form-row"> <div className="panel-form-row">
<label className="small">Tags (user discovery)</label> <label className="small">{this.props.title}</label>
</div> </div>
{this.state.activated ?
<div>
<ChipInput <ChipInput
chips={this.state.tags} chips={tags}
avatarDisabled={true} avatarDisabled={true}
prompt={this.state.noTagsMessage} prompt={this.state.noTagsMessage}
onEnter={this.handleAddTag} onEnter={this.handleAddTag}
onChipRemoved={this.handleRemoveTag} onChipRemoved={this.handleRemoveTag}
filterFunc={this.handleTagInput} /> filterFunc={this.handleTagInput} />
{this.props.onSubmit || this.props.onCancel ?
<div id="tag-manager-buttons" className="panel-form-row"> <div id="tag-manager-buttons" className="panel-form-row">
<button className="blue" onClick={this.handleSubmit}>OK</button> <button className="blue" onClick={this.handleSubmit}>OK</button>
<button className="white" onClick={this.handleCancel}>Cancel</button> <button className="white" onClick={this.handleCancel}>Cancel</button>
</div> </div>
: null}
</div>
:
<div>
<a href="#" className="flat-button" onClick={this.handleShowTagManager}>
<i className="material-icons">edit</i> Manage tags
</a>
<span>{tags}</span>
</div>
}
</div> </div>
); );
} }
@@ -2020,12 +2031,14 @@
<div className="panel-form-row"> <div className="panel-form-row">
<label className="small">Group members</label> <label className="small">Group members</label>
</div> </div>
<div className="panel-form-row">
<ChipInput <ChipInput
chips={this.state.members} chips={this.state.members}
required={this.props.requiredMember} required={this.props.requiredMember}
prompt="add members" prompt="add members"
filterFunc={this.handleContactFilter} filterFunc={this.handleContactFilter}
onChipRemoved={this.handleMemberRemoved} /> onChipRemoved={this.handleMemberRemoved} />
</div>
<div className="panel-form-row"> <div className="panel-form-row">
<label className="small">All contacts</label> <label className="small">All contacts</label>
</div> </div>
@@ -2123,7 +2136,8 @@
return { return {
fn: '', // full/formatted name fn: '', // full/formatted name
private: '', private: '',
imageDataUrl: null imageDataUrl: null,
tags: []
}; };
}, },
handleFnChange: function(e) { handleFnChange: function(e) {
@@ -2135,6 +2149,9 @@
handleImageChanged: function(img) { handleImageChanged: function(img) {
this.setState({imageDataUrl: img}); this.setState({imageDataUrl: img});
}, },
handleTagsUpdated: function(tags) {
this.setState({tags: tags});
},
handleSubmit: function(e) { handleSubmit: function(e) {
e.preventDefault(); e.preventDefault();
if (this.state.fn && this.state.fn.trim()) { if (this.state.fn && this.state.fn.trim()) {
@@ -2148,7 +2165,7 @@
submitClasses += " disabled"; submitClasses += " disabled";
} }
return ( return (
<div className="panel-form"> <form className="panel-form" onSubmit={this.handleSubmit}>
<div className="panel-form-row"> <div className="panel-form-row">
<div className="panel-form-column"> <div className="panel-form-column">
<label className="small" htmlFor="new-topic-fn">Group name</label> <label className="small" htmlFor="new-topic-fn">Group name</label>
@@ -2163,10 +2180,14 @@
onError={this.props.onError} onError={this.props.onError}
onImageChanged={this.handleImageChanged} /> onImageChanged={this.handleImageChanged} />
</div> </div>
<TagManager
tags={this.state.tags}
activated={true}
title="Optional tags (search and discovery)" />
<div className="dialog-buttons"> <div className="dialog-buttons">
<button className={submitClasses} onClick={this.handleSubmit}>Create</button> <button className={submitClasses}>Create</button>
</div>
</div> </div>
</form>
); );
} }
}); });