Blame view

src/components/message/index.js 3.26 KB
7c15ac9e   梁灏   add Message compo...
1
2
3
4
  import Notification from '../base/notification';
  
  const prefixCls = 'ivu-message';
  const iconPrefixCls = 'ivu-icon';
47e58396   梁灏   message component...
5
  const prefixKey = 'ivu_message_key_';
7c15ac9e   梁灏   add Message compo...
6
7
  
  let defaultDuration = 1.5;
7c15ac9e   梁灏   add Message compo...
8
9
  let top;
  let messageInstance;
6cadeba4   梁灏   support Message
10
  let name = 1;
7c15ac9e   梁灏   add Message compo...
11
12
13
14
15
16
17
18
19
20
21
22
  
  const iconTypes = {
      'info': 'information-circled',
      'success': 'checkmark-circled',
      'warning': 'android-alert',
      'error': 'close-circled',
      'loading': 'load-c'
  };
  
  function getMessageInstance () {
      messageInstance = messageInstance || Notification.newInstance({
          prefixCls: prefixCls,
6cadeba4   梁灏   support Message
23
          styles: {
7c15ac9e   梁灏   add Message compo...
24
25
26
27
28
29
30
              top: `${top}px`
          }
      });
  
      return messageInstance;
  }
  
e0bd31a6   Aresn   update Message
31
  function notice (content = '', duration = defaultDuration, type, onClose = function () {}, closable = false) {
40f8606f   梁灏   add Notice component
32
      const iconType = iconTypes[type];
7c15ac9e   梁灏   add Message compo...
33
34
35
36
37
38
39
  
      // if loading
      const loadCls = type === 'loading' ? ' ivu-load-loop' : '';
  
      let instance = getMessageInstance();
  
      instance.notice({
6cadeba4   梁灏   support Message
40
          name: `${prefixKey}${name}`,
7c15ac9e   梁灏   add Message compo...
41
          duration: duration,
6cadeba4   梁灏   support Message
42
          styles: {},
40f8606f   梁灏   add Notice component
43
          transitionName: 'move-up',
7c15ac9e   梁灏   add Message compo...
44
45
46
47
48
49
          content: `
              <div class="${prefixCls}-custom-content ${prefixCls}-${type}">
                  <i class="${iconPrefixCls} ${iconPrefixCls}-${iconType}${loadCls}"></i>
                  <span>${content}</span>
              </div>
          `,
e0bd31a6   Aresn   update Message
50
51
52
          onClose: onClose,
          closable: closable,
          type: 'message'
7c15ac9e   梁灏   add Message compo...
53
      });
47e58396   梁灏   message component...
54
55
56
  
      // 用于手动消除
      return (function () {
6cadeba4   梁灏   support Message
57
          let target = name++;
47e58396   梁灏   message component...
58
59
60
  
          return function () {
              instance.remove(`${prefixKey}${target}`);
b0893113   jingsam   :art: add eslint
61
          };
47e58396   梁灏   message component...
62
      })();
7c15ac9e   梁灏   add Message compo...
63
64
65
  }
  
  export default {
e0bd31a6   Aresn   update Message
66
67
68
69
70
71
72
73
      info (options) {
          const type = typeof options;
          if (type === 'string') {
              options = {
                  content: options
              };
          }
          return notice(options.content, options.duration, 'info', options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
74
      },
e0bd31a6   Aresn   update Message
75
76
77
78
79
80
81
82
      success (options) {
          const type = typeof options;
          if (type === 'string') {
              options = {
                  content: options
              };
          }
          return notice(options.content, options.duration, 'success', options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
83
      },
e0bd31a6   Aresn   update Message
84
85
86
87
88
89
90
91
      warning (options) {
          const type = typeof options;
          if (type === 'string') {
              options = {
                  content: options
              };
          }
          return notice(options.content, options.duration, 'warning', options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
92
      },
e0bd31a6   Aresn   update Message
93
94
95
96
97
98
99
100
      error (options) {
          const type = typeof options;
          if (type === 'string') {
              options = {
                  content: options
              };
          }
          return notice(options.content, options.duration, 'error', options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
101
      },
e0bd31a6   Aresn   update Message
102
103
104
105
106
107
108
109
      loading (options) {
          const type = typeof options;
          if (type === 'string') {
              options = {
                  content: options
              };
          }
          return notice(options.content, options.duration, 'loading', options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
110
111
112
113
114
115
116
117
      },
      config (options) {
          if (options.top) {
              top = options.top;
          }
          if (options.duration) {
              defaultDuration = options.duration;
          }
d3671687   梁灏   add destroy() for...
118
119
120
121
122
      },
      destroy () {
          let instance = getMessageInstance();
          messageInstance = null;
          instance.destroy();
7c15ac9e   梁灏   add Message compo...
123
      }
b0893113   jingsam   :art: add eslint
124
  };