Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Reply {
- //region consts
- private static readonly REDIRECT_TIMEOUT: number = 1000;
- //endregion
- //region fields
- private _errors: string[];
- private _info: string[];
- private _ok: string[];
- private _redirectUrl: string;
- private _result: any;
- //endregion
- //region properties
- public get errors(): string[] {
- return this._errors;
- }
- public get info(): string[] {
- return this._info;
- }
- public get ok(): string[] {
- return this._ok;
- }
- public get redirectUrl(): string {
- return this._redirectUrl;
- }
- public get result(): any {
- return this._result;
- }
- public get hasMessages() {
- return this._errors.length > 0 || this._info.length > 0 || this._ok.length > 0;
- }
- //endregion
- //region constructors
- public constructor()
- public constructor(jsonData: string)
- public constructor(formattedData: Object)
- public constructor(data?: any){
- if (data) {
- if (typeof data === 'string'){
- data = JSON.parse(data);
- }
- this._errors = data.errors || [];
- this._info = data.info || [];
- this._ok = data.ok || [];
- this._redirectUrl = data.redirect_url;
- this._result = data.result;
- }
- }
- //endregion
- //region public methods
- public redirect() {
- if (this._redirectUrl) {
- let url: string = this._redirectUrl;
- setTimeout(
- function () {
- window.location.replace(url);
- },
- Reply.REDIRECT_TIMEOUT
- );
- }
- }
- public showAllMessages() {
- this.showErrors();
- this.showInfo();
- this.showOk();
- }
- public showErrors() {
- this._showMessages(NotificationTypes.error, this._errors);
- }
- public showInfo() {
- this._showMessages(NotificationTypes.info, this._info);
- }
- public showOk() {
- this._showMessages(NotificationTypes.success, this._ok);
- }
- public showResult() {
- new Notification(NotificationTypes.info, JSON.stringify(this._result)).show();
- }
- //endregion
- //region private methods
- private _showMessages(type: NotificationTypes, messages: string[]) {
- messages.forEach(function (message: string) {
- new Notification(type, message).show();
- });
- }
- //endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement