102 lines
2.2 KiB
Plaintext
Raw Normal View History

import RestModel from 'discourse/models/rest';
2015-06-30 10:20:22 -07:00
let Query;
Query = RestModel.extend({
dirty: false,
2015-06-30 15:12:12 -07:00
params: {},
results: null,
2015-06-30 15:12:12 -07:00
2015-06-30 19:51:38 -07:00
_init: function() {
this._super();
if (!this.get('options')) {
this.set('options', {defaults:{}});
}
this.set('dirty', false);
}.on('init'),
2015-06-30 15:12:12 -07:00
_initParams: function() {
this.resetParams();
}.on('init').observes('param_names'),
2015-06-30 10:20:22 -07:00
2015-06-30 19:51:38 -07:00
options: Em.computed.alias('qopts'),
2015-06-30 10:20:22 -07:00
markDirty: function() {
this.set('dirty', true);
2015-06-30 15:12:12 -07:00
}.observes('name', 'description', 'sql', 'options', 'options.defaults'),
2015-06-30 10:20:22 -07:00
markNotDirty() {
this.set('dirty', false);
},
2015-06-30 15:12:12 -07:00
resetParams() {
let newParams = {};
let defaults = this.get('options.defaults');
if (!defaults) {
defaults = {};
}
(this.get('param_names') || []).forEach(function(name) {
if (defaults[name]) {
newParams[name] = defaults[name];
} else {
newParams[name] = '';
}
});
this.set('params', newParams);
},
saveDefaults() {
const currentParams = this.get('params');
let defaults = {};
(this.get('param_names') || []).forEach(function(name) {
2015-06-30 19:51:38 -07:00
if (currentParams[name]) {
defaults[name] = currentParams[name];
} else {
delete defaults[name];
}
2015-06-30 15:12:12 -07:00
});
this.set('options.defaults', defaults);
},
2015-06-30 12:52:17 -07: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 10:20:22 -07:00
listName: function() {
2015-06-30 12:52:17 -07:00
let name = this.get('name');
2015-06-30 10:20:22 -07:00
if (this.get('dirty')) {
2015-06-30 12:52:17 -07:00
name += " (*)";
}
if (this.get('destroyed')) {
name += " (deleted)";
2015-06-30 10:20:22 -07:00
}
2015-06-30 12:52:17 -07:00
return name;
}.property('name', 'dirty', 'destroyed'),
2015-06-30 10:20:22 -07:00
createProperties() {
2015-06-30 12:52:17 -07:00
if (this.get('sql')) {
// Importing
return this.updateProperties();
}
return this.getProperties("name");
},
updateProperties() {
2015-06-30 12:52:17 -07:00
let props = this.getProperties(Query.updatePropertyNames);
if (this.get('destroyed')) {
props.id = this.get('id');
}
return props;
},
run() {
console.log("Called query#run");
}
});
2015-06-30 10:20:22 -07:00
Query.reopenClass({
2015-06-30 19:51:38 -07:00
updatePropertyNames: ["name", "description", "sql", "qopts"]
2015-06-30 10:20:22 -07:00
});
export default Query;