dashUpload.js 1.6 KB

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