Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Reply {
  2.     //region consts
  3.     private static readonly REDIRECT_TIMEOUT: number = 1000;
  4.     //endregion
  5.  
  6.     //region fields
  7.     private _errors: string[];
  8.     private _info: string[];
  9.     private _ok: string[];
  10.  
  11.     private _redirectUrl: string;
  12.     private _result: any;
  13.     //endregion
  14.  
  15.     //region properties
  16.     public get errors(): string[] {
  17.         return this._errors;
  18.     }
  19.  
  20.     public get info(): string[] {
  21.         return this._info;
  22.     }
  23.  
  24.     public get ok(): string[] {
  25.         return this._ok;
  26.     }
  27.  
  28.     public get redirectUrl(): string {
  29.         return this._redirectUrl;
  30.     }
  31.  
  32.     public get result(): any {
  33.         return this._result;
  34.     }
  35.  
  36.     public get hasMessages() {
  37.         return this._errors.length > 0 || this._info.length > 0 || this._ok.length > 0;
  38.     }
  39.     //endregion
  40.  
  41.     //region constructors
  42.     public constructor()
  43.     public constructor(jsonData: string)
  44.     public constructor(formattedData: Object)
  45.     public constructor(data?: any){
  46.         if (data) {
  47.             if (typeof data === 'string'){
  48.                 data = JSON.parse(data);
  49.             }
  50.  
  51.             this._errors = data.errors || [];
  52.             this._info = data.info || [];
  53.             this._ok = data.ok || [];
  54.  
  55.             this._redirectUrl = data.redirect_url;
  56.             this._result = data.result;
  57.         }
  58.     }
  59.     //endregion
  60.  
  61.     //region public methods
  62.  
  63.     public redirect() {
  64.         if (this._redirectUrl) {
  65.             let url: string = this._redirectUrl;
  66.             setTimeout(
  67.                 function () {
  68.                     window.location.replace(url);
  69.                 },
  70.                 Reply.REDIRECT_TIMEOUT
  71.             );
  72.         }
  73.     }
  74.  
  75.     public showAllMessages() {
  76.         this.showErrors();
  77.         this.showInfo();
  78.         this.showOk();
  79.     }
  80.  
  81.     public showErrors() {
  82.         this._showMessages(NotificationTypes.error, this._errors);
  83.     }
  84.  
  85.     public showInfo() {
  86.         this._showMessages(NotificationTypes.info, this._info);
  87.     }
  88.  
  89.     public showOk() {
  90.         this._showMessages(NotificationTypes.success, this._ok);
  91.     }
  92.  
  93.     public showResult() {
  94.         new Notification(NotificationTypes.info, JSON.stringify(this._result)).show();
  95.     }
  96.     //endregion
  97.  
  98.     //region private methods
  99.  
  100.     private _showMessages(type: NotificationTypes, messages: string[]) {
  101.         messages.forEach(function (message: string) {
  102.             new Notification(type, message).show();
  103.         });
  104.     }
  105.     //endregion
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement