SaveAsImage.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. /* global window, Uint8Array, document */
  42. import env from 'zrender/lib/core/env.js';
  43. import { ToolboxFeature } from '../featureManager.js';
  44. var SaveAsImage = /** @class */function (_super) {
  45. __extends(SaveAsImage, _super);
  46. function SaveAsImage() {
  47. return _super !== null && _super.apply(this, arguments) || this;
  48. }
  49. SaveAsImage.prototype.onclick = function (ecModel, api) {
  50. var model = this.model;
  51. var title = model.get('name') || ecModel.get('title.0.text') || 'echarts';
  52. var isSvg = api.getZr().painter.getType() === 'svg';
  53. var type = isSvg ? 'svg' : model.get('type', true) || 'png';
  54. var url = api.getConnectedDataURL({
  55. type: type,
  56. backgroundColor: model.get('backgroundColor', true) || ecModel.get('backgroundColor') || '#fff',
  57. connectedBackgroundColor: model.get('connectedBackgroundColor'),
  58. excludeComponents: model.get('excludeComponents'),
  59. pixelRatio: model.get('pixelRatio')
  60. });
  61. var browser = env.browser;
  62. // Chrome, Firefox, New Edge
  63. if (typeof MouseEvent === 'function' && (browser.newEdge || !browser.ie && !browser.edge)) {
  64. var $a = document.createElement('a');
  65. $a.download = title + '.' + type;
  66. $a.target = '_blank';
  67. $a.href = url;
  68. var evt = new MouseEvent('click', {
  69. // some micro front-end framework, window maybe is a Proxy
  70. view: document.defaultView,
  71. bubbles: true,
  72. cancelable: false
  73. });
  74. $a.dispatchEvent(evt);
  75. }
  76. // IE or old Edge
  77. else {
  78. // @ts-ignore
  79. if (window.navigator.msSaveOrOpenBlob || isSvg) {
  80. var parts = url.split(',');
  81. // data:[<mime type>][;charset=<charset>][;base64],<encoded data>
  82. var base64Encoded = parts[0].indexOf('base64') > -1;
  83. var bstr = isSvg
  84. // should decode the svg data uri first
  85. ? decodeURIComponent(parts[1]) : parts[1];
  86. // only `atob` when the data uri is encoded with base64
  87. // otherwise, like `svg` data uri exported by zrender,
  88. // there will be an error, for it's not encoded with base64.
  89. // (just a url-encoded string through `encodeURIComponent`)
  90. base64Encoded && (bstr = window.atob(bstr));
  91. var filename = title + '.' + type;
  92. // @ts-ignore
  93. if (window.navigator.msSaveOrOpenBlob) {
  94. var n = bstr.length;
  95. var u8arr = new Uint8Array(n);
  96. while (n--) {
  97. u8arr[n] = bstr.charCodeAt(n);
  98. }
  99. var blob = new Blob([u8arr]); // @ts-ignore
  100. window.navigator.msSaveOrOpenBlob(blob, filename);
  101. } else {
  102. var frame = document.createElement('iframe');
  103. document.body.appendChild(frame);
  104. var cw = frame.contentWindow;
  105. var doc = cw.document;
  106. doc.open('image/svg+xml', 'replace');
  107. doc.write(bstr);
  108. doc.close();
  109. cw.focus();
  110. doc.execCommand('SaveAs', true, filename);
  111. document.body.removeChild(frame);
  112. }
  113. } else {
  114. var lang = model.get('lang');
  115. var html = '' + '<body style="margin:0;">' + '<img src="' + url + '" style="max-width:100%;" title="' + (lang && lang[0] || '') + '" />' + '</body>';
  116. var tab = window.open();
  117. tab.document.write(html);
  118. tab.document.title = title;
  119. }
  120. }
  121. };
  122. SaveAsImage.getDefaultOption = function (ecModel) {
  123. var defaultOption = {
  124. show: true,
  125. icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
  126. title: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'title']),
  127. type: 'png',
  128. // Default use option.backgroundColor
  129. // backgroundColor: '#fff',
  130. connectedBackgroundColor: '#fff',
  131. name: '',
  132. excludeComponents: ['toolbox'],
  133. // use current pixel ratio of device by default
  134. // pixelRatio: 1,
  135. lang: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'lang'])
  136. };
  137. return defaultOption;
  138. };
  139. return SaveAsImage;
  140. }(ToolboxFeature);
  141. export default SaveAsImage;