ibiz-app.ts 7.5 KB
Newer Older
ibiz's avatar
ibiz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * IBizApp 应用
 * 调用 getInstance() 获取实列
 *
 * @class IBizApp
 */
class IBizApp {

    /**
     * 单列变量声明  
     *
     * @static
     * @type {IBizApp}
     * @memberof IBizApp
     */
    public static iBizApp: IBizApp = null;

    /**
     * 当前窗口所有视图控制器  
     *
     * @type {Array<any>}
     * @memberof IBizApp
     */
    public viewControllers: Array<any> = [];

    /**
     * 父窗口window对象
     *
     * @type {*}
     * @memberof IBizApp
     */
    public parentWindow: any = null;

    /**
     * rxjs 流观察对象
     *
     * @type {Subject<any>}
     * @memberof IBizApp
     */
    public subject: Subject<any> = new rxjs.Subject();

    /**
     * postMessage 流观察对象
     *
     * @type {Subject<any>}
     * @memberof IBizApp
     */
    public postMessage: Subject<any> = new rxjs.Subject();

    /**
     * 主题颜色
     *
     * @type {String}
     * @memberof IBizApp
     */
    public themeClass: String = '';

    /**
     * 字体
     *
     * @type {String}
     * @memberof IBizApp
     */
    public fontFamily: String = '';

    /**
     * app应用功能数据
     *
     * @type {string}
     * @memberof IBizApp
     */
    public appData: string = '';

    /**
     * Creates an instance of IBizApp.
     * 私有构造,拒绝通过 new 创建对象
     * 
     * @memberof IBizApp
     */
    public constructor() {
        const win: any = window;
        win.addEventListener('message', (message: any) => {
            if (message.data && Object.is(message.data.ret, 'OK') && !Object.is(message.data.type, '')) {
                this.firePostMessage(message.data);
            }
        }, false);
    }

    /**
     * 获取 IBizApp 单列对象
     *
     * @static
     * @returns {IBizApp}
     * @memberof IBizApp
     */
    public static getInstance(): IBizApp {
        if (!IBizApp.iBizApp) {
            IBizApp.iBizApp = new IBizApp();
        }
        return IBizApp.iBizApp;
    }

    /**
     * 注册视图控制
     *
     * @param {*} ctrler
     * @memberof IBizApp
     */
    public regSRFController(ctrler: any): void {
        this.viewControllers.push(ctrler);
    }

    /**
     * 注销视图控制器
     *
     * @param {*} ctrler
     * @memberof IBizApp
     */
    public unRegSRFController(ctrler: any): void {
        const id = ctrler.getId();
        const viewUsage = ctrler.getViewUsage();
        const index = this.viewControllers.findIndex((ctrl: any) => Object.is(id, ctrl.getId()) && Object.is(viewUsage, ctrl.getViewUsage()));
        if (index !== -1) {
            this.viewControllers[index] = null;
            this.viewControllers.splice(index, 1);
        }
    }

    /**
     * 注销视图控制器
     *
     * @param {string} id 视图id
     * @param {number} viewUsage 视图使用模式
     * @memberof IBizApp
     */
    public unRegSRFController2(id: string, viewUsage: number): void {
        const index = this.viewControllers.findIndex((ctrl: any) => Object.is(id, ctrl.getId()) && Object.is(viewUsage, ctrl.getViewUsage()));
        if (index !== -1) {
            this.viewControllers[index] = null;
            this.viewControllers.splice(index, 1);
        }
    }

    /**
     * 获取视图控制器
     *
     * @param {string} id 视图id
     * @param {number} viewUsage 视图使用模式
     * @returns {*}
     * @memberof IBizApp
     */
    public getSRFController(id: string, viewUsage: number): any {
        let viewController = null;
        const index = this.viewControllers.findIndex((ctrl: any) => Object.is(id, ctrl.getId()) && Object.is(viewUsage, ctrl.getViewUsage()));
        if (index !== -1) {
            viewController = this.viewControllers[index];
        }
        return viewController;
    }

    /**
     * 获取父视图控制器
     *
     * @param {string} id 视图id
     * @param {number} viewUsage 视图使用模式
     * @returns {*}
     * @memberof IBizApp
     */
    public getParentController(id: string, viewUsage: number): any {
        let viewController = null;
        const index = this.viewControllers.findIndex((ctrl: any) => Object.is(id, ctrl.getId()) && Object.is(viewUsage, ctrl.getViewUsage()));
        if (index !== -1) {
            viewController = this.viewControllers[index - 1];
        }
        return viewController;
    }

    /**
     * 注册父窗口window 对象
     *
     * @param {Window} win
     * @memberof IBizApp
     */
    public regParentWindow(win: Window): void {
        this.parentWindow = win;
    }

    /**
     * 获取父窗口window 对象
     *
     * @returns {Window}
     * @memberof IBizApp
     */
    public getParentWindow(): Window {
        return this.parentWindow;
    }

    /**
     * 订阅刷新视图事件
     *
     * @returns {Subject<any>}
     * @memberof IBizApp
     */
    public onRefreshView(): Subject<any> {
        return this.subject;
    }

    /**
     * 通知视图刷新事件
     *
     * @param {*} data
     * @memberof IBizApp
     */
    public fireRefreshView(data: any = {}): void {
        this.subject.next(data);
    }

    /**
     * 订阅postMessage对象
     *
     * @returns {Subject<any>}
     * @memberof IBizApp
     */
    public onPostMessage(): Subject<any> {
        return this.postMessage;
    }

    /**
     * 处理postMessage对象
     *
     * @param {*} [data={}]
     * @memberof IBizApp
     */
    public firePostMessage(data: any = {}): void {
        this.postMessage.next(data);
    }

    /**
     * 设置主题颜色
     *
     * @param {String} name
     * @memberof IBizApp
     */
    public setThemeClass(name: String): void {
        this.themeClass = name;
        Cookies.set('theme-class', name);
    }

    /**
     * 获取主题颜色
     *
     * @returns {String}
     * @memberof IBizApp
     */
    public getThemeClass(): String {
        if (Object.is(this.themeClass, '')) {
            let name = Cookies.get('theme-class');
            if (name) {
                return name;
            }
            return 'ibiz-default-theme';
        } else {
            return this.themeClass;
        }
    }

    /**
     * 设置字体
     *
     * @param {String} font
     * @memberof IBizApp
     */
    public setFontFamily(font: String): void {
        this.fontFamily = font;
        Cookies.set('font-family', font);
    }

    /**
     * 获取字体
     *
     * @returns {String}
     * @memberof IBizApp
     */
    public getFontFamily(): String {
        if (Object.is(this.fontFamily, '')) {
            let font = Cookies.get('font-family');
            if (font) {
                return font;
            }
            return '';
        } else {
            return this.fontFamily;
        }
    }

    /**
     * 获取应用功能数据
     *
     * @returns {string}
     * @memberof IBizApp
     */
    public getAppData(): string {
        return this.appData;
    }

    /**
     * 设置应用功能数据
     *
     * @param {string} appdata
     * @memberof IBizApp
     */
    public setAppData(appdata: string) {
        this.appData = appdata;
    }
}

// 初始化IBizApp 对象, 挂载在window对象下
(() => {
    let win: any = window;
    if (!win.iBizApp) {
        win.iBizApp = IBizApp.getInstance();
    }
    win.getIBizApp = function () {
        if (!win.iBizApp) {
            win.iBizApp = IBizApp.getInstance();
        }
        return win.iBizApp;
    };

    if (window.opener && window.opener.window) {
        IBizApp.getInstance().regParentWindow(window.opener.window);
    }
})();