discourse-data-explorer/assets/javascripts/discourse/models/query.js.es6

86 lines
2.0 KiB
Plaintext
Raw Normal View History

import RestModel from 'discourse/models/rest';
2015-07-02 12:19:30 -04:00
const Query = RestModel.extend({
2015-06-30 13:20:22 -04:00
dirty: false,
2015-06-30 18:12:12 -04:00
params: {},
results: null,
2015-06-30 18:12:12 -04:00
2015-06-30 22:51:38 -04:00
_init: function() {
this._super();
this.set('dirty', false);
}.on('init'),
2015-06-30 18:12:12 -04:00
_initParams: function() {
this.resetParams();
2015-07-14 19:01:38 -04:00
}.on('init').observes('param_info'),
2015-06-30 22:51:38 -04:00
2015-06-30 13:20:22 -04:00
markDirty: function() {
this.set('dirty', true);
2015-07-14 19:01:38 -04:00
}.observes('name', 'description', 'sql'),
2015-06-30 13:20:22 -04:00
markNotDirty() {
this.set('dirty', false);
},
2015-07-14 19:01:38 -04:00
hasParams: function() {
return this.get('param_info.length') > 0;
}.property('param_info'),
2015-06-30 18:12:12 -04:00
resetParams() {
2015-07-02 12:19:30 -04:00
const newParams = {};
const oldParams = this.get('params');
2015-07-14 19:01:38 -04:00
const paramInfo = this.get('param_info') || [];
paramInfo.forEach(function(pinfo) {
const name = pinfo.identifier;
if (oldParams[pinfo.identifier]) {
2015-07-02 12:19:30 -04:00
newParams[name] = oldParams[name];
2015-07-14 19:01:38 -04:00
} else if (pinfo['default'] !== null) {
newParams[name] = pinfo['default'];
2015-07-15 14:00:31 -04:00
} else if (pinfo['type'] === 'boolean') {
newParams[name] = 'false';
2015-06-30 18:12:12 -04:00
} else {
newParams[name] = '';
}
});
this.set('params', newParams);
},
2015-06-30 15:52:17 -04:00
downloadUrl: function() {
// TODO - can we change this to use the store/adapter?
return Discourse.getURL("/admin/plugins/explorer/queries/" + this.get('id') + ".json?export=1");
}.property('id'),
2015-06-30 13:20:22 -04:00
listName: function() {
2015-06-30 15:52:17 -04:00
let name = this.get('name');
2015-06-30 13:20:22 -04:00
if (this.get('dirty')) {
2015-06-30 15:52:17 -04:00
name += " (*)";
}
if (this.get('destroyed')) {
name += " (deleted)";
2015-06-30 13:20:22 -04:00
}
2015-06-30 15:52:17 -04:00
return name;
}.property('name', 'dirty', 'destroyed'),
2015-06-30 13:20:22 -04:00
createProperties() {
2015-06-30 15:52:17 -04:00
if (this.get('sql')) {
// Importing
return this.updateProperties();
}
return this.getProperties("name");
},
updateProperties() {
2015-06-30 15:52:17 -04:00
let props = this.getProperties(Query.updatePropertyNames);
if (this.get('destroyed')) {
props.id = this.get('id');
}
return props;
}
});
2015-06-30 13:20:22 -04:00
Query.reopenClass({
2015-07-14 19:01:38 -04:00
updatePropertyNames: ["name", "description", "sql"]
2015-06-30 13:20:22 -04:00
});
export default Query;