UNPKG

ami-cjs.js

Version:

<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>

338 lines (277 loc) 9.53 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _helpers = require('../helpers/helpers.progressbar'); var _helpers2 = _interopRequireDefault(_helpers); var _events = require('events'); var _events2 = _interopRequireDefault(_events); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** Imports **/ /** * * It is typically used to load a DICOM image. Use loading manager for * advanced usage, such as multiple files handling. * * Demo: {@link https://fnndsc.github.io/vjs#loader_dicom} * * @module loaders/base * @extends EventEmitter * @example * var files = ['/data/dcm/fruit']; * * // Instantiate a dicom loader * var lDicomoader = new dicom(); * * // load a resource * loader.load( * // resource URL * files[0], * // Function when resource is loaded * function(object) { * //scene.add( object ); * window.console.log(object); * } * ); */ var LoadersBase = function (_EventEmitter) { _inherits(LoadersBase, _EventEmitter); /** * Create a Loader. * @param {dom} container - The dom container of loader. * @param {object} ProgressBar - The progressbar of loader. */ function LoadersBase() { var container = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; var ProgressBar = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _helpers2.default; _classCallCheck(this, LoadersBase); var _this = _possibleConstructorReturn(this, (LoadersBase.__proto__ || Object.getPrototypeOf(LoadersBase)).call(this)); _this._loaded = -1; _this._totalLoaded = -1; _this._parsed = -1; _this._totalParsed = -1; _this._data = []; _this._container = container; _this._progressBar = null; if (_this._container && ProgressBar) { _this._progressBar = new ProgressBar(_this._container); } return _this; } /** * free the reference. */ _createClass(LoadersBase, [{ key: 'free', value: function free() { this._container = null; // this._helpersProgressBar = null; if (this._progressBar) { this._progressBar.free(); this._progressBar = null; } } /** * load the resource by url. * @param {string} url - resource url. * @return {promise} promise. */ }, { key: 'fetch', value: function fetch(url) { var _this2 = this; return new Promise(function (resolve, reject) { var request = new XMLHttpRequest(); request.open('GET', url); request.crossOrigin = true; request.responseType = 'arraybuffer'; request.onloadstart = function (event) { // emit 'fetch-start' event _this2.emit('fetch-start', { file: url, time: new Date() }); }; request.onload = function (event) { if (request.status === 200) { _this2._loaded = event.loaded; _this2._totalLoaded = event.total; // will be removed after eventer set up if (_this2._progressBar) { _this2._progressBar.update(_this2._loaded, _this2._totalLoaded, 'load'); } var buffer = request.response; var response = { url: url, buffer: buffer }; // emit 'fetch-success' event _this2.emit('fetch-success', { file: url, time: new Date(), totalLoaded: event.total }); resolve(response); } else { reject(request.statusText); } }; request.onerror = function () { // emit 'fetch-error' event _this2.emit('fetch-error', { file: url, time: new Date() }); reject(request.statusText); }; request.onabort = function (event) { // emit 'fetch-start' event _this2.emit('fetch-abort', { file: url, time: new Date() }); reject(request.statusText); }; request.ontimeout = function () { // emit 'fetch-timeout' event _this2.emit('fetch-timeout', { file: url, time: new Date() }); reject(request.statusText); }; request.onprogress = function (event) { _this2._loaded = event.loaded; _this2._totalLoaded = event.total; // emit 'fetch-progress' event _this2.emit('fetch-progress', { file: url, total: event.total, loaded: event.loaded, time: new Date() }); // will be removed after eventer set up if (_this2._progressBar) { _this2._progressBar.update(_this2._loaded, _this2._totalLoaded, 'load'); } }; request.onloadend = function (event) { // emit 'fetch-end' event _this2.emit('fetch-end', { file: url, time: new Date() }); // just use onload when success and onerror when failure, etc onabort // reject(request.statusText); }; request.send(); }); } /** * parse the data loaded * SHOULD BE implementd by detail loader. * @param {object} response - loaded data. * @return {promise} promise. */ }, { key: 'parse', value: function parse(response) { return new Promise(function (resolve, reject) { resolve(response); }); } /** * default load sequence group promise. * @param {array} url - resource url. * @return {promise} promise. */ }, { key: 'loadSequenceGroup', value: function loadSequenceGroup(url) { var _this3 = this; var fetchSequence = []; url.forEach(function (file) { fetchSequence.push(_this3.fetch(file)); }); return Promise.all(fetchSequence).then(function (rawdata) { return _this3.parse(rawdata); }).then(function (data) { _this3._data.push(data); return data; }).catch(function (error) { window.console.log('oops... something went wrong...'); window.console.log(error); }); } /** * default load sequence promise. * @param {string} url - resource url. * @return {promise} promise. */ }, { key: 'loadSequence', value: function loadSequence(url) { var _this4 = this; return this.fetch(url).then(function (rawdata) { return _this4.parse(rawdata); }).then(function (data) { _this4._data.push(data); return data; }).catch(function (error) { window.console.log('oops... something went wrong...'); window.console.log(error); }); } /** * load the data by url(urls) * @param {string|array} url - resource url. * @return {promise} promise */ }, { key: 'load', value: function load(url) { var _this5 = this; // if we load a single file, convert it to an array if (!Array.isArray(url)) { url = [url]; } // emit 'load-start' event this.emit('load-start', { files: url, time: new Date() }); var loadSequences = []; url.forEach(function (file) { if (!Array.isArray(file)) { loadSequences.push(_this5.loadSequence(file)); } else { loadSequences.push(_this5.loadSequenceGroup(file)); } }); return Promise.all(loadSequences); } /** * Set data * @param {array} data */ }, { key: 'data', set: function set(data) { this._data = data; } /** * Get data * @return {array} data loaded */ , get: function get() { return this._data; } }]); return LoadersBase; }(_events2.default); exports.default = LoadersBase; module.exports = exports['default'];