UNPKG

@intuitionrobotics/thunderstorm

Version:
167 lines 6.52 kB
"use strict"; /* * Thunderstorm is a full web app framework! * * Typescript & Express backend infrastructure that natively runs on firebase function * Typescript & React frontend infrastructure * * Copyright (C) 2020 Intuition Robotics * * 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 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ToastModule = exports.ToastModule_Class = exports.ToastBuilder = exports.ToastType = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); // noinspection TypeScriptPreferShortImport const thunder_dispatcher_1 = require("../../core/thunder-dispatcher"); const Stylable_1 = require("../../tools/Stylable"); var ToastType; (function (ToastType) { ToastType[ToastType["success"] = 0] = "success"; ToastType[ToastType["error"] = 1] = "error"; ToastType[ToastType["info"] = 2] = "info"; })(ToastType || (exports.ToastType = ToastType = {})); const Interval_DefaultToast = 6 * ts_common_1.Second; class ToastBuilder extends Stylable_1.StylableBuilder { constructor() { super(...arguments); this.bgColor = "#eeffef"; this.duration = Interval_DefaultToast; this.type = ToastType.info; this.positionVertical = "top"; this.positionHorizontal = "center"; this.actions = []; this.content = "NO CONTENT"; } setType(type) { this.type = type; return this; } setContent(content) { this.content = content; return this; } setBackground(bgColor) { this.bgColor = bgColor; return this; } setDuration(duration) { this.duration = duration; return this; } setActions(actions) { this.actions = actions || []; return this; } addActions(...actions) { (0, ts_common_1.addAllItemToArray)(this.actions, actions); return this; } setVerticalPosition(positionVertical) { this.positionVertical = positionVertical; return this; } setHorizontalPosition(positionHorizontal) { this.positionHorizontal = positionHorizontal; return this; } show() { const toast = { duration: this.duration, type: this.type, content: this.content, positionVertical: this.positionVertical, positionHorizontal: this.positionHorizontal, actions: this.actions, bgColor: this.bgColor, className: this.className, style: this.style, }; // @ts-ignore exports.ToastModule.toastImpl(toast); } } exports.ToastBuilder = ToastBuilder; const dispatch_showToast = new thunder_dispatcher_1.ThunderDispatcher("__showToast"); class ToastModule_Class extends ts_common_1.Module { constructor() { super("ToastModule"); this.DefaultBuilder = new ToastBuilder(); this.adjustStringMessage = (_message) => { let message = _message; message = message.replace(/\n#### (.*?)\n/g, "\n<h4>$1</h4>\n"); message = message.replace(/\n### (.*?)\n/g, "\n<h3>$1</h3>\n"); message = message.replace(/\n## (.*?)\n/g, "\n<h2>$1</h2>\n"); message = message.replace(/\n# (.*?)\n/g, "\n<h1>$1</h1>\n"); message = message.replace(/(<\/?.*?>)\n/g, "$1"); message = message.replace(/([^>]?)\n/g, "$1<br/> "); const ignore = message.match(/`(.*?)`/g); if (ignore && ignore.length > 0) message = ignore.reduce((input, toEscape) => { const replaceValue = toEscape.substring(1, toEscape.length - 1) .replace(/([^\\]?)_/g, "$1\\_") .replace(/([^\\]?)\*/g, "$1\\*"); return input.replace(toEscape, replaceValue); }, message); message = message.replace(/([^\\]?)_(.*?)([^\\])_/g, "$1<i>$2$3</i>"); message = message.replace(/([^\\]?)\*(.*?)([^\\])\*/g, "$1<b>$2$3</b>"); message = message.replace(/\\_/g, "_"); message = message.replace(/\\\*/g, "*"); return message; }; this.hideToast = (toast) => { // in the future we can add more than one toast and manage a stack of them!! dispatch_showToast.dispatchUI(); }; } setDefaultBuilder(DefaultBuilder) { this.DefaultBuilder = DefaultBuilder; } toastError(errorMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) { this.toast(errorMessage, ToastType.error, interval, builder); } toastSuccess(successMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) { this.toast(successMessage, ToastType.success, interval, builder); } toastInfo(infoMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) { this.toast(infoMessage, ToastType.info, interval, builder); } toast(_message, type, interval = Interval_DefaultToast, builder = this.DefaultBuilder) { let color; switch (type) { case ToastType.success: color = "#2ee06f"; break; case ToastType.error: color = "#ff4436"; break; case ToastType.info: color = "#49addb"; break; default: color = "#e8e8e8"; break; } let content = _message; if (typeof _message === "string") content = exports.ToastModule.adjustStringMessage(_message); // console.log("_message:", _message) builder.setContent(content).setDuration(interval).setBackground(color).setType(type).show(); } toastImpl(toast) { dispatch_showToast.dispatchUI(toast); } } exports.ToastModule_Class = ToastModule_Class; exports.ToastModule = new ToastModule_Class(); //# sourceMappingURL=ToasterModule.js.map