upload.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ///<reference path="../../headers/common.d.ts" />
  2. import kbn from 'app/core/utils/kbn';
  3. import coreModule from 'app/core/core_module';
  4. var wnd: any = window;
  5. class DashboardImporter {
  6. prepareForImport(dash) {
  7. dash.id = null;
  8. return Promise.resolve(dash);
  9. }
  10. }
  11. /** @ngInject */
  12. function uploadDashboardDirective(timer, alertSrv, $location) {
  13. return {
  14. restrict: 'A',
  15. link: function(scope) {
  16. function file_selected(evt) {
  17. var files = evt.target.files; // FileList object
  18. var readerOnload = function() {
  19. return function(e) {
  20. var dash;
  21. try {
  22. dash = JSON.parse(e.target.result);
  23. } catch (err) {
  24. console.log(err);
  25. scope.appEvent('alert-error', ['Import failed', 'JSON -> JS Serialization failed: ' + err.message]);
  26. return;
  27. }
  28. var importer = new DashboardImporter();
  29. importer.prepareForImport(dash).then(modified => {
  30. wnd.grafanaImportDashboard = modified;
  31. var title = kbn.slugifyForUrl(dash.title);
  32. scope.$apply(function() {
  33. $location.path('/dashboard-import/' + title);
  34. });
  35. });
  36. };
  37. };
  38. for (var i = 0, f; f = files[i]; i++) {
  39. var reader = new FileReader();
  40. reader.onload = readerOnload();
  41. reader.readAsText(f);
  42. }
  43. }
  44. // Check for the various File API support.
  45. if (wnd.File && wnd.FileReader && wnd.FileList && wnd.Blob) {
  46. // Something
  47. document.getElementById('dashupload').addEventListener('change', file_selected, false);
  48. } else {
  49. alertSrv.set('Oops','Sorry, the HTML5 File APIs are not fully supported in this browser.','error');
  50. }
  51. }
  52. };
  53. }
  54. coreModule.directive('dashUpload', uploadDashboardDirective);