SaveAsImage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Uint8Array, document */
  42. import env from 'zrender/lib/core/env.js';
  43. import { ToolboxFeature } from '../featureManager.js';
  44. import { isFunction } from 'zrender/lib/core/util.js';
  45. /* global window, document */
  46. var SaveAsImage = /** @class */function (_super) {
  47. __extends(SaveAsImage, _super);
  48. function SaveAsImage() {
  49. return _super !== null && _super.apply(this, arguments) || this;
  50. }
  51. SaveAsImage.prototype.onclick = function (ecModel, api) {
  52. var model = this.model;
  53. var title = model.get('name') || ecModel.get('title.0.text') || 'echarts';
  54. var isSvg = api.getZr().painter.getType() === 'svg';
  55. var type = isSvg ? 'svg' : model.get('type', true) || 'png';
  56. var url = api.getConnectedDataURL({
  57. type: type,
  58. backgroundColor: model.get('backgroundColor', true) || ecModel.get('backgroundColor') || '#fff',
  59. connectedBackgroundColor: model.get('connectedBackgroundColor'),
  60. excludeComponents: model.get('excludeComponents'),
  61. pixelRatio: model.get('pixelRatio')
  62. });
  63. var browser = env.browser;
  64. // Chrome, Firefox, New Edge
  65. if (isFunction(MouseEvent) && (browser.newEdge || !browser.ie && !browser.edge)) {
  66. var $a = document.createElement('a');
  67. $a.download = title + '.' + type;
  68. $a.target = '_blank';
  69. $a.href = url;
  70. var evt = new MouseEvent('click', {
  71. // some micro front-end framework, window maybe is a Proxy
  72. view: document.defaultView,
  73. bubbles: true,
  74. cancelable: false
  75. });
  76. $a.dispatchEvent(evt);
  77. }
  78. // IE or old Edge
  79. else {
  80. // @ts-ignore
  81. if (window.navigator.msSaveOrOpenBlob || isSvg) {
  82. var parts = url.split(',');
  83. // data:[<mime type>][;charset=<charset>][;base64],<encoded data>
  84. var base64Encoded = parts[0].indexOf('base64') > -1;
  85. var bstr = isSvg
  86. // should decode the svg data uri first
  87. ? decodeURIComponent(parts[1]) : parts[1];
  88. // only `atob` when the data uri is encoded with base64
  89. // otherwise, like `svg` data uri exported by zrender,
  90. // there will be an error, for it's not encoded with base64.
  91. // (just a url-encoded string through `encodeURIComponent`)
  92. base64Encoded && (bstr = window.atob(bstr));
  93. var filename = title + '.' + type;
  94. // @ts-ignore
  95. if (window.navigator.msSaveOrOpenBlob) {
  96. var n = bstr.length;
  97. var u8arr = new Uint8Array(n);
  98. while (n--) {
  99. u8arr[n] = bstr.charCodeAt(n);
  100. }
  101. var blob = new Blob([u8arr]); // @ts-ignore
  102. window.navigator.msSaveOrOpenBlob(blob, filename);
  103. } else {
  104. var frame = document.createElement('iframe');
  105. document.body.appendChild(frame);
  106. var cw = frame.contentWindow;
  107. var doc = cw.document;
  108. doc.open('image/svg+xml', 'replace');
  109. doc.write(bstr);
  110. doc.close();
  111. cw.focus();
  112. doc.execCommand('SaveAs', true, filename);
  113. document.body.removeChild(frame);
  114. }
  115. } else {
  116. var lang = model.get('lang');
  117. var html = '' + '<body style="margin:0;">' + '<img src="' + url + '" style="max-width:100%;" title="' + (lang && lang[0] || '') + '" />' + '</body>';
  118. var tab = window.open();
  119. tab.document.write(html);
  120. tab.document.title = title;
  121. }
  122. }
  123. };
  124. SaveAsImage.getDefaultOption = function (ecModel) {
  125. var defaultOption = {
  126. show: true,
  127. 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',
  128. title: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'title']),
  129. type: 'png',
  130. // Default use option.backgroundColor
  131. // backgroundColor: '#fff',
  132. connectedBackgroundColor: '#fff',
  133. name: '',
  134. excludeComponents: ['toolbox'],
  135. // use current pixel ratio of device by default
  136. // pixelRatio: 1,
  137. lang: ecModel.getLocaleModel().get(['toolbox', 'saveAsImage', 'lang'])
  138. };
  139. return defaultOption;
  140. };
  141. return SaveAsImage;
  142. }(ToolboxFeature);
  143. export default SaveAsImage;