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

103 lines
2.4 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();
if (!this.get('options')) {
this.set('options', {defaults:{}});
}
this.set('dirty', false);
}.on('init'),
2015-06-30 18:12:12 -04:00
_initParams: function() {
this.resetParams();
}.on('init').observes('param_names'),
2015-06-30 13:20:22 -04:00
2015-07-02 12:19:30 -04:00
// the server uses 'qopts' and the client uses 'options' due to ActiveRecord
// freaking out if a serialized value is named 'options'
2015-06-30 22:51:38 -04:00
options: Em.computed.alias('qopts'),
2015-06-30 13:20:22 -04:00
markDirty: function() {
this.set('dirty', true);
2015-06-30 18:12:12 -04:00
}.observes('name', 'description', 'sql', 'options', 'options.defaults'),
2015-06-30 13:20:22 -04:00
markNotDirty() {
this.set('dirty', false);
},
2015-06-30 18:12:12 -04:00
resetParams() {
2015-07-02 12:19:30 -04:00
const newParams = {};
const oldParams = this.get('params');
const defaults = this.get('options.defaults') || {};
2015-06-30 18:12:12 -04:00
(this.get('param_names') || []).forEach(function(name) {
if (defaults[name]) {
newParams[name] = defaults[name];
2015-07-02 12:19:30 -04:00
} else if (oldParams[name]) {
newParams[name] = oldParams[name];
2015-06-30 18:12:12 -04:00
} 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 22:51:38 -04:00
if (currentParams[name]) {
defaults[name] = currentParams[name];
} else {
delete defaults[name];
}
2015-06-30 18:12:12 -04:00
});
this.set('options.defaults', defaults);
},
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;
},
run() {
console.log("Called query#run");
}
});
2015-06-30 13:20:22 -04:00
Query.reopenClass({
2015-06-30 22:51:38 -04:00
updatePropertyNames: ["name", "description", "sql", "qopts"]
2015-06-30 13:20:22 -04:00
});
export default Query;