Blame view

src/components/message/index.js 2.63 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
  
23e20abf   Sergio Crisostomo   DRY message/index...
7
8
9
10
11
  const defaults = {
      top: 24,
      duration: 1.5
  };
  
7c15ac9e   梁灏   add Message compo...
12
  let messageInstance;
6cadeba4   梁灏   support Message
13
  let name = 1;
7c15ac9e   梁灏   add Message compo...
14
15
16
17
18
19
20
21
22
23
24
25
  
  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
26
          styles: {
23e20abf   Sergio Crisostomo   DRY message/index...
27
              top: `${defaults.top}px`
7c15ac9e   梁灏   add Message compo...
28
29
30
31
32
33
          }
      });
  
      return messageInstance;
  }
  
23e20abf   Sergio Crisostomo   DRY message/index...
34
  function notice (content = '', duration = defaults.duration, type, onClose = function () {}, closable = false) {
40f8606f   梁灏   add Notice component
35
      const iconType = iconTypes[type];
7c15ac9e   梁灏   add Message compo...
36
37
38
39
40
41
42
  
      // if loading
      const loadCls = type === 'loading' ? ' ivu-load-loop' : '';
  
      let instance = getMessageInstance();
  
      instance.notice({
6cadeba4   梁灏   support Message
43
          name: `${prefixKey}${name}`,
7c15ac9e   梁灏   add Message compo...
44
          duration: duration,
6cadeba4   梁灏   support Message
45
          styles: {},
40f8606f   梁灏   add Notice component
46
          transitionName: 'move-up',
7c15ac9e   梁灏   add Message compo...
47
48
49
50
51
52
          content: `
              <div class="${prefixCls}-custom-content ${prefixCls}-${type}">
                  <i class="${iconPrefixCls} ${iconPrefixCls}-${iconType}${loadCls}"></i>
                  <span>${content}</span>
              </div>
          `,
e0bd31a6   Aresn   update Message
53
54
55
          onClose: onClose,
          closable: closable,
          type: 'message'
7c15ac9e   梁灏   add Message compo...
56
      });
47e58396   梁灏   message component...
57
58
59
  
      // 用于手动消除
      return (function () {
6cadeba4   梁灏   support Message
60
          let target = name++;
47e58396   梁灏   message component...
61
62
63
  
          return function () {
              instance.remove(`${prefixKey}${target}`);
b0893113   jingsam   :art: add eslint
64
          };
47e58396   梁灏   message component...
65
      })();
7c15ac9e   梁灏   add Message compo...
66
67
68
  }
  
  export default {
6e2f40ea   Rijn   assign names to C...
69
70
      name: 'Message',
  
e0bd31a6   Aresn   update Message
71
      info (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
72
          return this.message('info', options);
7c15ac9e   梁灏   add Message compo...
73
      },
e0bd31a6   Aresn   update Message
74
      success (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
75
          return this.message('success', options);
7c15ac9e   梁灏   add Message compo...
76
      },
e0bd31a6   Aresn   update Message
77
      warning (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
78
          return this.message('warning', options);
7c15ac9e   梁灏   add Message compo...
79
      },
e0bd31a6   Aresn   update Message
80
      error (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
81
          return this.message('error', options);
7c15ac9e   梁灏   add Message compo...
82
      },
e0bd31a6   Aresn   update Message
83
      loading (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
84
85
86
87
          return this.message('loading', options);
      },
      message(type, options){
          if (typeof options === 'string') {
e0bd31a6   Aresn   update Message
88
89
90
91
              options = {
                  content: options
              };
          }
23e20abf   Sergio Crisostomo   DRY message/index...
92
          return notice(options.content, options.duration, type, options.onClose, options.closable);
7c15ac9e   梁灏   add Message compo...
93
94
      },
      config (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
95
96
          if (options.top || options.top === 0) {
              defaults.top = options.top;
7c15ac9e   梁灏   add Message compo...
97
          }
23e20abf   Sergio Crisostomo   DRY message/index...
98
99
          if (options.duration || options.duration === 0) {
              defaults.duration = options.duration;
7c15ac9e   梁灏   add Message compo...
100
          }
d3671687   梁灏   add destroy() for...
101
102
103
104
      },
      destroy () {
          let instance = getMessageInstance();
          messageInstance = null;
1c82a9ab   Lawrence Lee   bugfix: notice & ...
105
          instance.destroy('ivu-message');
7c15ac9e   梁灏   add Message compo...
106
      }
23e20abf   Sergio Crisostomo   DRY message/index...
107
  };