UNPKG

@acpaas-ui/js-notification-store

Version:

The Notification Store provides a singleton store to save and easily access notifications throughout your app.

3 lines (2 loc) 8.82 kB
define(["exports","rxjs","escape-string-regexp"],function(exports,rxjs,escapeStringRegExp){"use strict";escapeStringRegExp=escapeStringRegExp&&escapeStringRegExp.hasOwnProperty("default")?escapeStringRegExp.default:escapeStringRegExp;var NOTIFICATION_STORE_DEFAULTS={allowOverrides:!1,defaultHandle:"system",defaultTarget:"system",defaultMessage:"Something went wrong.",defaultScope:"root",defaultTimer:0},NOTIFICATION_TYPES={INFO:"I",ERROR:"E",WARNING:"W",SUCCESS:"S",NOTIFICATION:"N"},NOTIFICATION_DEFAULTS={handle:NOTIFICATION_STORE_DEFAULTS.defaultHandle,target:NOTIFICATION_STORE_DEFAULTS.defaultTarget,type:NOTIFICATION_TYPES.NOTIFICATION,timer:NOTIFICATION_STORE_DEFAULTS.defaultTimer,scope:NOTIFICATION_STORE_DEFAULTS.defaultScope},classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")},createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}(),_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source)Object.prototype.hasOwnProperty.call(source,key)&&(target[key]=source[key])}return target},Notification=function(){function Notification(){var options=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,Notification),options=_extends({},NOTIFICATION_DEFAULTS,options),this.handle=options.handle,this.target=options.target,this.message=options.message,this.type=options.type,this.timer=options.timer,this.scope=options.scope}return createClass(Notification,null,[{key:"parseOptions",value:function(){var options=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},result={};for(var prop in NOTIFICATION_DEFAULTS)result[prop]=options[prop]||NOTIFICATION_DEFAULTS[prop];return result}},{key:"defaultOptions",get:function(){return _extends({},NOTIFICATION_DEFAULTS)}},{key:"availableTypes",get:function(){return _extends({},NOTIFICATION_TYPES)}}]),Notification}(),API={options:_extends({},NOTIFICATION_STORE_DEFAULTS),messages:{},notifications:new Map,subjects:new Map},NotificationStore=function(){function NotificationStore(){var newMessages=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,NotificationStore),this.loadMessages(newMessages),this.notifications=NotificationStore.subscriber(this)}return createClass(NotificationStore,null,[{key:"subscriber",value:function(store){if(!store)throw Error("No store provided to subscribe to!");return API.subjects.set(store,new rxjs.BehaviorSubject(NotificationStore.getFlatNotifications())),API.subjects.get(store)}},{key:"getFlatTarget",value:function(targetKey){var notifications=[],target=API.notifications.get(targetKey);return target&&target.forEach(function(notification){notifications.push(notification)}),notifications}},{key:"getFlatNotifications",value:function(target){if(target)return NotificationStore.getFlatTarget(target);var notifications={};return API.notifications.forEach(function(value,t){notifications[t]=NotificationStore.getFlatTarget(t)}),notifications}},{key:"deleteNotification",value:function(notification){if(!(notification&&notification instanceof Notification))throw Error("Invalid notification provided.");var target=API.notifications.get(notification.target);target&&(target.delete(notification),NotificationStore.updateSubjects())}},{key:"updateSubjects",value:function(){var currNotifications=NotificationStore.getFlatNotifications();API.subjects.forEach(function(subject){subject.next(currNotifications)})}},{key:"resetStore",value:function(){API.options=_extends({},NOTIFICATION_STORE_DEFAULTS),API.messages={},API.notifications=new Map,API.subjects=new Map}},{key:"allowOverrides",get:function(){return API.options.allowOverrides},set:function(){var newValue=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];return API.options.allowOverrides="boolean"==typeof newValue?newValue:NOTIFICATION_STORE_DEFAULTS.allowOverrides,API.options.allowOverrides}},{key:"defaultHandle",get:function(){return API.options.defaultHandle},set:function(){var newValue=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS.defaultHandle;return API.options.defaultHandle=newValue.toString(),API.options.defaultHandle}},{key:"defaultTarget",get:function(){return API.options.defaultTarget},set:function(){var newValue=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS.defaultTarget;return API.options.defaultTarget=newValue.toString(),API.options.defaultTarget}},{key:"defaultMessage",get:function(){return API.options.defaultMessage},set:function(){var newValue=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS.defaultMessage;return API.options.defaultMessage=newValue.toString(),API.options.defaultMessage}},{key:"defaultScope",get:function(){return API.options.defaultScope},set:function(){var newValue=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS.defaultScope;return API.options.defaultScope=newValue.toString(),API.options.defaultScope}},{key:"defaultTimer",get:function(){return API.options.defaultTimer},set:function(){var newValue=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS.defaultTimer;return API.options.defaultTimer="number"==typeof newValue?newValue:NOTIFICATION_STORE_DEFAULTS.defaultTimer,API.options.defaultTimer}},{key:"messages",get:function(){return _extends({},API.messages)},set:function(){var newMessages=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},messageLoader=[API.messages,newMessages];return API.messages=NotificationStore.allowOverrides?Object.assign.apply(Object,[{}].concat(messageLoader)):Object.assign.apply(Object,[{}].concat(function(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}return Array.from(arr)}(messageLoader.reverse()))),API.messages}},{key:"options",get:function(){return _extends({},API.options)},set:function(){var newOptions=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NOTIFICATION_STORE_DEFAULTS;return API.options=_extends({},NOTIFICATION_STORE_DEFAULTS,newOptions),API.options}},{key:"subjects",get:function(){return API.subjects}}]),createClass(NotificationStore,[{key:"getMessages",value:function(){return _extends({},API.messages)}},{key:"getMessage",value:function(handle){var replace=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},msg=API.messages.hasOwnProperty(handle)?API.messages[handle]:API.options.defaultMessage,patterns=Object.getOwnPropertyNames(replace).map(function(prop){return escapeStringRegExp(prop)}).join("|"),pattern=new RegExp("<% ("+patterns+") %>","g");return msg.replace(pattern,function(match,prop){return replace[prop]})}},{key:"loadMessages",value:function(newMessages){NotificationStore.messages=newMessages}},{key:"triggerNotification",value:function(){var handle=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NotificationStore.defaultHandle,target=arguments.length>1&&void 0!==arguments[1]?arguments[1]:NotificationStore.defaultTarget,options=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},replace=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},notification=new Notification(_extends({message:options.message||this.getMessage(handle,replace)},Notification.parseOptions(options),{handle:handle,target:target}));this.loadNotification(notification)}},{key:"loadNotification",value:function(notification){if(!notification||!notification.target)throw Error("Invalid notification provided.");API.notifications.has(notification.target)||API.notifications.set(notification.target,new Map),API.notifications.get(notification.target).set(notification,notification),NotificationStore.updateSubjects()}},{key:"getNotifications",value:function(target){return NotificationStore.getFlatNotifications(target)}},{key:"clearNotification",value:function(notification){NotificationStore.deleteNotification(notification)}},{key:"clearTarget",value:function(target){if(!target)throw Error("No target provided.");API.notifications.has(target)&&(API.notifications.delete(target),NotificationStore.updateSubjects())}}]),NotificationStore}();exports.Notification=Notification,exports.NotificationStore=NotificationStore,Object.defineProperty(exports,"__esModule",{value:!0})}); //# sourceMappingURL=acpaas-ui-js-notification-store.amd.js.map