79 lines
1.8 KiB
JavaScript
Raw Normal View History

import discourseComputed, {
2020-09-04 13:23:11 +02:00
observes,
2021-01-27 10:38:56 +01:00
on,
} from "discourse-common/utils/decorators";
2020-07-06 15:25:19 -04:00
import getURL from "discourse-common/lib/get-url";
2018-10-10 17:26:23 +05:30
import RestModel from "discourse/models/rest";
import { reads } from "@ember/object/computed";
2015-07-02 09:19:30 -07:00
const Query = RestModel.extend({
2015-06-30 15:12:12 -07:00
params: {},
results: null,
hasParams: reads("param_info.length"),
2015-06-30 19:51:38 -07:00
@on("init")
@observes("param_info")
_initParams() {
2015-06-30 15:12:12 -07:00
this.resetParams();
},
2015-06-30 19:51:38 -07:00
2015-06-30 15:12:12 -07:00
resetParams() {
2015-07-02 09:19:30 -07:00
const newParams = {};
const oldParams = this.params;
const paramInfo = this.param_info || [];
2020-09-04 13:23:11 +02:00
paramInfo.forEach((pinfo) => {
2015-07-14 16:01:38 -07:00
const name = pinfo.identifier;
if (oldParams[pinfo.identifier]) {
2015-07-02 09:19:30 -07:00
newParams[name] = oldParams[name];
2018-10-10 17:26:23 +05:30
} else if (pinfo["default"] !== null) {
newParams[name] = pinfo["default"];
} else if (pinfo["type"] === "boolean") {
newParams[name] = "false";
} else if (pinfo["type"] === "user_id") {
newParams[name] = null;
} else if (pinfo["type"] === "user_list") {
newParams[name] = null;
2015-06-30 15:12:12 -07:00
} else {
2018-10-10 17:26:23 +05:30
newParams[name] = "";
2015-06-30 15:12:12 -07:00
}
});
2018-10-10 17:26:23 +05:30
this.set("params", newParams);
2015-06-30 15:12:12 -07:00
},
@discourseComputed("id")
downloadUrl(id) {
2015-06-30 12:52:17 -07:00
// TODO - can we change this to use the store/adapter?
2020-07-06 15:25:19 -04:00
return getURL(`/admin/plugins/explorer/queries/${id}.json?export=1`);
},
2015-06-30 12:52:17 -07:00
createProperties() {
if (this.sql) {
2015-06-30 12:52:17 -07:00
// Importing
return this.updateProperties();
}
return this.getProperties("name");
},
updateProperties() {
const props = this.getProperties(Query.updatePropertyNames);
if (this.destroyed) {
props.id = this.id;
2015-06-30 12:52:17 -07:00
}
return props;
2020-09-04 13:23:11 +02:00
},
});
2015-06-30 10:20:22 -07:00
Query.reopenClass({
2019-06-14 13:27:41 -04:00
updatePropertyNames: [
"name",
"description",
"sql",
"user_id",
2019-06-14 13:27:41 -04:00
"created_at",
"group_ids",
2020-09-04 13:23:11 +02:00
"last_run_at",
],
2015-06-30 10:20:22 -07:00
});
export default Query;