1 | import React from 'react';
|
2 | import Notification from 'bee-notification';
|
3 | import classnames from 'classnames';
|
4 |
|
5 | let defaultDuration = 1.5;
|
6 | let newDuration;
|
7 | let defaultTop = 0;
|
8 | let defaultBottom = 48;
|
9 | let bottom = 90;
|
10 | let padding = 30;
|
11 | let width =200;
|
12 | let messageInstance;
|
13 | let key = 1;
|
14 | let clsPrefix = 'u-message';
|
15 | const noop = () => {};
|
16 |
|
17 | let positionObj = {
|
18 | "top": {
|
19 | messageStyle: {
|
20 | width: "100%"
|
21 | },
|
22 | notificationStyle: {
|
23 | top: defaultTop,
|
24 | width: "100%"
|
25 | },
|
26 | transitionName: 'top'
|
27 | },
|
28 | "bottom": {
|
29 | messageStyle: {
|
30 | width: "100%"
|
31 | },
|
32 | notificationStyle: {
|
33 | bottom: defaultBottom,
|
34 | width: "100%"
|
35 | },
|
36 | transitionName: 'bottom'
|
37 | },
|
38 | "topRight": {
|
39 | messageStyle: {
|
40 | width: width
|
41 | },
|
42 | notificationStyle: {
|
43 | top: padding,
|
44 | right: padding,
|
45 | width: width
|
46 | },
|
47 | transitionName: 'right'
|
48 | },
|
49 | "bottomRight": {
|
50 | messageStyle: {
|
51 | width: width
|
52 | },
|
53 | notificationStyle: {
|
54 | bottom: bottom,
|
55 | right: padding,
|
56 | width: width
|
57 | },
|
58 | transitionName: 'right'
|
59 | },
|
60 | "topLeft": {
|
61 | messageStyle: {
|
62 | width: width
|
63 | },
|
64 | notificationStyle: {
|
65 | top: padding,
|
66 | left: padding,
|
67 | width: width
|
68 | },
|
69 | transitionName: 'left'
|
70 | },
|
71 | "bottomLeft": {
|
72 | messageStyle: {
|
73 | width: width
|
74 | },
|
75 | notificationStyle: {
|
76 | bottom: bottom,
|
77 | left: padding,
|
78 | width: width
|
79 | },
|
80 | transitionName: 'left'
|
81 | }
|
82 | }
|
83 |
|
84 | function getMessageInstance(position = 'top', callback, keyboard, onEscapeKeyUp) {
|
85 | if (messageInstance) {
|
86 | callback(messageInstance);
|
87 | return;
|
88 | }
|
89 | switch(position){
|
90 | case 'top':
|
91 | positionObj[position].notificationStyle.top = defaultTop;
|
92 | break;
|
93 | case 'bottom':
|
94 | positionObj[position].notificationStyle.bottom = defaultBottom;
|
95 | break;
|
96 | case 'bottomRight':
|
97 | positionObj[position].notificationStyle.bottom = bottom;
|
98 | break;
|
99 | case 'bottomLeft':
|
100 | positionObj[position].notificationStyle.bottom = bottom;
|
101 | break;
|
102 | default:
|
103 | break;
|
104 | }
|
105 | if( position !== 'top' && position !== 'bottom'){
|
106 | positionObj[position].messageStyle.width = width;
|
107 | }
|
108 | var style = positionObj[position].notificationStyle;
|
109 | let instanceObj = {
|
110 | clsPrefix,
|
111 | transitionName: `${clsPrefix}-${positionObj[position].transitionName}`,
|
112 | style: style,
|
113 | position: '',
|
114 | }
|
115 | if (typeof keyboard === 'boolean') {
|
116 | instanceObj.keyboard = keyboard;
|
117 | }
|
118 | if (typeof onEscapeKeyUp === 'function') {
|
119 | instanceObj.onEscapeKeyUp = onEscapeKeyUp;
|
120 | }
|
121 | Notification.newInstance(instanceObj, instance => {
|
122 | messageInstance = instance;
|
123 | callback(instance);
|
124 | });
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 | function notice(content, duration, type, onClose, position, style, keyboard, onEscapeKeyUp, showIcon) {
|
130 | let iconType = ({
|
131 | info: 'uf uf-i-c-2',
|
132 | success: 'uf uf-correct',
|
133 | danger: 'uf uf-close-c',
|
134 | warning: 'uf uf-exc-t',
|
135 | light: 'uf uf-notification',
|
136 | dark: 'uf uf-bubble',
|
137 | news: 'uf uf-bell',
|
138 | infolight: 'uf uf-i-c-2',
|
139 | successlight: 'uf uf-correct',
|
140 | dangerlight: 'uf uf-close-c',
|
141 | warninglight: 'uf uf-exc-t',
|
142 | })[type];
|
143 |
|
144 | let positionStyle = positionObj[position].messageStyle;
|
145 | getMessageInstance(position, instance => {
|
146 | instance.notice({
|
147 | key,
|
148 | duration,
|
149 | color: type,
|
150 | style: Object.assign({}, positionStyle, style),
|
151 | content: (
|
152 | <div>
|
153 | {
|
154 | showIcon ? (
|
155 | <div className={`${clsPrefix}-notice-description-icon`}>
|
156 | <i className= { classnames(iconType) } />
|
157 | </div>
|
158 | ) : null
|
159 | }
|
160 | <div className={`${clsPrefix}-notice-description-content`}>{content}</div>
|
161 | </div>
|
162 | ),
|
163 | onClose,
|
164 | });
|
165 | }, keyboard, onEscapeKeyUp)
|
166 | return (function () {
|
167 | let target = key++;
|
168 | return function () {
|
169 | if (messageInstance) {
|
170 | messageInstance.removeNotice(target);
|
171 | }
|
172 | };
|
173 | }());
|
174 | }
|
175 |
|
176 | export default {
|
177 | create(obj) {
|
178 | if(newDuration){
|
179 | obj.duration = newDuration;
|
180 | }
|
181 | let content = obj.content || '';
|
182 | let duration = typeof obj.duration == 'undefined' ? defaultDuration : obj.duration;
|
183 | let color = obj.color || 'dark';
|
184 | let onClose = obj.onClose || noop;
|
185 | let position = obj.position || "top";
|
186 | let style = obj.style || {};
|
187 | let showIcon = obj.showIcon || false;
|
188 | return notice(content, duration, color, onClose, position, style, obj.keyboard, obj.onEscapeKeyUp, showIcon);
|
189 | },
|
190 | config(options) {
|
191 | if (options.top !== undefined) {
|
192 | defaultTop = options.top;
|
193 | }
|
194 | if (options.duration !== undefined) {
|
195 | defaultDuration = options.duration;
|
196 | newDuration = defaultDuration;
|
197 | }
|
198 | if (options.clsPrefix !== undefined) {
|
199 | clsPrefix = options.clsPrefix;
|
200 | }
|
201 | if (options.defaultBottom !== undefined) {
|
202 | defaultBottom = options.defaultBottom;
|
203 | }
|
204 | if (options.bottom !== undefined) {
|
205 | bottom = options.bottom;
|
206 | }
|
207 | if (options.width !== undefined) {
|
208 | width = options.width;
|
209 | }
|
210 | },
|
211 | destroy() {
|
212 | if (messageInstance) {
|
213 | messageInstance.destroy();
|
214 | messageInstance = null;
|
215 | }
|
216 | },
|
217 | };
|