discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6

153 lines
4.1 KiB
Plaintext
Raw Normal View History

import showModal from 'discourse/lib/show-modal';
import Query from 'discourse/plugins/discourse-data-explorer/discourse/models/query';
2015-06-30 15:52:17 -04:00
import { popupAjaxError } from 'discourse/lib/ajax-error';
2015-06-29 15:10:24 -04:00
export default Ember.ArrayController.extend({
queryParams: { selectedQueryId: "id" },
2015-06-30 13:20:22 -04:00
selectedQueryId: null,
2015-06-30 13:37:48 -04:00
results: null,
2015-06-30 18:12:12 -04:00
showResults: false,
2015-06-30 12:07:33 -04:00
loading: false,
explain: true,
2015-06-30 13:20:22 -04:00
saveDisabled: Ember.computed.not('selectedItem.dirty'),
runDisabled: Ember.computed.alias('selectedItem.dirty'),
selectedItem: function() {
const _id = this.get('selectedQueryId');
const id = parseInt(_id);
return this.get('content').find(function(q) {
return q.get('id') === id;
});
}.property('selectedQueryId'),
2015-06-30 18:12:12 -04:00
clearResults: function() {
this.set('showResults', false);
this.set('results', null);
}.observes('selectedQueryId'),
2015-06-30 15:52:17 -04:00
addCreatedRecord(record) {
this.pushObject(record);
this.set('selectedQueryId', Ember.get(record, 'id'));
this.get('selectedItem').set('dirty', false);
2015-06-30 18:12:12 -04:00
this.set('showResults', false);
2015-06-30 15:52:17 -04:00
this.set('results', null);
},
2015-06-30 13:20:22 -04:00
actions: {
dummy() {},
importQuery() {
showModal('import-query');
2015-06-30 13:37:48 -04:00
this.set('showCreate', false);
},
showCreate() {
this.set('showCreate', true);
},
2015-06-30 12:07:33 -04:00
editName() {
2015-06-30 13:20:22 -04:00
this.set('editName', true);
2015-06-30 12:07:33 -04:00
},
2015-06-30 15:52:17 -04:00
download() {
window.open(this.get('selectedItem.downloadUrl'), "_blank");
},
2015-06-30 18:12:12 -04:00
resetParams() {
this.get('selectedItem').resetParams();
},
saveDefaults() {
this.get('selectedItem').saveDefaults();
},
2015-06-30 15:52:17 -04:00
create() {
const self = this;
this.set('loading', true);
this.set('showCreate', false);
var newQuery = this.store.createRecord('query', {name: this.get('newQueryName')});
newQuery.save().then(function(result) {
self.addCreatedRecord(result.target);
}).catch(popupAjaxError).finally(function() {
self.set('loading', false);
});
},
2015-06-30 12:07:33 -04:00
save() {
const self = this;
this.set('loading', true);
2015-06-30 13:20:22 -04:00
this.get('selectedItem').save().then(function() {
const query = self.get('selectedItem');
query.markNotDirty();
self.set('editName', false);
2015-06-30 15:52:17 -04:00
}).catch(popupAjaxError).finally(function() {
2015-06-30 12:07:33 -04:00
self.set('loading', false);
});
},
discard() {
const self = this;
this.set('loading', true);
2015-06-30 13:20:22 -04:00
this.store.find('query', this.get('selectedItem.id')).then(function(result) {
const query = self.get('selectedItem');
query.setProperties(result.getProperties(Query.updatePropertyNames));
query.markNotDirty();
self.set('editName', false);
2015-06-30 13:37:48 -04:00
self.set('results', null);
2015-06-30 15:52:17 -04:00
}).catch(popupAjaxError).finally(function() {
self.set('loading', false);
});
},
destroy() {
const self = this;
const query = this.get('selectedItem');
this.set('loading', true);
this.store.destroyRecord('query', query).then(function() {
query.set('destroyed', true);
}).catch(popupAjaxError).finally(function() {
self.set('loading', false);
});
},
recover() {
const self = this;
const query = this.get('selectedItem');
this.set('loading', true);
query.save().then(function() {
query.set('destroyed', false);
}).catch(popupAjaxError).finally(function() {
2015-06-30 12:07:33 -04:00
self.set('loading', false);
});
},
run() {
2015-06-30 18:12:12 -04:00
const self = this;
2015-06-30 13:20:22 -04:00
if (this.get('selectedItem.dirty')) {
2015-06-30 12:07:33 -04:00
return;
}
2015-06-30 18:12:12 -04:00
this.set('loading', true);
2015-06-30 13:20:22 -04:00
Discourse.ajax("/admin/plugins/explorer/queries/" + this.get('selectedItem.id') + "/run", {
type: "POST",
data: {
2015-06-30 18:12:12 -04:00
params: JSON.stringify(this.get('selectedItem.params')),
explain: this.get('explain')
}
}).then(function(result) {
2015-06-30 18:12:12 -04:00
if (!result.success) {
return popupAjaxError(result);
}
console.log(result);
2015-06-30 18:12:12 -04:00
self.set('showResults', true);
self.set('results', result);
2015-06-30 15:52:17 -04:00
}).catch(popupAjaxError).finally(function() {
self.set('loading', false);
});
}
}
});