Skip to content

Commit

Permalink
Partially use strict types mode, add strictNullChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed May 1, 2019
1 parent 93d0ab6 commit 6d38eed
Show file tree
Hide file tree
Showing 170 changed files with 3,985 additions and 1,977 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ rules:
comma-dangle: 0
import/no-unresolved: 0
valid-jsdoc: 0
'@openlayers/valid-tsdoc':
- error
- requireReturn: false
max-len:
- error
- code: 110
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"esversion": 6
"esversion": 6,
"-W014": true
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ script:
- '! (npm run typecheck|sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"|grep ^src)'
- '! (npm run typecheck|grep api)'
- '! (npm run typecheck|grep gmf)'
- '! (npm run typecheck|grep test)'
- '! (npm run typecheck|grep test/)'
- '! (npm run typecheck|grep examples)'
- npm run doc
- npm run build-api
Expand Down
60 changes: 47 additions & 13 deletions api/src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ import {getFeaturesFromLayer} from './Querent.js';
import * as themes from './Themes.js';


/**
* @typedef {Object} MarkerOptions
* @property {[number, number]} [position]
* @property {string} [icon]
*/


/**
* @private
* @hidden
Expand All @@ -51,7 +58,9 @@ class Map {
* TODO: more options
*/
constructor(options) {

if (!constants.extent) {
throw new Error('Missing extent');
}
/**
* @private
* @type {View}
Expand Down Expand Up @@ -95,11 +104,15 @@ class Map {
}));
}
if (options.addMiniMap) {
const resolutions = this.view_.getResolutions();
if (!resolutions) {
throw new Error('Missing resolutions');
}
this.map_.addControl(new OverviewMap({
collapsed: !options.miniMapExpanded,
view: new View({
projection: this.view_.getProjection(),
resolutions: this.view_.getResolutions()
resolutions
})
}));
}
Expand Down Expand Up @@ -153,7 +166,7 @@ class Map {
const hasDescription = feature.get('description') !== undefined;
return hasId && hasTitle && hasDescription;
},
style: () => null
style: () => []
});
this.map_.addInteraction(this.selectInteraction_);

Expand Down Expand Up @@ -200,21 +213,27 @@ class Map {
}

/**
* @param {Object} options Options.
* @param {MarkerOptions} options Options.
* @property {import("ol/coordinate.js").Coordinate} position
* @property {string} [icon]
* @property {import("ol/size.js").Size} [size]
*/
addMarker(options = {}) {
const position = options.position ? options.position : this.view_.getCenter();
if (!position) {
throw new Error('Missing positon');
}
const marker = new Feature({
geometry: new Point(options.position ? options.position : this.view_.getCenter())
geometry: new Point(position)
});
if (options.icon) {
// FIXME: use size?
const image = new Icon({
src: options.icon
});
// @ts-ignore: OL issue
marker.setStyle(new Style({
image: new Icon({
src: options.icon
})
image
}));
}
this.vectorSource_.addFeature(marker);
Expand Down Expand Up @@ -267,7 +286,11 @@ class Map {
.then((text) => {
const attr = options.attr || ['title', 'description'];
const lines = text.split(/\r\n|\r|\n/);
const columns = lines.shift().split('\t');
const shiftedLines = lines.shift();
if (!shiftedLines) {
throw new Error('Missing shiftedLines');
}
const columns = shiftedLines.split('\t');
for (const line of lines) {
if (line) {
const values = zip(columns, line.split('\t'));
Expand All @@ -276,10 +299,14 @@ class Map {
});
marker.setProperties(filterByKeys(values, attr));
marker.setId(values.id);
// FIXME: handle values.iconSize
// FIXME: handle values.iconOffset
const image = new Icon({
src: values.icon
});
// @ts-ignore: OL issue
marker.setStyle(new Style({
image: new Icon({
src: values.icon
})
image
}));
this.vectorSource_.addFeature(marker);
}
Expand Down Expand Up @@ -308,7 +335,14 @@ class Map {
feature.getGeometry()
).getCoordinates();
const properties = feature.getProperties();
const content = this.overlay_.getElement().querySelector('.ol-popup-content');
const element = this.overlay_.getElement();
if (!element) {
throw new Error('Missing element');
}
const content = element.querySelector('.ol-popup-content');
if (!content) {
throw new Error('Missing content');
}
content.innerHTML = '';
content.innerHTML += `<div><b>${properties.title}</b></div>`;
content.innerHTML += `<p>${properties.description}</p>`;
Expand Down
30 changes: 19 additions & 11 deletions api/src/Themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ let themesPromise;
*/
function getThemesPromise() {
if (!themesPromise) {
if (!constants.themesUrl) {
throw new Error('Missing constants.themesUrl');
}
themesPromise = fetch(constants.themesUrl).then(response => response.json());
}
return themesPromise;
Expand Down Expand Up @@ -72,6 +75,7 @@ export function getBackgroundLayers() {
const layerWMS = /** @type {import('gmf/themes.js').GmfLayerWMS} */ (child);
return createWMSLayer(layerWMS, themes.ogcServers[child.ogcServer]);
}
throw new Error('Unknow layer type');
}));
promises.push(
groupPromise.then((layers) => {
Expand Down Expand Up @@ -123,14 +127,13 @@ export function getOverlayDefs() {
/**
* @param {Object} config Config
* @param {import('gmf/themes.js').GmfOgcServers} ogcServers OGC servers
* @param {import('gmf/themes.js').GmfOgcServer} [opt_ogcServer] OGC server
* @param {import('gmf/themes.js').GmfOgcServer} [opt_ogcServer] OGC server
* @returns {void}
* @hidden
*/
export function writeOverlayDefs(config, ogcServers, opt_ogcServer) {
const group = /** @type {import('gmf/themes.js').GmfGroup} */(config);
const ogcServer = opt_ogcServer ? opt_ogcServer :
group.ogcServer ? ogcServers[group.ogcServer] : undefined;
const ogcServer = opt_ogcServer ? opt_ogcServer : ogcServers[group.ogcServer];
if (group.children) {
for (const childConfig of group.children) {
writeOverlayDefs(childConfig, ogcServers, ogcServer);
Expand Down Expand Up @@ -187,15 +190,17 @@ export function getOverlayLayers(layerNames) {
* @hidden
*/
export function createWMSLayer(config, ogcServer) {
const source = new ImageWMS({
url: ogcServer.url,
projection: undefined, // should be removed in next OL version
params: {
'LAYERS': config.layers
},
serverType: ogcServer.type
});
// @ts-ignore: OL issue
const layer = new ImageLayer({
source: new ImageWMS({
url: ogcServer.url,
projection: undefined, // should be removed in next OL version
params: {
'LAYERS': config.layers
},
serverType: ogcServer.type
})
source
});
layer.set('title', config.name);
return Promise.resolve(layer);
Expand All @@ -213,6 +218,9 @@ export function createWMTSLayer(config) {
layer: config.layer,
matrixSet: config.matrixSet
});
if (!options) {
throw new Error('Missing options');
}
const source = new WMTS(options);
source.updateDimensions(config.dimensions);
const layer = new TileLayer({
Expand Down
17 changes: 13 additions & 4 deletions api/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import EPSG21781 from '@geoblocks/proj/src/EPSG_21781.js';


export default {
themesUrl: undefined,
/**
* @typedef {Object} APIConfig
* @property {?string} themesUrl
* @property {string} projection
* @property {Array<number>} resolutions
* @property {?[number, number, number, number]} extent
* @property {string} backgroundLayer
*/

export default /** @type {APIConfig} */({
themesUrl: null,
projection: EPSG21781,
resolutions: [250, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.25, 0.1, 0.05],
extent: undefined,
extent: null,
/**
* The name of the layer to use as background. May be a single value
* (WMTS) or a comma-separated list of layer names (WMS).
*/
backgroundLayer: 'orthophoto',
};
});
10 changes: 2 additions & 8 deletions contribs/gmf/examples/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ function MainController($scope, ngeoWMSTime) {
widget: TimePropertyWidgetEnum.DATEPICKER,
maxValue: '2013-12-31T00:00:00Z',
minValue: '2006-01-01T00:00:00Z',
maxDefValue: null,
minDefValue: null,
resolution: TimePropertyResolutionEnum.DAY,
mode: TimePropertyModeEnum.RANGE,
interval: [0, 1, 0, 0]
Expand All @@ -55,8 +53,6 @@ function MainController($scope, ngeoWMSTime) {
widget: /** @type {TimePropertyWidgetEnum} */ ('datepicker'),
maxValue: '2015-12-31T00:00:00Z',
minValue: '2014-01-01T00:00:00Z',
maxDefValue: null,
minDefValue: null,
resolution: /** @type {TimePropertyResolutionEnum}*/ ('month'),
mode: /** @type {TimePropertyModeEnum} */ ('value'),
interval: [0, 1, 0, 0]
Expand All @@ -65,12 +61,12 @@ function MainController($scope, ngeoWMSTime) {
/**
* @type {string}
*/
this.value;
this.value = '';

/**
* @type {string}
*/
this.rangeValue;
this.rangeValue = '';

this.onDateSelected = function(date) {
this.value = this.ngeoWMSTime_.formatWMSTimeParam(this.wmsTimeValueMode, date);
Expand All @@ -79,11 +75,9 @@ function MainController($scope, ngeoWMSTime) {
this.onDateRangeSelected = function(date) {
this.rangeValue = this.ngeoWMSTime_.formatWMSTimeParam(this.wmsTimeRangeMode, date);
};

}


module.controller('MainController', MainController);


export default module;
19 changes: 14 additions & 5 deletions contribs/gmf/examples/editfeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function MainController($scope, gmfEditFeature, gmfUser) {
* @type {import("ol/layer/Image.js").default}
* @private
*/
// @ts-ignore: OL issue
this.wmsLayer_ = new olLayerImage({
source: this.wmsSource_
});
Expand All @@ -95,7 +96,7 @@ function MainController($scope, gmfEditFeature, gmfUser) {
this.layerId_ = 113;

/**
* @type {import("ol/Feature.js").default}
* @type {?import("ol/Feature.js").default}
*/
this.feature = null;

Expand Down Expand Up @@ -143,6 +144,9 @@ MainController.prototype.handleMapSingleClick_ = function(evt) {
const map = this.map;
const view = map.getView();
const resolution = view.getResolution();
if (!resolution) {
throw new Error('Missing resolution');
}
const buffer = resolution * this.pixelBuffer_;
const extent = olExtent.buffer(
[coordinate[0], coordinate[1], coordinate[0], coordinate[1]],
Expand Down Expand Up @@ -186,6 +190,9 @@ MainController.prototype.insertFeature = function() {
const map = this.map;
const view = map.getView();
const resolution = view.getResolution();
if (!resolution) {
throw new Error('Missing resolution');
}
const buffer = resolution * -50; // 50 pixel buffer inside the extent
const size = /** @type {!Array.<number>} */ (map.getSize());
const extent = olExtent.buffer(
Expand Down Expand Up @@ -226,8 +233,9 @@ MainController.prototype.insertFeature = function() {
* Update the currently selected feature with a new name.
*/
MainController.prototype.updateFeature = function() {

console.assert(this.feature);
if (!this.feature) {
throw new Error('Missing feature');
}

this.pending = true;

Expand All @@ -248,8 +256,9 @@ MainController.prototype.updateFeature = function() {
* Delete currently selected feature.
*/
MainController.prototype.deleteFeature = function() {

console.assert(this.feature);
if (!this.feature) {
throw new Error('Missing feature');
}

// (1) Launch request
this.editFeature_.deleteFeature(
Expand Down
1 change: 1 addition & 0 deletions contribs/gmf/examples/layertree.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ function MainController(gmfTreeManager, gmfThemes, gmfThemeManager, ngeoLocation
if (n.id === node.id) {
return alreadyAdded = true;
}
return false;
});
if (!alreadyAdded) {
nodes.push(node);
Expand Down
1 change: 1 addition & 0 deletions contribs/gmf/examples/layertreeadd.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ function MainController(gmfTreeManager, gmfThemes, gmfThemeManager, ngeoLocation
if (n.id === node.id) {
return alreadyAdded = true;
}
return false;
});
if (!alreadyAdded) {
nodes.push(node);
Expand Down
2 changes: 1 addition & 1 deletion contribs/gmf/examples/lidarprofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.value('pytreeLidarprofileJsonUrl', 'https://sitn.ne.ch/pytree');
*/
function MainController($scope) {
/**
* @type {import("ol/geom/LineString.js").default}
* @type {?import("ol/geom/LineString.js").default}
*/
this.profileLine = null;

Expand Down
Loading

0 comments on commit 6d38eed

Please sign in to comment.