@nu-art/thunder
Version:
Thunder - React & Typescript based frontend framework
187 lines • 7.79 kB
JavaScript
;
/*
* A typescript & react boilerplate with api call example
*
* Copyright (C) 2018 Adam van der Kruk aka TacB0sS
*
* 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.
*/
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts_common_1 = require("@nu-art/ts-common");
// noinspection TypeScriptPreferShortImport
var Thunder_1 = require("../core/Thunder");
var dialog_module_1 = require("./dialog-module");
var ToastType;
(function (ToastType) {
ToastType[ToastType["success"] = 0] = "success";
ToastType[ToastType["error"] = 1] = "error";
ToastType[ToastType["info"] = 2] = "info";
})(ToastType = exports.ToastType || (exports.ToastType = {}));
var Interval_DefaultToast = 6000;
var ToastBuilder = /** @class */ (function (_super) {
__extends(ToastBuilder, _super);
function ToastBuilder() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.bgColor = "#eeffef";
_this.duration = 6 * ts_common_1.Second;
_this.type = ToastType.info;
_this.positionVertical = "top";
_this.positionHorizontal = "center";
_this.actions = [];
_this.content = "NO CONTENT";
return _this;
}
ToastBuilder.prototype.setType = function (type) {
this.type = type;
return this;
};
ToastBuilder.prototype.setContent = function (content) {
this.content = content;
return this;
};
ToastBuilder.prototype.setBackground = function (bgColor) {
this.bgColor = bgColor;
return this;
};
ToastBuilder.prototype.setDuration = function (duration) {
this.duration = duration;
return this;
};
ToastBuilder.prototype.setActions = function (actions) {
this.actions = actions || [];
return this;
};
ToastBuilder.prototype.addActions = function () {
var actions = [];
for (var _i = 0; _i < arguments.length; _i++) {
actions[_i] = arguments[_i];
}
ts_common_1.addAllItemToArray(this.actions, actions);
return this;
};
ToastBuilder.prototype.setVerticalPosition = function (positionVertical) {
this.positionVertical = positionVertical;
return this;
};
ToastBuilder.prototype.setHorizontalPosition = function (positionHorizontal) {
this.positionHorizontal = positionHorizontal;
return this;
};
ToastBuilder.prototype.show = function () {
var 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);
};
return ToastBuilder;
}(dialog_module_1.StylableBuilder));
exports.ToastBuilder = ToastBuilder;
var ToastModule_Class = /** @class */ (function (_super) {
__extends(ToastModule_Class, _super);
function ToastModule_Class() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.DefaultBuilder = new ToastBuilder();
_this.showToast = new Thunder_1.UIDispatcher("showToast");
_this.adjustStringMessage = function (_message) {
var 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/> ");
var ignore = message.match(/`(.*?)`/g);
if (ignore && ignore.length > 0)
message = ignore.reduce(function (input, toEscape) {
var 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 = function (toast) {
// in the future we can add more than one toast and manage a stack of them!!
_this.showToast.dispatch([]);
};
return _this;
}
ToastModule_Class.prototype.toastError = function (errorMessage, interval) {
if (interval === void 0) { interval = Interval_DefaultToast; }
this.toast(errorMessage, ToastType.error, interval);
};
ToastModule_Class.prototype.toastSuccess = function (successMessage, interval) {
if (interval === void 0) { interval = Interval_DefaultToast; }
this.toast(successMessage, ToastType.success, interval);
};
ToastModule_Class.prototype.toastInfo = function (infoMessage, interval) {
if (interval === void 0) { interval = Interval_DefaultToast; }
this.toast(infoMessage, ToastType.info, interval);
};
ToastModule_Class.prototype.toast = function (_message, type, interval) {
if (interval === void 0) { interval = Interval_DefaultToast; }
var 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;
}
var content = _message;
if (typeof _message === "string")
content = exports.ToastModule.adjustStringMessage(_message);
// console.log("_message:", _message)
this.DefaultBuilder.setContent(content).setDuration(interval).setBackground(color).show();
};
ToastModule_Class.prototype.toastImpl = function (toast) {
this.showToast.dispatch([toast]);
};
return ToastModule_Class;
}(ts_common_1.Module));
exports.ToastModule_Class = ToastModule_Class;
exports.ToastModule = new ToastModule_Class();
//# sourceMappingURL=toaster-module.js.map