Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ol.layer.Layer: default values #690

Merged
merged 1 commit into from Jun 27, 2013
Merged

Conversation

fredj
Copy link
Member

@fredj fredj commented May 6, 2013

Move the default values into the getters (same as ol.View2D.prototype.getRotation), cleanup constructor

@twpayne
Copy link
Contributor

twpayne commented May 6, 2013

@fredj, let's chat about this tomorrow. There are many good ideas here, but I'd like to talk through the implications first.

@tschaub
Copy link
Member

tschaub commented May 7, 2013

I'll take the blame for adding the getRotation default and am interested to hear what comes of this discussion.

@tschaub tschaub closed this May 7, 2013
@elemoine
Copy link
Member

elemoine commented May 7, 2013

@tschaub, did you really mean to close this PR?

Also, the commit link you provided gives a 404.

@tschaub tschaub reopened this May 7, 2013
@tschaub
Copy link
Member

tschaub commented May 7, 2013

@tschaub, did you really mean to close this PR?

No, that's what I get for trying to make comments while driving. I meant to delete my comment instead of closing the pull request :) Still interested in hearing what was discussed here.

@twpayne
Copy link
Contributor

twpayne commented May 8, 2013

The reason for initialising the properties in a specific order was to ensure that visible was set last, to avoid unnecessary state changes. However, since this occurs in the constructor - and therefore when nothing is listening for events on the object, I think it is OK to use a goog.base(this, options) to initialise properties in an unspecified order and allow arbitrary properties.

I think moving the defaults into the getters is likely to cause strange problems. In fact, I think the defaults can be removed because they are handled already in getLayerState.

@elemoine
Copy link
Member

So if we do as @twpayne suggests it we should also change View2D:getRotation and View2D:getView2DState. View2D:getRotation woulld return undefined if no specific rotation has been set. And View2D:getView2DState would return 0 for the rotation value if getRotation returns undefined.

@twpayne
Copy link
Contributor

twpayne commented Jun 25, 2013

We've just chatted about this, @fredj can you rebase this on master for a re-review?

@fredj
Copy link
Member Author

fredj commented Jun 25, 2013

Ready for review, thanks.

@twpayne
Copy link
Contributor

twpayne commented Jun 25, 2013

Perfect. Please merge.

@twpayne
Copy link
Contributor

twpayne commented Jun 25, 2013

So if we do as @twpayne suggests it we should also change View2D:getRotation and View2D:getView2DState. View2D:getRotation woulld return undefined if no specific rotation has been set. And View2D:getView2DState would return 0 for the rotation value if getRotation returns undefined.

For info, I'm creating a PR for this.

@fredj
Copy link
Member Author

fredj commented Jun 25, 2013

Ready for review (again)

this.setOpacity(goog.isDef(options.opacity) ? options.opacity : 1);
this.setSaturation(goog.isDef(options.saturation) ? options.saturation : 1);
this.setVisible(goog.isDef(options.visible) ? options.visible : true);
options.brightness = goog.isDef(options.brightness) ? options.brightness : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modifies the options object passed to the constructor, which might not be desirable. Maybe modify a clone instead?

@twpayne twpayne mentioned this pull request Jun 25, 2013
@@ -69,6 +70,7 @@ describe('ol.layer.Layer', function() {
expect(layer.getOpacity()).to.be(0.5);
expect(layer.getSaturation()).to.be(5);
expect(layer.getVisible()).to.be(false);
expect(layer.get('foo')).to.be(42);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems an expected new feature of ol.layer.Layer: I was just curious to know what use case do you have in mind for adding support for custom properties to a layer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E.g. give names to layers. And then search layers by names.

var layer = new ol.layer.TileLayer({
  name: 'my layer'
  ...
});
map.addLayer(layer);

and then:

var layers = map.getLayers().getArray();
var myLayer = jQuery.grep(layers, function(layer, i) { return layer.get('name') === 'my layer'; })[0];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case is to allow users to set properties like name and id to simplify their application logic. Without it, you'd have to maintain these properties elsewhere, outside ol.layer.Layer. With it, you can set them directly on the layer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks @elemoine and @twpayne for the explanation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, it works for ol.layer.Layer but not for (at least) ol.layer.TileLayer.
See build/src/external/src/exports.js: the option object passed to the constructor is limited to ol.layer.TileLayerOptions:

arg = {
      brightness: options.brightness,
      contrast: options.contrast,
      hue: options.hue,
      opacity: options.opacity,
      preload: options.preload,
      saturation: options.saturation,
      source: options.source,
      visible: options.visible
};
// ...
goog.base(this, arg);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That patch:

diff --git a/bin/generate-exports.py b/bin/generate-exports.py
index 2ae2cd8..d098e0c 100755
--- a/bin/generate-exports.py
+++ b/bin/generate-exports.py
@@ -92,7 +92,7 @@ class Class(Exportable):
             lines.append(' */\n')
             lines.append('%s = function(options) {\n' % (self.export_name(),))
             lines.append('  /** @type {%s} */\n' % (self.object_literal.name,))
-            lines.append('  var arg;\n');
+            lines.append('  var arg = /** @type {%s} */ (options);\n' % (self.object_literal.name,));
             lines.append('  if (goog.isDefAndNotNull(options)) {\n')
             # FIXME: we modify the user's options object
             lines.append(''.join(
@@ -103,12 +103,8 @@ class Class(Exportable):
                              {'o': o, 'base': b.name, 'ctor': k.export_name(),
                               'extern': ol.extern_name()} \
                                      for o, ol, k, b in self.nested_options()))
-            lines.append('    arg = {')
-            lines.extend(','.join('\n      %s: options.%s' % (key, key) for key in sorted(self.object_literal.prop_types.keys())))
-            lines.append('\n    };\n')
-            lines.append('  } else {\n')
-            lines.append('    arg = /** @type {%s} */ (options);\n' % (self.object_literal.name,))
-            lines.append('  }\n')
+            lines.extend('\n'.join('    arg.%s = options.%s;' % (key, key) for key in sorted(self.object_literal.prop_types.keys())))
+            lines.append('\n  }\n')
             lines.append('  goog.base(this, arg);\n')
             lines.append('};\n')
             lines.append('goog.inherits(\n')

Seems to work for me. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elemoine I suppose that it works for ol.layer.Layer because of the unit test ...
Thanks for the patch, I'll give a try

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the unit tests run against ol in debug mode. So TileLayer would also work in the tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your patch works like a charm, see https://github.com/fredj/ol3/compare/layer-options

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Note that the tests prove nothing here.

fredj added a commit that referenced this pull request Jun 27, 2013
ol.layer.Layer: default values
@fredj fredj merged commit 07d7902 into openlayers:master Jun 27, 2013
@fredj fredj deleted the layer-options branch June 27, 2013 11:35
afabiani pushed a commit to geosolutions-it/openlayers that referenced this pull request May 22, 2017
call stopObserving before setting img.src to blank image as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants