2017-12-20 12:33:33 +01:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-09-05 09:01:51 +02:00
|
|
|
import appEvents from 'app/core/app_events';
|
2018-09-12 09:47:16 +02:00
|
|
|
import angular from 'angular';
|
2016-05-11 16:18:52 +02:00
|
|
|
|
2018-08-26 20:19:23 +02:00
|
|
|
const template = `
|
2018-09-04 14:32:25 +02:00
|
|
|
<input type="file" id="dashupload" name="dashupload" class="hide" onchange="angular.element(this).scope().file_selected"/>
|
2017-12-05 12:19:01 +01:00
|
|
|
<label class="btn btn-success" for="dashupload">
|
2016-05-13 11:26:02 +02:00
|
|
|
<i class="fa fa-upload"></i>
|
2018-09-12 09:47:16 +02:00
|
|
|
{{btnText}}
|
2016-05-13 11:26:02 +02:00
|
|
|
</label>
|
|
|
|
|
`;
|
2016-05-11 16:18:52 +02:00
|
|
|
|
|
|
|
|
/** @ngInject */
|
2018-10-25 17:05:17 +02:00
|
|
|
function uploadDashboardDirective(timer, $location) {
|
2016-05-11 16:18:52 +02:00
|
|
|
return {
|
2017-12-20 12:33:33 +01:00
|
|
|
restrict: 'E',
|
2016-05-13 11:26:02 +02:00
|
|
|
template: template,
|
|
|
|
|
scope: {
|
2017-12-20 12:33:33 +01:00
|
|
|
onUpload: '&',
|
2018-09-12 09:47:16 +02:00
|
|
|
btnText: '@?',
|
2016-05-13 11:26:02 +02:00
|
|
|
},
|
2018-09-04 14:32:25 +02:00
|
|
|
link: (scope, elem) => {
|
2018-09-12 09:47:16 +02:00
|
|
|
scope.btnText = angular.isDefined(scope.btnText) ? scope.btnText : 'Upload .json File';
|
|
|
|
|
|
2016-05-11 16:18:52 +02:00
|
|
|
function file_selected(evt) {
|
2018-08-26 20:19:23 +02:00
|
|
|
const files = evt.target.files; // FileList object
|
2018-09-05 07:47:30 +02:00
|
|
|
const readerOnload = () => {
|
|
|
|
|
return e => {
|
2018-08-30 08:58:43 +02:00
|
|
|
let dash;
|
2016-05-11 16:18:52 +02:00
|
|
|
try {
|
|
|
|
|
dash = JSON.parse(e.target.result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
2018-09-05 09:01:51 +02:00
|
|
|
appEvents.emit('alert-error', ['Import failed', 'JSON -> JS Serialization failed: ' + err.message]);
|
2016-05-11 16:18:52 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 07:47:30 +02:00
|
|
|
scope.$apply(() => {
|
2017-12-19 16:06:54 +01:00
|
|
|
scope.onUpload({ dash: dash });
|
2016-05-13 17:39:22 +02:00
|
|
|
});
|
2016-05-11 16:18:52 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-05 10:53:58 +02:00
|
|
|
let i = 0;
|
2018-09-05 12:09:16 +02:00
|
|
|
let file = files[i];
|
|
|
|
|
|
|
|
|
|
while (file) {
|
2018-08-26 20:19:23 +02:00
|
|
|
const reader = new FileReader();
|
2016-05-11 16:18:52 +02:00
|
|
|
reader.onload = readerOnload();
|
2018-09-05 12:09:16 +02:00
|
|
|
reader.readAsText(file);
|
|
|
|
|
i += 1;
|
|
|
|
|
file = files[i];
|
2016-05-11 16:18:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-13 11:26:02 +02:00
|
|
|
|
2018-08-26 20:19:23 +02:00
|
|
|
const wnd: any = window;
|
2016-05-11 16:18:52 +02:00
|
|
|
// Check for the various File API support.
|
|
|
|
|
if (wnd.File && wnd.FileReader && wnd.FileList && wnd.Blob) {
|
|
|
|
|
// Something
|
2018-09-04 14:32:25 +02:00
|
|
|
elem[0].addEventListener('change', file_selected, false);
|
2016-05-11 16:18:52 +02:00
|
|
|
} else {
|
2018-10-25 17:05:17 +02:00
|
|
|
appEvents.emit('alert-error', ['Oops', 'The HTML5 File APIs are not fully supported in this browser']);
|
2016-05-11 16:18:52 +02:00
|
|
|
}
|
2017-12-20 12:33:33 +01:00
|
|
|
},
|
2016-05-11 16:18:52 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.directive('dashUpload', uploadDashboardDirective);
|