2016-10-31 09:12:01 +08:00
import { ajax } from 'discourse/lib/ajax' ;
2015-08-25 21:36:39 -07:00
import Badge from 'discourse/models/badge' ;
2016-11-09 11:58:18 -05:00
import { getOwner } from 'discourse-common/lib/get-owner' ;
2015-06-30 15:12:12 -07:00
2015-07-09 12:02:47 -07:00
function randomIdShort () {
return 'xxxxxxxx' . replace ( /[xy]/g , function () {
2015-09-14 15:34:57 -07:00
/*eslint-disable*/
2015-07-09 12:02:47 -07:00
return ( Math . random () * 16 | 0 ). toString ( 16 );
2015-09-14 15:34:57 -07:00
/*eslint-enable*/
2015-07-09 12:02:47 -07:00
});
}
2015-06-30 15:12:12 -07:00
2015-08-25 21:36:39 -07:00
function transformedRelTable ( table , modelClass ) {
2015-08-25 20:48:19 -07:00
const result = {};
table . forEach ( function ( item ) {
2015-08-25 21:36:39 -07:00
if ( modelClass ) {
result [ item . id ] = modelClass . create ( item );
} else {
result [ item . id ] = item ;
}
2015-08-25 20:48:19 -07:00
});
return result ;
}
2015-06-30 15:12:12 -07: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 20:48:19 -07:00
columnDispNames : function () {
2015-06-30 21:21:14 -07:00
if ( ! this . get ( 'columns' )) {
return [];
2015-06-30 15:12:12 -07:00
}
2015-09-14 15:34:57 -07:00
return this . get ( 'columns' ). map ( function ( colName ) {
2015-08-25 20:48:19 -07:00
if ( colName . endsWith ( "_id" )) {
return colName . slice ( 0 , - 3 );
2015-06-30 15:12:12 -07:00
}
2015-08-25 20:48:19 -07:00
const dIdx = colName . indexOf ( '$' );
if ( dIdx >= 0 ) {
return colName . substring ( dIdx + 1 );
}
return colName ;
2015-06-30 15:12:12 -07:00
});
}. property ( 'content' , 'columns.@each' ),
2015-09-14 16:00:39 -07:00
fallbackTemplate : function () {
2016-11-09 11:58:18 -05:00
return getOwner ( this ). lookup ( 'template:explorer/text.raw' );
2015-09-14 16:00:39 -07:00
}. property (),
2015-08-25 20:48:19 -07: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 ];
}
2016-12-27 11:47:50 -05:00
// After `findRawTemplates` is in stable this should be updated to use that
let template = getOwner ( self ). lookup ( 'template:explorer/' + viewName + '.raw' );
if ( ! template ) {
template = Discourse . RAW_TEMPLATES [ `javascripts/explorer/ ${ viewName } ` ];
}
2016-11-09 11:58:18 -05:00
return { name : viewName , template };
2015-08-25 20:48:19 -07:00
});
}. property ( 'content' , 'columns.@each' ),
transformedUserTable : function () {
return transformedRelTable ( this . get ( 'content.relations.user' ));
}. property ( 'content.relations.user' ),
2015-08-25 21:36:39 -07: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-08-25 20:48:19 -07:00
2015-09-21 14:43:23 -07:00
transformedGroupTable : function () {
return transformedRelTable ( this . get ( 'site.groups' ));
}. property ( 'site.groups' ),
2015-08-25 21:36:39 -07:00
lookupUser ( id ) {
2015-08-25 20:48:19 -07:00
return this . get ( 'transformedUserTable' )[ id ];
},
2015-08-25 21:36:39 -07: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 14:43:23 -07:00
lookupGroup ( id ) {
return this . get ( 'transformedGroupTable' )[ id ];
},
2015-08-25 21:36:39 -07:00
lookupCategory ( id ) {
return this . site . get ( 'categoriesById' )[ id ];
},
2015-08-25 20:48:19 -07:00
2015-08-03 15:07:29 -07:00
downloadResult ( format ) {
// Create a frame to submit the form in (?)
// to avoid leaving an about:blank behind
let windowName = randomIdShort ();
2015-08-03 15:19:31 -07: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 15:07:29 -07:00
2015-09-14 15:34:57 -07:00
window . open ( 'data:text/html;base64,' + btoa ( newWindowContents ), windowName );
2015-08-03 15:07:29 -07: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 15:34:57 -07:00
function addInput ( name , value ) {
2015-08-03 15:07:29 -07:00
let field ;
field = document . createElement ( 'input' );
field . setAttribute ( 'name' , name );
field . setAttribute ( 'value' , value );
form . appendChild ( field );
}
2015-09-14 15:34:57 -07:00
addInput ( 'params' , JSON . stringify ( this . get ( 'params' )));
addInput ( 'explain' , this . get ( 'hasExplain' ));
addInput ( 'limit' , '1000000' );
2015-08-03 15:07:29 -07:00
2016-10-31 09:12:01 +08:00
ajax ( '/session/csrf.json' ). then ( function ( csrf ) {
2015-09-14 15:34:57 -07:00
addInput ( 'authenticity_token' , csrf . csrf );
2015-08-03 15:07:29 -07:00
document . body . appendChild ( form );
form . submit ();
Em . run . next ( 'afterRender' , function () {
document . body . removeChild ( form );
})
});
},
2015-07-09 12:02:47 -07:00
actions : {
2015-08-03 15:07:29 -07:00
downloadResultJson () {
this . downloadResult ( 'json' );
},
downloadResultCsv () {
this . downloadResult ( 'csv' );
2015-07-09 12:02:47 -07:00
}
2016-11-09 12:49:25 -05:00
}
2015-06-30 15:12:12 -07:00
});
export default QueryResultComponent ;