@iotile/iotile-common
Version:
Common utilities for IoTile Packages and Applications
1,201 lines (1,173 loc) • 287 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('typescript-logging')) :
typeof define === 'function' && define.amd ? define(['exports', 'typescript-logging'], factory) :
(factory((global.iotileCommon = {}),global.typescriptLogging));
}(this, (function (exports,typescriptLogging) { '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 = 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]; };
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __awaiter(thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, 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 };
}
}
var LoggingBase = /** @class */ (function () {
function LoggingBase(category) {
this.category = category;
}
/**
* @param level
* @param applyChildren default: fase. If set to `true` then the level will be set for all
* child categories.
*
* @description **WARNING**: This will override the entire category configuration, not just the LogLevel.
* **DO NOT** use in production.
*
* @todo Keep current config settings and only update LogLevel. It is not apparent how
* to do this and may require a PR for https://github.com/mreuvers/typescript-logging
*/
LoggingBase.prototype.setLogLevel = function (level, applyChildren) {
if (applyChildren === void 0) { applyChildren = false; }
typescriptLogging.CategoryServiceFactory.setConfigurationCategory(new typescriptLogging.CategoryConfiguration(level), this.category, applyChildren);
};
LoggingBase.prototype.buildLogMessage = function (msg, extraArgs) {
if (extraArgs == null)
extraArgs = undefined;
return {
msg: msg,
data: extraArgs
};
};
LoggingBase.prototype.logException = function (msg, error, extraArgs) {
this.category.error(this.buildLogMessage(msg, extraArgs), error);
};
LoggingBase.prototype.logError = function (msg, extraArgs) {
this.category.error(this.buildLogMessage(msg, extraArgs), null);
};
LoggingBase.prototype.logDebug = function (msg, extraArgs) {
this.category.debug(this.buildLogMessage(msg, extraArgs));
};
LoggingBase.prototype.logInfo = function (msg, extraArgs) {
this.category.info(this.buildLogMessage(msg, extraArgs));
};
LoggingBase.prototype.logTrace = function (msg, extraArgs) {
this.category.trace(this.buildLogMessage(msg, extraArgs));
};
LoggingBase.prototype.logWarning = function (msg, extraArgs) {
this.category.warn(this.buildLogMessage(msg, extraArgs));
};
return LoggingBase;
}());
/**
* Legacy base object that previously assumed we were using angular logging
*/
var ObjectBase = /** @class */ (function (_super) {
__extends(ObjectBase, _super);
function ObjectBase(name, $injector) {
var _this = _super.call(this, ObjectBase.createOrGetCategory(name)) || this;
_this.className = name;
_this.$injector = $injector;
return _this;
}
ObjectBase.createOrGetCategory = function (name) {
if (!(name in ObjectBase.categoryMap))
this.categoryMap[name] = new typescriptLogging.Category(name, ObjectBase.categoryBase);
return this.categoryMap[name];
};
ObjectBase.prototype.log_debug = function (message) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.logDebug(message, args);
};
ObjectBase.prototype.log_warn = function (message) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.logWarning(message, args);
};
ObjectBase.prototype.log_error = function (message) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.logError(message, args);
};
ObjectBase.prototype.log_info = function (message) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.logInfo(message, args);
};
ObjectBase.categoryMap = {};
ObjectBase.categoryBase = new typescriptLogging.Category("object");
return ObjectBase;
}(LoggingBase));
/**
* @ngdoc object
* @name Errors.type:BaseError
* @description
* The base error class for all exceptions. All exceptions must
* have a name that should match the name of their class and a user
* settable message. Typically, subclasses of BaseError will fill in the
* name field in their own constructor, so users do not have to set it.
*
* @property {string} name The name of of the exception, which is intended
* to be used as a method of filtering different exceptions.
* @property {string} message A freeform message that provides more detail
* into what caused this error.
*/
var BaseError = /** @class */ (function () {
function BaseError(name, message) {
this.name = name;
this.message = message;
}
return BaseError;
}());
/**
* @ngdoc object
* @name Errors.type:ArgumentError
* @description
* There was an issue with one of the parameters passed to this function.
* The parameter was invalid. The included message should contain more
* details about what was expected vs received.
*/
var ArgumentError = /** @class */ (function (_super) {
__extends(ArgumentError, _super);
function ArgumentError(message) {
return _super.call(this, 'ArgumentError', message) || this;
}
return ArgumentError;
}(BaseError));
/**
* @ngdoc object
* @name Errors.type:InvalidOperationError
* @description
* The operation could not be performed at the time that it
* was requested. This may be because the application's state
* did not allow its preconditions to be met.
*/
var InvalidOperationError = /** @class */ (function (_super) {
__extends(InvalidOperationError, _super);
function InvalidOperationError(message) {
return _super.call(this, 'InvalidOperationError', message) || this;
}
return InvalidOperationError;
}(BaseError));
/**
* @ngdoc object
* @name Errors.type:UnknownError
* @description
* There was an unspecified error with the operation.
* See the message property for more details.
*/
var UnknownError = /** @class */ (function (_super) {
__extends(UnknownError, _super);
function UnknownError(message) {
return _super.call(this, 'UnknownError', message) || this;
}
return UnknownError;
}(BaseError));
/**
* @ngdoc object
* @name Errors.type:InsufficientSpaceError
* @description
* There was not sufficient space to perform this operation. This error
* could be thrown, if, for example, you were trying to append data to a fixed
* size buffer and there was not room in the buffer for all the data you wanted
* to append.
*/
var InsufficientSpaceError = /** @class */ (function (_super) {
__extends(InsufficientSpaceError, _super);
function InsufficientSpaceError(message) {
return _super.call(this, 'InsufficientSpaceError', message) || this;
}
return InsufficientSpaceError;
}(BaseError));
/**
* @ngdoc object
* @name Errors.type:UnknownKeyError
* @description
* A key was passed to a lookup table and it was not found.
*/
var UnknownKeyError = /** @class */ (function (_super) {
__extends(UnknownKeyError, _super);
function UnknownKeyError(message) {
return _super.call(this, 'UnknownKeyError', message) || this;
}
return UnknownKeyError;
}(BaseError));
/**
* @ngdoc object
* @name Errors.type:FileSystemError
* @description
* There was an error interacting with the file system on
* the device.
*/
var FileSystemError = /** @class */ (function () {
function FileSystemError(name, message, code) {
this.name = name;
this.message = message;
this.code = code;
}
FileSystemError.NOT_FOUND_ERR = 1;
FileSystemError.SECURITY_ERR = 2;
FileSystemError.ABORT_ERR = 3;
FileSystemError.NOT_READABLE_ERR = 4;
FileSystemError.ENCODING_ERR = 5;
FileSystemError.NO_MODIFICATION_ALLOWED_ERR = 6;
FileSystemError.INVALID_STATE_ERR = 7;
FileSystemError.SYNTAX_ERR = 8;
FileSystemError.INVALID_MODIFICATION_ERR = 9;
FileSystemError.QUOTA_EXCEEDED_ERR = 10;
FileSystemError.TYPE_MISMATCH_ERR = 11;
FileSystemError.PATH_EXISTS_ERR = 12;
return FileSystemError;
}());
/**
* @ngdoc object
* @name Errors.type:UnknownFileSystemError
*
* @description
* There was an error interacting with the file system on
* the device.
*/
var UnknownFileSystemError = /** @class */ (function (_super) {
__extends(UnknownFileSystemError, _super);
function UnknownFileSystemError(code, path) {
return _super.call(this, 'UnknownFileSystemError', "Unknown filesystem error: " + code + " path: " + path, code) || this;
}
return UnknownFileSystemError;
}(FileSystemError));
/**
* @ngdoc object
* @name Errors.type:BatchOperationError
*
* @description
* A batch operation failed for one or more elements that
* were addressed. The individual errors are listed in the
* errors property.
*/
var BatchOperationError = /** @class */ (function () {
function BatchOperationError(message, errors) {
this.name = "BatchOperationError";
this.message = message;
this.errors = errors;
}
return BatchOperationError;
}());
var InvalidDataError = /** @class */ (function () {
function InvalidDataError(name, message) {
this.name = name;
this.message = message;
}
return InvalidDataError;
}());
var UserNotLoggedInError = /** @class */ (function () {
function UserNotLoggedInError(message) {
this.message = message;
}
return UserNotLoggedInError;
}());
var DataCorruptedError = /** @class */ (function (_super) {
__extends(DataCorruptedError, _super);
function DataCorruptedError(message) {
return _super.call(this, "DataCorruptedError", message) || this;
}
return DataCorruptedError;
}(InvalidDataError));
var DataStaleError = /** @class */ (function (_super) {
__extends(DataStaleError, _super);
function DataStaleError(message) {
return _super.call(this, "DataStaleError", message) || this;
}
return DataStaleError;
}(InvalidDataError));
var CorruptDeviceError = /** @class */ (function (_super) {
__extends(CorruptDeviceError, _super);
function CorruptDeviceError(message) {
return _super.call(this, "CorruptDeviceError", message) || this;
}
return CorruptDeviceError;
}(InvalidDataError));
/*
* Classes for reporting progress about complex
* multistep operations.
*/
var ProgressNotifier = /** @class */ (function () {
function ProgressNotifier() {
}
ProgressNotifier.prototype.finishOne = function () {
};
ProgressNotifier.prototype.startOne = function (desc, subparts) {
return null;
};
ProgressNotifier.prototype.setTotal = function (total) {
};
ProgressNotifier.prototype.addMessage = function (severity, message, priority) {
if (priority === void 0) { priority = false; }
};
ProgressNotifier.prototype.addError = function (message, priority) {
if (priority === void 0) { priority = false; }
this.addMessage(exports.MessageSeverity.Error, message, priority);
};
ProgressNotifier.prototype.addInfo = function (message, priority) {
if (priority === void 0) { priority = false; }
this.addMessage(exports.MessageSeverity.Info, message, priority);
};
ProgressNotifier.prototype.addWarning = function (message, priority) {
if (priority === void 0) { priority = false; }
this.addMessage(exports.MessageSeverity.Warn, message, priority);
};
ProgressNotifier.prototype.fatalError = function (message) {
};
ProgressNotifier.prototype.updateDescription = function (desc) {
};
Object.defineProperty(ProgressNotifier.prototype, "hasErrors", {
get: function () {
return false;
},
enumerable: true,
configurable: true
});
return ProgressNotifier;
}());
var TaskProgressNotifier = /** @class */ (function (_super) {
__extends(TaskProgressNotifier, _super);
function TaskProgressNotifier(manager) {
var _this = _super.call(this) || this;
_this.manager = manager;
return _this;
}
TaskProgressNotifier.prototype.finishOne = function () {
this.manager.finishTask();
};
TaskProgressNotifier.prototype.startOne = function (desc, subparts) {
return this.manager.startTask(desc, subparts);
};
TaskProgressNotifier.prototype.updateDescription = function (desc) {
this.manager.subTaskDescription = desc;
this.manager.updateScope();
};
TaskProgressNotifier.prototype.setTotal = function (total) {
this.manager.totalTasks = total;
};
TaskProgressNotifier.prototype.addMessage = function (severity, message, priority) {
if (priority === void 0) { priority = false; }
this.manager.addMessage(severity, message, priority);
};
TaskProgressNotifier.prototype.fatalError = function (message) {
this.manager.fatalError(message);
};
Object.defineProperty(TaskProgressNotifier.prototype, "hasErrors", {
get: function () {
return this.manager.hasErrors;
},
enumerable: true,
configurable: true
});
return TaskProgressNotifier;
}(ProgressNotifier));
var SubTaskProgressNotifier = /** @class */ (function (_super) {
__extends(SubTaskProgressNotifier, _super);
function SubTaskProgressNotifier(manager) {
var _this = _super.call(this) || this;
_this.manager = manager;
return _this;
}
SubTaskProgressNotifier.prototype.finishOne = function () {
this.manager.updateSubtask();
};
SubTaskProgressNotifier.prototype.startOne = function (desc, subparts) {
this.manager.subSubTaskDescription = desc;
return new ProgressNotifier();
};
SubTaskProgressNotifier.prototype.setTotal = function (total) {
this.manager.subTaskTotal = total;
};
SubTaskProgressNotifier.prototype.addMessage = function (severity, message, priority) {
if (priority === void 0) { priority = false; }
this.manager.addMessage(severity, message, priority);
};
SubTaskProgressNotifier.prototype.fatalError = function (message) {
this.manager.fatalError(message);
};
SubTaskProgressNotifier.prototype.updateDescription = function (desc) {
this.manager.subSubTaskDescription = desc;
this.manager.updateScope();
};
return SubTaskProgressNotifier;
}(ProgressNotifier));
(function (MessageSeverity) {
MessageSeverity[MessageSeverity["Info"] = 0] = "Info";
MessageSeverity[MessageSeverity["Warn"] = 1] = "Warn";
MessageSeverity[MessageSeverity["Error"] = 2] = "Error";
MessageSeverity[MessageSeverity["Success"] = 3] = "Success";
})(exports.MessageSeverity || (exports.MessageSeverity = {}));
(function (UISeverity) {
UISeverity["Info"] = "info";
UISeverity["Warn"] = "warn";
UISeverity["Error"] = "error";
UISeverity["Success"] = "success";
})(exports.UISeverity || (exports.UISeverity = {}));
var OperationMessage = /** @class */ (function () {
function OperationMessage(severity, message) {
this.message = message;
this.severity = severity;
}
Object.defineProperty(OperationMessage.prototype, "iconClass", {
get: function () {
switch (this.severity) {
case exports.MessageSeverity.Info:
return 'ion-information-circled positive';
case exports.MessageSeverity.Warn:
return 'ion-alert-circled energized';
case exports.MessageSeverity.Error:
return 'ion-minus-circled assertive';
case exports.MessageSeverity.Success:
return 'ion-checkmark-circled balanced';
}
},
enumerable: true,
configurable: true
});
return OperationMessage;
}());
var ProgressManager = /** @class */ (function () {
function ProgressManager(totalTasks, $scope) {
this.totalTasks = totalTasks;
this.finishedTasks = 0;
this.subTaskDescription = null;
this.subSubTaskDescription = null;
this.subTaskTotal = 0;
this.subTaskFinished = 0;
this.subTaskInProgress = false;
this.messages = [];
this.errorState = false;
//If we need to autoupdate some bindings on a scope
//by triggering a digest cycle
this.scope = $scope;
}
ProgressManager.prototype.clear = function () {
this.finishedTasks = 0;
this.subTaskDescription = null;
this.subTaskTotal = 0;
this.subTaskFinished = 0;
this.subTaskInProgress = false;
this.priorityMessage = undefined;
this.messages = [];
this.errorState = false;
};
ProgressManager.prototype.getNotifier = function () {
return new TaskProgressNotifier(this);
};
ProgressManager.prototype.startTask = function (desc, totalParts) {
if (this.errorState) {
return null;
}
this.subTaskTotal = totalParts;
this.subTaskFinished = 0;
this.subTaskDescription = desc;
this.subTaskInProgress = true;
this.updateScope();
return new SubTaskProgressNotifier(this);
};
ProgressManager.prototype.finishTask = function () {
if (this.errorState) {
return;
}
if (this.subTaskInProgress) {
this.subTaskInProgress = false;
this.subTaskTotal = 0;
this.subSubTaskDescription = null;
}
if (this.finishedTasks < this.totalTasks) {
this.finishedTasks += 1;
}
this.updateScope();
};
ProgressManager.prototype.addMessage = function (severity, message, priority) {
if (priority === void 0) { priority = false; }
if (priority) {
this.priorityMessage = new OperationMessage(severity, message);
}
else {
this.messages.push(new OperationMessage(severity, message));
}
};
ProgressManager.prototype.fatalError = function (message) {
this.subTaskInProgress = false;
this.finishedTasks = this.totalTasks;
this.errorState = true;
this.addMessage(exports.MessageSeverity.Error, message);
this.updateScope();
};
ProgressManager.prototype.updateSubtask = function () {
if (this.errorState) {
return;
}
if (this.subTaskFinished < this.subTaskTotal) {
this.subTaskFinished += 1;
this.subTaskInProgress = true;
}
this.updateScope();
};
Object.defineProperty(ProgressManager.prototype, "hasErrors", {
get: function () {
for (var _i = 0, _a = this.messages; _i < _a.length; _i++) {
var msg = _a[_i];
if (msg.severity === exports.MessageSeverity.Error) {
return true;
}
}
return false;
},
enumerable: true,
configurable: true
});
ProgressManager.prototype.updateScope = function () {
if (this.scope) {
this.scope.$applyAsync();
}
};
return ProgressManager;
}());
var ControllerBase = /** @class */ (function (_super) {
__extends(ControllerBase, _super);
function ControllerBase(name, $injector, $scope, options) {
if (options === void 0) { options = { manualInitCleanup: false }; }
var _this = _super.call(this, name, $injector) || this;
_this.$scope = $scope;
_this.$ionicPopup = $injector.get("$ionicPopup");
_this.$ionicHistory = $injector.get("$ionicHistory");
_this.$ionicLoading = $injector.get("$ionicLoading");
_this.$ionicModal = $injector.get("$ionicModal");
_this.$cordovaInAppBrowser = $injector.get("$cordovaInAppBrowser");
_this.net = $injector.get('NetService');
_this.error = null;
_this.currentModal = null;
var that = _this;
/*
* For generally good practice as well as for testability we need to know when the controller
* has been properly initialized. So we have an initialized promise that can be awaited to
* make sure the controller has been properly initialized.
*
* NB the promise executor runs synchronously so there is no race condition here
* setting resolve and reject handler and using them in the $scope.on clause below. If a subclass
* wants to manually initialize itself, it needs to assign a promise to this.initialized in the
* constructor.
*/
_this.initialized = null;
if (options.manualInitCleanup === false) {
var resolveInitialized_1;
var rejectInitialized_1;
_this.initialized = new Promise(function (resolve, reject) {
resolveInitialized_1 = resolve;
rejectInitialized_1 = reject;
});
$scope.$on('$ionicView.beforeEnter', function (ev) {
return __awaiter(this, void 0, void 0, function () {
var err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
that.error = null;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, that.initialize()];
case 2:
_a.sent();
resolveInitialized_1();
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
rejectInitialized_1(err_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
});
$scope.$on('$ionicView.beforeLeave', function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, that.cleanup()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
});
}
return _this;
}
ControllerBase.prototype.showLoading = function (message, autoCloseOnTransition) {
if (autoCloseOnTransition === void 0) { autoCloseOnTransition = false; }
return this.showLoadingEx({ template: message, hideOnStateChange: autoCloseOnTransition });
};
ControllerBase.prototype.showLoadingEx = function (details) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicLoading.show(details).then(function () {
resolve();
}).catch(function (err) { return resolve(); });
})];
});
});
};
ControllerBase.prototype.hideLoading = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicLoading.hide().then(function () {
resolve();
}).catch(function (err) { return resolve(); });
})];
});
});
};
//FIXME: We may want to eliminate this function
ControllerBase.prototype.showIsolatedModal = function (modalController) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.currentModal = modalController;
return [4 /*yield*/, modalController.show()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
ControllerBase.prototype.hideIsolatedModal = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.currentModal === null) {
throw new InvalidOperationError('Hiding modal when there is no modal shown.');
}
return [4 /*yield*/, this.currentModal.hide()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
ControllerBase.prototype.openExternal = function (url) {
var browserOptions = {
location: 'yes',
clearcache: 'yes',
toolbar: 'yes'
};
var that = this;
return new Promise(function (resolve, reject) {
that.$cordovaInAppBrowser.open(url, '_blank', browserOptions).then(function () {
resolve();
}).catch(function (err) {
reject(err);
});
});
};
//Deprecated, we should be using isolated modals now
ControllerBase.prototype.showModal = function (templateURL) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicModal.fromTemplateUrl(templateURL, { scope: that.$scope, animation: 'slide-in-up' }).then(function (modal) {
that.currentModal = modal;
modal.show().then(function () {
resolve();
});
});
})];
});
});
};
//Deprecated, we should be using isolated modals now
ControllerBase.prototype.hideModal = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
if (this.currentModal === null) {
throw new InvalidOperationError('Hiding modal when there is no modal shown.');
}
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.currentModal.hide().then(function () {
that.currentModal.remove();
that.currentModal = null;
resolve();
});
})];
});
});
};
ControllerBase.prototype.setError = function (message) {
this.error = message;
if (!this.$scope.$$phase) {
this.$scope.$apply();
}
};
ControllerBase.prototype.isOnline = function () {
return this.net.isOnline();
};
ControllerBase.prototype.initialize = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
ControllerBase.prototype.cleanup = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
ControllerBase.prototype.leaveFromError = function (message, title) {
return __awaiter(this, void 0, void 0, function () {
var err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.hideLoading()];
case 1:
_a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, this.hideModal()];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
err_2 = _a.sent();
return [3 /*break*/, 5];
case 5:
if (!title) {
title = "Fatal Error";
}
return [4 /*yield*/, this.alert(title, message)];
case 6:
_a.sent();
this.$ionicHistory.goBack();
return [2 /*return*/];
}
});
});
};
ControllerBase.prototype.confirm = function (title, message, severity) {
if (severity === void 0) { severity = exports.UISeverity.Info; }
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicPopup.confirm({
title: title,
template: message,
cssClass: severity + '-popup',
buttons: [{
text: "Cancel",
type: "button-assertive",
onTap: function (e) { return false; }
}, {
text: "Okay",
type: "button-balanced",
onTap: function (e) { return true; }
}]
}).then(function (res) {
resolve(res);
});
})];
});
});
};
ControllerBase.prototype.alert = function (title, message, severity) {
if (severity === void 0) { severity = exports.UISeverity.Info; }
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicPopup.alert({
title: title,
cssClass: severity + '-popup',
template: message
}).then(function () {
resolve();
});
})];
});
});
};
return ControllerBase;
}(ObjectBase));
/**
* ComponentBase should be used to create components
* that can be invoked dynamically by name rather than having
* to know the component's name at compile time.
*/
var ComponentBase = /** @class */ (function (_super) {
__extends(ComponentBase, _super);
function ComponentBase(name, $injector, $scope) {
var _this = _super.call(this, name, $injector, $scope, { manualInitCleanup: true }) || this;
var that = _this;
_this.initialized = new Promise(function (resolve, reject) {
that.resolve = resolve;
that.reject = reject;
});
return _this;
}
ComponentBase.prototype.$onInit = function () {
return __awaiter(this, void 0, void 0, function () {
var initialized, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.initialize()];
case 1:
initialized = _a.sent();
if (this.resolve) {
this.resolve();
}
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
this.log_error("Unable to initialize component; " + error_1);
if (this.reject) {
this.reject();
}
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
ComponentBase.prototype.$onDestroy = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.cleanup();
return [2 /*return*/];
});
});
};
ComponentBase.prototype.initialize = function () {
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); });
};
ComponentBase.prototype.cleanup = function () {
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); });
};
return ComponentBase;
}(ControllerBase));
/**
* CustomModalBase should be used to create modal controllers
* that can be invoked dynamically by name rather than having
* to know the modal's name at compile time.
*/
var CustomModalBase = /** @class */ (function (_super) {
__extends(CustomModalBase, _super);
function CustomModalBase(name, $injector, $scope) {
var _this = _super.call(this, name, $injector, $scope, { manualInitCleanup: true }) || this;
_this.modal = null;
return _this;
}
//We need initialize to be accessible publically since it will be called by ModalBase
//when we are created
CustomModalBase.prototype.initialize = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
//We need initialize to be accessible publically since it will be called by ModalBase
//when we are created
CustomModalBase.prototype.cleanup = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
CustomModalBase.prototype.hide = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
if (!this.modal) {
this.log_error("Custom modal improperly constructed with no modal member");
return [2 /*return*/];
}
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.modal.hide().then(resolve);
})];
});
});
};
CustomModalBase.prototype.remove = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
if (!this.modal) {
this.log_error("Custom modal improperly constructed with no modal member");
return [2 /*return*/];
}
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.modal.remove().then(resolve);
})];
});
});
};
return CustomModalBase;
}(ControllerBase));
var ModalBase = /** @class */ (function (_super) {
__extends(ModalBase, _super);
function ModalBase(name, templateURL, $injector, options) {
var _this = this;
var $rootScope = $injector.get("$rootScope");
var $scope = $rootScope.$new();
_this = _super.call(this, name, $injector, $scope, { manualInitCleanup: true }) || this;
_this.modal = null;
_this.customController = null;
_this.templateURL = templateURL;
_this.options = options;
//Normally we expect users to subclass ModalBase in order to create custom controllers.
//However if we need to dynamically create new controllers based on a string name like in the
//gateway page, we need a way to find the appropriate controller programmatically using angular's
//$injector.
//
//If we should use a custom controller, create that and attach it to our scope instead of ourselves
if (options.customController != null) {
var controllerService = $injector.get("$controller");
_this.customController = controllerService(options.customController, { $scope: $scope });
if (!(_this.customController instanceof CustomModalBase)) {
_this.log_error("Attempted to create a modal with a custom controller that did not inherit from CustomModalBase", options.customController);
throw new ArgumentError("Invalid controller type specified: " + options.customController);
}
$scope.vm = _this.customController;
}
else {
$scope.vm = _this; //Inject ourselves as vm so we can access our data from the template
}
_this.initialized = _this.createModal();
var that = _this;
$scope.$on('modal.removed', function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(that.customController != null)) return [3 /*break*/, 2];
return [4 /*yield*/, that.customController.cleanup()];
case 1:
_a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, that.cleanup()];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
});
return _this;
}
ModalBase.prototype.createModal = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var that;
return __generator(this, function (_a) {
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.$ionicModal.fromTemplateUrl(_this.templateURL, {
scope: _this.$scope,
animation: _this.options.animation,
backdropClickToClose: _this.options.backdropClickToClose,
hardwareBackButtonClose: _this.options.hardwareBackButtonClose
}).then(function (modal) {
that.modal = modal;
if (that.customController) {
that.customController.modal = modal;
}
resolve();
}).catch(function (err) {
reject(err);
});
})];
});
});
};
ModalBase.prototype.show = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.initialized];
case 1:
_a.sent();
that = this;
if (this.modal.isShown()) {
this.log_error("Show called multiple times");
return [2 /*return*/];
}
return [2 /*return*/, new Promise(function (resolve, reject) {
that.modal.show().then(function () {
return __awaiter(this, void 0, void 0, function () {
var err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
if (!(that.customController != null)) return [3 /*break*/, 2];
return [4 /*yield*/, that.customController.initialize()];
case 1:
_a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, that.initialize()];
case 3:
_a.sent();
_a.label = 4;
case 4:
resolve();
return [3 /*break*/, 6];
case 5:
err_1 = _a.sent();
reject(err_1);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
});
})];
}
});
});
};
ModalBase.prototype.hide = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.initialized];
case 1:
_a.sent();
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.modal.hide().then(resolve);
})];
}
});
});
};
ModalBase.prototype.remove = function () {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.initialized];
case 1:
_a.sent();
that = this;
return [2 /*return*/, new Promise(function (resolve, reject) {
that.modal.remove().then(resolve);
})];
}
});
});
};
return ModalBase;
}(ControllerBase));
var DataGatheringModal = /** @class */ (function (_super) {
__extends(DataGatheringModal, _super);
function DataGatheringModal($injector, name, templateURL) {
var _this = _super.call(this, name, templateURL, $injector, { animation: 'slide-in-up', backdropClickToClose: false, hardwareBackButtonClose: false }) || this;
_this.deferredReject = null;
_this.deferredResolve = null;
_this.args = null;
return _this;
}
DataGatheringModal.prototype.launch = function (args) {
return __awaiter(this, void 0, void 0, function () {
var that;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.args = args;
return [4 /*yield*/, this.show()]