Blame view

src/components/table/export-csv.js 2.36 KB
43509ad8   梁灏   Table support exp...
1
2
3
4
5
  function has (browser) {
      const ua = navigator.userAgent;
      if (browser === 'ie') {
          const isIE = ua.indexOf('compatible') > -1 && ua.indexOf('MSIE') > -1;
          if (isIE) {
b0893113   jingsam   :art: add eslint
6
              const reIE = new RegExp('MSIE (\\d+\\.\\d+);');
43509ad8   梁灏   Table support exp...
7
              reIE.test(ua);
b0893113   jingsam   :art: add eslint
8
              return parseFloat(RegExp['$1']);
43509ad8   梁灏   Table support exp...
9
          } else {
b0893113   jingsam   :art: add eslint
10
              return false;
43509ad8   梁灏   Table support exp...
11
12
13
14
15
16
17
18
19
20
21
          }
      } else {
          return ua.indexOf(browser) > -1;
      }
  }
  
  const csv = {
      _isIE11 () {
          let iev = 0;
          const ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
          const trident = !!navigator.userAgent.match(/Trident\/7.0/);
b0893113   jingsam   :art: add eslint
22
          const rv = navigator.userAgent.indexOf('rv:11.0');
43509ad8   梁灏   Table support exp...
23
24
25
26
  
          if (ieold) {
              iev = Number(RegExp.$1);
          }
b0893113   jingsam   :art: add eslint
27
          if (navigator.appVersion.indexOf('MSIE 10') !== -1) {
43509ad8   梁灏   Table support exp...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
              iev = 10;
          }
          if (trident && rv !== -1) {
              iev = 11;
          }
  
          return iev === 11;
      },
  
      _isEdge () {
          return /Edge/.test(navigator.userAgent);
      },
  
      _getDownloadUrl (text) {
b0893113   jingsam   :art: add eslint
42
          const BOM = '\uFEFF';
43509ad8   梁灏   Table support exp...
43
          // Add BOM to text for open in excel correctly
5d284605   lambert   allow use Blob to...
44
          if (window.Blob && window.URL && window.URL.createObjectURL) {
43509ad8   梁灏   Table support exp...
45
46
47
48
49
50
51
52
53
54
              const csvData = new Blob([BOM + text], { type: 'text/csv' });
              return URL.createObjectURL(csvData);
          } else {
              return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(text);
          }
      },
  
      download (filename, text) {
          if (has('ie') && has('ie') < 10) {
              // has module unable identify ie11 and Edge
b0893113   jingsam   :art: add eslint
55
              const oWin = window.top.open('about:blank', '_blank');
43509ad8   梁灏   Table support exp...
56
57
58
59
60
              oWin.document.charset = 'utf-8';
              oWin.document.write(text);
              oWin.document.close();
              oWin.document.execCommand('SaveAs', filename);
              oWin.close();
b0893113   jingsam   :art: add eslint
61
62
          } else if (has('ie') === 10 || this._isIE11() || this._isEdge()) {
              const BOM = '\uFEFF';
43509ad8   梁灏   Table support exp...
63
64
65
66
67
68
              const csvData = new Blob([BOM + text], { type: 'text/csv' });
              navigator.msSaveBlob(csvData, filename);
          } else {
              const link = document.createElement('a');
              link.download = filename;
              link.href = this._getDownloadUrl(text);
43509ad8   梁灏   Table support exp...
69
70
71
72
73
74
75
76
              document.body.appendChild(link);
              link.click();
              document.body.removeChild(link);
          }
      }
  };
  
  export default csv;