Blame view

src/components/message/index.js 2.7 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
  
  const iconTypes = {
11793743   梁灏   update Message Icons
16
17
18
19
20
      'info': 'ios-information-circle',
      'success': 'ios-checkmark-circle',
      'warning': 'ios-alert',
      'error': 'ios-close-circle',
      'loading': 'ios-sync'
7c15ac9e   梁灏   add Message compo...
21
22
23
24
25
  };
  
  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;
  }
  
b24be35a   zhigang.li   make Message and ...
34
  function notice (content = '', duration = defaults.duration, type, onClose = function () {}, closable = false, render = function () {}) {
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
          content: `
              <div class="${prefixCls}-custom-content ${prefixCls}-${type}">
11793743   梁灏   update Message Icons
49
                  <i class="${iconPrefixCls} ${iconPrefixCls}-${iconType} ${loadCls}"></i>
7c15ac9e   梁灏   add Message compo...
50
51
52
                  <span>${content}</span>
              </div>
          `,
b24be35a   zhigang.li   make Message and ...
53
          render: render,
e0bd31a6   Aresn   update Message
54
55
56
          onClose: onClose,
          closable: closable,
          type: 'message'
7c15ac9e   梁灏   add Message compo...
57
      });
47e58396   梁灏   message component...
58
59
60
  
      // 用于手动消除
      return (function () {
6cadeba4   梁灏   support Message
61
          let target = name++;
47e58396   梁灏   message component...
62
63
64
  
          return function () {
              instance.remove(`${prefixKey}${target}`);
b0893113   jingsam   :art: add eslint
65
          };
47e58396   梁灏   message component...
66
      })();
7c15ac9e   梁灏   add Message compo...
67
68
69
  }
  
  export default {
6e2f40ea   Rijn   assign names to C...
70
71
      name: 'Message',
  
e0bd31a6   Aresn   update Message
72
      info (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
73
          return this.message('info', options);
7c15ac9e   梁灏   add Message compo...
74
      },
e0bd31a6   Aresn   update Message
75
      success (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
76
          return this.message('success', options);
7c15ac9e   梁灏   add Message compo...
77
      },
e0bd31a6   Aresn   update Message
78
      warning (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
79
          return this.message('warning', options);
7c15ac9e   梁灏   add Message compo...
80
      },
e0bd31a6   Aresn   update Message
81
      error (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
82
          return this.message('error', options);
7c15ac9e   梁灏   add Message compo...
83
      },
e0bd31a6   Aresn   update Message
84
      loading (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
85
86
87
88
          return this.message('loading', options);
      },
      message(type, options){
          if (typeof options === 'string') {
e0bd31a6   Aresn   update Message
89
90
91
92
              options = {
                  content: options
              };
          }
b24be35a   zhigang.li   make Message and ...
93
          return notice(options.content, options.duration, type, options.onClose, options.closable, options.render);
7c15ac9e   梁灏   add Message compo...
94
95
      },
      config (options) {
23e20abf   Sergio Crisostomo   DRY message/index...
96
97
          if (options.top || options.top === 0) {
              defaults.top = options.top;
7c15ac9e   梁灏   add Message compo...
98
          }
23e20abf   Sergio Crisostomo   DRY message/index...
99
100
          if (options.duration || options.duration === 0) {
              defaults.duration = options.duration;
7c15ac9e   梁灏   add Message compo...
101
          }
d3671687   梁灏   add destroy() for...
102
103
104
105
      },
      destroy () {
          let instance = getMessageInstance();
          messageInstance = null;
1c82a9ab   Lawrence Lee   bugfix: notice & ...
106
          instance.destroy('ivu-message');
7c15ac9e   梁灏   add Message compo...
107
      }
23e20abf   Sergio Crisostomo   DRY message/index...
108
  };