Github user garrensmith commented on a diff in the pull request:
https://github.com/apache/couchdb-fauxton/pull/741#discussion_r69896784
--- Diff: app/addons/documents/doc-editor/components.react.jsx ---
@@ -218,44 +218,137 @@ var AttachmentsPanelButton = React.createClass({
};
},
+ getInitialState: function () {
+ return {
+ filter: '',
+ maxHeight: 'none'
+ };
+ },
+
+ componentDidMount: function () {
+ $(window).on('resize.attachment-filter', this.updateMaxHeight);
+ Stores.attachmentFilterStore.on('change', this.onChange);
+ },
+
+ componentWillUnmount: function () {
+ $(window).off('resize.attachment-filter', this.updateMaxHeight);
+ Stores.attachmentFilterStore.off('change', this.onChange);
+ },
+
+ onChange: function () {
+ this.setState({
+ maxHeight: this.state.maxHeight,
+ filter: Stores.attachmentFilterStore.filter()
+ });
+ },
+
+ getMaxHeight: function () {
+ var $btn = $(this._button);
+ return $(document).height() - $btn.offset().top - $btn.outerHeight();
+ },
+
+ updateMaxHeight: function (button) {
+ if (!this._button && button) {
+ this._button = button;
+ }
+
+ this.setState({
+ maxHeight: this.getMaxHeight(),
+ filter: this.state.filter
+ });
+ },
+
getAttachmentList: function () {
var db = this.props.doc.database.get('id');
var doc = this.props.doc.get('_id');
+ var attachments = this.props.doc.get('_attachments');
- return _.map(this.props.doc.get('_attachments'), function (item,
filename) {
+ function create(item, filename) {
var url = FauxtonAPI.urls('document', 'attachment', db, doc,
app.utils.safeURLName(filename));
return (
- <li key={filename}>
- <a href={url} target="_blank" data-bypass="true">
<strong>{filename}</strong>
- <span className="attachment-delimiter">-</span>
- <span>{item.content_type},
{Helpers.formatSize(item.length)}</span>
- </a>
- </li>
+ <li key={filename} className="attachment-row">
+ <a href={url} target="_blank" data-bypass="true">
<strong>{filename}</strong>
+ <span className="attachment-delimiter">-</span>
+ <span>{item.content_type},
{Helpers.formatSize(item.length)}</span>
+ </a>
+ </li>
);
- });
+ }
+
+ function filter(predicate) {
+ return _.reduce(attachments, function(list, item, filename) {
+ if (predicate(filename)) {
+ list.push(create(item, filename));
+ }
+
+ return list;
+ }, []);
+ }
+
+ if (this.state.filter === '') {
+ return _.map(attachments, create);
+ } else {
+ var strong = new RegExp('^' + this.state.filter);
+ var weak = new RegExp(this.state.filter);
+ return filter(f => strong.test(f)).concat(filter(f =>
!strong.test(f) && weak.test(f)));
+ }
},
render: function () {
if (this.props.isLoading || !this.props.doc.get('_attachments')) {
return false;
}
+ var ulStyle = {
+ maxHeight: this.state ? this.state.maxHeight : "none"
+ };
+
return (
- <div className="panel-section view-attachments-section btn-group">
+ <div className="panel-section view-attachments-section btn-group"
ref={this.updateMaxHeight}>
<button className="panel-button dropdown-toggle btn"
data-bypass="true" data-toggle="dropdown" title="View Attachments"
id="view-attachments-menu">
<i className="icon icon-paper-clip"></i>
<span className="button-text">View Attachments</span>
<span className="caret"></span>
</button>
- <ul className="dropdown-menu" role="menu"
aria-labelledby="view-attachments-menu">
- {this.getAttachmentList()}
- </ul>
+ <ul style={ulStyle} className="dropdown-menu" role="menu"
aria-labelledby="view-attachments-menu">
+ <li><AttachmentsPanelFilter /></li>
+ {this.getAttachmentList()}
+ </ul>
</div>
);
}
});
+var AttachmentsPanelFilter = React.createClass({
+ propTypes: {
+ filter: React.PropTypes.string
+ },
+
+ getDefaultProps: function () {
+ return {
+ filter: this._filter
+ };
+ },
+
+ onChange: function (event) {
+ Stores.attachmentFilterStore.updateFilter(event.target.value);
--- End diff --
With our react components we are using the [flux
pattern](https://facebook.github.io/flux/) so a store must never have any
values passed to it from any components. Rather in a component you would call
an action e.g `updateFilter` and then that would dispatch an event with the
details. The store would then react to that event.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---