import { Collection, Evented, Zondy } from '../../../base'
import {defaultValue, getGUID, toJSON} from '../../../util'
import { LayerType } from '../../../base/enum'
/**
* 子图层基类
* @class SubLayer
* @moduleEX LayerModule
* @extends Evented
* @param {Object} options 构造参数
* @param {String} [options.id] 图层id,不给则生成随机id
* @param {Layer} [options.layer] 父图层对象
* @param {Array} [options.sublayers = []] 包含的子图层对象
* @param {String} [options.title] 图层名称
* @param {Boolean} [options.visible] 图层可见性
*/
class SubLayer extends Evented {
constructor(options) {
super(options)
options = defaultValue(options, {})
/**
* 图层id,不给则生成随机id
* @member {String} SubLayer.prototype.id
*/
this.id = defaultValue(options.id, getGUID())
/**
* 父图层对象
* @member {Layer} SubLayer.prototype.layer
*/
this.layer = defaultValue(options.layer, undefined)
/**
* 包含的子图层对象
* @member {Array} SubLayer.prototype.sublayers
*/
this.sublayers = defaultValue(options.sublayers, new Collection())
/**
* 图层名称
* @member {String} SubLayer.prototype.title
*/
this.title = defaultValue(options.title || options.layerName, undefined)
/**
* 图层可见性
* @member {Boolean} SubLayer.prototype.visible
*/
// eslint-disable-next-line no-nested-ternary
this._visible = options.hasOwnProperty('visible')
? options.visible
: options.hasOwnProperty('isVisible')
? options.isVisible
: true
}
/**
* @description 转换为json对象
* @return {Object} json对象
*/
toJSON() {
const _sublayers = []
this.sublayers.forEach(function (sublayer) {
_sublayers.push(toJSON(sublayer, SubLayer))
})
return {
id: this.id,
sublayers: _sublayers,
title: this.title,
visible: this.visible
}
}
/**
* @description 克隆方法
* @return {SubLayer} 图层
*/
clone() {
return new SubLayer(this.toJSON())
}
}
Object.defineProperties(SubLayer.prototype, {
/**
* 子图层显示或隐藏
* @member {Boolean} SubLayer.prototype.visible
*/
visible: {
get() {
return this._visible
},
set(value) {
this._visible = value
// 更新子图层
if (this.layer && this.layer.refresh) {
switch (this.layer.type) {
case LayerType.igsMapImage:
this.layer._setLayers()
this.layer.refresh()
break
case LayerType.wms:
this.layer.setSubLayer(this)
break
default:
break
}
}
}
}
})
Zondy.Layer.SubLayer = SubLayer
export default SubLayer