diff --git a/externs/olx.js b/externs/olx.js index 07660ef0f1..601ba00637 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1718,7 +1718,7 @@ olx.interaction.DragZoomOptions.prototype.style; * snapTolerance: (number|undefined), * type: ol.geom.GeometryType, * minPointsPerRing: (number|undefined), - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined), + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * geometryName: (string|undefined), * condition: (ol.events.ConditionType|undefined)}} * @api @@ -1765,7 +1765,7 @@ olx.interaction.DrawOptions.prototype.minPointsPerRing; /** * Style for sketch features. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.interaction.DrawOptions.prototype.style; @@ -1846,7 +1846,7 @@ olx.interaction.KeyboardZoomOptions.prototype.delta; /** * @typedef {{deleteCondition: (ol.events.ConditionType|undefined), * pixelTolerance: (number|undefined), - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined), + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * features: ol.Collection}} * @api */ @@ -1873,7 +1873,7 @@ olx.interaction.ModifyOptions.prototype.pixelTolerance; /** * FeatureOverlay style. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.interaction.ModifyOptions.prototype.style; @@ -1931,7 +1931,7 @@ olx.interaction.PinchZoomOptions.prototype.duration; * @typedef {{addCondition: (ol.events.ConditionType|undefined), * condition: (ol.events.ConditionType|undefined), * layers: (Array.|function(ol.layer.Layer): boolean|undefined), - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined), + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * removeCondition: (ol.events.ConditionType|undefined), * toggleCondition: (ol.events.ConditionType|undefined)}} * @api @@ -1976,7 +1976,7 @@ olx.interaction.SelectOptions.prototype.layers; /** * Style for the selected features (those in the FeatureOverlay). - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.interaction.SelectOptions.prototype.style; @@ -2501,7 +2501,7 @@ olx.layer.TileOptions.prototype.useInterimTilesOnError; * opacity: (number|undefined), * saturation: (number|undefined), * source: ol.source.Vector, - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined), + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined), * visible: (boolean|undefined)}} * @api */ @@ -2583,7 +2583,7 @@ olx.layer.VectorOptions.prototype.source; /** * Layer style. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.layer.VectorOptions.prototype.style; @@ -2598,7 +2598,7 @@ olx.layer.VectorOptions.prototype.visible; /** * @typedef {{features: (Array.|ol.Collection|undefined), * map: (ol.Map|undefined), - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined)}} + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined)}} * @api */ olx.FeatureOverlayOptions; @@ -2620,7 +2620,7 @@ olx.FeatureOverlayOptions.prototype.map; /** * Feature style. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.FeatureOverlayOptions.prototype.style; @@ -3549,7 +3549,7 @@ olx.source.ImageCanvasOptions.prototype.state; * ratio: (number|undefined), * resolutions: (Array.|undefined), * source: ol.source.Vector, - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined)}} + * style: (ol.style.Style|Array.|ol.style.StyleFunction|undefined)}} * @api */ olx.source.ImageVectorOptions; @@ -3602,7 +3602,7 @@ olx.source.ImageVectorOptions.prototype.source; /** * Style to use when rendering features to the canvas. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction|undefined} + * @type {ol.style.Style|Array.|ol.style.StyleFunction|undefined} */ olx.source.ImageVectorOptions.prototype.style; diff --git a/src/ol/feature.js b/src/ol/feature.js index 42b9466416..fd48539f50 100644 --- a/src/ol/feature.js +++ b/src/ol/feature.js @@ -291,69 +291,6 @@ ol.Feature.prototype.setGeometryName = function(name) { ol.feature.FeatureStyleFunction; -/** - * Default style function for features. - * @param {number} resolution Resolution. - * @return {Array.} Style. - * @this {ol.Feature} - */ -ol.feature.defaultFeatureStyleFunction = function(resolution) { - var fill = new ol.style.Fill({ - color: 'rgba(255,255,255,0.4)' - }); - var stroke = new ol.style.Stroke({ - color: '#3399CC', - width: 1.25 - }); - var styles = [ - new ol.style.Style({ - image: new ol.style.Circle({ - fill: fill, - stroke: stroke, - radius: 5 - }), - fill: fill, - stroke: stroke - }) - ]; - - // now that we've run it the first time, - // replace the function with a constant version - ol.feature.defaultFeatureStyleFunction = - /** @type {function(this:ol.Feature):Array.} */( - function(resolution) { - return styles; - }); - - return styles; -}; - - -/** - * A function that takes an {@link ol.Feature} and a `{number}` representing - * the view's resolution. The function should return an array of - * {@link ol.style.Style}. This way e.g. a vector layer can be styled. - * - * @typedef {function(ol.Feature, number): Array.} - * @api - */ -ol.feature.StyleFunction; - - -/** - * @param {ol.Feature} feature Feature. - * @param {number} resolution Resolution. - * @return {Array.} Style. - */ -ol.feature.defaultStyleFunction = function(feature, resolution) { - var featureStyleFunction = feature.getStyleFunction(); - if (!goog.isDef(featureStyleFunction)) { - featureStyleFunction = ol.feature.defaultFeatureStyleFunction; - } - return featureStyleFunction.call(feature, resolution); -}; - - /** * Convert the provided object into a feature style function. Functions passed * through unchanged. Arrays of ol.style.Style or single style objects wrapped @@ -388,39 +325,6 @@ ol.feature.createFeatureStyleFunction = function(obj) { }; -/** - * Convert the provided object into a style function. Functions passed through - * unchanged. Arrays of ol.style.Style or single style objects wrapped in a - * new style function. - * @param {ol.feature.StyleFunction|Array.|ol.style.Style} obj - * A style function, a single style, or an array of styles. - * @return {ol.feature.StyleFunction} A style function. - */ -ol.feature.createStyleFunction = function(obj) { - /** - * @type {ol.feature.StyleFunction} - */ - var styleFunction; - - if (goog.isFunction(obj)) { - styleFunction = /** @type {ol.feature.StyleFunction} */ (obj); - } else { - /** - * @type {Array.} - */ - var styles; - if (goog.isArray(obj)) { - styles = obj; - } else { - goog.asserts.assertInstanceof(obj, ol.style.Style); - styles = [obj]; - } - styleFunction = goog.functions.constant(styles); - } - return styleFunction; -}; - - /** * Default styles for editing features. * @return {Object.>} Styles diff --git a/src/ol/featureoverlay.js b/src/ol/featureoverlay.js index d955ee3251..09b7151342 100644 --- a/src/ol/featureoverlay.js +++ b/src/ol/featureoverlay.js @@ -8,9 +8,9 @@ goog.require('goog.object'); goog.require('ol.Collection'); goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); -goog.require('ol.feature'); goog.require('ol.render.EventType'); goog.require('ol.renderer.vector'); +goog.require('ol.style.Style'); @@ -63,19 +63,18 @@ ol.FeatureOverlay = function(opt_options) { /** * @private - * @type {ol.style.Style|Array.|ol.feature.StyleFunction} + * @type {ol.style.Style|Array.|ol.style.StyleFunction} */ this.style_ = null; /** * @private - * @type {ol.feature.StyleFunction|undefined} + * @type {ol.style.StyleFunction|undefined} */ this.styleFunction_ = undefined; - if (goog.isDef(options.style)) { - this.setStyle(options.style); - } + this.setStyle(goog.isDef(options.style) ? + options.style : ol.style.defaultStyleFunction); if (goog.isDef(options.features)) { if (goog.isArray(options.features)) { @@ -169,7 +168,7 @@ ol.FeatureOverlay.prototype.handleMapPostCompose_ = function(event) { } var styleFunction = this.styleFunction_; if (!goog.isDef(styleFunction)) { - styleFunction = ol.feature.defaultStyleFunction; + styleFunction = ol.style.defaultStyleFunction; } var replayGroup = /** @type {ol.render.IReplayGroup} */ (event.replayGroup); @@ -270,13 +269,13 @@ ol.FeatureOverlay.prototype.setMap = function(map) { * Set the style for features. This can be a single style object, an array * of styles, or a function that takes a feature and resolution and returns * an array of styles. - * @param {ol.style.Style|Array.|ol.feature.StyleFunction} style + * @param {ol.style.Style|Array.|ol.style.StyleFunction} style * Overlay style. * @api */ ol.FeatureOverlay.prototype.setStyle = function(style) { this.style_ = style; - this.styleFunction_ = ol.feature.createStyleFunction(style); + this.styleFunction_ = ol.style.createStyleFunction(style); this.render_(); }; @@ -284,7 +283,7 @@ ol.FeatureOverlay.prototype.setStyle = function(style) { /** * Get the style for features. This returns whatever was passed to the `style` * option at construction or to the `setStyle` method. - * @return {ol.style.Style|Array.|ol.feature.StyleFunction} + * @return {ol.style.Style|Array.|ol.style.StyleFunction} * Overlay style. * @api */ @@ -295,7 +294,7 @@ ol.FeatureOverlay.prototype.getStyle = function() { /** * Get the style function. - * @return {ol.feature.StyleFunction|undefined} Style function. + * @return {ol.style.StyleFunction|undefined} Style function. * @api */ ol.FeatureOverlay.prototype.getStyleFunction = function() { diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index d2ab9de4cc..efef4bb5a7 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -205,7 +205,7 @@ goog.inherits(ol.interaction.Draw, ol.interaction.Pointer); /** - * @return {ol.feature.StyleFunction} Styles. + * @return {ol.style.StyleFunction} Styles. */ ol.interaction.Draw.getDefaultStyleFunction = function() { var styles = ol.feature.createDefaultEditingStyles(); diff --git a/src/ol/interaction/modifyinteraction.js b/src/ol/interaction/modifyinteraction.js index 3d66b33c2d..28066d44cf 100644 --- a/src/ol/interaction/modifyinteraction.js +++ b/src/ol/interaction/modifyinteraction.js @@ -769,7 +769,7 @@ ol.interaction.Modify.prototype.updateSegmentIndices_ = function( /** - * @return {ol.feature.StyleFunction} Styles. + * @return {ol.style.StyleFunction} Styles. */ ol.interaction.Modify.getDefaultStyleFunction = function() { var style = ol.feature.createDefaultEditingStyles(); diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index d9edae5165..752d17f5ac 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -204,7 +204,7 @@ ol.interaction.Select.prototype.setMap = function(map) { /** - * @return {ol.feature.StyleFunction} Styles. + * @return {ol.style.StyleFunction} Styles. */ ol.interaction.Select.getDefaultStyleFunction = function() { var styles = ol.feature.createDefaultEditingStyles(); diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js index 90aae8bc6d..e7a9561967 100644 --- a/src/ol/layer/vectorlayer.js +++ b/src/ol/layer/vectorlayer.js @@ -1,8 +1,8 @@ goog.provide('ol.layer.Vector'); goog.require('goog.object'); -goog.require('ol.feature'); goog.require('ol.layer.Layer'); +goog.require('ol.style.Style'); /** @@ -39,21 +39,20 @@ ol.layer.Vector = function(opt_options) { /** * User provided style. - * @type {ol.style.Style|Array.|ol.feature.StyleFunction} + * @type {ol.style.Style|Array.|ol.style.StyleFunction} * @private */ this.style_ = null; /** * Style function for use within the library. - * @type {ol.feature.StyleFunction|undefined} + * @type {ol.style.StyleFunction|undefined} * @private */ this.styleFunction_ = undefined; - if (goog.isDef(options.style)) { - this.setStyle(options.style); - } + this.setStyle(goog.isDefAndNotNull(options.style) ? + options.style : ol.style.defaultStyleFunction); }; goog.inherits(ol.layer.Vector, ol.layer.Layer); @@ -72,7 +71,7 @@ ol.layer.Vector.prototype.getRenderOrder = function() { /** * Get the style for features. This returns whatever was passed to the `style` * option at construction or to the `setStyle` method. - * @return {ol.style.Style|Array.|ol.feature.StyleFunction} + * @return {ol.style.Style|Array.|ol.style.StyleFunction} * Layer style. * @api */ @@ -83,7 +82,7 @@ ol.layer.Vector.prototype.getStyle = function() { /** * Get the style function. - * @return {ol.feature.StyleFunction|undefined} Layer style function. + * @return {ol.style.StyleFunction|undefined} Layer style function. * @api */ ol.layer.Vector.prototype.getStyleFunction = function() { @@ -104,12 +103,12 @@ ol.layer.Vector.prototype.setRenderOrder = function(renderOrder) { * Set the style for features. This can be a single style object, an array * of styles, or a function that takes a feature and resolution and returns * an array of styles. - * @param {ol.style.Style|Array.|ol.feature.StyleFunction} style + * @param {ol.style.Style|Array.|ol.style.StyleFunction} style * Layer style. * @api */ ol.layer.Vector.prototype.setStyle = function(style) { this.style_ = style; - this.styleFunction_ = ol.feature.createStyleFunction(style); + this.styleFunction_ = ol.style.createStyleFunction(style); this.dispatchChangeEvent(); }; diff --git a/src/ol/renderer/canvas/canvasvectorlayerrenderer.js b/src/ol/renderer/canvas/canvasvectorlayerrenderer.js index 9160be7192..a2a1b2706b 100644 --- a/src/ol/renderer/canvas/canvasvectorlayerrenderer.js +++ b/src/ol/renderer/canvas/canvasvectorlayerrenderer.js @@ -6,7 +6,6 @@ goog.require('goog.events'); goog.require('ol.ViewHint'); goog.require('ol.dom'); goog.require('ol.extent'); -goog.require('ol.feature'); goog.require('ol.layer.Vector'); goog.require('ol.render.EventType'); goog.require('ol.render.canvas.ReplayGroup'); @@ -155,7 +154,7 @@ ol.renderer.canvas.VectorLayer.prototype.handleImageChange_ = ol.renderer.canvas.VectorLayer.prototype.prepareFrame = function(frameState, layerState) { - var vectorLayer = this.getLayer(); + var vectorLayer = /** @type {ol.layer.Vector} */ (this.getLayer()); goog.asserts.assertInstanceof(vectorLayer, ol.layer.Vector); var vectorSource = vectorLayer.getSource(); goog.asserts.assertInstanceof(vectorSource, ol.source.Vector); @@ -202,10 +201,6 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame = this.dirty_ = false; - var styleFunction = vectorLayer.getStyleFunction(); - if (!goog.isDef(styleFunction)) { - styleFunction = ol.feature.defaultStyleFunction; - } var replayGroup = new ol.render.canvas.ReplayGroup( ol.renderer.vector.getTolerance(resolution, pixelRatio), extent, @@ -217,10 +212,17 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame = * @this {ol.renderer.canvas.VectorLayer} */ function(feature) { - goog.asserts.assert(goog.isDef(styleFunction)); - var dirty = this.renderFeature( - feature, resolution, pixelRatio, styleFunction, replayGroup); - this.dirty_ = this.dirty_ || dirty; + var styles; + if (goog.isDef(feature.getStyleFunction())) { + styles = feature.getStyleFunction().call(feature, resolution); + } else if (goog.isDef(vectorLayer.getStyleFunction())) { + styles = vectorLayer.getStyleFunction()(feature, resolution); + } + if (goog.isDefAndNotNull(styles)) { + var dirty = this.renderFeature( + feature, resolution, pixelRatio, styles, replayGroup); + this.dirty_ = this.dirty_ || dirty; + } }; if (!goog.isNull(vectorLayerRenderOrder)) { /** @type {Array.} */ @@ -252,13 +254,12 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame = * @param {ol.Feature} feature Feature. * @param {number} resolution Resolution. * @param {number} pixelRatio Pixel ratio. - * @param {ol.feature.StyleFunction} styleFunction Style function. + * @param {Array.} styles Array of styles * @param {ol.render.canvas.ReplayGroup} replayGroup Replay group. * @return {boolean} `true` if an image is loading. */ ol.renderer.canvas.VectorLayer.prototype.renderFeature = - function(feature, resolution, pixelRatio, styleFunction, replayGroup) { - var styles = styleFunction(feature, resolution); + function(feature, resolution, pixelRatio, styles, replayGroup) { if (!goog.isDefAndNotNull(styles)) { return false; } diff --git a/src/ol/source/imagevectorsource.js b/src/ol/source/imagevectorsource.js index 4ce73c04cf..b9a7e44bbd 100644 --- a/src/ol/source/imagevectorsource.js +++ b/src/ol/source/imagevectorsource.js @@ -6,11 +6,11 @@ goog.require('goog.events.EventType'); goog.require('goog.vec.Mat4'); goog.require('ol.dom'); goog.require('ol.extent'); -goog.require('ol.feature'); goog.require('ol.render.canvas.ReplayGroup'); goog.require('ol.renderer.vector'); goog.require('ol.source.ImageCanvas'); goog.require('ol.source.Vector'); +goog.require('ol.style.Style'); goog.require('ol.vec.Mat4'); @@ -42,11 +42,11 @@ ol.source.ImageVector = function(options) { /** * @private - * @type {!ol.feature.StyleFunction} + * @type {!ol.style.StyleFunction} */ - this.styleFunction_ = goog.isDef(options.style) ? - ol.feature.createStyleFunction(options.style) : - ol.feature.defaultStyleFunction; + this.styleFunction_ = goog.isDefAndNotNull(options.style) ? + ol.style.createStyleFunction(options.style) : + ol.style.defaultStyleFunction; /** * @private diff --git a/src/ol/style/style.js b/src/ol/style/style.js index 2f84cbe1ca..b2aa63dabb 100644 --- a/src/ol/style/style.js +++ b/src/ol/style/style.js @@ -1,7 +1,11 @@ goog.provide('ol.style.Style'); +goog.require('goog.asserts'); +goog.require('goog.functions'); +goog.require('ol.style.Circle'); goog.require('ol.style.Fill'); goog.require('ol.style.Image'); +goog.require('ol.style.Stroke'); @@ -93,3 +97,84 @@ ol.style.Style.prototype.getText = function() { ol.style.Style.prototype.getZIndex = function() { return this.zIndex_; }; + + +/** + * A function that takes an {@link ol.Feature} and a `{number}` representing + * the view's resolution. The function should return an array of + * {@link ol.style.Style}. This way e.g. a vector layer can be styled. + * + * @typedef {function(ol.Feature, number): Array.} + * @api + */ +ol.style.StyleFunction; + + +/** + * Convert the provided object into a style function. Functions passed through + * unchanged. Arrays of ol.style.Style or single style objects wrapped in a + * new style function. + * @param {ol.style.StyleFunction|Array.|ol.style.Style} obj + * A style function, a single style, or an array of styles. + * @return {ol.style.StyleFunction} A style function. + */ +ol.style.createStyleFunction = function(obj) { + /** + * @type {ol.style.StyleFunction} + */ + var styleFunction; + + if (goog.isFunction(obj)) { + styleFunction = /** @type {ol.style.StyleFunction} */ (obj); + } else { + /** + * @type {Array.} + */ + var styles; + if (goog.isArray(obj)) { + styles = obj; + } else { + goog.asserts.assertInstanceof(obj, ol.style.Style); + styles = [obj]; + } + styleFunction = goog.functions.constant(styles); + } + return styleFunction; +}; + + +/** + * @param {ol.Feature} feature Feature. + * @param {number} resolution Resolution. + * @return {Array.} Style. + */ +ol.style.defaultStyleFunction = function(feature, resolution) { + var fill = new ol.style.Fill({ + color: 'rgba(255,255,255,0.4)' + }); + var stroke = new ol.style.Stroke({ + color: '#3399CC', + width: 1.25 + }); + var styles = [ + new ol.style.Style({ + image: new ol.style.Circle({ + fill: fill, + stroke: stroke, + radius: 5 + }), + fill: fill, + stroke: stroke + }) + ]; + + // now that we've run it the first time, + // replace the function with a constant version + ol.style.defaultStyleFunction = + /** @type {function(this:ol.Feature):Array.} */( + function(resolution) { + return styles; + }); + + return styles; +}; diff --git a/test/spec/ol/feature.test.js b/test/spec/ol/feature.test.js index 89cc2330bc..fefcec6c08 100644 --- a/test/spec/ol/feature.test.js +++ b/test/spec/ol/feature.test.js @@ -411,35 +411,6 @@ describe('ol.Feature', function() { }); -describe('ol.feature.createStyleFunction()', function() { - var style = new ol.style.Style(); - - it('creates a style function from a single style', function() { - var styleFunction = ol.feature.createStyleFunction(style); - expect(styleFunction()).to.eql([style]); - }); - - it('creates a style function from an array of styles', function() { - var styleFunction = ol.feature.createStyleFunction([style]); - expect(styleFunction()).to.eql([style]); - }); - - it('passes through a function', function() { - var original = function() { - return [style]; - }; - var styleFunction = ol.feature.createStyleFunction(original); - expect(styleFunction).to.be(original); - }); - - it('throws on (some) unexpected input', function() { - expect(function() { - ol.feature.createStyleFunction({bogus: 'input'}); - }).to.throwException(); - }); - -}); - describe('ol.feature.createFeatureStyleFunction()', function() { var style = new ol.style.Style(); diff --git a/test/spec/ol/layer/vectorlayer.test.js b/test/spec/ol/layer/vectorlayer.test.js index f00c97ba41..92e1d790b7 100644 --- a/test/spec/ol/layer/vectorlayer.test.js +++ b/test/spec/ol/layer/vectorlayer.test.js @@ -1,5 +1,7 @@ goog.provide('ol.test.layer.Vector'); +goog.require('ol.feature'); + describe('ol.layer.Vector', function() { describe('constructor', function() { @@ -74,9 +76,10 @@ describe('ol.layer.Vector', function() { var layer = new ol.layer.Vector({ source: source }); - expect(layer.getStyleFunction()).to.be(undefined); + expect(layer.getStyleFunction()).to.be(ol.style.defaultStyleFunction); layer.setStyle(style); - expect(layer.getStyleFunction()).to.be.a('function'); + expect(layer.getStyleFunction()).not.to.be( + ol.style.defaultStyleFunction); }); }); @@ -91,7 +94,7 @@ describe('ol.layer.Vector', function() { source: source }); - expect(layer.getStyle()).to.be(null); + expect(layer.getStyle()).to.be(ol.style.defaultStyleFunction); layer.setStyle(style); expect(layer.getStyle()).to.be(style); diff --git a/test/spec/ol/renderer/canvas/canvasvectorlayerrenderer.test.js b/test/spec/ol/renderer/canvas/canvasvectorlayerrenderer.test.js new file mode 100644 index 0000000000..ada7c28f77 --- /dev/null +++ b/test/spec/ol/renderer/canvas/canvasvectorlayerrenderer.test.js @@ -0,0 +1,72 @@ +goog.provide('ol.test.renderer.canvas.VectorLayer'); + +describe('ol.renderer.canvas.VectorLayer', function() { + + describe('constructor', function() { + + it('creates a new instance', function() { + var map = new ol.Map({ + target: document.createElement('div') + }); + var layer = new ol.layer.Vector({ + source: new ol.source.Vector() + }); + var renderer = new ol.renderer.canvas.VectorLayer(map.getRenderer(), + layer); + expect(renderer).to.be.a(ol.renderer.canvas.VectorLayer); + }); + + it('gives precedence to feature styles over layer styles', function() { + var target = document.createElement('div'); + target.style.width = '256px'; + target.style.height = '256px'; + document.body.appendChild(target); + var map = new ol.Map({ + view: new ol.View({ + center: [0, 0], + zoom: 0 + }), + target: target + }); + var layerStyle = [new ol.style.Style({ + text: new ol.style.Text({ + text: 'layer' + }) + })]; + var featureStyle = [new ol.style.Style({ + text: new ol.style.Text({ + text: 'feature' + }) + })]; + var feature1 = new ol.Feature(new ol.geom.Point([0, 0])); + var feature2 = new ol.Feature(new ol.geom.Point([0, 0])); + feature2.setStyle(featureStyle); + var layer = new ol.layer.Vector({ + source: new ol.source.Vector({ + features: [feature1, feature2] + }), + style: layerStyle + }); + map.addLayer(layer); + var spy = sinon.spy(map.getRenderer().getLayerRenderer(layer), + 'renderFeature'); + map.renderSync(); + expect(spy.getCall(0).args[3]).to.be(layerStyle); + expect(spy.getCall(1).args[3]).to.be(featureStyle); + document.body.removeChild(target); + }); + + }); + +}); + + +goog.require('ol.Feature'); +goog.require('ol.Map'); +goog.require('ol.View'); +goog.require('ol.geom.Point'); +goog.require('ol.layer.Vector'); +goog.require('ol.renderer.canvas.VectorLayer'); +goog.require('ol.source.Vector'); +goog.require('ol.style.Style'); +goog.require('ol.style.Text'); diff --git a/test/spec/ol/style.test.js b/test/spec/ol/style.test.js new file mode 100644 index 0000000000..17eae20b5e --- /dev/null +++ b/test/spec/ol/style.test.js @@ -0,0 +1,32 @@ +goog.provide('ol.test.style.Style'); + +describe('ol.style.createStyleFunction()', function() { + var style = new ol.style.Style(); + + it('creates a style function from a single style', function() { + var styleFunction = ol.style.createStyleFunction(style); + expect(styleFunction()).to.eql([style]); + }); + + it('creates a style function from an array of styles', function() { + var styleFunction = ol.style.createStyleFunction([style]); + expect(styleFunction()).to.eql([style]); + }); + + it('passes through a function', function() { + var original = function() { + return [style]; + }; + var styleFunction = ol.style.createStyleFunction(original); + expect(styleFunction).to.be(original); + }); + + it('throws on (some) unexpected input', function() { + expect(function() { + ol.style.createStyleFunction({bogus: 'input'}); + }).to.throwException(); + }); + +}); + +goog.require('ol.style.Style');