mirror of
https://github.com/Viglino/ol-ext.git
synced 2026-01-25 17:36:21 +00:00
136 lines
4.0 KiB
JavaScript
136 lines
4.0 KiB
JavaScript
/* Copyright (c) 2016 Jean-Marc VIGLINO,
|
|
released under the CeCILL-B license (French BSD license)
|
|
(http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt).
|
|
*/
|
|
/** Control bar for OL3
|
|
* The control bar is a container for other controls. It can be used to create toolbars.
|
|
* Control bars can be nested and combined with ol.control.Toggle to handle activate/deactivate.
|
|
*
|
|
* @constructor
|
|
* @extends {ol.control.Control}
|
|
* @param {Object=} opt_options Control options.
|
|
* className {String} class of the control
|
|
* group {bool} is a group, default false
|
|
* toggleOne {bool} only one toggle control is active at a time, default false
|
|
* autoDeactivate {bool} used with subbar to deactivate all control when top level control deactivate, default false
|
|
* controls {Array<ol.control>} a list of control to add to the bar
|
|
*/
|
|
ol.control.Bar = function(options)
|
|
{ if (!options) options={};
|
|
var element = $("<div>").addClass('ol-unselectable ol-control ol-bar');
|
|
if (options.className) element.addClass(options.className);
|
|
if (options.group) element.addClass('ol-group');
|
|
|
|
ol.control.Control.call(this,
|
|
{ element: element.get(0),
|
|
target: options.target
|
|
});
|
|
|
|
this.set('toggleOne', options.toggleOne);
|
|
this.set('autoDeactivate', options.autoDeactivate);
|
|
|
|
this.controls_ = [];
|
|
if (options.controls instanceof Array)
|
|
{ for (var i=0; i<options.controls.length; i++)
|
|
{ this.addControl(options.controls[i]);
|
|
}
|
|
}
|
|
};
|
|
ol.inherits(ol.control.Bar, ol.control.Control);
|
|
|
|
/**
|
|
* Set the map instance the control is associated with.
|
|
* @param {ol.Map} map The map instance.
|
|
*/
|
|
ol.control.Bar.prototype.setMap = function (map)
|
|
{ ol.control.Control.prototype.setMap.call(this, map);
|
|
|
|
for (var i=0; i<this.controls_.length; i++)
|
|
{ var c = this.controls_[i];
|
|
map.addControl(c);
|
|
}
|
|
};
|
|
|
|
/** Get controls in the panel
|
|
* @param {Array<ol.control>}
|
|
*/
|
|
ol.control.Bar.prototype.getControls = function ()
|
|
{ return this.controls_;
|
|
};
|
|
|
|
/** Set tool bar position
|
|
* @param {top|left|bottom|right}
|
|
*/
|
|
ol.control.Bar.prototype.setPosition = function (pos)
|
|
{ $(this.element).removeClass('ol-left ol-top ol-bottom ol-right');
|
|
pos=pos.split ('-');
|
|
for (var i=0; i<pos.length; i++)
|
|
{ switch (pos[i])
|
|
{ case 'top':
|
|
case 'left':
|
|
case 'bottom':
|
|
case 'right':
|
|
$(this.element).addClass ("ol-"+pos[i]);
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
};
|
|
|
|
/** Add a control to the bar
|
|
* @param {ol.control} c control to add
|
|
*/
|
|
ol.control.Bar.prototype.addControl = function (c)
|
|
{ this.controls_.push(c);
|
|
c.setTarget(this.element);
|
|
if (this.getMap())
|
|
{ this.getMap().addControl(c);
|
|
}
|
|
// Activate and toogleOne
|
|
c.on ('change:active', this.onActivateControl_, this);
|
|
if (c.getActive && c.getActive())
|
|
{ c.dispatchEvent({ type:'change:active', key:'active', oldValue:false, active:true });
|
|
}
|
|
};
|
|
|
|
/** Deativate all controls in a bar
|
|
* @param {ol.control} except a control
|
|
*/
|
|
ol.control.Bar.prototype.deactivateControls = function (except)
|
|
{ for (var i=0; i<this.controls_.length; i++)
|
|
{ if (this.controls_[i] !== except && this.controls_[i].setActive)
|
|
{ this.controls_[i].setActive(false);
|
|
}
|
|
}
|
|
};
|
|
|
|
/** Auto activate/deactivate controls in the bar
|
|
* @param {boolean} b activate/deactivate
|
|
*/
|
|
ol.control.Bar.prototype.setActive = function (b)
|
|
{ if (!b && this.get("autoDeactivate"))
|
|
{ this.deactivateControls();
|
|
}
|
|
if (b)
|
|
{ var ctrls = this.getControls();
|
|
for (var i=0, sb; (sb = ctrls[i]); i++)
|
|
{ if (sb.get("autoActivate")) sb.setActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Post-process an activated/deactivated control
|
|
* @param {ol.event} an object with a target {ol.control} and active flag {bool}
|
|
*/
|
|
ol.control.Bar.prototype.onActivateControl_ = function (e)
|
|
{ if (!e.active || !this.get('toggleOne')) return;
|
|
var n;
|
|
var ctrl = e.target;
|
|
for (n=0; n<this.controls_.length; n++)
|
|
{ if (this.controls_[n]===ctrl) break;
|
|
}
|
|
// Not here!
|
|
if (n==this.controls_.length) return;
|
|
this.deactivateControls (this.controls_[n]);
|
|
};
|