SaveAsImage.js 5.7 KB

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