let ol = require('openlayers');
let AbstractStyle = require('./abstract');
let SymbolFabric = require('./../symbols/symbolFabric');
let LegendItem = require('./legendItem');
/**
* Класс UniformStyle обеспечивает рендеринг объектов слоя в одном стиле
* @extends AbstractStyle
*/
class UniformStyle extends AbstractStyle {
/**
* Создает экземпляр стиля
* @param params {object} - опции создания стиля
* @param params.geometryType {string} - тип геометрии
*/
constructor(params) {
super(params);
this.params = params;
this.type = "uniform";
this.geometryType = params.geometryType;
this.style = this.create(params);
}
/**
* Возвращает функцию стиля
* @return {styleFunction}
*/
getStyle() {
let self = this;
let style = self.style;
let params = self.params;
const styleFunction = function (feature, resolution) {
if (params.label) {
let minResolution = params.label.minResolution || 0;
let maxResolution = params.label.maxResolution || Infinity;
style.setText(SymbolFabric().text(params, feature));
if (resolution < minResolution || resolution > maxResolution) {
style.getText().setText('');
}
}
return style;
};
return styleFunction;
}
/**
* Устанавливает функцию стиля
* @param style
*/
setStyle(style) {
this.style = style;
}
create(params) {
let style;
try {
let fill = SymbolFabric().fill(params.fill);
let stroke = SymbolFabric().stroke(params.stroke);
let image = SymbolFabric().image(params.image);
let text = SymbolFabric().text(params.text);
let icon = SymbolFabric().icon(params.icon);
let diagram = SymbolFabric().diagram(params.diagram);
if (!diagram) {
style = new ol.style.Style({
fill: fill,
image: icon || image,
stroke: stroke,
text: text
});
}
if (diagram) {
style = diagram;
}
}
catch (e) {
console.log(e);
}
return style;
}
/**
* Массив объектов слоя
* @return {array}
*/
styleArray() {
let self = this;
let geometryType = this.geometryType || super.testGeometry(self.params);
let properties = self.params.defaultTemplate || {};
return [new LegendItem({
properties: properties,
style: self.params,
geometryType: geometryType
})];
}
/**
* Легенда слоя
* @return {*}
*/
legend() {
return this.styleArray();
}
}
module.exports = UniformStyle;