Blame view

src/components/notice/index.js 3.4 KB
40f8606f   梁灏   add Notice component
1
2
3
4
5
6
7
8
9
  import Notification from '../base/notification';
  
  const prefixCls = 'ivu-notice';
  const iconPrefixCls = 'ivu-icon';
  const prefixKey = 'ivu_notice_key_';
  
  let top = 24;
  let defaultDuration = 4.5;
  let noticeInstance;
833501a4   梁灏   support Notice
10
  let name = 1;
40f8606f   梁灏   add Notice component
11
12
  
  const iconTypes = {
f9d5b27b   梁灏   update Notice Icons
13
14
15
16
      'info': 'ios-information-circle',
      'success': 'ios-checkmark-circle',
      'warning': 'ios-alert',
      'error': 'ios-close-circle'
40f8606f   梁灏   add Notice component
17
18
19
20
21
  };
  
  function getNoticeInstance () {
      noticeInstance = noticeInstance || Notification.newInstance({
          prefixCls: prefixCls,
833501a4   梁灏   support Notice
22
          styles: {
40f8606f   梁灏   add Notice component
23
24
25
26
27
28
29
30
31
32
33
              top: `${top}px`,
              right: 0
          }
      });
  
      return noticeInstance;
  }
  
  function notice (type, options) {
      const title = options.title || '';
      const desc = options.desc || '';
833501a4   梁灏   support Notice
34
      const noticeKey = options.name || `${prefixKey}${name}`;
40f8606f   梁灏   add Notice component
35
      const onClose = options.onClose || function () {};
b24be35a   zhigang.li   make Message and ...
36
      const render = options.render;
40f8606f   梁灏   add Notice component
37
38
39
      // todo const btn = options.btn || null;
      const duration = (options.duration === 0) ? 0 : options.duration || defaultDuration;
  
833501a4   梁灏   support Notice
40
      name++;
40f8606f   梁灏   add Notice component
41
42
43
44
45
  
      let instance = getNoticeInstance();
  
      let content;
  
b24be35a   zhigang.li   make Message and ...
46
47
      let withIcon;
  
efc65a34   zhigang.li   fixed bug of noti...
48
      const with_desc = (options.render && !title) ? '' : (desc || options.render) ? ` ${prefixCls}-with-desc` : '';
03441255   梁灏   optimize Notice s...
49
  
40f8606f   梁灏   add Notice component
50
      if (type == 'normal') {
b24be35a   zhigang.li   make Message and ...
51
          withIcon = false;
40f8606f   梁灏   add Notice component
52
          content = `
ab26fdc6   何志勇   Class string error
53
              <div class="${prefixCls}-custom-content ${prefixCls}-with-normal ${with_desc}">
40f8606f   梁灏   add Notice component
54
55
56
57
58
59
                  <div class="${prefixCls}-title">${title}</div>
                  <div class="${prefixCls}-desc">${desc}</div>
              </div>
          `;
      } else {
          const iconType = iconTypes[type];
d3f31e39   梁灏   update Notice Icons
60
          const outlineIcon = with_desc === '' ? '' : '-outline';
b24be35a   zhigang.li   make Message and ...
61
          withIcon = true;
40f8606f   梁灏   add Notice component
62
          content = `
ab26fdc6   何志勇   Class string error
63
              <div class="${prefixCls}-custom-content ${prefixCls}-with-icon ${prefixCls}-with-${type} ${with_desc}">
40f8606f   梁灏   add Notice component
64
                  <span class="${prefixCls}-icon ${prefixCls}-icon-${type}">
d3f31e39   梁灏   update Notice Icons
65
                      <i class="${iconPrefixCls} ${iconPrefixCls}-${iconType}${outlineIcon}"></i>
40f8606f   梁灏   add Notice component
66
67
68
69
70
71
                  </span>
                  <div class="${prefixCls}-title">${title}</div>
                  <div class="${prefixCls}-desc">${desc}</div>
              </div>
          `;
      }
40f8606f   梁灏   add Notice component
72
      instance.notice({
833501a4   梁灏   support Notice
73
          name: noticeKey.toString(),
40f8606f   梁灏   add Notice component
74
          duration: duration,
833501a4   梁灏   support Notice
75
          styles: {},
be0769d4   Rijn   added move up tra...
76
          transitionName: 'move-notice',
40f8606f   梁灏   add Notice component
77
          content: content,
b24be35a   zhigang.li   make Message and ...
78
79
          withIcon: withIcon,
          render: render,
3f7a5f1a   zhigang.li   udpate notice
80
          hasTitle: !!title,
40f8606f   梁灏   add Notice component
81
          onClose: onClose,
e0bd31a6   Aresn   update Message
82
83
          closable: true,
          type: 'notice'
40f8606f   梁灏   add Notice component
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      });
  }
  
  export default {
      open (options) {
          return notice('normal', options);
      },
      info (options) {
          return notice('info', options);
      },
      success (options) {
          return notice('success', options);
      },
      warning (options) {
          return notice('warning', options);
      },
      error (options) {
          return notice('error', options);
      },
      config (options) {
          if (options.top) {
              top = options.top;
          }
          if (options.duration || options.duration === 0) {
              defaultDuration = options.duration;
          }
      },
833501a4   梁灏   support Notice
111
112
113
      close (name) {
          if (name) {
              name = name.toString();
40f8606f   梁灏   add Notice component
114
              if (noticeInstance) {
833501a4   梁灏   support Notice
115
                  noticeInstance.remove(name);
40f8606f   梁灏   add Notice component
116
117
118
119
120
121
122
123
              }
          } else {
              return false;
          }
      },
      destroy () {
          let instance = getNoticeInstance();
          noticeInstance = null;
1c82a9ab   Lawrence Lee   bugfix: notice & ...
124
          instance.destroy('ivu-notice');
40f8606f   梁灏   add Notice component
125
      }
ab26fdc6   何志勇   Class string error
126
  };