Skip to content

Commit

Permalink
Allow return single Style instance from style function
Browse files Browse the repository at this point in the history
  • Loading branch information
ghettovoice committed Oct 3, 2019
1 parent 02a16d1 commit 42614ff
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 113 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

205 changes: 99 additions & 106 deletions src/component/style-func/style.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import { Style } from 'ol/style'
import { style, stylesContainer } from '../../mixin'
import { hasMap } from '../../util/assert'
import { warn } from '../../util/log'
Expand All @@ -10,115 +11,9 @@
* Plays the role of both a style that mounts itself to style target component (vl-layer-vector, vl-feature & etc.)
* and style target for inner style containers (vl-style-box) as fallback style.
*/
const props = {
/**
* @type {function(): function(feature: Feature): Style}
*/
factory: {
type: Function,
required: true,
},
}
const computed = {
styleFunc () {
let func = this.factory()
if (!isFunction(func)) {
if (process.env.NODE_ENV !== 'production') {
warn(`Factory returned a value not of Function type, fallback style will be used`)
}
func = noop
}
return func
},
}
const methods = {
/**
* @return {function(feature: Feature): Style}
* @protected
*/
createStyle () {
hasMap(this)
// user provided style function
const providedStyleFunc = this.styleFunc
// fallback style function made from inner style containers
const fallbackStyleFunc = this.createStyleFunc()
return function __styleFunc (feature, resolution) {
const styles = providedStyleFunc(feature, resolution)
// not empty or null style
if (
styles === null ||
(Array.isArray(styles) && styles.length)
) {
return styles
}
return fallbackStyleFunc(feature, resolution)
}
},
/**
* @return {void}
* @protected
*/
mount () {
this.$stylesContainer && this.$stylesContainer.addStyle(this)
},
/**
* @return {void}
* @protected
*/
unmount () {
this.$stylesContainer && this.$stylesContainer.removeStyle(this)
},
/**
* @returns {Object}
* @protected
*/
getServices () {
const vm = this
return mergeDescriptors(this::style.methods.getServices(), {
get stylesContainer () { return vm },
})
},
/**
* Overrides stylesContainer `setStyle` method
* @param {Array<{ style: Style, condition: (function|boolean|undefined) }>|function(feature: Feature): Style|Vue|undefined} styles
* @return {void}
*/
setStyle (styles) {
if (styles !== this._styles) {
// simply save all inner styles and
// use them later in style function as fallback
this._styles = styles
this.scheduleRefresh()
}
},
/**
* @return {Promise}
*/
refresh () {
// recreate style
return this.recreate()
},
}
const watch = {
factory () {
this.scheduleRefresh()
},
}
export default {
name: 'vl-style-func',
mixins: [style, stylesContainer],
props,
computed,
methods,
watch,
stubVNode: {
empty: false,
attrs () {
Expand All @@ -128,5 +23,103 @@
}
},
},
props: {
/**
* @type {function(): function(feature: Feature): Style}
*/
factory: {
type: Function,
required: true,
},
},
computed: {
styleFunc () {
let func = this.factory()
if (!isFunction(func)) {
if (process.env.NODE_ENV !== 'production') {
warn(`Factory returned a value not of Function type, fallback style will be used`)
}
func = noop
}
return func
},
},
methods: {
/**
* @return {function(feature: Feature): Style}
* @protected
*/
createStyle () {
hasMap(this)
// user provided style function
const providedStyleFunc = this.styleFunc
// fallback style function made from inner style containers
const fallbackStyleFunc = this.createStyleFunc()
return function __styleFunc (feature, resolution) {
const styles = providedStyleFunc(feature, resolution)
// not empty or null style
if (
styles === null ||
(Array.isArray(styles) && styles.length) ||
styles instanceof Style
) {
return styles
}
return fallbackStyleFunc(feature, resolution)
}
},
/**
* @return {void}
* @protected
*/
mount () {
this.$stylesContainer && this.$stylesContainer.addStyle(this)
},
/**
* @return {void}
* @protected
*/
unmount () {
this.$stylesContainer && this.$stylesContainer.removeStyle(this)
},
/**
* @returns {Object}
* @protected
*/
getServices () {
const vm = this
return mergeDescriptors(this::style.methods.getServices(), {
get stylesContainer () { return vm },
})
},
/**
* Overrides stylesContainer `setStyle` method
* @param {Array<{ style: Style, condition: (function|boolean|undefined) }>|function(feature: Feature): Style|Vue|undefined} styles
* @return {void}
*/
setStyle (styles) {
if (styles !== this._styles) {
// simply save all inner styles and
// use them later in style function as fallback
this._styles = styles
this.scheduleRefresh()
}
},
/**
* @return {Promise}
*/
refresh () {
// recreate style
return this.recreate()
},
},
watch: {
factory () {
this.scheduleRefresh()
},
},
}
</script>
4 changes: 3 additions & 1 deletion src/mixin/styles-container.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import { Style } from 'ol/style'
import { warn } from '../util/log'
import { isFunction, reduce } from '../util/minilo'

Expand Down Expand Up @@ -149,7 +150,8 @@ export default {
// not empty or null style
if (
styles === null ||
(Array.isArray(styles) && styles.length)
(Array.isArray(styles) && styles.length) ||
styles instanceof Style
) {
return styles
}
Expand Down
11 changes: 10 additions & 1 deletion test/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<vl-layer-vector id="countries" render-mode="image">
<vl-source-vector ident="countries-source" :features.sync="countries" :url="countriesUrl" />
<vl-style-func :factory="createCountriesStyleFunc" />
</vl-layer-vector>

<vl-layer-vector id="draw-target">
Expand All @@ -33,7 +34,7 @@
</template>

<script>
import { findPointOnSurface } from '../src/ol-ext'
import { findPointOnSurface, createStyle } from '../src/ol-ext'
export default {
name: 'app',
Expand Down Expand Up @@ -76,6 +77,14 @@
},
methods: {
pointOnSurface: findPointOnSurface,
createCountriesStyleFunc () {
return () => {
return createStyle({
fillColor: 'rgba(0, 0, 0, 0.4)',
strokeColor: 'blue',
})
}
},
},
mounted () {
},
Expand Down

0 comments on commit 42614ff

Please sign in to comment.