UNPKG

cbcore-ts

Version:

CBCore is a library to build web applications using pure Typescript.

173 lines (172 loc) 6.23 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var CBCore_exports = {}; __export(CBCore_exports, { CBCore: () => CBCore }); module.exports = __toCommonJS(CBCore_exports); var import_uicore_ts = require("../../uicore-ts"); var import_CBLanguageService = require("./CBLanguageService"); var import_CBServerClient = require("./CBServerClient"); var import_CBSocketClient = require("./CBSocketClient"); const _CBCore = class _CBCore extends import_uicore_ts.UIObject { constructor() { super(); this.viewCores = []; this._isUserLoggedIn = import_uicore_ts.nil; this._cachedMinimizedChatInquiryIDs = import_uicore_ts.nil; this._socketClient = new import_CBSocketClient.CBSocketClient(this); this._serverClient = new import_CBServerClient.CBServerClient(this); this._functionsToCallForEachSocketClient = []; this._models = []; this.dialogViewShowerClass = import_uicore_ts.nil; if (CBCoreInitializerObject) { import_CBLanguageService.CBLanguageService.useStoredLanguageValues(CBCoreInitializerObject.languageValues); } window.addEventListener("storage", function(event) { if (event.newValue == event.oldValue) { return; } if (event.key == "CBLanguageKey") { this.didSetLanguageKey(); } }.bind(this)); this.didSetLanguageKey(); } static initIfNeededWithViewCore(viewCore) { _CBCore.sharedInstance.viewCores.push(viewCore); } static get sharedInstance() { if (!_CBCore._sharedInstance) { _CBCore._sharedInstance = new _CBCore(); } return _CBCore._sharedInstance; } broadcastMessageInRootViewTree(message) { this.viewCores.everyElement.rootViewController.view.broadcastEventInSubtree(message); } get socketClient() { return this._socketClient; } get serverClient() { return this._serverClient; } set isUserLoggedIn(isUserLoggedIn) { const previousValue = this.isUserLoggedIn; localStorage.setItem("CBIsUserLoggedIn", "" + isUserLoggedIn); this.didSetIsUserLoggedIn(previousValue); } didSetIsUserLoggedIn(previousValue) { const isUserLoggedIn = this.isUserLoggedIn; if (isUserLoggedIn && previousValue != isUserLoggedIn) { this.broadcastMessageInRootViewTree({ name: _CBCore.broadcastEventName.userDidLogIn, parameters: import_uicore_ts.nil }); this.updateLinkTargets(); } else if (previousValue != isUserLoggedIn) { this.performFunctionWithDelay(0.01, function() { import_uicore_ts.UIRoute.currentRoute.routeByRemovingComponentsOtherThanOnesNamed([ "settings" ]).apply(); this.broadcastMessageInRootViewTree({ name: _CBCore.broadcastEventName.userDidLogOut, parameters: import_uicore_ts.nil }); this.updateLinkTargets(); }.bind(this)); } } updateLinkTargets() { this.viewCores.everyElement.rootViewController.view.forEachViewInSubtree(function(view) { if (view instanceof import_uicore_ts.UILink) { view.updateTarget(); } }); } get isUserLoggedIn() { return localStorage.getItem("CBIsUserLoggedIn") == "true"; } get userProfile() { const text = localStorage.getItem("CBUserProfile"); if (text) { return JSON.parse(text); } return void 0; } set userProfile(userProfile) { if ((0, import_uicore_ts.IS_NOT)(userProfile)) { localStorage.removeItem("CBUserProfile"); } localStorage.setItem("CBUserProfile", JSON.stringify(userProfile)); this.didSetUserProfile(); } didSetUserProfile() { this.isUserLoggedIn = (0, import_uicore_ts.IS)(this.userProfile); } set languageKey(languageKey) { if ((0, import_uicore_ts.IS_NOT)(languageKey)) { localStorage.removeItem("CBLanguageKey"); } localStorage.setItem("CBLanguageKey", JSON.stringify(languageKey)); this.didSetLanguageKey(); } get languageKey() { return (0, import_uicore_ts.FIRST)(localStorage.getItem("CBLanguageKey"), import_CBLanguageService.CBLanguageService.defaultLanguageKey).replace( '"', "" ).replace('"', ""); } didSetLanguageKey() { import_uicore_ts.UIRoute.currentRoute.routeWithComponent( "settings", { "language": this.languageKey }, import_uicore_ts.YES ).applyByReplacingCurrentRouteInHistory(); } reloadSocketConnection() { this.socketClient.socket.disconnect(); const messagesToBeSent = this.socketClient._messagesToBeSent.filter(function(messageItem, index, array) { return !messageItem.isBoundToUserWithID || messageItem.isBoundToUserWithID == _CBCore.sharedInstance.userProfile._id; }); this._socketClient = new import_CBSocketClient.CBSocketClient(this); this._socketClient._messagesToBeSent = messagesToBeSent; const socketClient = this._socketClient; this._models.forEach(function(model, index, array) { model.setSocketClient(socketClient); }); this._functionsToCallForEachSocketClient.forEach(function(functionToCall, index, array) { functionToCall(); }); } callFunctionForEachSocketClient(functionToCall) { this._functionsToCallForEachSocketClient.push(functionToCall); functionToCall(); } }; _CBCore.broadcastEventName = { "userDidLogIn": "UserDidLogIn", "userDidLogOut": "UserDidLogOut" }; let CBCore = _CBCore; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CBCore }); //# sourceMappingURL=CBCore.js.map