Replace IconOrigin enum with typedef

This commit is contained in:
Maximilian Krög
2022-07-17 03:22:12 +02:00
parent 7ec2725c1e
commit 62e335ceda
4 changed files with 36 additions and 66 deletions

View File

@@ -5,7 +5,6 @@ import Feature from '../Feature.js';
import Fill from '../style/Fill.js'; import Fill from '../style/Fill.js';
import GeometryCollection from '../geom/GeometryCollection.js'; import GeometryCollection from '../geom/GeometryCollection.js';
import Icon from '../style/Icon.js'; import Icon from '../style/Icon.js';
import IconOrigin from '../style/IconOrigin.js';
import ImageState from '../ImageState.js'; import ImageState from '../ImageState.js';
import LineString from '../geom/LineString.js'; import LineString from '../geom/LineString.js';
import MultiLineString from '../geom/MultiLineString.js'; import MultiLineString from '../geom/MultiLineString.js';
@@ -58,7 +57,7 @@ import {transformGeometryWithOptions} from './Feature.js';
* @property {import("../style/Icon.js").IconAnchorUnits} xunits Units of x. * @property {import("../style/Icon.js").IconAnchorUnits} xunits Units of x.
* @property {number} y Y coordinate. * @property {number} y Y coordinate.
* @property {import("../style/Icon.js").IconAnchorUnits} yunits Units of Y. * @property {import("../style/Icon.js").IconAnchorUnits} yunits Units of Y.
* @property {import("../style/IconOrigin.js").default} [origin] Origin. * @property {import("../style/Icon.js").IconOrigin} [origin] Origin.
*/ */
/** /**
@@ -333,7 +332,7 @@ function createStyleDefaults() {
DEFAULT_IMAGE_STYLE = new Icon({ DEFAULT_IMAGE_STYLE = new Icon({
anchor: DEFAULT_IMAGE_STYLE_ANCHOR, anchor: DEFAULT_IMAGE_STYLE_ANCHOR,
anchorOrigin: IconOrigin.BOTTOM_LEFT, anchorOrigin: 'bottom-left',
anchorXUnits: DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS, anchorXUnits: DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS,
anchorYUnits: DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS, anchorYUnits: DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS,
crossOrigin: 'anonymous', crossOrigin: 'anonymous',
@@ -1173,18 +1172,19 @@ function readStyleURL(node) {
function readVec2(node) { function readVec2(node) {
const xunits = node.getAttribute('xunits'); const xunits = node.getAttribute('xunits');
const yunits = node.getAttribute('yunits'); const yunits = node.getAttribute('yunits');
/** @type {import('../style/Icon.js').IconOrigin} */
let origin; let origin;
if (xunits !== 'insetPixels') { if (xunits !== 'insetPixels') {
if (yunits !== 'insetPixels') { if (yunits !== 'insetPixels') {
origin = IconOrigin.BOTTOM_LEFT; origin = 'bottom-left';
} else { } else {
origin = IconOrigin.TOP_LEFT; origin = 'top-left';
} }
} else { } else {
if (yunits !== 'insetPixels') { if (yunits !== 'insetPixels') {
origin = IconOrigin.BOTTOM_RIGHT; origin = 'bottom-right';
} else { } else {
origin = IconOrigin.TOP_RIGHT; origin = 'top-right';
} }
} }
return { return {
@@ -1265,7 +1265,8 @@ function iconStyleParser(node, objectStack) {
src = DEFAULT_IMAGE_STYLE_SRC; src = DEFAULT_IMAGE_STYLE_SRC;
} }
let anchor, anchorXUnits, anchorYUnits; let anchor, anchorXUnits, anchorYUnits;
let anchorOrigin = IconOrigin.BOTTOM_LEFT; /** @type {import('../style/Icon.js').IconOrigin|undefined} */
let anchorOrigin = 'bottom-left';
const hotSpot = /** @type {Vec2|undefined} */ (object['hotSpot']); const hotSpot = /** @type {Vec2|undefined} */ (object['hotSpot']);
if (hotSpot) { if (hotSpot) {
anchor = [hotSpot.x, hotSpot.y]; anchor = [hotSpot.x, hotSpot.y];
@@ -1325,7 +1326,7 @@ function iconStyleParser(node, objectStack) {
anchorYUnits: anchorYUnits, anchorYUnits: anchorYUnits,
crossOrigin: this.crossOrigin_, crossOrigin: this.crossOrigin_,
offset: offset, offset: offset,
offsetOrigin: IconOrigin.BOTTOM_LEFT, offsetOrigin: 'bottom-left',
rotation: rotation, rotation: rotation,
scale: scale, scale: scale,
size: size, size: size,

View File

@@ -2,7 +2,6 @@
* @module ol/style/Icon * @module ol/style/Icon
*/ */
import EventType from '../events/EventType.js'; import EventType from '../events/EventType.js';
import IconOrigin from './IconOrigin.js';
import ImageState from '../ImageState.js'; import ImageState from '../ImageState.js';
import ImageStyle from './Image.js'; import ImageStyle from './Image.js';
import {asArray} from '../color.js'; import {asArray} from '../color.js';
@@ -15,10 +14,15 @@ import {getUid} from '../util.js';
* Anchor unit can be either a fraction of the icon size or in pixels. * Anchor unit can be either a fraction of the icon size or in pixels.
*/ */
/**
* @typedef {'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'} IconOrigin
* Icon origin. One of 'bottom-left', 'bottom-right', 'top-left', 'top-right'.
*/
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {Array<number>} [anchor=[0.5, 0.5]] Anchor. Default value is the icon center. * @property {Array<number>} [anchor=[0.5, 0.5]] Anchor. Default value is the icon center.
* @property {import("./IconOrigin.js").default} [anchorOrigin='top-left'] Origin of the anchor: `bottom-left`, `bottom-right`, * @property {IconOrigin} [anchorOrigin='top-left'] Origin of the anchor: `bottom-left`, `bottom-right`,
* `top-left` or `top-right`. * `top-left` or `top-right`.
* @property {IconAnchorUnits} [anchorXUnits='fraction'] Units in which the anchor x value is * @property {IconAnchorUnits} [anchorXUnits='fraction'] Units in which the anchor x value is
* specified. A value of `'fraction'` indicates the x value is a fraction of the icon. A value of `'pixels'` indicates * specified. A value of `'fraction'` indicates the x value is a fraction of the icon. A value of `'pixels'` indicates
@@ -37,7 +41,7 @@ import {getUid} from '../util.js';
* @property {Array<number>} [offset=[0, 0]] Offset, which, together with the size and the offset origin, define the * @property {Array<number>} [offset=[0, 0]] Offset, which, together with the size and the offset origin, define the
* sub-rectangle to use from the original icon image. * sub-rectangle to use from the original icon image.
* @property {Array<number>} [displacement=[0,0]] Displacement of the icon. * @property {Array<number>} [displacement=[0,0]] Displacement of the icon.
* @property {import("./IconOrigin.js").default} [offsetOrigin='top-left'] Origin of the offset: `bottom-left`, `bottom-right`, * @property {IconOrigin} [offsetOrigin='top-left'] Origin of the offset: `bottom-left`, `bottom-right`,
* `top-left` or `top-right`. * `top-left` or `top-right`.
* @property {number} [opacity=1] Opacity of the icon. * @property {number} [opacity=1] Opacity of the icon.
* @property {number|import("../size.js").Size} [scale=1] Scale. * @property {number|import("../size.js").Size} [scale=1] Scale.
@@ -108,12 +112,10 @@ class Icon extends ImageStyle {
/** /**
* @private * @private
* @type {import("./IconOrigin.js").default} * @type {IconOrigin}
*/ */
this.anchorOrigin_ = this.anchorOrigin_ =
options.anchorOrigin !== undefined options.anchorOrigin !== undefined ? options.anchorOrigin : 'top-left';
? options.anchorOrigin
: IconOrigin.TOP_LEFT;
/** /**
* @private * @private
@@ -192,12 +194,10 @@ class Icon extends ImageStyle {
this.offset_ = options.offset !== undefined ? options.offset : [0, 0]; this.offset_ = options.offset !== undefined ? options.offset : [0, 0];
/** /**
* @private * @private
* @type {import("./IconOrigin.js").default} * @type {IconOrigin}
*/ */
this.offsetOrigin_ = this.offsetOrigin_ =
options.offsetOrigin !== undefined options.offsetOrigin !== undefined ? options.offsetOrigin : 'top-left';
? options.offsetOrigin
: IconOrigin.TOP_LEFT;
/** /**
* @private * @private
@@ -270,7 +270,7 @@ class Icon extends ImageStyle {
} }
} }
if (this.anchorOrigin_ != IconOrigin.TOP_LEFT) { if (this.anchorOrigin_ != 'top-left') {
if (!size) { if (!size) {
return null; return null;
} }
@@ -278,14 +278,14 @@ class Icon extends ImageStyle {
anchor = this.anchor_.slice(); anchor = this.anchor_.slice();
} }
if ( if (
this.anchorOrigin_ == IconOrigin.TOP_RIGHT || this.anchorOrigin_ == 'top-right' ||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT this.anchorOrigin_ == 'bottom-right'
) { ) {
anchor[0] = -anchor[0] + size[0]; anchor[0] = -anchor[0] + size[0];
} }
if ( if (
this.anchorOrigin_ == IconOrigin.BOTTOM_LEFT || this.anchorOrigin_ == 'bottom-left' ||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT this.anchorOrigin_ == 'bottom-right'
) { ) {
anchor[1] = -anchor[1] + size[1]; anchor[1] = -anchor[1] + size[1];
} }
@@ -369,7 +369,7 @@ class Icon extends ImageStyle {
} }
let offset = this.offset_; let offset = this.offset_;
if (this.offsetOrigin_ != IconOrigin.TOP_LEFT) { if (this.offsetOrigin_ != 'top-left') {
const size = this.getSize(); const size = this.getSize();
const iconImageSize = this.iconImage_.getSize(); const iconImageSize = this.iconImage_.getSize();
if (!size || !iconImageSize) { if (!size || !iconImageSize) {
@@ -377,14 +377,14 @@ class Icon extends ImageStyle {
} }
offset = offset.slice(); offset = offset.slice();
if ( if (
this.offsetOrigin_ == IconOrigin.TOP_RIGHT || this.offsetOrigin_ == 'top-right' ||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT this.offsetOrigin_ == 'bottom-right'
) { ) {
offset[0] = iconImageSize[0] - size[0] - offset[0]; offset[0] = iconImageSize[0] - size[0] - offset[0];
} }
if ( if (
this.offsetOrigin_ == IconOrigin.BOTTOM_LEFT || this.offsetOrigin_ == 'bottom-left' ||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT this.offsetOrigin_ == 'bottom-right'
) { ) {
offset[1] = iconImageSize[1] - size[1] - offset[1]; offset[1] = iconImageSize[1] - size[1] - offset[1];
} }

View File

@@ -1,30 +0,0 @@
/**
* @module ol/style/IconOrigin
*/
/**
* Icon origin. One of 'bottom-left', 'bottom-right', 'top-left', 'top-right'.
* @enum {string}
*/
export default {
/**
* Origin is at bottom left
* @api
*/
BOTTOM_LEFT: 'bottom-left',
/**
* Origin is at bottom right
* @api
*/
BOTTOM_RIGHT: 'bottom-right',
/**
* Origin is at top left
* @api
*/
TOP_LEFT: 'top-left',
/**
* Origin is at top right
* @api
*/
TOP_RIGHT: 'top-right',
};

View File

@@ -4,7 +4,6 @@ import Fill from '../../../../../src/ol/style/Fill.js';
import GeoJSON from '../../../../../src/ol/format/GeoJSON.js'; import GeoJSON from '../../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js'; import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import Icon from '../../../../../src/ol/style/Icon.js'; import Icon from '../../../../../src/ol/style/Icon.js';
import IconOrigin from '../../../../../src/ol/style/IconOrigin.js';
import ImageState from '../../../../../src/ol/ImageState.js'; import ImageState from '../../../../../src/ol/ImageState.js';
import KML, { import KML, {
getDefaultFillStyle, getDefaultFillStyle,
@@ -2429,7 +2428,7 @@ describe('ol.format.KML', function () {
if (f.getId() == 1) { if (f.getId() == 1) {
expect(imageStyle.anchor_[0]).to.be(0.5); expect(imageStyle.anchor_[0]).to.be(0.5);
expect(imageStyle.anchor_[1]).to.be(0.5); expect(imageStyle.anchor_[1]).to.be(0.5);
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_LEFT); expect(imageStyle.anchorOrigin_).to.be('bottom-left');
expect(imageStyle.anchorXUnits_).to.be('fraction'); expect(imageStyle.anchorXUnits_).to.be('fraction');
expect(imageStyle.anchorYUnits_).to.be('fraction'); expect(imageStyle.anchorYUnits_).to.be('fraction');
} else { } else {
@@ -2438,16 +2437,16 @@ describe('ol.format.KML', function () {
expect(imageStyle.anchorXUnits_).to.be('pixels'); expect(imageStyle.anchorXUnits_).to.be('pixels');
expect(imageStyle.anchorYUnits_).to.be('pixels'); expect(imageStyle.anchorYUnits_).to.be('pixels');
if (f.getId() == 2) { if (f.getId() == 2) {
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_LEFT); expect(imageStyle.anchorOrigin_).to.be('bottom-left');
} }
if (f.getId() == 3) { if (f.getId() == 3) {
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_RIGHT); expect(imageStyle.anchorOrigin_).to.be('bottom-right');
} }
if (f.getId() == 4) { if (f.getId() == 4) {
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.TOP_LEFT); expect(imageStyle.anchorOrigin_).to.be('top-left');
} }
if (f.getId() == 5) { if (f.getId() == 5) {
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.TOP_RIGHT); expect(imageStyle.anchorOrigin_).to.be('top-right');
} }
} }
expect(imageStyle.getRotation()).to.eql(0); expect(imageStyle.getRotation()).to.eql(0);