Skip to content

Commit

Permalink
SUpport a 'custom' constructor. Closes yourcelf#60
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkp authored and philipn committed Sep 26, 2011
1 parent a10f5a8 commit d255a1f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
26 changes: 25 additions & 1 deletion doc-src/olwidget.js.rst
Expand Up @@ -306,7 +306,31 @@ General map display
``'ve.shaded'``, ``'ve.aerial'``, ``'ve.hybrid'``, ``'wms.map'``,
``'wms.nasa'``, ``'yahoo.map'``, and ``'cloudmade.<num>'`` (where ``<num>``
is the number for a cloudmade style). A blank map can be obtained using
``'wms.blank'``. Additional providers or options can be manually added
``'wms.blank'``.
Other providers can be added by choosing ``custom.<name>`` where
``<name>`` corresponds to a property in a ``customBaseLayers``
object that you would need to create. It must have a ``class``
property corresponding to an OpenLayers layer subclass, and an
``args`` property that is a list of arguments to pass to that
constructor. For example::
var customBaseLayers = {
'opengeo_osm': # to use this, your olwidget layers would include ['custom.opengeo_osm']
{"class": "WMS", # The OpenLayers.Layer subclass to use.
"args": [ # These are passed as arguments to the constructor.
"OpenStreetMap (OpenGeo)",
"http://maps.opengeo.org/geowebcache/service/wms",
{"layers": "openstreetmap",
"format": "image/png",
"bgcolor": "#A1BDC4",
},
{"wrapDateLine": True
},
],
}}
Additional options and layers can also be manually added
using the normal OpenLayers apis (see `this provider example
<examples/other_providers.html>`_).
Expand Down
26 changes: 26 additions & 0 deletions js/olwidget.js
Expand Up @@ -144,6 +144,32 @@ var olwidget = {
});
}
},
custom: {
// Support for arbitrary OpenLayers base layers.
// To use this, ensure that customBaseLayers[type] exists, and
// has both a 'class' string (name of the OL constructor) and
// an 'args' array to pass to that constructor.
map: function(type) {
var classname = customBaseLayers[type]['class'];
var class_ = OpenLayers.Layer[classname];
var args = customBaseLayers[type].args;
// Can't use .apply() directly with an OL constructor,
// because we don't have a suitable `this` argument.
// Instead make a constructor function with a .prototype
// property the same as our class.prototype.
// The `new` keyword creates an instance from that prototype
// which will be used as `this` in the constructor call.
// `new` is also the only way to get the new instance to have
// the correct actual prototype, which is *not* the same as
// class.prototype. Thanks Tim Schaub for explaining this bit of
// javascript OOP to me.
var constructor = function() {
class_.prototype.initialize.apply(this, args);
};
constructor.prototype = class_.prototype;
return new constructor();
}
},
/*
* Utilities
*/
Expand Down

0 comments on commit d255a1f

Please sign in to comment.