2017-05-19 16:00:01 +02:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
|
import {coreModule, JsonExplorer} from 'app/core/core';
|
|
|
|
|
|
|
|
|
|
const template = `
|
2017-05-19 17:35:36 +02:00
|
|
|
<collapse-box title="Query Troubleshooter" is-open="ctrl.isOpen" state-changed="ctrl.stateChanged()"
|
|
|
|
|
ng-class="{'collapse-box--error': ctrl.hasError}">
|
2017-05-19 16:00:01 +02:00
|
|
|
<collapse-box-actions>
|
|
|
|
|
<a class="pointer"><i class="fa fa-clipboard"></i> Copy to clipboard</a>
|
|
|
|
|
</collapse-box-actions>
|
|
|
|
|
<collapse-box-body>
|
|
|
|
|
<div class="query-troubleshooter-json"></div>
|
|
|
|
|
</collapse-box-body>
|
|
|
|
|
</collapse-box>
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export class QueryTroubleshooterCtrl {
|
2017-05-19 17:35:36 +02:00
|
|
|
isOpen: any;
|
2017-05-19 16:00:01 +02:00
|
|
|
showResponse: boolean;
|
|
|
|
|
panelCtrl: any;
|
|
|
|
|
renderJsonExplorer: (data) => void;
|
2017-05-19 17:35:36 +02:00
|
|
|
onRequestErrorEventListener: any;
|
|
|
|
|
onRequestResponseEventListener: any;
|
|
|
|
|
hasError: boolean;
|
2017-05-19 16:00:01 +02:00
|
|
|
|
|
|
|
|
/** @ngInject **/
|
|
|
|
|
constructor($scope, private $timeout) {
|
2017-05-19 17:35:36 +02:00
|
|
|
this.onRequestErrorEventListener = this.onRequestError.bind(this);
|
|
|
|
|
this.onRequestResponseEventListener = this.onRequestResponse.bind(this);
|
2017-05-19 16:00:01 +02:00
|
|
|
|
2017-05-19 17:35:36 +02:00
|
|
|
appEvents.on('ds-request-error', this.onRequestErrorEventListener);
|
|
|
|
|
$scope.$on('$destroy', this.removeEventsListeners.bind(this));
|
2017-05-19 16:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-19 17:35:36 +02:00
|
|
|
removeEventsListeners() {
|
|
|
|
|
appEvents.off('ds-request-response', this.onRequestResponseEventListener);
|
|
|
|
|
appEvents.off('ds-request-error', this.onRequestErrorEventListener);
|
2017-05-19 16:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onRequestError(err) {
|
2017-05-19 17:35:36 +02:00
|
|
|
this.isOpen = true;
|
|
|
|
|
this.hasError = true;
|
|
|
|
|
this.onRequestResponse(err);
|
2017-05-19 16:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-19 17:35:36 +02:00
|
|
|
stateChanged() {
|
|
|
|
|
console.log(this.isOpen);
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
|
appEvents.on('ds-request-response', this.onRequestResponseEventListener);
|
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
|
} else {
|
|
|
|
|
this.hasError = false;
|
2017-05-19 16:00:01 +02:00
|
|
|
}
|
2017-05-19 17:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onRequestResponse(data) {
|
|
|
|
|
data = _.cloneDeep(data);
|
2017-05-19 16:00:01 +02:00
|
|
|
|
|
|
|
|
if (data.headers) {
|
|
|
|
|
delete data.headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.config) {
|
|
|
|
|
data.request = data.config;
|
|
|
|
|
delete data.config;
|
|
|
|
|
delete data.request.transformRequest;
|
|
|
|
|
delete data.request.transformResponse;
|
|
|
|
|
delete data.request.paramSerializer;
|
|
|
|
|
delete data.request.jsonpCallbackParam;
|
|
|
|
|
delete data.request.headers;
|
|
|
|
|
delete data.request.requestId;
|
|
|
|
|
delete data.request.inspect;
|
|
|
|
|
delete data.request.retry;
|
|
|
|
|
delete data.request.timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.data) {
|
|
|
|
|
data.response = data.data;
|
|
|
|
|
|
|
|
|
|
delete data.data;
|
|
|
|
|
delete data.status;
|
|
|
|
|
delete data.statusText;
|
|
|
|
|
delete data.$$config;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 17:35:36 +02:00
|
|
|
this.$timeout(_.partial(this.renderJsonExplorer, data));
|
2017-05-19 16:00:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function queryTroubleshooter() {
|
|
|
|
|
return {
|
|
|
|
|
restrict: 'E',
|
|
|
|
|
template: template,
|
|
|
|
|
controller: QueryTroubleshooterCtrl,
|
|
|
|
|
bindToController: true,
|
|
|
|
|
controllerAs: 'ctrl',
|
|
|
|
|
scope: {
|
|
|
|
|
panelCtrl: "="
|
|
|
|
|
},
|
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
|
|
|
|
|
|
ctrl.renderJsonExplorer = function(data) {
|
|
|
|
|
var jsonElem = elem.find('.query-troubleshooter-json');
|
|
|
|
|
|
2017-05-19 17:35:36 +02:00
|
|
|
const formatter = new JsonExplorer(data, 3, {
|
2017-05-19 16:00:01 +02:00
|
|
|
theme: 'dark',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const html = formatter.render(true);
|
|
|
|
|
jsonElem.html(html);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coreModule.directive('queryTroubleshooter', queryTroubleshooter);
|