upload.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 template = `
  5. <input type="file" id="dashupload" name="dashupload" class="hide"/>
  6. <label class="btn btn-secondary" for="dashupload">
  7. <i class="fa fa-upload"></i>
  8. Upload .json File
  9. </label>
  10. `;
  11. /** @ngInject */
  12. function uploadDashboardDirective(timer, alertSrv, $location) {
  13. return {
  14. restrict: 'E',
  15. template: template,
  16. scope: {
  17. onUpload: '&',
  18. },
  19. link: function(scope) {
  20. function file_selected(evt) {
  21. var files = evt.target.files; // FileList object
  22. var readerOnload = function() {
  23. return function(e) {
  24. var dash;
  25. try {
  26. dash = JSON.parse(e.target.result);
  27. } catch (err) {
  28. console.log(err);
  29. scope.appEvent('alert-error', ['Import failed', 'JSON -> JS Serialization failed: ' + err.message]);
  30. return;
  31. }
  32. scope.$apply(function() {
  33. scope.onUpload({dash: dash});
  34. });
  35. };
  36. };
  37. for (var i = 0, f; f = files[i]; i++) {
  38. var reader = new FileReader();
  39. reader.onload = readerOnload();
  40. reader.readAsText(f);
  41. }
  42. }
  43. var wnd: any = window;
  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);