Add a layer selectionAPI property.
The active property can now be changed.
MODIFY
src/feature.js
src/feature.js
@@ -53,7 +53,7 @@ var feature = function (arg) {
// Don't bind handlers for improved performance on features that don't
// require it.
- if (!m_selectionAPI) {
+ if (!this.selectionAPI()) {
return;
}
@@ -545,16 +545,26 @@ var feature = function (arg) {
////////////////////////////////////////////////////////////////////////////
/**
- * Query or set if the selection API is enabled for this feature.
- * @returns {bool}
+ * Get/Set if the selection API is enabled for this feature.
+ *
+ * @param {boolean|undefined} val: undefined to return the selectionAPI
+ * state, or a boolean to change the state.
+ * @param {boolean} direct: if true, when getting the selectionAPI state,
+ * disregard the state of the parent layer, and when setting, refresh the
+ * state regardless of whether it has changed or not.
+ * @return {boolean|object} either the selectionAPI state (if getting) or the
+ * feature (if setting).
*/
////////////////////////////////////////////////////////////////////////////
- this.selectionAPI = function (arg) {
+ this.selectionAPI = function (arg, direct) {
if (arg === undefined) {
+ if (!direct && m_layer && m_layer.selectionAPI && !m_layer.selectionAPI()) {
+ return false;
+ }
return m_selectionAPI;
}
arg = !!arg;
- if (arg !== m_selectionAPI) {
+ if (arg !== m_selectionAPI || direct) {
m_selectionAPI = arg;
this._unbindMouseHandlers();
this._bindMouseHandlers();

MODIFY
src/featureLayer.js
src/featureLayer.js
@@ -31,6 +31,7 @@ var featureLayer = function (arg) {
s_exit = this._exit,
s_update = this._update,
s_visible = this.visible,
+ s_selectionAPI = this.selectionAPI,
s_draw = this.draw;
////////////////////////////////////////////////////////////////////////////
@@ -275,6 +276,33 @@ var featureLayer = function (arg) {
return m_this;
};
+ ////////////////////////////////////////////////////////////////////////////
+ /**
+ * Get/Set selectionAPI of the layer
+ *
+ * @param {boolean|undefined} val: undefined to return the selectionAPI
+ * state, or a boolean to change it.
+ * @return {boolean|object} either the selectionAPI state (if getting) or the
+ * layer (if setting).
+ */
+ ////////////////////////////////////////////////////////////////////////////
+ this.selectionAPI = function (val) {
+ if (val === undefined) {
+ return s_selectionAPI();
+ }
+ if (m_this.selectionAPI() !== val) {
+ s_selectionAPI(val);
+
+ // take a copy of the features; changing selectionAPI could mutate them.
+ var features = m_features.slice(), i;
+
+ for (i = 0; i < features.length; i += 1) {
+ features[i].selectionAPI(features[i].selectionAPI(undefined, true), true);
+ }
+ }
+ return m_this;
+ };
+
////////////////////////////////////////////////////////////////////////////
/**
* Clear all features in layer

MODIFY
src/layer.js
src/layer.js
@@ -56,6 +56,7 @@ var layer = function (arg) {
m_opacity = arg.opacity === undefined ? 1 : arg.opacity,
m_attribution = arg.attribution || null,
m_visible = arg.visible === undefined ? true : arg.visible,
+ m_selectionAPI = arg.selectionAPI === undefined ? true : arg.selectionAPI,
m_zIndex;
m_rendererName = checkRenderer(m_rendererName);
@@ -193,20 +194,27 @@ var layer = function (arg) {
////////////////////////////////////////////////////////////////////////////
/**
- * Get whether or not the layer is active. An active layer will receive
+ * Get/Set whether or not the layer is active. An active layer will receive
* native mouse when the layer is on top. Non-active layers will never
* receive native mouse events.
*
- * @returns {Boolean}
+ * @returns {Boolean|object}
*/
////////////////////////////////////////////////////////////////////////////
- this.active = function () {
+ this.active = function (arg) {
+ if (arg === undefined) {
return m_active;
+ }
+ if (m_active !== arg) {
+ m_active = arg;
+ m_node.toggleClass('active', m_active);
+ }
+ return this;
};
////////////////////////////////////////////////////////////////////////////
/**
- * Get/Set root node of the layer
+ * Get root node of the layer
*
* @returns {div}
*/
@@ -375,6 +383,26 @@ var layer = function (arg) {
return m_this;
};
+ ////////////////////////////////////////////////////////////////////////////
+ /**
+ * Get/Set selectionAPI of the layer
+ *
+ * @param {boolean|undefined} val: undefined to return the selectionAPI
+ * state, or a boolean to change it.
+ * @return {boolean|object} either the selectionAPI state (if getting) or the
+ * layer (if setting).
+ */
+ ////////////////////////////////////////////////////////////////////////////
+ this.selectionAPI = function (val) {
+ if (val === undefined) {
+ return m_selectionAPI;
+ }
+ if (m_selectionAPI !== val) {
+ m_selectionAPI = val;
+ }
+ return m_this;
+ };
+
////////////////////////////////////////////////////////////////////////////
/**
* Init layer

MODIFY
tests/cases/feature.js
tests/cases/feature.js
@@ -295,6 +295,21 @@ describe('geo.feature', function () {
expect(feat.selectionAPI()).toBe(true);
expect(feat.selectionAPI(0)).toBe(feat);
expect(feat.selectionAPI()).toBe(false);
+
+ // the layer can control the visibility
+ feat.selectionAPI(true);
+ expect(feat.selectionAPI()).toBe(true);
+ expect(feat.selectionAPI(undefined, true)).toBe(true);
+ layer.selectionAPI(false);
+ expect(feat.selectionAPI()).toBe(false);
+ expect(feat.selectionAPI(undefined, true)).toBe(true);
+ expect(feat.selectionAPI(false, true)).toBe(feat);
+ expect(feat.selectionAPI()).toBe(false);
+ expect(feat.selectionAPI(undefined, true)).toBe(false);
+ layer.selectionAPI(true);
+ expect(feat.selectionAPI()).toBe(false);
+ expect(feat.selectionAPI(true, true)).toBe(feat);
+ expect(feat.selectionAPI()).toBe(true);
});
});
});

MODIFY
tests/cases/featureLayer.js
tests/cases/featureLayer.js
@@ -140,6 +140,30 @@ describe('geo.featureLayer', function () {
expect(feat1.visible()).toBe(true);
expect(feat1.visible(undefined, true)).toBe(true);
});
+ it('selectionAPI', function () {
+ feat1.selectionAPI(true);
+ expect(layer.selectionAPI()).toBe(true);
+ expect(feat1.selectionAPI()).toBe(true);
+ expect(feat1.selectionAPI(undefined, true)).toBe(true);
+ expect(layer.selectionAPI(false)).toBe(layer);
+ expect(layer.selectionAPI()).toBe(false);
+ expect(feat1.selectionAPI()).toBe(false);
+ expect(feat1.selectionAPI(undefined, true)).toBe(true);
+ expect(layer.selectionAPI(true)).toBe(layer);
+ expect(layer.selectionAPI()).toBe(true);
+ expect(feat1.selectionAPI()).toBe(true);
+ expect(feat1.selectionAPI(undefined, true)).toBe(true);
+ });
+ it('active', function () {
+ expect(layer.active()).toBe(true);
+ expect(layer.node().hasClass('active')).toBe(true);
+ expect(layer.active(false)).toBe(layer);
+ expect(layer.active()).toBe(false);
+ expect(layer.node().hasClass('active')).toBe(false);
+ expect(layer.active(true)).toBe(layer);
+ expect(layer.active()).toBe(true);
+ expect(layer.node().hasClass('active')).toBe(true);
+ });
it('draw', function () {
sinon.stub(feat1, 'draw', function () {});
expect(layer.draw()).toBe(layer);

Add a layer selectionAPI property.

The active property can now be changed.