SaveAsImage.js 5.7 KB

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