|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + Alert, |
| 4 | + Col |
| 5 | +} from 'reactstrap'; |
| 6 | + |
| 7 | +class NotificationAlert extends React.Component{ |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + this.state = { |
| 11 | + notifyTL: [], |
| 12 | + notifyTC: [], |
| 13 | + notifyTR: [], |
| 14 | + notifyBL: [], |
| 15 | + notifyBC: [], |
| 16 | + notifyBR: [], |
| 17 | + }; |
| 18 | + this.onDismiss = this.onDismiss.bind(this); |
| 19 | + this.notify = this.notify.bind(this); |
| 20 | + } |
| 21 | + onDismiss(nNumber,place,noAnimate){ |
| 22 | + var notify = []; |
| 23 | + var sNotify = this.state["notify"+place.toUpperCase()]; |
| 24 | + var dNotify; |
| 25 | + for(var i = 0 ; i < sNotify.length; i++){ |
| 26 | + if(sNotify[i].key!==nNumber+""){ |
| 27 | + if(sNotify[i].props.className.indexOf("fadeOutUp") !== -1){ |
| 28 | + dNotify = React.cloneElement( |
| 29 | + sNotify[i] |
| 30 | + ); |
| 31 | + } else { |
| 32 | + if(noAnimate===undefined){ |
| 33 | + var animation; |
| 34 | + if(place.indexOf("b")!==-1){ |
| 35 | + animation = sNotify[i].key>nNumber+"" ? " animated moveDown":""; |
| 36 | + } else { |
| 37 | + animation = sNotify[i].key>nNumber+"" ? " animated moveUp":""; |
| 38 | + } |
| 39 | + dNotify = React.cloneElement( |
| 40 | + sNotify[i], |
| 41 | + {className: "alert-with-icon"+animation} |
| 42 | + ); |
| 43 | + } |
| 44 | + else { |
| 45 | + dNotify = React.cloneElement( |
| 46 | + sNotify[i], |
| 47 | + {className: "alert-with-icon"} |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + notify.push(dNotify); |
| 52 | + } else { |
| 53 | + if(noAnimate===undefined){ |
| 54 | + dNotify = React.cloneElement( |
| 55 | + sNotify[i], |
| 56 | + {className: "alert-with-icon animated fadeOutUp"} |
| 57 | + ); |
| 58 | + notify.push(dNotify); |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + if(noAnimate===undefined){ |
| 64 | + setTimeout(function(){this.onDismiss(nNumber,place,"noAnimate")}.bind(this),800); |
| 65 | + } |
| 66 | + sNotify = {}; |
| 67 | + sNotify["notify"+place.toUpperCase()] = notify; |
| 68 | + this.setState(sNotify); |
| 69 | + } |
| 70 | + addNotification(options){ |
| 71 | + var notify = this.state["notify"+options.place.toUpperCase()]; |
| 72 | + var nNumber = notify.length; |
| 73 | + if(notify.length > 0){ |
| 74 | + if(options.place.indexOf("b")!==-1){ |
| 75 | + nNumber = parseInt(notify[0].key,10)+1; |
| 76 | + } else { |
| 77 | + nNumber = parseInt(notify[notify.length-1].key,10)+1; |
| 78 | + } |
| 79 | + } |
| 80 | + var notification = ( |
| 81 | + <Alert color={options.type} className="alert-with-icon animated fadeInDown" closeClassName="now-ui-icons ui-1_simple-remove" toggle={() => this.onDismiss(nNumber,options.place)} key={nNumber} > |
| 82 | + <span data-notify="icon" className="now-ui-icons ui-1_bell-53"></span> |
| 83 | + <span data-notify="message">{options.message}</span> |
| 84 | + </Alert> |
| 85 | + ); |
| 86 | + if(options.place.indexOf("b")!==-1){ |
| 87 | + notify.unshift( |
| 88 | + notification |
| 89 | + ); |
| 90 | + } else { |
| 91 | + notify.push( |
| 92 | + notification |
| 93 | + ); |
| 94 | + } |
| 95 | + var sNotify = {}; |
| 96 | + sNotify["notify"+options.place.toUpperCase()] = notify; |
| 97 | + // aici pui notify[notify.length-1].key |
| 98 | + if( options.autoDismiss > 0) |
| 99 | + setTimeout(function(){this.onDismiss(nNumber,options.place);}.bind(this),options.autoDismiss*1000+(notify.length-1)*1000); |
| 100 | + this.setState(sNotify); |
| 101 | + } |
| 102 | + showAllNotifications(place){ |
| 103 | + if(this.state["notify"+place.toUpperCase()].length > 0){ |
| 104 | + var style = {display: "inline-block",margin: "0px auto",position: "fixed",transition: "all 0.5s ease-in-out",zIndex: "1031"}; |
| 105 | + if(place.indexOf("t")!==-1){ |
| 106 | + style["top"] = "20px"; |
| 107 | + switch (place) { |
| 108 | + case "tl": |
| 109 | + style["left"] = "20px"; |
| 110 | + break; |
| 111 | + case "tc": |
| 112 | + style["left"] = "0px"; |
| 113 | + style["right"] = "0px"; |
| 114 | + break; |
| 115 | + case "tr": |
| 116 | + style["right"] = "20px"; |
| 117 | + break; |
| 118 | + default: |
| 119 | + break; |
| 120 | + } |
| 121 | + } else { |
| 122 | + style["bottom"] = "20px"; |
| 123 | + switch (place) { |
| 124 | + case "bl": |
| 125 | + style["left"] = "20px"; |
| 126 | + break; |
| 127 | + case "bc": |
| 128 | + style["left"] = "0px"; |
| 129 | + style["right"] = "0px"; |
| 130 | + break; |
| 131 | + case "br": |
| 132 | + style["right"] = "20px"; |
| 133 | + break; |
| 134 | + default: |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + return ( |
| 139 | + <Col xs={11} sm={4} style={style}> |
| 140 | + { |
| 141 | + this.state["notify"+place.toUpperCase()].map((prop,key)=>{ |
| 142 | + return prop; |
| 143 | + }) |
| 144 | + } |
| 145 | + </Col> |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + render(){ |
| 150 | + return ( |
| 151 | + <div ref="notifications"> |
| 152 | + {this.showAllNotifications("tl")} |
| 153 | + {this.showAllNotifications("tc")} |
| 154 | + {this.showAllNotifications("tr")} |
| 155 | + {this.showAllNotifications("bl")} |
| 156 | + {this.showAllNotifications("bc")} |
| 157 | + {this.showAllNotifications("br")} |
| 158 | + </div> |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +export default NotificationAlert; |
0 commit comments