openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

notice.min.js (4196B)


      1 import $ from  '../jquery-global.min.js';
      2 import Workbench          from './workbench.min.js';
      3 import Callback           from "./callback.min.js";
      4 import WorkbenchNavigator from "./navigator.min.js";
      5 export default class Notice  {
      6 'use strict';
      7 static type = Object.freeze({
      8 warning: 0,
      9 validation: 1,
     10 info: 2,
     11 success: 3,
     12 error: 3,
     13 loading: 3,
     14 inactive: 4
     15 });
     16 constructor() {
     17 this.typ  = '';
     18 this.id   = 0;
     19 this.name = '';
     20 this.status = 'inactive';
     21 this.msg  = '';
     22 this.log  = '';
     23 this.timeout = 0;
     24 this.element = $.create('div')
     25 .addClass('notice'                   )
     26 .addClass('notice--is-inactive'      )
     27 .addClass('collapsible'           )
     28 .addClass('collapsible--is-closed');
     29 this.onClick = new Callback();
     30 }
     31 before() {
     32 };
     33 close() {
     34 this.element.remove();
     35 }
     36 setStatus( status ) {
     37 this.element.removeClass('notice--' + this.status );
     38 this.status = status;
     39 this.element.addClass('notice--' + this.status );
     40 }
     41 inProgress() {
     42 }
     43 stopProgress() {
     44 }
     45 show() {
     46 console.debug('user notice: ' + this.msg);
     47 let notice = this;
     48 this.element.removeClass('notice--is-inactive');
     49 this.element.appendTo( $('.or-notice-container') ); 
     50 let toolbar = $.create('div').addClass("notice-toolbar");
     51 toolbar.appendTo(this.element);
     52 toolbar.append( $.create('i').addClass('image-icon').addClass('image-icon--menu-close').addClass('act-notice-close') );
     53 this.element.append( $.create('i').addClass('image-icon').addClass('image-icon--node-open'  ).addClass('collapsible--on-open'  ) );
     54 this.element.append( $.create('i').addClass('image-icon').addClass('image-icon--node-closed').addClass('collapsible--on-closed') );
     55 this.element.append( $.create('span').addClass('notice-text').addClass('collapsible-act-switch').text( Notice.htmlEntities(this.msg) ) );
     56 if (this.name) {
     57 this.element.append( $.create('div').addClass('notice-name').addClass('collapsible-value').append( $.create('a').addClass('act-clickable').attr('href',WorkbenchNavigator.createShortUrl(this.typ, this.id)).data('type',open).data('action',this.typ).data('id',this.id).append( $.create('i').addClass('notice-action-full').addClass('image-icon').addClass('image-icon--action-' + this.typ )).append( $.create('span').text(this.name ))).orLinkify() );
     58 }
     59 if (this.log)
     60 this.element.append( $.create('div').addClass('notice-log').addClass('collapsible-value').append( $.create('pre').text(Notice.htmlEntities(this.log))));
     61 this.element.append( $.create('div').addClass('notice-date').addClass('collapsible-value').text(new Date().toLocaleTimeString()));
     62 this.element.find('.or-notice-text').click( function () {
     63 notice.onClick.fire();
     64 } );
     65 Workbench.registerOpenClose( this.element );
     66 this.element.find('.or-act-notice-close').click(function () {
     67 notice.close();
     68 });
     69 if   ( !this.timeout ) {
     70 switch( this.status ) {
     71 case 'ok'     : this.timeout =  3; break;
     72 case 'info'   : this.timeout = 30; break;
     73 case 'warning': this.timeout = 40; break;
     74 case 'error'  : this.timeout = 50; break;
     75 default:        this.timeout = 10; console.error('unknown notice status: '+this.status);
     76 }
     77 }
     78 if (this.timeout) {
     79 let timer = setTimeout(function () {
     80 notice.close();
     81 }, this.timeout * 1000);
     82 this.element.click( function () {
     83 window.clearTimeout( timer );
     84 } );
     85 }
     86 }
     87 setContext(type,id,name) {
     88 this.typ  = type;
     89 this.id   = id;
     90 this.name = name;
     91 }
     92 start(type, id, name, status, msg, log = null, notifyTheBrowser = false) {
     93 this.setContext(type,id,name);
     94 this.msg = msg;
     95 this.log = log;
     96 if (notifyTheBrowser)
     97 this.notifyBrowser(msg);  
     98 this.setStatus(status);
     99 }
    100 notifyBrowser()
    101 {
    102 let text = this.msg;
    103 if (!("Notification" in window)) {
    104 return;
    105 }
    106 else if (Notification.permission === "granted") {
    107 let notification = new Notification(text);
    108 }
    109 else if (Notification.permission !== 'denied') {
    110 Notification.requestPermission(function (permission) {
    111 if (permission === "granted") {
    112 let notification = new Notification(text);
    113 }
    114 });
    115 }
    116 }
    117 static htmlEntities( str ) {
    118 return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    119 }
    120 static removeNoticesWithStatus( status) {
    121 $('.or-notice-container').find('.or-notice--'+status).remove();
    122 }
    123 static removeAllNotices( status) {
    124 $('.or-notice-container').find('.or-notice').remove();
    125 }
    126 }