Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F8922306
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/cosealious-src/lib/ResourceDropdown.jsx b/cosealious-src/lib/ResourceDropdown.jsx
index 7f1cc9e2..f2bc5ec9 100644
--- a/cosealious-src/lib/ResourceDropdown.jsx
+++ b/cosealious-src/lib/ResourceDropdown.jsx
@@ -1,86 +1,78 @@
-import resourceTypeCollection from "./mixins/resourceTypeCollection";
import React from "react";
import merge from "merge";
+import Collection from "./collection";
const default_props = {
displayAttr: "id",
valueAttr: "id",
noValueOptionName: "--",
allowNoValue: false,
onValueChange: value => alert(value),
label: "Select resource:",
disabled: false,
value: "",
resources: [],
displayAttrIsSafe: false,
};
function getAttr(name, resource, props) {
const propname = name + "Attr";
if (typeof props[propname] === "string") {
return resource[props[propname]];
} else if (typeof props[propname] === "function") {
return props[propname](resource);
}
}
-function getOptionValue(resource, props) {
- return getAttr("value", resource, props);
-}
+function ResourceDropdownPure(config) {
+ const { collection, get_forced_query, query_store_class } = config;
-function getOptionName(resource, props) {
- return getAttr("display", resource, props);
-}
+ return Collection({ collection, get_forced_query, query_store_class }, _props => {
-function ResourceDropdownPure(props_arg) {
- const props = merge(true, default_props, props_arg);
+ const props = merge(true, default_props, _props, config);
- function handleChange(e) {
- props.onValueChange(e.target.value);
- }
+ const options = props.resources.map(resource => {
+ const value = getAttr("value", resource, props);
+ const name = getAttr("display", resource, props);
+ const key = resource.id;
+ if (props.displayAttrIsSafe) {
+ return (
+ <option
+ value={value}
+ key={key}
+ dangerouslySetInnerHTML={{ __html: name }}
+ />
+ );
+ } else {
+ return (
+ <option value={value} key={key}>
+ {name}
+ </option>
+ );
+ }
+ });
- const options = props.resources.map(resource => {
- const value = getOptionValue(resource, props);
- const name = getOptionName(resource, props);
- const key = resource.id;
- if (props.displayAttrIsSafe) {
- return (
- <option
- value={value}
- key={key}
- dangerouslySetInnerHTML={{ __html: name }}
- />
- );
- } else {
- return (
- <option value={value} key={key}>
- {name}
+ if (props.allowNoValue) {
+ options.unshift(
+ <option value="" key="empty">
+ {props.noValueOptionName}
</option>
);
}
- });
-
- if (props.allowNoValue) {
- options.unshift(
- <option value="" key="empty">
- {props.noValueOptionName}
- </option>
+ return (
+ <React.Fragment>
+ <label className={props.labelClassName}>{props.label}</label>
+ <select
+ onChange={e => props.onValueChange(e.target.value)}
+ value={props.value}
+ disabled={props.disabled}
+ className={props.className}
+ >
+ {options}
+ </select>
+ </React.Fragment>
);
- }
-
- return (
- <React.Fragment>
- <label className={props.labelClassName}>{props.label}</label>
- <select
- onChange={handleChange}
- value={props.value}
- disabled={props.disabled}
- className={props.className}
- >
- {options}
- </select>
- </React.Fragment>
- );
+ });
}
-module.exports = resourceTypeCollection(ResourceDropdownPure);
+module.exports = ResourceDropdownPure;
diff --git a/cosealious-src/lib/collection.jsx b/cosealious-src/lib/collection.jsx
index fdefb7a5..294ecc5f 100644
--- a/cosealious-src/lib/collection.jsx
+++ b/cosealious-src/lib/collection.jsx
@@ -1,80 +1,81 @@
const React = require("react");
const CachedHttp = require("./cached-http.js");
+const QueryStore = require("./query-stores/query-store");
const default_forced_query = props => ({
filter: {},
format: {},
sort: {},
});
function Collection(
- { collection, query_store, get_forced_query = default_forced_query },
+ { collection, query_store_class = QueryStore.Stateful, get_forced_query = default_forced_query },
component
) {
return class Component extends React.Component {
constructor() {
super();
- this.query_store = query_store;
+ this.query_store = new query_store_class();
this.state = {
loading: true,
resources: [],
response: { attachments: {}, items: [] },
};
}
componentDidMount() {
this.refreshComponent();
this.query_store.on("change", () => this.refreshComponent());
}
componentDidUpdate(prevProps, prevState) {
const serialized_last_filter = JSON.stringify(
get_forced_query(prevProps).filter
);
const serialized_current_filter = JSON.stringify(
get_forced_query(this.props).filter
);
if (serialized_last_filter !== serialized_current_filter) {
this.refreshComponent();
}
}
refreshComponent(options) {
const default_options = {
force: false,
show_loading: true,
};
const { force, show_loading } = {
...default_options,
...options,
};
if (force) CachedHttp.flush();
if (show_loading) this.setState({ loading: true });
CachedHttp.get(`/api/v1/collections/${collection}`, {
filter: {
- ...query_store.getQuery().filter,
+ ...this.query_store.getQuery().filter,
...get_forced_query(this.props).filter,
},
format: get_forced_query(this.props).format,
sort: {
- ...query_store.getQuery().sort,
+ ...this.query_store.getQuery().sort,
...get_forced_query(this.props).sort,
},
}).then(response => {
this.setState({
response,
resources: response.items,
loading: false,
});
});
}
render() {
return React.createElement(component, {
collection,
query_store: this.query_store,
...this.state,
metadata: this.props.metadata,
refresh: this.refreshComponent.bind(this),
});
}
};
}
module.exports = Collection;
diff --git a/cosealious-src/lib/mixins/resourceTypeCollection.jsx b/cosealious-src/lib/mixins/resourceTypeCollection.jsx
index 20e83217..5b55f9bc 100644
--- a/cosealious-src/lib/mixins/resourceTypeCollection.jsx
+++ b/cosealious-src/lib/mixins/resourceTypeCollection.jsx
@@ -1,111 +1,111 @@
import React from "react";
import rest from "qwest";
import merge from "merge";
import deep_equal from "deep-equal";
import clone from "clone";
import Promise from "bluebird";
const Loading = require("../loading");
const CachedHttp = require("../cached-http");
export default function resourceTypeCollection(ComponentClass) {
class resourceTypeWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
resources: [],
response: { attachments: {}, items: [] },
};
this.generateQuery = this.generateQuery.bind(this);
this.fetch = this.fetch.bind(this);
this.refresh = this.refresh.bind(this);
}
generateQuery(props) {
let query = {};
if (props.filter) {
query.filter = props.filter;
for (let i in query.filter) {
if (
query.filter[i] == "undefined" ||
query.filter[i] === null
) {
delete query.filter;
}
}
}
const to_add = ["sort", "format", "search", "pagination"];
to_add.forEach(function(key) {
if (props[key]) {
query[key] = props[key];
}
});
return query;
}
reloadNeeded(query) {
return !deep_equal(query, this.state.last_query);
}
fetch(query) {
this.setState({
loading: true,
});
return CachedHttp.get(this.props.url, query, {
cache: true,
}).then(response => {
this.setState({
loading: false,
resources: this.props.customSort(response.items),
response,
last_query: clone(query),
});
});
}
refresh(force) {
let query = this.generateQuery(this.props);
this.fetch(query);
}
componentDidMount() {
this.refresh();
}
componentWillReceiveProps(next_props) {
if (!this.props.will_not_update)
setTimeout(() => {
this.refresh();
}, 0);
}
delete(resource) {
rest.delete(
this.props.url + "/" + resource.id,
{},
{ cache: true }
).then(() => {
this.refresh(true);
});
}
render() {
if (this.state.loading) {
return React.createElement(this.props.loadingComponent);
}
const customProps = {
...this.state,
delete: this.delete,
};
const child_props = merge(true, this.props, customProps);
return React.createElement(ComponentClass, child_props);
}
}
resourceTypeWrapper.defaultProps = {
pagination: undefined, // could be: {page: 1, items: 12}
filter: {},
format: {},
search: "",
url: "/api/v1/collections/users",
loadingComponent: () => React.createElement(Loading),
customSort: list => list,
};
return resourceTypeWrapper;
-}
+}
\ No newline at end of file
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Sep 20, 14:28 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
949276
Default Alt Text
(8 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment