/**
* @fileOverview
* Copyright (c) 2013 Regione Autonoma della Sardegna
* Published under the GPL license.
* See https://sardegnageoportale.it/webgis/license.txt
* for the full text of the license.
* @version 0.1
*/
/**
* @require plugins/LayerTree.js
* @require GeoExt/plugins/TreeNodeComponent.js
* @require GeoExt/widgets/WMSLegend.js
* @require GeoExt/widgets/VectorLegend.js
*/
/**
* @namespace framework.plugins
*/
Ext.namespace("framework.plugins");
/** @example
* If you want to change the vendor-specific legend_options parameter that
* is sent to the WMS for GetLegendGraphic you can use ``baseAttrs`` on the
* ``loader`` config:
*
* .. code-block:: javascript
*
* var layerManager = new gxp.plugins.LayerManager({
* loader: {
* baseAttrs: {
* baseParams: {
* legend_options: "fontAntiAliasing:true;fontSize:11;fontName:Arial;fontColor:#FFFFFF"
* }
* }
* }
* });
*
*/
/**
* Plugin for adding a tree of layers with their legend to a 'gxp.Viewer'.
* Also provides a context menu on layer nodes.
* @name_ LayerManager
* @class Plugin for adding a tree of layers with their legend
* @constructor
* @extends gxp.plugins.LayerTree
*/
framework.plugins.LayerManager = Ext.extend(gxp.plugins.LayerTree,
/**
* @lends framework.plugins.LayerManager.prototype
*/
{
/**
* gxp_layermanager plugin type.
* @public
* @type String
*/
ptype: "framework_layermanager",
/**
* Text for baselayer node of layer tree (i18n).
* @public
* @type String
*/
baseNodeText: "Base Maps",
/**
* 'Object' The groups to show in the layer tree. Keys are group names,
* and values are either group titles or an object with 'title' and
* ``exclusive`` properties. 'exclusive' means that nodes will have
* radio buttons instead of checkboxes, so only one layer of the group can
* be active at a time. Optional, the default is
*
* .. code-block:: javascript
*
* groups: {
* "default": "Overlays", // title can be overridden with overlayNodeText
* "background": {
* title: "Base Maps", // can be overridden with baseNodeText
* exclusive: true
* }
* }
*/
/**
* output configuration
* @private
*/
createOutputConfig: function() {
this.tree = this.createPreOutputConfig(this, arguments);
var self = this;
this.target.layer_group = this.groups;
self.target.addEvents('nodeselectionchange');
self.tree.selModel.on({
selectionchange: function(obj, node, last) {
self.target.fireEvent('nodeselectionchange',obj.tree.root, node, last);
}
});
Ext.applyIf(this.tree, Ext.apply({
cls: "gxp-layermanager-tree",
lines: false,
useArrows: false,
plugins: [{
ptype: "gx_treenodecomponent"
}]
}, this.treeConfig));
return this.tree;
},
/**
* layer node configuration
* @private
*/
configureLayerNode: function(loader, attr) {
framework.plugins.LayerManager.superclass.configureLayerNode.apply(this, arguments);
var legendXType;
// add a WMS legend to each node created
if (OpenLayers.Layer.WMS && attr.layer instanceof OpenLayers.Layer.WMS) {
legendXType = "gx_wmslegend";
} else if (OpenLayers.Layer.Vector && attr.layer instanceof OpenLayers.Layer.Vector) {
legendXType = "gx_vectorlegend";
}
if (legendXType) {
var baseParams;
var clsParam; //hide legend in layers inactive on default
if (loader && loader.baseAttrs && loader.baseAttrs.baseParams) {
baseParams = loader.baseAttrs.baseParams;
}
//hide legend in layers inactive on default
if (this.target.mapPanel.layers.getByLayer(attr.layer).data.layer.options.visibility &&
this.target.mapPanel.layers.getByLayer(attr.layer).data.layer.minScale >= this.target.mapPanel.layers.getByLayer(attr.layer).data.layer.scales[this.target.mapPanel.zoom])
clsParam = "legend";
else
clsParam = "legend x-hide-display";
Ext.apply(attr, {
component: {
xtype: legendXType,
// TODO these baseParams were only tested with GeoServer,
// so maybe they should be configurable - and they are
// only relevant for gx_wmslegend.
hidden: !attr.layer.getVisibility(),
baseParams: Ext.apply({
transparent: true
// format: "image/png", //da disattivare per ArcGIS online... forzato per i layer gruppi di GeoServer
// legend_options: "fontAntiAliasing:true;fontSize:11;fontName:Arial;forceLabels:on;"
}, baseParams),
layerRecord: this.target.mapPanel.layers.getByLayer(attr.layer),
showTitle: false,
// custom class for css positioning
// see tree-legend.html
cls: clsParam
}
});
}
},
/**
* @private
* @returns: 'Object' Configuration object for an Ext.tree.TreePanel
*/
createPreOutputConfig: function() {
var treeRoot = new Ext.tree.TreeNode({
text: this.rootNodeText,
expanded: true,
isTarget: false,
allowDrop: true
});
var baseAttrs;
if (this.initialConfig.loader && this.initialConfig.loader.baseAttrs) {
baseAttrs = this.initialConfig.loader.baseAttrs;
}
var defaultGroup = this.defaultGroup,
plugin = this,
groupConfig,
exclusive;
for (var group in this.groups) {
groupConfig = typeof this.groups[group] == "string" ?
{title: this.groups[group]} : this.groups[group];
exclusive = groupConfig.exclusive;
var newNode = this.createNode(plugin,group,groupConfig,defaultGroup,exclusive,baseAttrs);
newNode.addVisibilityEventHandlers();
treeRoot.appendChild(newNode);
}
return {
xtype: "treepanel",
root: treeRoot,
rootVisible: false,
shortTitle: this.shortTitle,
border: false,
enableDD: true,
selModel: new Ext.tree.DefaultSelectionModel({
listeners: {
beforeselect: this.handleBeforeSelect,
scope: this
}
}),
listeners: {
contextmenu: this.handleTreeContextMenu,
beforemovenode: this.handleBeforeMoveNode,
scope: this
},
contextMenu: new Ext.menu.Menu({
items: []
})
};
},
/** create group node
* @public
* @param {plugin} 'framework.plugins.LayerManager' object self
* @param {group} 'String' group name
* @param {groupConfig} 'object' group object
* @param {defaultGroup} 'String' default group name
* @param {exclusive} 'boolean' exclusive content (one layer on for time)
* @param {baseAttrs} 'object' configuration attrubutes
* @returns {framework.plugins.RASGroupNode} group node
*/
createNode: function(plugin,group,groupConfig,defaultGroup,exclusive,baseAttrs){
return new framework.plugins.RASGroupNode(Ext.apply({
text: groupConfig.title,
iconCls: "gxp-folder",
expanded: true,
groupKey: group,
group: group == defaultGroup ? undefined : group,
loader: new GeoExt.tree.LayerLoader({
baseAttrs: exclusive ?
Ext.apply({checkedGroup: Ext.isString(exclusive) ? exclusive : group}, baseAttrs) :
baseAttrs,
store: this.target.mapPanel.layers,
filter: (function(group) {
return function(record) {
return (record.get("group") || defaultGroup) == group &&
record.getLayer().displayInLayerSwitcher == true;
};
})(group),
createNode: function(attr) {
plugin.configureLayerNode(this, attr);
return GeoExt.tree.LayerLoader.prototype.createNode.apply(this, arguments);
}
}),
singleClickExpand: true,
allowDrag: true,
listeners: {
append: function(tree, node) {
node.expand();
},
move: function( tree, obj, oldParent, newParent, newIndex ){
var groups = self.group_layer;
var value,key;
//test
// var keysInit = [];
// for(var k in groups){
// keysInit.push(k);
// }
for(var child=0;child 0) {
// find index by neighboring node in the same container
var searchIndex = (index === 0) ? index + 1 : index - 1;
newRecordIndex = this.store.findBy(function(r) {
return parent.childNodes[searchIndex].layer === r.getLayer();
});
index === 0 && newRecordIndex++;
} else {
// find index by last node of a container above
var prev = parent;
do {
prev = prev.previousSibling;
} while (prev && !(prev instanceof GeoExt.tree.LayerContainer && prev.lastChild));
if(prev) {
newRecordIndex = this.store.findBy(function(r) {
return prev.lastChild.layer === r.getLayer();
});
} else {
// find indext by first node of a container below
var next = parent;
do {
next = next.nextSibling;
} while (next && !(next instanceof GeoExt.tree.LayerContainer && next.firstChild));
if(next) {
newRecordIndex = this.store.findBy(function(r) {
return next.firstChild.layer === r.getLayer();
});
}
newRecordIndex++;
}
}
if(newRecordIndex !== undefined) {
this.store.insert(newRecordIndex, [record]);
window.setTimeout(function() {
parent.reload();
//oldParent.reload();
});
} else {
this.store.insert(oldRecordIndex, [record]);
}
delete parent.loader._reordering;
}
delete this._reordering;
}
//reorder layers on map
for(var i=0;i