inspectCtrl.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('InspectCtrl', function($scope) {
  9. var model = $scope.inspector;
  10. function getParametersFromQueryString(queryString) {
  11. var result = [];
  12. var parameters = queryString.split("&");
  13. for (var i = 0; i < parameters.length; i++) {
  14. var keyValue = parameters[i].split("=");
  15. if (keyValue[1].length > 0) {
  16. result.push({ key: keyValue[0], value: window.unescape(keyValue[1]) });
  17. }
  18. }
  19. return result;
  20. }
  21. $scope.init = function () {
  22. $scope.editor = { index: 0 };
  23. if (!model.error) {
  24. return;
  25. }
  26. if (_.isString(model.error.data)) {
  27. $scope.response = model.error.data;
  28. }
  29. if (model.error.config && model.error.config.params) {
  30. $scope.request_parameters = _.map(model.error.config.params, function(value, key) {
  31. return { key: key, value: value};
  32. });
  33. }
  34. if (model.error.stack) {
  35. $scope.editor.index = 2;
  36. $scope.stack_trace = model.error.stack;
  37. $scope.message = model.error.message;
  38. }
  39. else if (model.error.config && model.error.config.data) {
  40. $scope.editor.index = 1;
  41. $scope.request_parameters = getParametersFromQueryString(model.error.config.data);
  42. if (model.error.data.indexOf('DOCTYPE') !== -1) {
  43. $scope.response_html = model.error.data;
  44. }
  45. }
  46. };
  47. });
  48. angular
  49. .module('grafana.directives')
  50. .directive('iframeContent', function($parse) {
  51. return {
  52. restrict: 'A',
  53. link: function($scope, elem, attrs) {
  54. var getter = $parse(attrs.iframeContent), value = getter($scope);
  55. $scope.$on("$destroy",function() {
  56. elem.remove();
  57. });
  58. var iframe = document.createElement('iframe');
  59. iframe.width = '100%';
  60. iframe.height = '400px';
  61. iframe.style.border = 'none';
  62. iframe.src = 'about:blank';
  63. elem.append(iframe);
  64. iframe.contentWindow.document.open('text/html', 'replace');
  65. iframe.contentWindow.document.write(value);
  66. iframe.contentWindow.document.close();
  67. }
  68. };
  69. });
  70. });