2016-10-30 21:12:01 -04:00
import { ajax } from 'discourse/lib/ajax';
2015-08-26 00:36:39 -04:00
import Badge from 'discourse/models/badge';
2015-06-30 18:12:12 -04:00
2015-07-09 15:02:47 -04:00
function randomIdShort() {
return 'xxxxxxxx'.replace(/[xy]/g, function() {
2015-09-14 18:34:57 -04:00
/*eslint-disable*/
2015-07-09 15:02:47 -04:00
return (Math.random() * 16 | 0).toString(16);
2015-09-14 18:34:57 -04:00
/*eslint-enable*/
2015-07-09 15:02:47 -04:00
});
}
2015-06-30 18:12:12 -04:00
2015-08-26 00:36:39 -04:00
function transformedRelTable(table, modelClass) {
2015-08-25 23:48:19 -04:00
const result = {};
table.forEach(function(item) {
2015-08-26 00:36:39 -04:00
if (modelClass) {
result[item.id] = modelClass.create(item);
} else {
result[item.id] = item;
}
2015-08-25 23:48:19 -04:00
});
return result;
}
2015-06-30 18:12:12 -04:00
const QueryResultComponent = Ember.Component.extend({
layoutName: 'explorer-query-result',
rows: Em.computed.alias('content.rows'),
columns: Em.computed.alias('content.columns'),
params: Em.computed.alias('content.params'),
explainText: Em.computed.alias('content.explain'),
hasExplain: Em.computed.notEmpty('content.explain'),
colCount: function() {
return this.get('content.columns').length;
}.property('content.columns.length'),
duration: function() {
return I18n.t('explorer.run_time', {value: I18n.toNumber(this.get('content.duration'), {precision: 1})});
}.property('content.duration'),
parameterAry: function() {
let arr = [];
const params = this.get('params');
for (var key in params) {
if (params.hasOwnProperty(key)) {
arr.push({key: key, value: params[key]});
}
}
return arr;
}.property('params.@each'),
2015-08-25 23:48:19 -04:00
columnDispNames: function() {
2015-07-01 00:21:14 -04:00
if (!this.get('columns')) {
return [];
2015-06-30 18:12:12 -04:00
}
2015-09-14 18:34:57 -04:00
return this.get('columns').map(function(colName) {
2015-08-25 23:48:19 -04:00
if (colName.endsWith("_id")) {
return colName.slice(0, -3);
2015-06-30 18:12:12 -04:00
}
2015-08-25 23:48:19 -04:00
const dIdx = colName.indexOf('$');
if (dIdx >= 0) {
return colName.substring(dIdx + 1);
}
return colName;
});
}.property('content', 'columns.@each'),
2015-06-30 18:12:12 -04:00
2015-09-14 19:00:39 -04:00
fallbackTemplate: function() {
return this.container.lookup('template:explorer/text.raw');
}.property(),
2015-08-25 23:48:19 -04:00
columnTemplates: function() {
const self = this;
if (!this.get('columns')) {
return [];
}
return this.get('columns').map(function(colName, idx) {
let viewName = "text";
if (self.get('content.colrender')[idx]) {
viewName = self.get('content.colrender')[idx];
}
return {name: viewName, template: self.container.lookup('template:explorer/' + viewName + '.raw')};
2015-06-30 18:12:12 -04:00
});
}.property('content', 'columns.@each'),
2015-08-25 23:48:19 -04:00
transformedUserTable: function() {
return transformedRelTable(this.get('content.relations.user'));
}.property('content.relations.user'),
2015-08-26 00:36:39 -04:00
transformedBadgeTable: function() {
return transformedRelTable(this.get('content.relations.badge'), Badge);
}.property('content.relations.badge'),
transformedPostTable: function() {
return transformedRelTable(this.get('content.relations.post'));
}.property('content.relations.post'),
transformedTopicTable: function() {
return transformedRelTable(this.get('content.relations.topic'));
}.property('content.relations.topic'),
2015-09-21 17:43:23 -04:00
transformedGroupTable: function() {
return transformedRelTable(this.get('site.groups'));
}.property('site.groups'),
2015-08-26 00:36:39 -04:00
lookupUser(id) {
2015-08-25 23:48:19 -04:00
return this.get('transformedUserTable')[id];
},
2015-08-26 00:36:39 -04:00
lookupBadge(id) {
return this.get('transformedBadgeTable')[id];
},
lookupPost(id) {
return this.get('transformedPostTable')[id];
},
lookupTopic(id) {
return this.get('transformedTopicTable')[id];
},
2015-09-21 17:43:23 -04:00
lookupGroup(id) {
return this.get('transformedGroupTable')[id];
},
2015-08-26 00:36:39 -04:00
lookupCategory(id) {
return this.site.get('categoriesById')[id];
},
2015-08-25 23:48:19 -04:00
2015-08-03 18:07:29 -04:00
downloadResult(format) {
// Create a frame to submit the form in (?)
// to avoid leaving an about:blank behind
let windowName = randomIdShort();
2015-08-03 18:19:31 -04:00
const newWindowContents = "<style>body{font-size:36px;display:flex;justify-content:center;align-items:center;}</style><body>Click anywhere to close this window once the download finishes.<script>window.onclick=function(){window.close()};</script>";
2015-08-03 18:07:29 -04:00
2015-09-14 18:34:57 -04:00
window.open('data:text/html;base64,' + btoa(newWindowContents), windowName);
2015-08-03 18:07:29 -04:00
let form = document.createElement("form");
form.setAttribute('id', 'query-download-result');
form.setAttribute('method', 'post');
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.' + format + '?download=1'));
form.setAttribute('target', windowName);
form.setAttribute('style', 'display:none;');
2015-09-14 18:34:57 -04:00
function addInput(name, value) {
2015-08-03 18:07:29 -04:00
let field;
field = document.createElement('input');
field.setAttribute('name', name);
field.setAttribute('value', value);
form.appendChild(field);
}
2015-07-09 15:02:47 -04:00
2015-09-14 18:34:57 -04:00
addInput('params', JSON.stringify(this.get('params')));
addInput('explain', this.get('hasExplain'));
addInput('limit', '1000000');
2015-07-09 15:02:47 -04:00
2016-10-30 21:12:01 -04:00
ajax('/session/csrf.json').then(function(csrf) {
2015-09-14 18:34:57 -04:00
addInput('authenticity_token', csrf.csrf);
2015-07-09 15:02:47 -04:00
2015-08-03 18:07:29 -04:00
document.body.appendChild(form);
form.submit();
Em.run.next('afterRender', function() {
document.body.removeChild(form);
})
});
},
actions: {
downloadResultJson() {
this.downloadResult('json');
},
downloadResultCsv() {
this.downloadResult('csv');
2015-07-09 15:02:47 -04:00
}
},
2015-06-30 18:12:12 -04:00
parent: function() { return this; }.property()
});
export default QueryResultComponent;