diff --git a/.gitignore b/.gitignore index 889d38a5bd..43ed93388a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /bin/plovr*.jar /build/OpenLayers.js /build/ol3.js +/build/ol3-api.js /build/ol3-compiled.js /demos/*/advanced-optimizations.* /demos/*/debug.html diff --git a/Makefile b/Makefile index 7157ab6584..bd57db5f09 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ PLOVR_JAR=bin/plovr-4b3caf2b7d84.jar SRC = $(shell find externs src/ol -name \*.js) +API = $(shell find src/api -name \*.js) TARGETS = $(shell find demos -name advanced-optimizations.js -o -name simple-optimizations.js) comma := , empty := @@ -9,7 +10,13 @@ space := $(empty) $(empty) all: build demos webgl-debug.js .PHONY: build -build: build/ol3-compiled.js +build: build/ol3-api.js build/ol3-compiled.js + +build/ol3-api.js: $(PLOVR_JAR) $(SRC) base.json \ + build/ol3-api.json src/api/api.js + java -jar $(PLOVR_JAR) build build/ol3-api.json >$@ + @echo $@ "uncompressed:" $$(wc -c <$@) bytes + @echo $@ " compressed:" $$(gzip -9 -c <$@ | wc -c) bytes build/ol3-compiled.js: $(PLOVR_JAR) $(SRC) base.json \ build/ol3.json build/ol3.js @@ -55,11 +62,11 @@ demos/side-by-side/simple-optimizations.js: $(PLOVR_JAR) $(SRC) base.json \ .PHONY: serve serve: $(PLOVR_JAR) - java -jar $(PLOVR_JAR) serve build/ol3.json demos/*/*.json + java -jar $(PLOVR_JAR) serve build/ol3.json build/ol3-api.json demos/*/*.json .PHONY: lint lint: - gjslint --strict --limited_doc_files=$(subst $(space),$(comma),$(shell find externs -name \*.js)) $(SRC) $(filter-out $(TARGETS),$(shell find demos -name \*.js)) + gjslint --strict --limited_doc_files=$(subst $(space),$(comma),$(shell find externs -name \*.js)) $(SRC) $(API) $(filter-out $(TARGETS),$(shell find demos -name \*.js)) webgl-debug.js: curl https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/debug/webgl-debug.js > $@ diff --git a/build/ol3-api.json b/build/ol3-api.json new file mode 100644 index 0000000000..f45d301498 --- /dev/null +++ b/build/ol3-api.json @@ -0,0 +1,9 @@ +{ + + "id": "ol3-api", + + "inherits": "../base.json", + + "inputs": "src/api/api.js" + +} diff --git a/src/api/api.js b/src/api/api.js new file mode 100644 index 0000000000..d01d3c23bb --- /dev/null +++ b/src/api/api.js @@ -0,0 +1,142 @@ +goog.provide('ol3'); +goog.provide('ol3.layer'); + +goog.require('goog.dom'); +goog.require('ol.Coordinate'); +goog.require('ol.Layer'); +goog.require('ol.Map'); +goog.require('ol.Projection'); +goog.require('ol.createMap'); +goog.require('ol.layer.OpenStreetMap'); + + +goog.exportSymbol('ol3', ol3); + + +/** + * @typedef {Array.|ol.Coordinate|{x: number, y: number}} + */ +ol3.Coordinate; + + +/** + * @typedef {Array.|ol.Collection} + */ +ol3.Layers; + + +/** + * @typedef {{center: (ol3.Coordinate|undefined), + * layers: (ol3.Layers|undefined), + * renderTo: (Element|string|undefined), + * resolution: (number|undefined), + * zoom: (number|undefined)}} + */ +ol3.MapOptions; + + +/** + * @typedef {ol.Projection|string} + */ +ol3.Projection; + + +/** + * @param {ol3.Coordinate} coordinate Coordinate. + * @return {ol.Coordinate} Coordinate. + */ +ol3.coordinate = function(coordinate) { + if (coordinate instanceof ol.Coordinate) { + return coordinate; + } else if (goog.isArray(coordinate)) { + var array = /** @type {Array.} */ coordinate; + return new ol.Coordinate(array[1], array[0]); + } else if (goog.isObject(coordinate)) { + var object = /** @type {{x: number, y: number}} */ coordinate; + return new ol.Coordinate(object.x, object.y); + } else { + return null; + } +}; +goog.exportProperty(ol3, 'coordinate', ol3.coordinate); + + +goog.exportProperty(ol3, 'layer', ol3.layer); + + +/** + * @return {ol.Layer} Layer. + */ +ol3.layer.osm = function() { + return new ol.layer.OpenStreetMap(); +}; +goog.exportProperty(ol3.layer, 'osm', ol3.layer.osm); + + +/** + * @param {ol3.Layers} layers Layers. + * @return {ol.Collection} Layers. + */ +ol3.layers = function(layers) { + if (layers instanceof ol.Collection) { + return layers; + } else if (goog.isArray(layers)) { + return new ol.Collection(layers); + } else { + return null; + } +}; +goog.exportProperty(ol3, 'layers', ol3.layers); + + +/** + * @param {ol3.MapOptions=} opt_mapOptions Options. + * @return {ol.Map} Map. + */ +ol3.map = function(opt_mapOptions) { + var options = opt_mapOptions || {}; + var center = ol3.coordinate(/** @type {ol3.Coordinate} */ + (goog.object.get(options, 'center', null))); + var layers = ol3.layers(/** @type {ol3.Layers} */ + (goog.object.get(options, 'layers', null))); + var projection = ol3.projection(/** @type {ol3.Projection} */ + (goog.object.get(options, 'projection', 'EPSG:3857'))); + var resolution = /** @type {number|undefined} */ + goog.object.get(options, 'resolution'); + if (!goog.isDef(resolution) && goog.object.containsKey(options, 'zoom')) { + var zoom = /** @type {number} */ goog.object.get(options, 'zoom'); + resolution = ol.Projection.EPSG_3857_HALF_SIZE / (128 << zoom); + } + var target = goog.dom.getElement(/** @type {Element|string} */ + (goog.object.get(options, 'renderTo', 'map'))); + var userProjection = ol3.projection(/** @type {ol3.Projection} */ + (goog.object.get(options, 'userProjection', 'EPSG:4326'))); + var map = ol.createMap(target, { + 'layers': layers, + 'projection': projection, + 'resolution': resolution, + 'userProjection': userProjection + }); + if (!goog.isNull(center)) { + map.setUserCenter(center); + } + return map; +}; +goog.exportProperty(ol3, 'map', ol3.map); + + +/** + * @param {ol3.Projection} projection Projection. + * @return {ol.Projection} Projection. + */ +ol3.projection = function(projection) { + if (projection instanceof ol.Projection) { + return projection; + } else if (goog.isString(projection)) { + var code = /** @type {string} */ projection; + return ol.Projection.getFromCode(code); + } else { + return null; + } +}; +goog.exportProperty(ol3, 'projection', ol3.projection);