dash_upload.js 1.6 KB

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