UNPKG

9.33 kBJavaScriptView Raw
1"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var Util=_interopDefault(require("../util.js")),extendStatics=function(t,e){return(extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function __extends(t,e){function n(){this.constructor=t}extendStatics(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function __awaiter(r,s,a,l){return new(a||(a=Promise))(function(t,e){function n(t){try{o(l.next(t))}catch(t){e(t)}}function i(t){try{o(l.throw(t))}catch(t){e(t)}}function o(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(n,i)}o((l=l.apply(r,s||[])).next())})}function __generator(n,i){var o,r,s,t,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,r&&(s=2&e[0]?r.return:e[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,e[1])).done)return s;switch(r=0,s&&(e=[2&e[0],s.value]),e[0]){case 0:case 1:s=e;break;case 4:return a.label++,{value:e[1],done:!1};case 5:a.label++,r=e[1],e=[0];continue;case 7:e=a.ops.pop(),a.trys.pop();continue;default:if(!(s=0<(s=a.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){a=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3])){a.label=e[1];break}if(6===e[0]&&a.label<s[1]){a.label=s[1],s=e;break}if(s&&a.label<s[2]){a.label=s[2],a.ops.push(e);break}s[2]&&a.ops.pop(),a.trys.pop();continue}e=i.call(n,a)}catch(t){e=[6,t],r=0}finally{o=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var Size,Color,Direction,Component=function(){function t(t,e,n){var i=this;this.template="",this.id=null,this.eventHandlers=[],this.registeredElements=[],this.name=t;var o="string"==typeof n.element?document.querySelector(n.element):n.element,r={};if(o){var s=Util.Selector.attrConfig(o);s&&(r=s)}this.defaultProps=e,this.props=Object.assign(e,r,n,{element:o}),this.id=this.uid(),this.elementListener=function(t){return i.onBeforeElementEvent(t)},this.setEventsHandler()}return t.prototype.setTemplate=function(t){this.template=t},t.prototype.getTemplate=function(){return this.template},t.prototype.getElement=function(){return this.getProp("element")||null},t.prototype.setElement=function(t){this.props.element=t},t.prototype.getId=function(){return this.id},t.prototype.uid=function(){return Math.random().toString(36).substr(2,10)},t.prototype.getName=function(){return this.name},t.prototype.getProps=function(){return this.props},t.prototype.getProp=function(t){var e=this.defaultProps[t];return void 0!==this.props[t]?this.props[t]:e},t.prototype.setProps=function(t){var e=Object.assign({},t);this.props=Object.assign(this.props,e)},t.prototype.setProp=function(t,e){if(void 0===this.props[t])throw new Error("Cannot set an invalid prop");this.props[t]=e},t.prototype.registerElements=function(t){var e=this;t.forEach(function(t){return e.registerElement(t)})},t.prototype.registerElement=function(t){t.target.addEventListener(t.event,this.elementListener),this.registeredElements.push(t)},t.prototype.unregisterElements=function(){var e=this;this.registeredElements.forEach(function(t){e.unregisterElement(t)})},t.prototype.unregisterElement=function(e){var t=this.registeredElements.findIndex(function(t){return t.target===e.target&&t.event===e.event});-1<t?(e.target.removeEventListener(e.event,this.elementListener),this.registeredElements.splice(t,1)):console.error("Warning! Could not remove element: "+e.target+" with event: "+e.event+".")},t.prototype.triggerEvent=function(t,e,n){var i=this;void 0===e&&(e={}),void 0===n&&(n=!1);var o=t.split(".").reduce(function(t,e,n){return 0===n?e:t+e.charAt(0).toUpperCase()+e.slice(1)}),r="on"+o.charAt(0).toUpperCase()+o.slice(1),s=this.getProps();if(this.eventHandlers.forEach(function(t){"function"==typeof t[o]&&t[o].apply(i,[e]),"function"==typeof t[r]&&s[r].apply(i,[e])}),!n){var a=this.getElement();a?Util.Dispatch.elementEvent(a,t,this.name,e):Util.Dispatch.winDocEvent(t,this.name,e)}},t.prototype.preventClosable=function(){return!1},t.prototype.destroy=function(){this.unregisterElements()},t.prototype.onElementEvent=function(t){},t.prototype.setEventsHandler=function(){var n=this.getProps(),t=Object.keys(n).reduce(function(t,e){return"function"==typeof n[e]&&(t[e]=n[e]),t},{});0<Object.keys(t).length&&this.eventHandlers.push(t)},t.prototype.onBeforeElementEvent=function(t){this.preventClosable()||this.onElementEvent(t)},t}();!function(t){t.sm="sm",t.md="md",t.lg="lg",t.xl="xl"}(Size||(Size={})),function(t){t.primary="primary",t.secondary="secondary",t.success="success",t.warning="warning",t.danger="danger"}(Color||(Color={})),function(t){t.top="top",t.right="right",t.bottom="bottom",t.left="left"}(Direction||(Direction={}));var Notification=function(n){function i(t){void 0===t&&(t={element:null,title:"",button:!0});var e=n.call(this,"notification",{button:!0,timeout:null,title:"",message:null,background:"primary",appendIn:document.body,directionX:"right",directionY:"top",offsetX:0,offsetY:0},t)||this;return e.timeoutCallback=null,e.elementGenerated=!1,e.setTemplate('<div class="notification" data-no-boot><div class="notification-inner"><div class="notification-header"><h5 class="notification-title"></h5><button type="button" class="icon-close" data-dismiss="notification" aria-label="Close"><span class="icon" aria-hidden="true"></span></button></div><div class="notification-body"></div></div></div>'),null===e.getElement()&&e.build(),e}return __extends(i,n),i.attachDOM=function(){Util.Observer.subscribe({componentClass:"notification",onAdded:function(t,e){e(new i({element:t}))},onRemoved:function(t,e){e("Notification",t)}})},i.prototype.build=function(){this.elementGenerated=!0;var t=document.createElement("div");t.innerHTML=this.getTemplate(),this.setElement(t.firstChild);var e=this.getElement();e.querySelector(".notification-title").innerHTML=this.getProp("title"),this.getProp("message")?e.querySelector(".notification-body").innerHTML=this.getProp("message"):e.querySelector(".notification-body").style.display="none",this.getProp("button")||(e.querySelector("button").style.display="none"),this.getProp("appendIn").appendChild(e)},i.prototype.setPosition=function(){var t=this.getProp("directionX"),e=this.getProp("directionY"),n=this.getProp("offsetX"),i=this.getProp("offsetY"),o=this.getElement();Util.Selector.removeClasses(o,Object.values(Direction)),o.style.marginLeft="0px",o.style.marginRight="0px",o.classList.add("notification-"+t),o.classList.add("notification-"+e);var r=Array.from(document.querySelectorAll(".notification.show")||[]),s=0;r.forEach(function(t){if(o!==t){var e=getComputedStyle(t),n=parseInt(e.marginTop,10),i=parseInt(e.marginBottom,10);s+=t.offsetHeight+n+i}}),o.style.transform="translateY("+("top"===e?"":"-")+s+"px)",o.style["margin"+t.replace(/^\w/,function(t){return t.toUpperCase()})]=n+"px",o.style["margin"+e.replace(/^\w/,function(t){return t.toUpperCase()})]=i+"px"},i.prototype.show=function(){var t=this;null===this.getElement()&&this.build();var o=this.getElement();if(o.classList.contains("show"))return!1;var e=this.getProp("background");e&&(o.removeAttribute("class"),o.setAttribute("class","notification"),o.classList.add("notification-"+e),o.querySelector("button").classList.add("btn-"+e));var n=o.querySelector("button[data-dismiss]");this.getProp("button")&&n&&this.registerElement({target:n,event:Util.Event.CLICK});o.querySelector("button[data-dismiss]");return this.registerElement({target:n,event:Util.Event.CLICK}),__awaiter(t,void 0,void 0,function(){var e,n,i=this;return __generator(this,function(t){switch(t.label){case 0:return[4,Util.sleep(20)];case 1:return t.sent(),this.setPosition(),e=this.getProp("timeout"),Number.isInteger(e)&&0<e&&(this.timeoutCallback=setTimeout(function(){i.hide()},e+1)),o.classList.add("show"),this.triggerEvent(Util.Event.SHOW),n=function(){i.triggerEvent(Util.Event.SHOWN),o.removeEventListener(Util.Event.TRANSITION_END,n)},o.addEventListener(Util.Event.TRANSITION_END,n),[2]}})}),!0},i.prototype.hideBody=function(){var t=this.getElement().querySelector(".notification-body");t.classList.contains("show")&&t.classList.remove("show")},i.prototype.hide=function(){var t=this;this.timeoutCallback&&(clearTimeout(this.timeoutCallback),this.timeoutCallback=null);var e=this.getElement();if(!e.classList.contains("show"))return!1;this.triggerEvent(Util.Event.HIDE);var n=this.getProp("button"),i=e.querySelector("button[data-dismiss]");n&&i&&this.unregisterElement({target:i,event:Util.Event.CLICK}),e.classList.remove("show"),e.classList.add("hide"),this.hideBody();var o=function(){e.removeEventListener(Util.Event.TRANSITION_END,o),e.classList.remove("hide"),t.triggerEvent(Util.Event.HIDDEN),t.elementGenerated&&document.body.removeChild(e)};return e.addEventListener(Util.Event.TRANSITION_END,o),!0},i.prototype.onElementEvent=function(){this.hide()},i}(Component);module.exports=Notification;
2//# sourceMappingURL=/home/qathom/Workspaces/Web/phonon/dist/js/components/notification.min.js.map
\No newline at end of file