UNPKG

@egova/flagwind-core

Version:
1,497 lines (1,496 loc) 213 kB
/*! * flagwind-core v1.1.3 * * Authors: * jason <jasonsoop@gmail.com> * * Licensed under the MIT License. * Copyright (C) 2010-2020 Flagwind Inc. All rights reserved. */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global.flagwind = {}))); }(this, (function (exports) { 'use strict'; var __extends = (this && this.__extends) || (function () { 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); }; 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) { 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()); }); }; 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 = 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 }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { 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; }; var flagwind; (function (flagwind) { /** * 应用程序类,负责整个应用的启动和退出。 * @static * @class * @version 1.0.0 */ var Application = /** @class */ (function () { function Application() { } Object.defineProperty(Application, "eventProvider", { /** * 获取一个事件提供程序实例。 * @private * @property * @returns IEventProvider */ get: function () { if (!this._eventProvider) { this._eventProvider = new flagwind.EventProvider(this); } return this._eventProvider; }, enumerable: true, configurable: true }); Object.defineProperty(Application, "isStarted", { /** * 获取一个布尔值,表示当前应用是否启动完成。 * @static * @property * @returns boolean */ get: function () { return this._isStarted; }, enumerable: true, configurable: true }); Object.defineProperty(Application, "context", { /** * 获取应用程序上下文实例。 * @static * @property * @returns ApplicationContextBase */ get: function () { return this._context; }, enumerable: true, configurable: true }); /** * 启动应用程序。 * @static * @param {ApplicationContextBase} context 应用程序上下文实例。 * @param {Array<string>} args 启动参数。 * @returns void */ Application.start = function (context, args) { var _this = this; if (!context) { throw new flagwind.ArgumentException("context"); } if (this._isStarted) { return; } // 激发 "starting" 事件 this.dispatchEvent(new flagwind.ApplicationEventArgs(this.STARTING, context)); try { // 保存应用程序上下文 this._context = context; // 将应用上下文对象注册到默认服务容器中 context.serviceFactory.default.register("applicationContext", context); // 初始化全局模块 this.initializeGlobalModules(context); // 获取工作台对象 var workbench = context.getWorkbench(args); // 如果工作台对象不为空则运行工作台 if (workbench) { // 挂载工作台打开事件 workbench.addListener(workbench.OPENED, function (e) { // 标识应用程序启动完成 _this._isStarted = true; // 激发 "started" 事件 _this.dispatchEvent(new flagwind.ApplicationEventArgs(_this.STARTED, context)); }); // 挂载工作台关闭事件 workbench.addListener(workbench.CLOSED, function (e) { _this.exit(); }); // 启动工作台 workbench.open(args); } } catch (ex) { // 应用无法启动,写入日志 flagwind.Logger.error(this, ex); // 重抛异常 throw ex; } }; /** * 关闭当前应用程序。 * @static * @returns void */ Application.exit = function () { var context = this._context; // 如果上下文对象为空,则表示尚未启动 if (!context) { return; } // 重置启动标记 this._isStarted = false; // 创建取消事件参数 var args = new flagwind.CancelEventArgs(this.EXITING, this); // 激发 "exiting" 事件 this.dispatchEvent(args); // 判断是否取消退出,如果是则退出 if (args.cancel) { return; } // 关闭工作台 if (context.workbench) { context.workbench.close(); } // 卸载全局模块 this.disposeGlobalModules(context); // 释放应用程序上下文 this._context = null; }; /** * 为指定的事件类型注册一个侦听器,以使侦听器能够接收事件通知。 * @summary 如果不再需要某个事件侦听器,可调用 removeListener() 删除它,否则会产生内存问题。 * 由于垃圾回收器不会删除仍包含引用的对象,因此不会从内存中自动删除使用已注册事件侦听器的对象。 * @param {string} type 事件类型。 * @param {Function} 处理事件的侦听器函数。 * @param {any} scope? 侦听函数绑定的 this 对象。 * @param {boolean} once? 是否添加仅回调一次的事件侦听器,如果此参数设为 true 则在第一次回调时就自动移除监听。 * @returns void */ Application.addListener = function (type, listener, scope, once) { this.eventProvider.addListener(type, listener, scope, once); }; /** * 移除侦听器。如果没有注册任何匹配的侦听器,则对此方法的调用没有任何效果。 * @param {string} type 事件类型。 * @param {Function} listener 处理事件的侦听器函数。 * @param {any} scope? 侦听函数绑定的 this 对象。 * @returns void */ Application.removeListener = function (type, listener, scope) { this.eventProvider.removeListener(type, listener, scope); }; /** * 派发一个指定参数的事件。 * @param {EventArgs} eventArgs 事件参数实例。 * @returns void */ Application.dispatchEvent = function (args) { this.eventProvider.dispatchEvent(args); }; /** * 初始化全局模块。 * @private * @static * @param {ApplicationContextBase} context * @returns void */ Application.initializeGlobalModules = function (context) { context.modules.forEach(function (module, modules) { if (module) { module.initialize(context); } }); }; /** * 卸载全局模块。 * @private * @static * @param {ApplicationContextBase} context * @returns void */ Application.disposeGlobalModules = function (context) { context.modules.forEach(function (module, modules) { if (module) { module.dispose(); } }); }; Application._isStarted = false; // 标识应用程序是否启动完成 Application._context = null; // 应用程序上下文实例 /** * 当应用程序启动时产生的事件。 * @event ApplicationEventArgs */ Application.STARTING = "starting"; /** * 当应用程序启动后产生的事件。 * @event ApplicationEventArgs */ Application.STARTED = "started"; /** * 当应用程序即将退出时产生的事件。 * @event CancelEventArgs */ Application.EXITING = "exiting"; return Application; }()); flagwind.Application = Application; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { var CREDENTIALL_SYMBOL = "__credential__"; /** * 表示实现该抽象类的是一个应用程序上下文。 * @class * @version 1.0.0 */ var ApplicationContextBase = /** @class */ (function () { /** * 初始化应用程序上下文的新实例。 * @protected * @param {string} applicationId? 应用程序的唯一代号。 */ function ApplicationContextBase(applicationId) { if (applicationId) { this._applicationId = applicationId; } } Object.defineProperty(ApplicationContextBase.prototype, "applicationId", { /** * 获取或设置当前应用程序唯一代号。 * @summary 注意:本属性一旦被设置则不能被更改。 * @property * @returns string */ get: function () { return this._applicationId; }, set: function (value) { if (!value) { throw new flagwind.ArgumentException("value"); } if (this._applicationId) { throw new flagwind.InvalidOperationException("The ApplicationId has specified already."); } this._applicationId = value.trim(); }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "title", { /** * 获取或设置当前应用程序的标题。 * @property * @returns string */ get: function () { return this._title; }, set: function (value) { this._title = value || ""; }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "serviceFactory", { /** * 获取当前应用程序的服务管理对象。 * @property * @returns IServiceProviderFactory */ get: function () { return flagwind.ServiceProviderFactory.instance; }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "modules", { /** * 获取当前应用程序的模块集合。 * @property * @returns ISet */ get: function () { if (!this._modules) { this._modules = new flagwind.Set(); } return this._modules; }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "credential", { /** * 获取或设置当前用户的安全凭证。 * @property * @returns ICredential */ get: function () { // 如果内存没有凭证,则从 LocalStorage 中获取 if (!this._credential) { this._credential = flagwind.LocalStorage.get(CREDENTIALL_SYMBOL); } return this._credential; }, set: function (value) { this._credential = value; // 将安全凭证存入 LocalStorage 中 flagwind.LocalStorage.set(CREDENTIALL_SYMBOL, value); }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "states", { /** * 获取当前应用的状态字典。 * @property * @returns IMap */ get: function () { if (!this._states) { this._states = new flagwind.Map(); } return this._states; }, enumerable: true, configurable: true }); Object.defineProperty(ApplicationContextBase.prototype, "workbench", { /** * 获取当前应用程序的工作台(主界面)。 * 必须使用 Application 类的 start 方法,启动应用程序后才能使用该属性获取到创建成功的工作台对象。 * @property * @returns IWorkbench */ get: function () { return this._workbench; }, enumerable: true, configurable: true }); /** * 返回当前应用程序的工作台(主界面)。 * @param {Array<string>} args 初始化的参数。 * @returns IWorkbench 返回新建或已创建的工作台对象。 */ ApplicationContextBase.prototype.getWorkbench = function (args) { if (!this._workbench) { // 创建工作台对象 this._workbench = this.createWorkbench(args); } return this._workbench; }; return ApplicationContextBase; }()); flagwind.ApplicationContextBase = ApplicationContextBase; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * EventArgs 类作为创建事件参数的基类,当发生事件时,EventArgs 实例将作为参数传递给事件侦听器。 * @class * @version 1.0.0 */ var EventArgs = /** @class */ (function () { /** * 初始化 EventArgs 类的新实例。 * @constructor * @param {string} type 事件类型。 * @param {any?} data 可选数据。 */ function EventArgs(type, data) { if (!type) { throw new flagwind.ArgumentException(); } this._type = type; this._data = data; } Object.defineProperty(EventArgs.prototype, "type", { /** * 获取一个字符串值,表示事件的类型。 * @property * @returns string */ get: function () { return this._type; }, enumerable: true, configurable: true }); Object.defineProperty(EventArgs.prototype, "source", { /** * 获取或设置事件源对象。 * @property * @returns any */ get: function () { return this._source; }, set: function (value) { if (!value) { throw new flagwind.ArgumentException(); } this._source = value; }, enumerable: true, configurable: true }); Object.defineProperty(EventArgs.prototype, "data", { /** * 获取或设置与事件关联的可选数据。 * @property * @returns any */ get: function () { return this._data; }, set: function (value) { this._data = value; }, enumerable: true, configurable: true }); return EventArgs; }()); flagwind.EventArgs = EventArgs; })(flagwind || (flagwind = {})); /// <reference path="../events/EventArgs.ts" /> var flagwind; (function (flagwind) { /** * 应用程序事件参数类。 * @class * @version 1.0.0 */ var ApplicationEventArgs = /** @class */ (function (_super) { __extends(ApplicationEventArgs, _super); /** * 初始化应用程序事件参数类的新实例。 * @param {string} type 事件类型。 * @param {ApplicationContextBase} context 应用程序上下文实例。 */ function ApplicationEventArgs(type, context) { var _this = _super.call(this, type) || this; _this.context = context; return _this; } return ApplicationEventArgs; }(flagwind.EventArgs)); flagwind.ApplicationEventArgs = ApplicationEventArgs; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * 表示一个事件项。 * @internal * @class * @version 1.0.0 */ var EventEntry = /** @class */ (function () { /** * 初始化事件项的新实例。 * @param {string} type 事件类型。 * @param {Function} listener 侦听函数。 * @param {any} scope 侦听函数中的 this 对象。 * @param {boolean} scope 是否为仅回掉一次。 */ function EventEntry(type, listener, scope, once) { this.type = type; this.listener = listener; this.scope = scope; this.once = once; } return EventEntry; }()); /** * 事件提供程序类。 * @description 用于添加或删除事件侦听器的方法,检查是否已注册特定类型的事件侦听器,并调度事件。 * @class * @version 1.0.0 */ var EventProvider = /** @class */ (function () { /** * 初始化事件提供程序的新实例。 * @param {any} source? 事件源实例。 */ function EventProvider(source) { // 保存事件源对象 this._source = source || this; // 初始化事件字典 this._events = new flagwind.Map(); } /** * 为指定的事件类型注册一个侦听器,以使侦听器能够接收事件通知。 * @summary 如果不再需要某个事件侦听器,可调用 removeListener() 删除它,否则会产生内存问题。 * 由于垃圾回收器不会删除仍包含引用的对象,因此不会从内存中自动删除使用已注册事件侦听器的对象。 * @param {string} type 事件类型。 * @param {Function} 处理事件的侦听器函数。 * @param {any} scope? 侦听函数绑定的 this 对象。 * @param {boolean} once? 是否添加仅回调一次的事件侦听器,如果此参数设为 true 则在第一次回调时就自动移除监听。 * @returns void+ */ EventProvider.prototype.addListener = function (type, listener, scope, once) { if (scope === void 0) { scope = this; } if (once === void 0) { once = false; } if (!type || !listener) { throw new flagwind.ArgumentException(); } var entries = this._events.get(type); if (!entries) { entries = new Array(); this._events.set(type, entries); } for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) { var entry = entries_1[_i]; // 防止添加重复的侦听函数 if (entry.listener === listener && entry.scope === scope) { return; } } entries.push(new EventEntry(type, listener, scope, once)); }; /** * 移除侦听器。如果没有注册任何匹配的侦听器,则对此方法的调用没有任何效果。 * @param {string} type 事件类型。 * @param {Function} listener 处理事件的侦听器函数。 * @param {any} scope? 侦听函数绑定的 this 对象。 * @returns void */ EventProvider.prototype.removeListener = function (type, listener, scope) { if (scope === void 0) { scope = this; } if (!type || !listener) { throw new flagwind.ArgumentException(); } var entries = this._events.get(type); if (!entries) { return; } for (var i = 0, len = entries.length; i < len; i++) { var entry = entries[i]; if (entry.listener === listener && entry.scope === scope) { entries.splice(i, 1); break; } } // 如果事件项为空,则需要释放资源 if (entries.length === 0) { this._events.delete(type); } }; /** * 检查是否为特定事件类型注册了侦听器。 * @param {string} type 事件类型。 * @returns boolean 如果指定类型的侦听器已注册,则值为 true;否则,值为 false。 */ EventProvider.prototype.hasListener = function (type) { var entries = this._events.get(type); return !!entries && entries.length > 0; }; EventProvider.prototype.dispatchEvent = function () { var params = arguments, args; switch (params.length) { // 重载匹配: // dispatchEvent(args: EventArgs): void; // dispatchEvent(type: string): void; case 1: { if (params[0] instanceof flagwind.EventArgs) { // 参数匹配: args: EventArgs args = params[0]; } else if (flagwind.Type.isString(params[0])) { // 参数匹配: type: string args = new flagwind.EventArgs(params[0]); } break; } // 重载匹配: // dispatchEvent(type: string, data: any): void; case 2: { // 参数匹配: type: string, data: any args = new flagwind.EventArgs(params[0], params[1]); break; } } // 设置事件源 args.source = this._source; // 根据事件类型获取所有事件项 var entries = this._events.get(args.type); if (!entries || entries.length === 0) { return; } // 临时数组用于保存只回掉一次的事件项 var onces = new Array(); for (var _i = 0, entries_2 = entries; _i < entries_2.length; _i++) { var entry = entries_2[_i]; entry.listener.call(entry.scope, args); if (entry.once) { onces.push(entry); } } // 清除所有只回调一次的事件项 while (onces.length) { var entry = onces.pop(); this.removeListener(entry.type, entry.listener, entry.scope); } }; return EventProvider; }()); flagwind.EventProvider = EventProvider; })(flagwind || (flagwind = {})); /// <reference path="../events/EventProvider.ts" /> var flagwind; (function (flagwind) { /** * 提供工作台的基本封装,建议自定义工作台从此类继承。 * @abstract * @class * @version 1.0.0 */ var WorkbenchBase = /** @class */ (function (_super) { __extends(WorkbenchBase, _super); /** * 初始化工作台的新实例。 * @protected * @param {ApplicationContextBase} applicationContext+ */ function WorkbenchBase(applicationContext) { var _this = _super.call(this) || this; /** * 当工作台正在打开时产生的事件。 * @event EventArgs */ _this.OPENING = "opening"; /** * 当工作台被打开后产生的事件。 * @event EventArgs */ _this.OPENED = "opened"; /** * 当工作台正在取消激活时产生的事件。 * @event EventArgs */ _this.DEACTIVATING = "deactivating"; /** * 当工作台取消激活后产生的事件。 * @event EventArgs */ _this.DEACTIVATED = "deactivated"; /** * 当工作台正在激活时产生的事件。 * @event EventArgs */ _this.ACTIVATING = "activating"; /** * 当工作台正在关闭时产生的事件。 * @event CancelEventArgs */ _this.CLOSING = "closing"; /** * 当工作台被关闭后产生的事件。 * @event EventArgs */ _this.CLOSED = "closed"; /** * 当工作台标题被更改后产生的事件。 * @event EventArgs */ _this.TITLE_CHANGED = "title_changed"; if (!applicationContext) { throw new flagwind.ArgumentException(); } _this._status = 0 /* closed */; _this._title = applicationContext.title; _this._applicationContext = applicationContext; return _this; } Object.defineProperty(WorkbenchBase.prototype, "status", { /** * 获取工作台的当前状态。 * @property * @returns WorkbenchStatus */ get: function () { return this._status; }, enumerable: true, configurable: true }); Object.defineProperty(WorkbenchBase.prototype, "title", { /** * 获取或设置工作台的标题。 * @property * @returns string */ get: function () { return this._title; }, set: function (value) { // 如果与之前的标题相等,则不处理 if (this._title === value) { return; } this._title = value ? value : ""; // 通知标题被更改 this.onTitleChanged(); }, enumerable: true, configurable: true }); Object.defineProperty(WorkbenchBase.prototype, "applicationContext", { /** * 获取工作台所属的应用程序上下文实例。 * @property * @returns ApplicationContextBase */ get: function () { return this._applicationContext; }, enumerable: true, configurable: true }); /** * 打开工作台。 * @async * @param {Array<string>} args * @returns void */ WorkbenchBase.prototype.open = function (args) { return __awaiter(this, void 0, void 0, function () { var ex_1; return __generator(this, function (_a) { switch (_a.label) { case 0: // 如果工作台已经启动过则不处理 if (this._status !== 0 /* closed */) { return [2 /*return*/]; } try { // 通知工作台正在打开中 this.onOpening(); } catch (ex) { // 注意:可能因为预打开事件处理程序或工作台构建过程出错,都必须重置工作台状态为"closed" this._status = 0 /* closed */; // 重抛异常,导致后续的关闭代码不能继续 throw ex; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); // 调用虚拟方法以执行实际启动的操作 return [4 /*yield*/, this.onOpen(args)]; case 2: // 调用虚拟方法以执行实际启动的操作 _a.sent(); return [3 /*break*/, 4]; case 3: ex_1 = _a.sent(); // 注意:如果在实际启动操作中,子类可能已经通过 onOpened 方法设置了工作台状态为运行,则无需再重置工作台状态; // 否则必须重置工作台状态为"closed" if (this._status === 1 /* opening */) { this._status = 0 /* closed */; } // 重抛异常,导致后续的关闭代码不能继续,故而上面代码重置了工作台状态 throw ex_1; case 4: if (this._status === 1 /* opening */) { // 通知工作台打开完成 this.onOpened(); } return [2 /*return*/]; } }); }); }; /** * 关闭工作台。 * @async * @returns boolean */ WorkbenchBase.prototype.close = function () { return __awaiter(this, void 0, void 0, function () { var originalStatus, args, ex_2; return __generator(this, function (_a) { switch (_a.label) { case 0: originalStatus = this._status; // 如果工作台正在关闭或已经关闭,则直接退出 if (originalStatus === 6 /* closing */ || originalStatus === 0 /* closed */) { return [2 /*return*/, false]; } if (originalStatus === 1 /* opening */) { throw new flagwind.InvalidOperationException(); } args = new flagwind.CancelEventArgs(this.CLOSING); try { this.onClosing(args); } catch (ex) { // 注意:由于事件处理程序出错,必须还原工作台状态 this._status = originalStatus; // 重抛异常,导致后续的关闭代码不能继续,故而上面代码重置了工作台状态 throw ex; } // 如果事件处理程序要取消后续的关闭操作,则重置工作台状态 if (args.cancel === true) { // 还原工作台状态 this._status = originalStatus; // 因为取消关闭,所以退出后续关闭操作 return [2 /*return*/, false]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); // 调用虚拟方法以进行实际的关闭操作 return [4 /*yield*/, this.onClose()]; case 2: // 调用虚拟方法以进行实际的关闭操作 _a.sent(); return [3 /*break*/, 4]; case 3: ex_2 = _a.sent(); // 注意:如果在实际关闭操作中,子类可能已经通过 onClosed 方法设置了工作台状态为关闭,则无需再重置工作台状态; // 否则必须还原工作台状态 if (this._status === 6 /* closing */) { this._status = originalStatus; } // 重抛异常,导致后续的关闭代码不能继续,故而上面代码重置了工作台状态 throw ex_2; case 4: if (this._status !== 0 /* closed */) { // 通知工作台关闭完成 this.onClosed(); } // 返回成功 return [2 /*return*/, true]; } }); }); }; /** * 取消激活工作台。 * @returns void */ WorkbenchBase.prototype.deactivate = function () { // 保存原来的状态 var originalStatus = this._status; // 如果工作台不是在运行中,则直接退出 if (originalStatus !== 2 /* running */) { return; } try { // 通知工作台正在失去焦点中 this.onDeactivateing(); } catch (ex) { // 还原状态 this._status = originalStatus; // 重抛异常,导致后续的代码不能继续 throw ex; } try { // 调用虚拟方法以执行实际失去焦点操作 this.onDeactivate(); } catch (ex) { // 注意:如果在实际取消激活操作中,子类可能已经通过 onDeactivated 方法设置了工作台状态为已经取消激活,则无需再重置工作台状态; // 否则必须重置工作台状态为原来的状态 if (this._status === 3 /* deactivating */) { // 还原状态 this._status = originalStatus; } // 重抛异常,导致后续的关闭代码不能继续,故而上面代码重置了工作台状态 throw ex; } if (this._status !== 4 /* deactivated */) { // 通知工作台取消激活完成 this.onDeactivated(); } }; /** * 激活工作台。 * @returns void */ WorkbenchBase.prototype.activate = function () { // 保存原来的状态 var originalStatus = this._status; // 如果工作台不是在取消激活中,则直接退出 if (originalStatus !== 4 /* deactivated */) { return; } try { // 通知工作台正在获得焦点中 this.onActivating(); } catch (ex) { // 还原状态 this._status = originalStatus; // 重抛异常,导致后续的代码不能继续 throw ex; } try { // 调用虚拟方法以执行实际获得焦点操作 this.onActivate(); } catch (ex) { // 注意:如果在实际取消激活操作中,子类可能已经通过 onActivated 方法设置了工作台状态为正在运行,则无需再重置工作台状态; // 否则必须重置工作台状态为原来的状态 if (this._status === 5 /* activating */) { // 还原状态 this._status = originalStatus; } // 重抛异常,导致后续的关闭代码不能继续,故而上面代码重置了工作台状态 throw ex; } if (this._status === 5 /* activating */) { // 通知工作台取消激活完成 this.onActivated(); } }; /** * 当准备打开工作台时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onOpening = function () { // 更改工作台状态为"opening" this._status = 1 /* opening */; // 激发工作台"opening"事件 this.dispatchEvent(this.OPENING); }; /** * 当工作台打开时调用。 * @async * @protected * @virtual * @param {Array<string>} args * @returns void */ WorkbenchBase.prototype.onOpen = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; /** * 当工作台打开完成时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onOpened = function () { // 更改工作台状态为"running" this._status = 2 /* running */; // 激发工作台"opened"事件 this.dispatchEvent(this.OPENED); }; /** * 当准备关闭工作台时调用。 * @protected * @virtual * @param {CancelEventArgs} event */ WorkbenchBase.prototype.onClosing = function (event) { // 设置工作台的状态为"closing" this._status = 6 /* closing */; // 激发工作台"closing"事件 this.dispatchEvent(event); }; /** * 当工作台关闭时调用。 * @async * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onClose = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; /** * 当工作台关闭完成时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onClosed = function () { // 更改工作台状态为"closed" this._status = 0 /* closed */; // 激发工作台"closed"事件 this.dispatchEvent(this.CLOSED); }; /** * 当工作台准备失去焦点时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onDeactivateing = function () { // 设置工作台的状态为"deactivating" this._status = 3 /* deactivating */; // 激发工作台"deactivating"事件 this.dispatchEvent(this.DEACTIVATING); }; /** * 当工作台失去焦点时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onDeactivate = function () { // virtual }; /** * 当工作台失去焦点后调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onDeactivated = function () { // 设置工作台的状态为"deactivated" this._status = 4 /* deactivated */; // 激发工作台"deactivated"事件 this.dispatchEvent(this.DEACTIVATED); }; /** * 当工作台准备获得焦时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onActivating = function () { // 设置工作台的状态为"activating" this._status = 5 /* activating */; // 激发工作台"activating"事件 this.dispatchEvent(this.ACTIVATING); }; /** * 当工作台获得焦时调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onActivate = function () { // virtual }; /** * 当工作台获得焦后调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onActivated = function () { // 设置工作台的状态为"running" this._status = 2 /* running */; }; /** * 当工作台标题更改后调用。 * @protected * @virtual * @returns void */ WorkbenchBase.prototype.onTitleChanged = function () { // 激发工作台"titleChanged"事件 this.dispatchEvent(this.TITLE_CHANGED); }; return WorkbenchBase; }(flagwind.EventProvider)); flagwind.WorkbenchBase = WorkbenchBase; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * 表示一条广播信息。 * @class * @version 1.0.0 */ var Broadcast = /** @class */ (function () { /** * 初始化一个广播新实例。 * @property * @param {string} uri 广播描述符。 * @param {Map<string, any>} extras? 携带的数据。 */ function Broadcast(uri, extras) { if (!uri || !flagwind.RegexUtils.uri.test(uri)) { throw new flagwind.ArgumentException(); } this._uri = uri; this._scheme = RegExp.$1; this._action = RegExp.$2; this._extras = extras || new flagwind.Map(); } Object.defineProperty(Broadcast.prototype, "scheme", { /** * 获取广播的方案。 * @property * @returns string */ get: function () { return this._scheme; }, enumerable: true, configurable: true }); Object.defineProperty(Broadcast.prototype, "action", { /** * 获取广播的动作。 * @property * @returns string */ get: function () { return this._action; }, enumerable: true, configurable: true }); Object.defineProperty(Broadcast.prototype, "uri", { /** * 获取广播的标识符。 * @property * @returns string */ get: function () { return this._uri; }, enumerable: true, configurable: true }); Object.defineProperty(Broadcast.prototype, "extras", { /** * 获取广播携带的数据。 * @property * @returns Map */ get: function () { return this._extras; }, enumerable: true, configurable: true }); return Broadcast; }()); flagwind.Broadcast = Broadcast; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * 广播在传播过程中使用的上下文参数。 * @class * @version 1.0.0 */ var BroadcastContext = /** @class */ (function () { /** * 初始化广播上下文的新实例。 * @param {BroadcastManager} manager 广播管理器。 * @param {Broadcast} broadcast 广播实例。 */ function BroadcastContext(manager, broadcast) { if (!manager || !broadcast) { throw new flagwind.ArgumentException(); } this._manager = manager; this._scheme = broadcast.scheme; this._action = broadcast.action; this._uri = broadcast.uri; this._extras = broadcast.extras; this._aborted = false; } Object.defineProperty(BroadcastContext.prototype, "manager", { /** * 获取广播所在的广播管理器实例。 * @property * @returns BroadcastManager */ get: function () { return this._manager; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContext.prototype, "scheme", { /** * 获取广播的方案。 * @property * @returns string */ get: function () { return this._scheme; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContext.prototype, "action", { /** * 获取广播的动作。 * @property * @returns string */ get: function () { return this._action; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContext.prototype, "uri", { /** * 获取广播的标识符。 * @property * @returns string */ get: function () { return this._uri; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContext.prototype, "extras", { /** * 获取广播携带的数据。 * @property * @returns Map<string, any> */ get: function () { return this._extras; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContext.prototype, "aborted", { /** * 表示是否阻止广播继续传播。 * @property * @returns boolean */ get: function () { return this._aborted; }, enumerable: true, configurable: true }); /** * 阻止广播在本次传播中继续进行传播。 * @returns void */ BroadcastContext.prototype.abort = function () { this._aborted = true; }; return BroadcastContext; }()); flagwind.BroadcastContext = BroadcastContext; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * 表示一个广播契约。 * @class * @version 1.0.0 */ var BroadcastContract = /** @class */ (function () { function BroadcastContract() { var args = arguments; // 匹配签名: constructor(uri: string) if (args.length === 1) { var uri = args[0]; if (!flagwind.RegexUtils.uri.test(uri)) { throw new flagwind.ArgumentException(); } this._scheme = RegExp.$1; this.actions.add(RegExp.$2); } // 匹配签名: constructor(scheme: string, actions: Array<string>) else { var scheme = args[0], actions = args[1]; if (!scheme || !flagwind.RegexUtils.scheme.test(scheme) || !actions) { throw new flagwind.ArgumentException(); } // 保存协议方案 this._scheme = scheme; // 保存协议动作 for (var _i = 0, actions_1 = actions; _i < actions_1.length; _i++) { var method = actions_1[_i]; this.actions.add(method); } } // 设置默认优先级 this._priority = 0 /* normal */; } Object.defineProperty(BroadcastContract.prototype, "scheme", { /** * 获取广播契约的方案。 * @property * @returns string */ get: function () { return this._scheme; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContract.prototype, "priority", { /** * 获取或设置广播接收时的优先顺序。 * @property * @returns number */ get: function () { return this._priority; }, set: function (value) { this._priority = value; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastContract.prototype, "actions", { /** * 获取广播契约所拥有的动作。 * @property * @returns Set */ get: function () { if (!this._actions) { this._actions = new flagwind.Set(); } return this._actions; }, enumerable: true, configurable: true }); return BroadcastContract; }()); flagwind.BroadcastContract = BroadcastContract; })(flagwind || (flagwind = {})); var flagwind; (function (flagwind) { /** * 提供用于广播注册发布等功能。 * @class * @version 1.0.0 */ var BroadcastManager = /** @class */ (function () { /** * 初始化广播管理器的新实例。 * @param {IBroadcastReceiverProvider} receiverProvider? 广播接收器提供程序。 */ function BroadcastManager(receiverProvider) { this._eventProvider = new flagwind.EventProvider(this); this._receiverProvider = receiverProvider || new flagwind.BroadcastReceiverProvider(); } Object.defineProperty(BroadcastManager.prototype, "eventProvider", { /** * 获取一个事件提供程序。 * @protected * @property * @returns IEventProvider */ get: function () { return this._eventProvider; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastManager.prototype, "receiverProvider", { /** * 获取一个广播接收器提供程序。 * @protected * @property * @returns IBroadcastReceiverProvider */ get: function () { return this._receiverProvider; }, enumerable: true, configurable: true }); Object.defineProperty(BroadcastManager, "instance", { /** * 获取广播管理器的单实例。 * @static * @returns BroadcastManager */ get: function () { if (!this._instance) { this._instance = new BroadcastManager(); } return this._instance; }, enumerable: true, configurable: true }); /** * 是否存在指定契约的uri接收器 * @param uri 广播契约uri */ BroadcastManager.prototype.hasReceiver = function (uri) { return this.receiverProvider.has(uri); }; /** * 基于指定的契约注册一个广播接收程序。 * @param {BroadcastContract} contract 广播契约。 * @param {IBroadcastReceiver} receiver 接收程序。 * @returns void */ BroadcastManager.prototype.register = function (contract, receiver) { // 将协议注册至事件提供程序中 if (!this.eventProvider.hasListener(contract.scheme)) { this.eventProvider.addListener(contract.scheme, this.onReceive, this); } // 将接收程序注册至服务提供程序中 this.receiverProvider.register(contract, receiver); }; /** * 移除指定契约的广播接收器。 * @param {BroadcastContract} contract 广播