/* Copyright (c) 2015 Jean-Marc VIGLINO, released under the CeCILL-B license (French BSD license) (http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt). */ /** * @classdesc OpenLayers 3 Permalink Control. * Layers with a permalink properties are handled by the control. * The permalink propertie is used to name the layer in the url. * The control must be added after all layer are inserted in the map. * * @constructor * @extends {ol.control.Control} * @param {Object=} Control options. * - urlReplace {bool} replace url or not, default true * - fixed {integer} number of digit in coords, default 6 * - anchor {bool} use "#" instead of "?" in href * - onclick {function} a function called when control is clicked * * Layers attributes extension: * - permalink {char} a short string to identify layer in the url */ ol.control.Permalink = function(opt_options) { var options = opt_options || {}; var self = this; var button = document.createElement('button'); this.replaceState_ = (options.urlReplace!==false); this.fixed_ = options.fixed || 6; this.hash_ = options.anchor ? "#" : "?"; function linkto() { if (typeof(options.onclick) == 'function') options.onclick(self.getLink()); else self.setUrlReplace(!self.replaceState_); } button.addEventListener('click', linkto, false); button.addEventListener('touchstart', linkto, false); var element = document.createElement('div'); element.className = (options.className || "ol-permalink") + " ol-unselectable ol-control"; element.appendChild(button); ol.control.Control.call(this, { element: element, target: options.target }); this.on ('change', this.viewChange_, this); // Save search params this.search_ = {}; var hash = document.location.hash || document.location.search; if (hash) { hash = hash.replace(/(^#|^\?)/,"").split("&"); for (var i=0; i