UNPKG

@iotile/iotile-common

Version:

Common utilities for IoTile Packages and Applications

337 lines 15.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { 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]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (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()); }); }; var __generator = (this && this.__generator) || function (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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var all_base_1 = require("./all-base"); var app_errors_1 = require("../app-errors"); var progress_1 = require("../progress"); 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 app_errors_1.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 app_errors_1.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 = progress_1.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 = progress_1.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; }(all_base_1.ObjectBase)); exports.ControllerBase = ControllerBase; //# sourceMappingURL=controller-base.js.map