import { Zondy, defaultValue } from '@mapgis/webclient-common'
import * as L from '@mapgis/leaflet'
import EasyPrint from '@mapgis/leaflet-easyprint/src/index'
import Widget from '../Widget'
/**
* 打印工具
* @class PrintControl
* @moduleEX ToolModule
* @extends Widget
* @param {Object} options 构造参数
* @param {Array<String>} [options.sizeModes] 目前支持 ['Current', 'A4Landscape', 'A4Portrait']
* @param {String} [options.filename] 打印文件名
* @param {Boolean} [options.exportOnly] 是否仅导出
* @param {Boolean} [options.hideControlContainer] 隐藏控件容器
* @example <caption><h7>初始化打印工具</h7></caption>
* // ES5引入方式
* const { Map, MapView } = Zondy
* const { PrintControl } = Zondy.Widget
* // ES6引入方式
* import { Map, MapView, PrintControl } from "@mapgis/webclient-leaflet-plugin"
* // 初始化图层管理容器
* const map = new Map();
* // 初始化地图视图对象
* const mapView = new MapView({
* // 视图id
* viewId: "viewer-id",
* // 图层管理容器
* map: map
* });
* // 打印控件
* const printControl = new PrintControl({
* view: mapView,
* visible: true
* })
* // 添加控件
* mapView.ui.add(printControl)
*/
class PrintControl extends Widget {
constructor(options) {
super(options)
options = defaultValue(options, {})
/**
* 打印对象
*/
this._print = undefined
/**
* 打印图片类型,支持输出png、jpeg、svg格式图片
*/
this.format = defaultValue(options.format, 'png')
/**
* 浏览器下载文件名
*/
this.filename = defaultValue(options.filename, 'screenshotFile')
/**
* 加载EasyPrint插件
* @private
* */
this._initEasyPrint()
// 初始化截屏
this._addView()
}
_initEasyPrint() {
EasyPrint(L)
}
/**
* 添加视图打印对象
* @private
* */
_addView() {
const mapView = this._view._innerView
if (mapView && this._visible) {
// 添加打印控件
const printOptions = {
format: this.format,
sizeModes: ['Current', 'A4Landscape', 'A4Portrait'],
filename: '新地图',
exportOnly: true,
hidden: true,
hideControlContainer: true
}
if (this.width) {
printOptions.width = this.width
}
if (this.height) {
printOptions.height = this.height
}
this._print = L.EasyPrint(printOptions).addTo(mapView)
return this._print.printMap('CurrentSize', this.filename)
}
}
/**
* 添加视图打印对象
* @private
* */
_removeView() {
super._removeView()
const mapView = this._view._innerView
if (mapView && this._visible) {
if (this._print) {
mapView.removeControl(this._print)
this._print = null
}
}
}
}
Zondy.Widget.PrintControl = PrintControl
export default PrintControl