Skip to content

Commit 663a5c4

Browse files
authored
Merge pull request #682 from OpenGeoscience/layer-visible
Add a visible() and selectionAPI() functions to layers.
2 parents ac3de62 + a3df5ce commit 663a5c4

File tree

10 files changed

+289
-34
lines changed

10 files changed

+289
-34
lines changed

src/feature.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var feature = function (arg) {
5353

5454
// Don't bind handlers for improved performance on features that don't
5555
// require it.
56-
if (!m_selectionAPI) {
56+
if (!this.selectionAPI()) {
5757
return;
5858
}
5959

@@ -413,24 +413,37 @@ var feature = function (arg) {
413413
////////////////////////////////////////////////////////////////////////////
414414
/**
415415
* Get/Set visibility of the feature
416+
*
417+
* @param {boolean|undefined} val: undefined to return the visibility, a
418+
* boolean to change the visibility.
419+
* @param {boolean} direct: if true, when getting the visibility, disregard
420+
* the visibility of the parent layer, and when setting, refresh the state
421+
* regardless of whether it has changed or not.
422+
* @return {boolean|object} either the visibility (if getting) or the feature
423+
* (if setting).
416424
*/
417425
////////////////////////////////////////////////////////////////////////////
418-
this.visible = function (val) {
426+
this.visible = function (val, direct) {
419427
if (val === undefined) {
428+
if (!direct && m_layer && m_layer.visible && !m_layer.visible()) {
429+
return false;
430+
}
420431
return m_visible;
421432
}
422-
if (m_visible !== val) {
433+
if (m_visible !== val || direct) {
423434
m_visible = val;
424435
m_this.modified();
425-
436+
if (m_layer && m_layer.visible && !m_layer.visible()) {
437+
val = false;
438+
}
426439
// bind or unbind mouse handlers on visibility change
427-
if (m_visible) {
440+
if (val) {
428441
m_this._bindMouseHandlers();
429442
} else {
430443
m_this._unbindMouseHandlers();
431444
}
432445
for (var i = 0; i < m_dependentFeatures.length; i += 1) {
433-
m_dependentFeatures[i].visible(val);
446+
m_dependentFeatures[i].visible(m_visible, direct);
434447
}
435448
}
436449
return m_this;
@@ -532,16 +545,26 @@ var feature = function (arg) {
532545

533546
////////////////////////////////////////////////////////////////////////////
534547
/**
535-
* Query or set if the selection API is enabled for this feature.
536-
* @returns {bool}
548+
* Get/Set if the selection API is enabled for this feature.
549+
*
550+
* @param {boolean|undefined} val: undefined to return the selectionAPI
551+
* state, or a boolean to change the state.
552+
* @param {boolean} direct: if true, when getting the selectionAPI state,
553+
* disregard the state of the parent layer, and when setting, refresh the
554+
* state regardless of whether it has changed or not.
555+
* @return {boolean|object} either the selectionAPI state (if getting) or the
556+
* feature (if setting).
537557
*/
538558
////////////////////////////////////////////////////////////////////////////
539-
this.selectionAPI = function (arg) {
559+
this.selectionAPI = function (arg, direct) {
540560
if (arg === undefined) {
561+
if (!direct && m_layer && m_layer.selectionAPI && !m_layer.selectionAPI()) {
562+
return false;
563+
}
541564
return m_selectionAPI;
542565
}
543566
arg = !!arg;
544-
if (arg !== m_selectionAPI) {
567+
if (arg !== m_selectionAPI || direct) {
545568
m_selectionAPI = arg;
546569
this._unbindMouseHandlers();
547570
this._bindMouseHandlers();

src/featureLayer.js

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var featureLayer = function (arg) {
3030
s_init = this._init,
3131
s_exit = this._exit,
3232
s_update = this._update,
33+
s_visible = this.visible,
34+
s_selectionAPI = this.selectionAPI,
3335
s_draw = this.draw;
3436

3537
////////////////////////////////////////////////////////////////////////////
@@ -231,13 +233,72 @@ var featureLayer = function (arg) {
231233
*/
232234
////////////////////////////////////////////////////////////////////////////
233235
this.draw = function () {
234-
// Call sceneObject.draw, which calls draw on all child objects.
235-
s_draw();
236+
if (m_this.visible()) {
237+
// Call sceneObject.draw, which calls draw on all child objects.
238+
s_draw();
236239

237-
// Now call render on the renderer. In certain cases it may not do
238-
// anything if the if the child objects are drawn on the screen already.
239-
if (m_this.renderer()) {
240-
m_this.renderer()._render();
240+
// Now call render on the renderer. In certain cases it may not do
241+
// anything if the child objects are drawn on the screen already.
242+
if (m_this.renderer()) {
243+
m_this.renderer()._render();
244+
}
245+
}
246+
return m_this;
247+
};
248+
249+
////////////////////////////////////////////////////////////////////////////
250+
/**
251+
* Get/Set visibility of the layer
252+
*
253+
* @param {boolean|undefined} val: undefined to return the visibility, a
254+
* boolean to change the visibility.
255+
* @return {boolean|object} either the visibility (if getting) or the layer
256+
* (if setting).
257+
*/
258+
////////////////////////////////////////////////////////////////////////////
259+
this.visible = function (val) {
260+
if (val === undefined) {
261+
return s_visible();
262+
}
263+
if (m_this.visible() !== val) {
264+
s_visible(val);
265+
266+
// take a copy of the features; changing visible could mutate them.
267+
var features = m_features.slice(), i;
268+
269+
for (i = 0; i < features.length; i += 1) {
270+
features[i].visible(features[i].visible(undefined, true), true);
271+
}
272+
if (val) {
273+
m_this.draw();
274+
}
275+
}
276+
return m_this;
277+
};
278+
279+
////////////////////////////////////////////////////////////////////////////
280+
/**
281+
* Get/Set selectionAPI of the layer
282+
*
283+
* @param {boolean|undefined} val: undefined to return the selectionAPI
284+
* state, or a boolean to change it.
285+
* @return {boolean|object} either the selectionAPI state (if getting) or the
286+
* layer (if setting).
287+
*/
288+
////////////////////////////////////////////////////////////////////////////
289+
this.selectionAPI = function (val) {
290+
if (val === undefined) {
291+
return s_selectionAPI();
292+
}
293+
if (m_this.selectionAPI() !== val) {
294+
s_selectionAPI(val);
295+
296+
// take a copy of the features; changing selectionAPI could mutate them.
297+
var features = m_features.slice(), i;
298+
299+
for (i = 0; i < features.length; i += 1) {
300+
features[i].selectionAPI(features[i].selectionAPI(undefined, true), true);
301+
}
241302
}
242303
return m_this;
243304
};

src/gl/quadFeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ var gl_quadFeature = function (arg) {
373373
m_this._build();
374374
}
375375
if (m_actor_color) {
376-
m_actor_color.setVisible(m_this.visible());
376+
m_actor_color.setVisible(m_this.visible(undefined, true));
377377
m_actor_color.material().setBinNumber(m_this.bin());
378378
}
379379
if (m_actor_image) {
380-
m_actor_image.setVisible(m_this.visible());
380+
m_actor_image.setVisible(m_this.visible(undefined, true));
381381
m_actor_image.material().setBinNumber(m_this.bin());
382382
}
383383
m_this.updateTime().modified();

src/layer.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ var layer = function (arg) {
5555
m_active = arg.active === undefined ? true : arg.active,
5656
m_opacity = arg.opacity === undefined ? 1 : arg.opacity,
5757
m_attribution = arg.attribution || null,
58+
m_visible = arg.visible === undefined ? true : arg.visible,
59+
m_selectionAPI = arg.selectionAPI === undefined ? true : arg.selectionAPI,
5860
m_zIndex;
5961

6062
m_rendererName = checkRenderer(m_rendererName);
@@ -192,20 +194,27 @@ var layer = function (arg) {
192194

193195
////////////////////////////////////////////////////////////////////////////
194196
/**
195-
* Get whether or not the layer is active. An active layer will receive
197+
* Get/Set whether or not the layer is active. An active layer will receive
196198
* native mouse when the layer is on top. Non-active layers will never
197199
* receive native mouse events.
198200
*
199-
* @returns {Boolean}
201+
* @returns {Boolean|object}
200202
*/
201203
////////////////////////////////////////////////////////////////////////////
202-
this.active = function () {
203-
return m_active;
204+
this.active = function (arg) {
205+
if (arg === undefined) {
206+
return m_active;
207+
}
208+
if (m_active !== arg) {
209+
m_active = arg;
210+
m_node.toggleClass('active', m_active);
211+
}
212+
return this;
204213
};
205214

206215
////////////////////////////////////////////////////////////////////////////
207216
/**
208-
* Get/Set root node of the layer
217+
* Get root node of the layer
209218
*
210219
* @returns {div}
211220
*/
@@ -352,6 +361,48 @@ var layer = function (arg) {
352361
return m_attribution;
353362
};
354363

364+
////////////////////////////////////////////////////////////////////////////
365+
/**
366+
* Get/Set visibility of the layer
367+
*
368+
* @param {boolean|undefined} val: undefined to return the visibility, a
369+
* boolean to change the visibility.
370+
* @return {boolean|object} either the visibility (if getting) or the layer
371+
* (if setting).
372+
*/
373+
////////////////////////////////////////////////////////////////////////////
374+
this.visible = function (val) {
375+
if (val === undefined) {
376+
return m_visible;
377+
}
378+
if (m_visible !== val) {
379+
m_visible = val;
380+
m_node.css('display', m_visible ? '' : 'none');
381+
m_this.modified();
382+
}
383+
return m_this;
384+
};
385+
386+
////////////////////////////////////////////////////////////////////////////
387+
/**
388+
* Get/Set selectionAPI of the layer
389+
*
390+
* @param {boolean|undefined} val: undefined to return the selectionAPI
391+
* state, or a boolean to change it.
392+
* @return {boolean|object} either the selectionAPI state (if getting) or the
393+
* layer (if setting).
394+
*/
395+
////////////////////////////////////////////////////////////////////////////
396+
this.selectionAPI = function (val) {
397+
if (val === undefined) {
398+
return m_selectionAPI;
399+
}
400+
if (m_selectionAPI !== val) {
401+
m_selectionAPI = val;
402+
}
403+
return m_this;
404+
};
405+
355406
////////////////////////////////////////////////////////////////////////////
356407
/**
357408
* Init layer

src/pixelmapFeature.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,14 @@ var pixelmapFeature = function (arg) {
339339
m_quadFeature = m_this.layer().createFeature('quad', {
340340
selectionAPI: false,
341341
gcs: m_this.gcs(),
342-
visible: m_this.visible()
342+
visible: m_this.visible(undefined, true)
343343
});
344344
m_this.dependentFeatures([m_quadFeature]);
345-
m_quadFeature.style({image: m_info.canvas,
346-
position: m_this.style.get('position')})
347-
.data([{}])
348-
.draw();
345+
m_quadFeature.style({
346+
image: m_info.canvas,
347+
position: m_this.style.get('position')})
348+
.data([{}])
349+
.draw();
349350
}
350351
/* If we prepared the pixelmap and rendered it, send a prepared event */
351352
if (prepared) {

src/polygonFeature.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ var polygonFeature = function (arg) {
271271
m_lineFeature = m_this.layer().createFeature('line', {
272272
selectionAPI: false,
273273
gcs: m_this.gcs(),
274-
visible: m_this.visible()
274+
visible: m_this.visible(undefined, true)
275275
});
276276
m_this.dependentFeatures([m_lineFeature]);
277277
}

src/tileLayer.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ module.exports = (function () {
138138
*/
139139
//////////////////////////////////////////////////////////////////////////////
140140
var tileLayer = function (options) {
141+
'use strict';
142+
if (!(this instanceof tileLayer)) {
143+
return new tileLayer(options);
144+
}
145+
featureLayer.call(this, options);
141146

142147
var $ = require('jquery');
143148
var geo_event = require('./event');
@@ -147,11 +152,6 @@ module.exports = (function () {
147152
var adjustLayerForRenderer = require('./registry').adjustLayerForRenderer;
148153
var Tile = require('./tile');
149154

150-
if (!(this instanceof tileLayer)) {
151-
return new tileLayer(options);
152-
}
153-
featureLayer.call(this, options);
154-
155155
options = $.extend(true, {}, this.constructor.defaults, options || {});
156156
if (!options.cacheSize) {
157157
// this size should be sufficient for a 4k display
@@ -177,6 +177,7 @@ module.exports = (function () {
177177

178178
var s_init = this._init,
179179
s_exit = this._exit,
180+
s_visible = this.visible,
180181
m_lastTileSet = [],
181182
m_maxBounds = [],
182183
m_exited;
@@ -1063,6 +1064,9 @@ module.exports = (function () {
10631064
evt.event.event === geo_event.rotate)) {
10641065
return;
10651066
}
1067+
if (!this.visible()) {
1068+
return;
1069+
}
10661070
var map = this.map(),
10671071
bounds = map.bounds(undefined, null),
10681072
mapZoom = map.zoom(),
@@ -1430,6 +1434,30 @@ module.exports = (function () {
14301434
return m_tileOffsetValues[level];
14311435
};
14321436

1437+
////////////////////////////////////////////////////////////////////////////
1438+
/**
1439+
* Get/Set visibility of the layer
1440+
*
1441+
* @param {boolean|undefined} val: undefined to return the visibility, a
1442+
* boolean to change the visibility.
1443+
* @return {boolean|object} either the visibility (if getting) or the layer
1444+
* (if setting).
1445+
*/
1446+
////////////////////////////////////////////////////////////////////////////
1447+
this.visible = function (val) {
1448+
if (val === undefined) {
1449+
return s_visible();
1450+
}
1451+
if (this.visible() !== val) {
1452+
s_visible(val);
1453+
1454+
if (val) {
1455+
this._update();
1456+
}
1457+
}
1458+
return this;
1459+
};
1460+
14331461
/**
14341462
* Initialize after the layer is added to the map.
14351463
*/

0 commit comments

Comments
 (0)