UNPKG

ionic-image-loader-v5

Version:
1,215 lines (1,187 loc) 105 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('@ionic-native/file/ngx'), require('@ionic-native/ionic-webview/ngx'), require('@ionic/angular'), require('rxjs'), require('rxjs/operators'), require('@ionic-native/file/ngx/index'), require('@ionic-native/ionic-webview/ngx/index'), require('@angular/common')) : typeof define === 'function' && define.amd ? define('ionic-image-loader-v5', ['exports', '@angular/common/http', '@angular/core', '@ionic-native/file/ngx', '@ionic-native/ionic-webview/ngx', '@ionic/angular', 'rxjs', 'rxjs/operators', '@ionic-native/file/ngx/index', '@ionic-native/ionic-webview/ngx/index', '@angular/common'], factory) : (global = global || self, factory(global['ionic-image-loader-v5'] = {}, global.ng.common.http, global.ng.core, global.ngx, global.ngx$1, global.angular, global.rxjs, global.rxjs.operators, global.index, global.index$1, global.ng.common)); }(this, (function (exports, http, core, ngx, ngx$1, angular, rxjs, operators, index, index$1, common) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __exportStar(m, exports) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } function __values(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result.default = mod; return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } function __classPrivateFieldGet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); } function __classPrivateFieldSet(receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; } /** * @fileoverview added by tsickle * Generated from: lib/services/image-loader-config.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var ImageLoaderConfigService = /** @class */ (function () { function ImageLoaderConfigService() { this.debugMode = false; this.spinnerEnabled = true; this.fallbackAsPlaceholder = false; this.backgroundSize = 'contain'; this.backgroundRepeat = 'no-repeat'; this.display = 'block'; this.width = '100%'; this.height = '100%'; this.useImg = false; this.concurrency = 5; this.maxCacheSize = -1; this.maxCacheAge = -1; this.imageReturnType = 'uri'; // Must be default 'true' for the new WebView to show images this.fileNameCachedWithExtension = true; this.fallbackFileNameCachedExtension = '.jpg'; this.cacheDirectoryType = 'cache'; this._cacheDirectoryName = 'image-loader-cache'; } Object.defineProperty(ImageLoaderConfigService.prototype, "cacheDirectoryName", { get: /** * @return {?} */ function () { return this._cacheDirectoryName; }, set: /** * @param {?} name * @return {?} */ function (name) { name.replace(/\W/g, ''); this._cacheDirectoryName = name; }, enumerable: true, configurable: true }); /** * Enables debug mode to receive console logs, errors, warnings */ /** * Enables debug mode to receive console logs, errors, warnings * @return {?} */ ImageLoaderConfigService.prototype.enableDebugMode = /** * Enables debug mode to receive console logs, errors, warnings * @return {?} */ function () { this.debugMode = true; }; /** * Enable/Disable the spinner by default. Defaults to true. * @param enable set to true to enable */ /** * Enable/Disable the spinner by default. Defaults to true. * @param {?} enable set to true to enable * @return {?} */ ImageLoaderConfigService.prototype.enableSpinner = /** * Enable/Disable the spinner by default. Defaults to true. * @param {?} enable set to true to enable * @return {?} */ function (enable) { this.spinnerEnabled = enable; }; /** * Enable/Disable the fallback image as placeholder instead of the spinner. Defaults to false. * @param enable set to true to enable */ /** * Enable/Disable the fallback image as placeholder instead of the spinner. Defaults to false. * @param {?} enable set to true to enable * @return {?} */ ImageLoaderConfigService.prototype.enableFallbackAsPlaceholder = /** * Enable/Disable the fallback image as placeholder instead of the spinner. Defaults to false. * @param {?} enable set to true to enable * @return {?} */ function (enable) { this.fallbackAsPlaceholder = enable; }; /** * Sets the cache directory name. Defaults to 'image-loader-cache' * @param name name of directory */ /** * Sets the cache directory name. Defaults to 'image-loader-cache' * @param {?} name name of directory * @return {?} */ ImageLoaderConfigService.prototype.setCacheDirectoryName = /** * Sets the cache directory name. Defaults to 'image-loader-cache' * @param {?} name name of directory * @return {?} */ function (name) { this.cacheDirectoryName = name; }; /** * Set default height for images that are not using <img> tag * @param height height */ /** * Set default height for images that are not using <img> tag * @param {?} height height * @return {?} */ ImageLoaderConfigService.prototype.setHeight = /** * Set default height for images that are not using <img> tag * @param {?} height height * @return {?} */ function (height) { this.height = height; }; /** * Set default width for images that are not using <img> tag * @param width Width */ /** * Set default width for images that are not using <img> tag * @param {?} width Width * @return {?} */ ImageLoaderConfigService.prototype.setWidth = /** * Set default width for images that are not using <img> tag * @param {?} width Width * @return {?} */ function (width) { this.width = width; }; /** * Enable display mode for images that are not using <img> tag * @param display Display mode */ /** * Enable display mode for images that are not using <img> tag * @param {?} display Display mode * @return {?} */ ImageLoaderConfigService.prototype.setDisplay = /** * Enable display mode for images that are not using <img> tag * @param {?} display Display mode * @return {?} */ function (display) { this.display = display; }; /** * Use <img> tag by default * @param use set to true to use <img> tag by default */ /** * Use <img> tag by default * @param {?} use set to true to use <img> tag by default * @return {?} */ ImageLoaderConfigService.prototype.useImageTag = /** * Use <img> tag by default * @param {?} use set to true to use <img> tag by default * @return {?} */ function (use) { this.useImg = use; }; /** * Set default background size for images that are not using <img> tag * @param backgroundSize Background size */ /** * Set default background size for images that are not using <img> tag * @param {?} backgroundSize Background size * @return {?} */ ImageLoaderConfigService.prototype.setBackgroundSize = /** * Set default background size for images that are not using <img> tag * @param {?} backgroundSize Background size * @return {?} */ function (backgroundSize) { this.backgroundSize = backgroundSize; }; /** * Set background repeat for images that are not using <img> tag * @param backgroundRepeat Background repeat */ /** * Set background repeat for images that are not using <img> tag * @param {?} backgroundRepeat Background repeat * @return {?} */ ImageLoaderConfigService.prototype.setBackgroundRepeat = /** * Set background repeat for images that are not using <img> tag * @param {?} backgroundRepeat Background repeat * @return {?} */ function (backgroundRepeat) { this.backgroundRepeat = backgroundRepeat; }; /** * Set fallback URL to use when image src is undefined or did not resolve. * This image will not be cached. This should ideally be a locally saved image. * @param fallbackUrl The remote or local URL of the image */ /** * Set fallback URL to use when image src is undefined or did not resolve. * This image will not be cached. This should ideally be a locally saved image. * @param {?} fallbackUrl The remote or local URL of the image * @return {?} */ ImageLoaderConfigService.prototype.setFallbackUrl = /** * Set fallback URL to use when image src is undefined or did not resolve. * This image will not be cached. This should ideally be a locally saved image. * @param {?} fallbackUrl The remote or local URL of the image * @return {?} */ function (fallbackUrl) { this.fallbackUrl = fallbackUrl; }; /** * Set the maximum number of allowed connections at the same time. * @param concurrency */ /** * Set the maximum number of allowed connections at the same time. * @param {?} concurrency * @return {?} */ ImageLoaderConfigService.prototype.setConcurrency = /** * Set the maximum number of allowed connections at the same time. * @param {?} concurrency * @return {?} */ function (concurrency) { this.concurrency = concurrency; }; /** * Sets the maximum allowed cache size * @param cacheSize Cache size in bytes */ /** * Sets the maximum allowed cache size * @param {?} cacheSize Cache size in bytes * @return {?} */ ImageLoaderConfigService.prototype.setMaximumCacheSize = /** * Sets the maximum allowed cache size * @param {?} cacheSize Cache size in bytes * @return {?} */ function (cacheSize) { this.maxCacheSize = cacheSize; }; /** * Sets the maximum allowed cache age * @param cacheAge Maximum cache age in milliseconds */ /** * Sets the maximum allowed cache age * @param {?} cacheAge Maximum cache age in milliseconds * @return {?} */ ImageLoaderConfigService.prototype.setMaximumCacheAge = /** * Sets the maximum allowed cache age * @param {?} cacheAge Maximum cache age in milliseconds * @return {?} */ function (cacheAge) { this.maxCacheAge = cacheAge; }; /** * Set the return type of cached images * @param imageReturnType The return type; either 'base64' or 'uri' */ /** * Set the return type of cached images * @param {?} imageReturnType The return type; either 'base64' or 'uri' * @return {?} */ ImageLoaderConfigService.prototype.setImageReturnType = /** * Set the return type of cached images * @param {?} imageReturnType The return type; either 'base64' or 'uri' * @return {?} */ function (imageReturnType) { this.imageReturnType = imageReturnType; }; /** * Set the default spinner name * @param name */ /** * Set the default spinner name * @param {?} name * @return {?} */ ImageLoaderConfigService.prototype.setSpinnerName = /** * Set the default spinner name * @param {?} name * @return {?} */ function (name) { this.spinnerName = name; }; /** * Set the default spinner color * @param color */ /** * Set the default spinner color * @param {?} color * @return {?} */ ImageLoaderConfigService.prototype.setSpinnerColor = /** * Set the default spinner color * @param {?} color * @return {?} */ function (color) { this.spinnerColor = color; }; /** * Set headers options for the HttpClient transfers. * @param headers */ /** * Set headers options for the HttpClient transfers. * @param {?} headers * @return {?} */ ImageLoaderConfigService.prototype.setHttpHeaders = /** * Set headers options for the HttpClient transfers. * @param {?} headers * @return {?} */ function (headers) { this.httpHeaders = headers; }; /** * Set options for the FileTransfer plugin * @param options * @deprecated FileTransfer plugin removed. */ /** * Set options for the FileTransfer plugin * @deprecated FileTransfer plugin removed. * @param {?} options * @return {?} */ ImageLoaderConfigService.prototype.setFileTransferOptions = /** * Set options for the FileTransfer plugin * @deprecated FileTransfer plugin removed. * @param {?} options * @return {?} */ function (options) { // do nothing, plugin deprecated. }; /** * Enable/Disable the save filename of cached images with extension. Defaults to false. * @param enable set to true to enable */ /** * Enable/Disable the save filename of cached images with extension. Defaults to false. * @param {?} enable set to true to enable * @return {?} */ ImageLoaderConfigService.prototype.setFileNameCachedWithExtension = /** * Enable/Disable the save filename of cached images with extension. Defaults to false. * @param {?} enable set to true to enable * @return {?} */ function (enable) { this.fileNameCachedWithExtension = enable; }; /** * Set fallback extension filename of cached images. Defaults to '.jpg'. * @param extension fallback extension (e.x .jpg) */ /** * Set fallback extension filename of cached images. Defaults to '.jpg'. * @param {?} extension fallback extension (e.x .jpg) * @return {?} */ ImageLoaderConfigService.prototype.setFallbackFileNameCachedExtension = /** * Set fallback extension filename of cached images. Defaults to '.jpg'. * @param {?} extension fallback extension (e.x .jpg) * @return {?} */ function (extension) { this.fallbackFileNameCachedExtension = extension; }; ImageLoaderConfigService.decorators = [ { type: core.Injectable, args: [{ providedIn: 'root', },] } ]; /** @nocollapse */ ImageLoaderConfigService.ngInjectableDef = core.ɵɵdefineInjectable({ factory: function ImageLoaderConfigService_Factory() { return new ImageLoaderConfigService(); }, token: ImageLoaderConfigService, providedIn: "root" }); return ImageLoaderConfigService; }()); if (false) { /** @type {?} */ ImageLoaderConfigService.prototype.debugMode; /** @type {?} */ ImageLoaderConfigService.prototype.spinnerEnabled; /** @type {?} */ ImageLoaderConfigService.prototype.fallbackAsPlaceholder; /** @type {?} */ ImageLoaderConfigService.prototype.backgroundSize; /** @type {?} */ ImageLoaderConfigService.prototype.backgroundRepeat; /** @type {?} */ ImageLoaderConfigService.prototype.display; /** @type {?} */ ImageLoaderConfigService.prototype.width; /** @type {?} */ ImageLoaderConfigService.prototype.height; /** @type {?} */ ImageLoaderConfigService.prototype.useImg; /** @type {?} */ ImageLoaderConfigService.prototype.fallbackUrl; /** @type {?} */ ImageLoaderConfigService.prototype.concurrency; /** @type {?} */ ImageLoaderConfigService.prototype.maxCacheSize; /** @type {?} */ ImageLoaderConfigService.prototype.maxCacheAge; /** @type {?} */ ImageLoaderConfigService.prototype.imageReturnType; /** @type {?} */ ImageLoaderConfigService.prototype.spinnerName; /** @type {?} */ ImageLoaderConfigService.prototype.spinnerColor; /** @type {?} */ ImageLoaderConfigService.prototype.httpHeaders; /** @type {?} */ ImageLoaderConfigService.prototype.fileNameCachedWithExtension; /** @type {?} */ ImageLoaderConfigService.prototype.fallbackFileNameCachedExtension; /** @type {?} */ ImageLoaderConfigService.prototype.cacheDirectoryType; /** * @type {?} * @private */ ImageLoaderConfigService.prototype._cacheDirectoryName; } /** * @fileoverview added by tsickle * Generated from: lib/services/image-loader.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function IndexItem() { } if (false) { /** @type {?} */ IndexItem.prototype.name; /** @type {?} */ IndexItem.prototype.modificationTime; /** @type {?} */ IndexItem.prototype.size; } /** * @record */ function QueueItem() { } if (false) { /** @type {?} */ QueueItem.prototype.imageUrl; /** @type {?} */ QueueItem.prototype.resolve; /** @type {?} */ QueueItem.prototype.reject; } /** @type {?} */ var EXTENSIONS = ['jpg', 'png', 'jpeg', 'gif', 'svg', 'tiff']; var ImageLoaderService = /** @class */ (function () { function ImageLoaderService(config, file, http, platform, webview) { var _this = this; this.config = config; this.file = file; this.http = http; this.platform = platform; this.webview = webview; /** * Indicates if the cache service is ready. * When the cache service isn't ready, images are loaded via browser instead. */ this.isCacheReady = false; /** * Indicates if this service is initialized. * This service is initialized once all the setup is done. */ this.isInit = false; this.initPromise = new Promise((/** * @param {?} resolve * @return {?} */ function (resolve) { return _this.initPromiseResolve = resolve; })); this.lockSubject = new rxjs.Subject(); this.lock$ = this.lockSubject.asObservable(); /** * Number of concurrent requests allowed */ this.concurrency = 5; /** * Queue items */ this.queue = []; this.processing = 0; /** * Fast accessible Object for currently processing items */ this.currentlyProcessing = {}; this.cacheIndex = []; this.currentCacheSize = 0; this.indexed = false; this.lockedCallsQueue = []; if (!platform.is('cordova')) { // we are running on a browser, or using livereload // plugin will not function in this case this.isInit = true; this.throwWarning('You are running on a browser or using livereload, IonicImageLoader will not function, falling back to browser loading.'); this.initPromiseResolve(); } else { rxjs.fromEvent(document, 'deviceready') .pipe(operators.first()) .subscribe((/** * @param {?} res * @return {?} */ function (res) { if (_this.nativeAvailable) { _this.initCache(); } else { // we are running on a browser, or using livereload // plugin will not function in this case _this.isInit = true; _this.initPromiseResolve(); _this.throwWarning('You are running on a browser or using livereload, IonicImageLoader will not function, falling back to browser loading.'); } })); } } Object.defineProperty(ImageLoaderService.prototype, "nativeAvailable", { get: /** * @return {?} */ function () { return ngx.File.installed(); }, enumerable: true, configurable: true }); Object.defineProperty(ImageLoaderService.prototype, "isCacheSpaceExceeded", { get: /** * @private * @return {?} */ function () { return (this.config.maxCacheSize > -1 && this.currentCacheSize > this.config.maxCacheSize); }, enumerable: true, configurable: true }); Object.defineProperty(ImageLoaderService.prototype, "isWKWebView", { get: /** * @private * @return {?} */ function () { return (this.platform.is('ios') && ((/** @type {?} */ (window))).webkit && ((/** @type {?} */ (window))).webkit.messageHandlers); }, enumerable: true, configurable: true }); Object.defineProperty(ImageLoaderService.prototype, "isIonicWKWebView", { get: /** * @private * @return {?} */ function () { return ( // Important: isWKWebview && isIonicWKWebview must be mutually excluse. // Otherwise the logic for copying to tmp under IOS will fail. (this.platform.is('android') && this.webview) || (this.platform.is('android')) && (location.host === 'localhost:8080') || ((/** @type {?} */ (window))).LiveReload); }, enumerable: true, configurable: true }); Object.defineProperty(ImageLoaderService.prototype, "isDevServer", { get: /** * @private * @return {?} */ function () { return window['IonicDevServer'] !== undefined; }, enumerable: true, configurable: true }); Object.defineProperty(ImageLoaderService.prototype, "canProcess", { /** * Check if we can process more items in the queue */ get: /** * Check if we can process more items in the queue * @private * @return {?} */ function () { return this.queue.length > 0 && this.processing < this.concurrency; }, enumerable: true, configurable: true }); /** * @return {?} */ ImageLoaderService.prototype.ready = /** * @return {?} */ function () { return this.initPromise; }; /** * Preload an image * @param imageUrl Image URL * @returns returns a promise that resolves with the cached image URL */ /** * Preload an image * @param {?} imageUrl Image URL * @return {?} returns a promise that resolves with the cached image URL */ ImageLoaderService.prototype.preload = /** * Preload an image * @param {?} imageUrl Image URL * @return {?} returns a promise that resolves with the cached image URL */ function (imageUrl) { return this.getImagePath(imageUrl); }; /** * @return {?} */ ImageLoaderService.prototype.getFileCacheDirectory = /** * @return {?} */ function () { if (this.config.cacheDirectoryType === 'data') { return this.file.dataDirectory; } else if (this.config.cacheDirectoryType === 'external') { return this.platform.is('android') ? this.file.externalDataDirectory : this.file.documentsDirectory; } return this.file.cacheDirectory; }; /** * Clears cache of a single image * @param imageUrl Image URL */ /** * Clears cache of a single image * @param {?} imageUrl Image URL * @return {?} */ ImageLoaderService.prototype.clearImageCache = /** * Clears cache of a single image * @param {?} imageUrl Image URL * @return {?} */ function (imageUrl) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.platform.is('cordova')) { return [2 /*return*/]; } return [4 /*yield*/, this.ready()]; case 1: _a.sent(); this.runLocked((/** * @return {?} */ function () { return __awaiter(_this, void 0, void 0, function () { var fileName, route, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: fileName = this.createFileName(imageUrl); route = this.getFileCacheDirectory() + this.config.cacheDirectoryName; // pause any operations this.isInit = false; _a.label = 1; case 1: _a.trys.push([1, 5, , 6]); return [4 /*yield*/, this.file.removeFile(route, fileName)]; case 2: _a.sent(); if (!(this.isWKWebView && !this.isIonicWKWebView)) return [3 /*break*/, 4]; return [4 /*yield*/, this.file.removeFile(this.file.tempDirectory + this.config.cacheDirectoryName, fileName)]; case 3: _a.sent(); _a.label = 4; case 4: return [3 /*break*/, 6]; case 5: err_1 = _a.sent(); this.throwError(err_1); return [3 /*break*/, 6]; case 6: return [2 /*return*/, this.initCache(true)]; } }); }); })); return [2 /*return*/]; } }); }); }; /** * Clears the cache */ /** * Clears the cache * @return {?} */ ImageLoaderService.prototype.clearCache = /** * Clears the cache * @return {?} */ function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.platform.is('cordova')) { return [2 /*return*/]; } return [4 /*yield*/, this.ready()]; case 1: _a.sent(); this.runLocked((/** * @return {?} */ function () { return __awaiter(_this, void 0, void 0, function () { var err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.file.removeRecursively(this.getFileCacheDirectory(), this.config.cacheDirectoryName)]; case 1: _a.sent(); if (this.isWKWebView && !this.isIonicWKWebView) { // also clear the temp files try { this.file.removeRecursively(this.file.tempDirectory, this.config.cacheDirectoryName); } catch (err) { // Noop catch. Removing the tempDirectory might fail, // as it is not persistent. } } return [3 /*break*/, 3]; case 2: err_2 = _a.sent(); this.throwError(err_2); return [3 /*break*/, 3]; case 3: return [2 /*return*/, this.initCache(true)]; } }); }); })); return [2 /*return*/]; } }); }); }; /** * Gets the filesystem path of an image. * This will return the remote path if anything goes wrong or if the cache service isn't ready yet. * @param imageUrl The remote URL of the image * @returns Returns a promise that will always resolve with an image URL */ /** * Gets the filesystem path of an image. * This will return the remote path if anything goes wrong or if the cache service isn't ready yet. * @param {?} imageUrl The remote URL of the image * @return {?} Returns a promise that will always resolve with an image URL */ ImageLoaderService.prototype.getImagePath = /** * Gets the filesystem path of an image. * This will return the remote path if anything goes wrong or if the cache service isn't ready yet. * @param {?} imageUrl The remote URL of the image * @return {?} Returns a promise that will always resolve with an image URL */ function (imageUrl) { return __awaiter(this, void 0, void 0, function () { var err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (typeof imageUrl !== 'string' || imageUrl.length <= 0) { throw new Error('The image url provided was empty or invalid.'); } return [4 /*yield*/, this.ready()]; case 1: _a.sent(); if (!this.isCacheReady) { this.throwWarning('The cache system is not running. Images will be loaded by your browser instead.'); return [2 /*return*/, imageUrl]; } if (this.isImageUrlRelative(imageUrl)) { return [2 /*return*/, imageUrl]; } _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4 /*yield*/, this.getCachedImagePath(imageUrl)]; case 3: return [2 /*return*/, _a.sent()]; case 4: err_3 = _a.sent(); // image doesn't exist in cache, lets fetch it and save it return [2 /*return*/, this.addItemToQueue(imageUrl)]; case 5: return [2 /*return*/]; } }); }); }; /** * @private * @return {?} */ ImageLoaderService.prototype.processLockedQueue = /** * @private * @return {?} */ function () { return __awaiter(this, void 0, void 0, function () { var err_4; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getLockedState()]; case 1: if (_a.sent()) { return [2 /*return*/]; } if (!(this.lockedCallsQueue.length > 0)) return [3 /*break*/, 8]; return [4 /*yield*/, this.setLockedState(true)]; case 2: _a.sent(); _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); return [4 /*yield*/, this.lockedCallsQueue.slice(0, 1)[0]()]; case 4: _a.sent(); return [3 /*break*/, 6]; case 5: err_4 = _a.sent(); console.log('Error running locked function: ', err_4); return [3 /*break*/, 6]; case 6: return [4 /*yield*/, this.setLockedState(false)]; case 7: _a.sent(); return [2 /*return*/, this.processLockedQueue()]; case 8: return [2 /*return*/]; } }); }); }; /** * @private * @return {?} */ ImageLoaderService.prototype.getLockedState = /** * @private * @return {?} */ function () { return this.lock$ .pipe(operators.take(1)) .toPromise(); }; /** * @private * @return {?} */ ImageLoaderService.prototype.awaitUnlocked = /** * @private * @return {?} */ function () { return this.lock$ .pipe(operators.filter((/** * @param {?} locked * @return {?} */ function (locked) { return !!locked; })), operators.take(1)) .toPromise(); }; /** * @private * @param {?} locked * @return {?} */ ImageLoaderService.prototype.setLockedState = /** * @private * @param {?} locked * @return {?} */ function (locked) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this.lockSubject.next(locked); return [2 /*return*/]; }); }); }; /** * @private * @param {?} fn * @return {?} */ ImageLoaderService.prototype.runLocked = /** * @private * @param {?} fn * @return {?} */ function (fn) { this.lockedCallsQueue.push(fn); this.processLockedQueue(); }; /** * Returns if an imageUrl is an relative path * @param imageUrl */ /** * Returns if an imageUrl is an relative path * @private * @param {?} imageUrl * @return {?} */ ImageLoaderService.prototype.isImageUrlRelative = /** * Returns if an imageUrl is an relative path * @private * @param {?} imageUrl * @return {?} */ function (imageUrl) { return !/^(https?|file):\/\/\/?/i.test(imageUrl); }; /** * Add an item to the queue * @param imageUrl * @param resolve * @param reject */ /** * Add an item to the queue * @private * @param {?} imageUrl * @param {?=} resolve * @param {?=} reject * @return {?} */ ImageLoaderService.prototype.addItemToQueue = /** * Add an item to the queue * @private * @param {?} imageUrl * @param {?=} resol