Source: widgets/SpwLightIdentify.js

Retour à la documentation
/**
 * @class spw.widgets.SpwLightIdentify
 */
define([
        "dojo/_base/declare", "spw/api/SpwBaseWidget",
        "dijit/TitlePane", "dojo/_base/lang", "spw/api/ProjectionManager", "esri/tasks/IdentifyTask",
        "esri/tasks/IdentifyParameters", "dojo/dom-construct", "dojo/_base/array", "dojo/on", "dojo/mouse",
        "dojo/dom-style", "esri/geometry/Polygon", "esri/geometry/Point", "esri/graphic", "dojo/aspect",
        "dojo/promise/all", "esri/SpatialReference", "dijit/layout/ContentPane", "dojo/touch", "spw/api/GraphicsMapService",
        "esri/request", "spw/api/GeometryConverter", "spw/api/MapServiceFactory", "dijit/form/Select", "dojo/store/Memory",
        "spw/api/MessageManager", "spw/api/SpwDrawToolbar", "esri/tasks/QueryTask", "esri/tasks/query", "dojo/Deferred",
        "spw/api/GeometryUtils", "spw/api/SpwViewer", "spw/api/Printer", "spw/api/Utils", "esri/InfoTemplate", "esri/symbols/PictureMarkerSymbol"
    ],
    function(declare, SpwBaseWidget, TitlePane, lang, ProjectionManager,
             IdentifyTask, IdentifyParameters, domConstruct, array, on, mouse, domStyle,
             Polygon, Point, Graphic, aspect, all, SpatialReference, ContentPane, touch,
             GraphicsMapService, request, GeometryConverter, MapServiceFactory, Select, Memory,
             MessageManager, Draw, QueryTask, Query, Deferred, GeometryUtils, SpwViewer, Printer, Utils, InfoTemplate, PictureMarkerSymbol) {

        return declare("spw.widgets.SpwLightIdentify",[SpwBaseWidget], /** @lends spw.widgets.SpwLightIdentify.prototype */{

            _handlers: [],
            _toggleHandlers: [],
            _features: [],
            _identifyPoint: null,
            ignoreFields: [],
            popupWidth: 350,
            popupHeight: 250,
            linkTest: 'http|https|ftp|file',
            linksName: 'Cliquez pour accéder',
            allowRetryOnOffsetSymbolLayers: false,

            onActivate: function(){
                this.inherited(arguments);
                var map = this.spwViewer.get('spwMap');
                this._handlers.push(on(map, map.events.MapClicked, lang.hitch(this, function(x, y, srid){
                    var evt = {
                        mapPoint: new Point(x, y, new SpatialReference({wkid: srid}))
                    };
                    this._identifyPoint = evt.mapPoint;
                    this.identifyServices(evt);
                })));
            },

            identifyServices: function(evt){
                this._features = [];
                var infoWindow = this.spwViewer.get('spwMap').get('esriMap').infoWindow;
                infoWindow.clearFeatures();
                this._clearHandlers();
                var services = this.spwViewer.get('spwMap').getMapServices({ isBaseMap: false, visible: true });
                var promises = [];
                array.forEach(services, lang.hitch(this, function(service) {
                    promises.push(
                        service.identify(lang.mixin(evt, {
                            ignoreFields: this.ignoreFields,
                            tolerance: 10,
                            allowRetryOnOffsetSymbolLayers: this.allowRetryOnOffsetSymbolLayers
                        }), lang.hitch(this, function(identifyResults, service, data){
                            this.displayIdentifyResult(identifyResults, service, data);
                        }))
                    );
                }));

                all(promises).then(lang.hitch(this, function() {
                    this.showPopupInfo();
                }));
            },

            showPopupInfo: function() {
                var infoWindow = this.spwViewer.get('spwMap').get('esriMap').infoWindow;
                infoWindow.resize(this.popupWidth, this.popupHeight);
                infoWindow.setFeatures(this._features);
                this.spwViewer.get('spwMap').get('esriMap').infoWindow.show(this._identifyPoint, this.spwViewer.get('spwMap').get('esriMap').getInfoWindowAnchor(this._identifyPoint));
            },

            displayIdentifyResult: function(identifyResults, service, data){

                //TITLE
                var title = service.label;

                //FIELDS
                array.forEach(data, lang.hitch(this, function(d, idx) {
                    var serviceDiv = domConstruct.create("div", {style: "height: 100%;"});
                    var serviceContent = domConstruct.create("div", {"class": "lightIdentify"}, serviceDiv);
                    var layerName = service.mapServiceLayers[d.layerId].name;
                    //TITLE OF LAYER
                    var serviceTitleDiv = domConstruct.create("div", {
                        innerHTML: layerName,
                        "class": "title"
                    }, serviceContent);
                    var serviceContentDiv = domConstruct.create("div", {"class": "content"}, serviceContent);

                    var datasWrapper;

                    datasWrapper = domConstruct.create("div", {}, serviceContentDiv);

                    var table = domConstruct.create("table", {
                        style: 'word-break: break-word; width: 100%;'
                    }, datasWrapper);

                    if (identifyResults[idx]) {
                        this._addHandlers({
                            node: table,
                            feature: identifyResults[idx].feature
                        });
                    } else {
                        this._removeHandlers(table);
                    }

                    var dataWrapper = domConstruct.create("tbody", null, table);
                    var mapServiceLayer = service.getMapServiceLayer(d.layerId);

                    var cpt = 0;
                    for (var k in d.data) {
                        if (!d.data.hasOwnProperty(k)) {
                            continue;
                        }

                        if (d.data[k] instanceof Object) {
                            var rowWrapper = domConstruct.create("tr", {}, dataWrapper);

                            if (cpt === 0 && identifyResults[idx]) {
                                this._addZoomIcon({
                                    node: domConstruct.create("td", null, rowWrapper),
                                    feature: identifyResults[idx].feature
                                });
                                cpt += 1;
                            }
                            else {
                                domConstruct.create("td", null, rowWrapper);
                            }

                            domConstruct.create("td", {innerHTML: '', style: 'display: none'}, rowWrapper);

                            //If data[k] is an object
                            if (d.data[k] != null && d.data[k].length > 0 && this._currentHandleRelationships) {

                                var cell = domConstruct.create('td', {'colspan': '2'}, rowWrapper, 'last');
                                //For each relationship
                                array.forEach(d.data[k], lang.hitch(this, function (relation) {
                                    var content = domConstruct.create('table', {style: "width: 100%"});
                                    var tbody = domConstruct.create('tbody', null, content);
                                    var i = 0, zoomNode;

                                    for (var key in relation) {
                                        var row = domConstruct.create('tr', null, tbody);
                                        var firstTd = domConstruct.create('td', {}, row);
                                        if (i === 0) {
                                            zoomNode = firstTd;
                                        }
                                        domConstruct.create('td', {
                                            style: "font-weight: bold; text-transform: capitalize;",
                                            innerHTML: key + ' : '
                                        }, row);
                                        domConstruct.create('td', {innerHTML: relation[key]}, row);
                                        i++;
                                    }

                                    var relationTitlePane = new TitlePane({
                                        title: k,
                                        content: content,
                                        open: true
                                    }, domConstruct.create('div', null, cell, 'last'));
                                }));
                            }
                        } else {
                            var rowWrapper = domConstruct.create("tr", {}, dataWrapper);

                            if (cpt === 0 && identifyResults[idx]) {
                                this._addZoomIcon({
                                    node: domConstruct.create("td", null, rowWrapper),
                                    feature: identifyResults[idx].feature
                                });
                                cpt += 1;
                            }
                            else {
                                domConstruct.create("td", null, rowWrapper);
                            }

                            domConstruct.create("td", {
                                innerHTML: k.toString() + " : ",
                                style: "font-weight: bold;min-width:100px;",
                                "class": "key"
                            }, rowWrapper);

                            if (mapServiceLayer && mapServiceLayer.fields && mapServiceLayer.fields[k] && (mapServiceLayer.fields[k].isHyperLink || mapServiceLayer.fields[k].hyperLinkType)) {
                                var linkType = mapServiceLayer.fields[k].hyperLinkType || 'http';

                                if (linkType === 'network-file') {
                                    if (mapServiceLayer.fields[k].serverKey) {
                                        domConstruct.create('a', {
                                            href: this.accessFileService + "?serverKey=" + mapServiceLayer.fields[k].serverKey + "&fileName=" + d.data[k],
                                            innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                            target: '_blank'
                                        }, domConstruct.create("td", null, rowWrapper));
                                    } else {
                                        domConstruct.create('a', {
                                            href: this.accessFileService + "?protocol=" + mapServiceLayer.fields[k].protocol + "&url=" + mapServiceLayer.fields[k].url + d.data[k],
                                            innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                            target: '_blank'
                                        }, domConstruct.create("td", null, rowWrapper));
                                    }
                                    return;
                                }

                                d.data[k] = this.addLinkProtocol(d.data[k], linkType);

                                domConstruct.create("a", {
                                    href: d.data[k],
                                    innerHTML: mapServiceLayer.fields[k].hyperLinkLabel || this.linksName,
                                    target: '_blank'
                                }, domConstruct.create("td", null, rowWrapper));

                            }
                            else if ((this.isLink(d.data[k])) || (service.fileProperties && service.fileProperties.indexOf(k) > -1)) {
                                if (service.fileProperties && service.fileProperties.indexOf(k) > -1) {
                                    d.data[k] = this.addLinkProtocol(d.data[k], 'file');
                                }

                                domConstruct.create("a", {
                                    href: d.data[k],
                                    innerHTML: this.linksName,
                                    target: '_blank'
                                }, domConstruct.create("td", null, rowWrapper));
                            }
                            else if (this.replaceFiles && (
                                    (service.networkFileProperties && service.networkFileProperties.indexOf(k) > -1) || this.isNetworkFile(d.data[k])
                                )) {
                                domConstruct.create('a', {
                                    href: this.accessFileService + d.data[k],
                                    innerHTML: this.linksName,
                                    target: '_blank'
                                }, domConstruct.create("td", null, rowWrapper));
                            }
                            else {
                                domConstruct.create("td", {innerHTML: this.replaceNullsFunc(d.data[k])}, rowWrapper);
                            }

                            ++cpt;
                        }
                    }

                    //INFO TEMPLATE AND GRAPHIC
                    var infoTemplate = new InfoTemplate(title, serviceDiv.innerHTML);
                    var marker = new Graphic(identifyResults[idx].feature.geometry, null, {}, infoTemplate);
                    this._features.push(marker);
                }));
            },

            onDeactivate: function() {
                this.inherited(arguments);
                this.disableIdentification();
            },

            disableIdentification: function() {
                array.forEach(this._handlers, lang.hitch(this, function (h) {
                    h.remove();
                }));

                this._handlers = [];
            },


            isLink: function(prop) {
                return new RegExp('^(' + this.linkTest + ')').test(prop);
            },

            isNetworkFile: function(prop) {
                return new RegExp('^(' + this.networkFileTest + ')').test(prop);
            },

            addLinkProtocol: function(url, protocol) {
                if (url.indexOf(protocol + ':') === 0) {
                    return url;
                }

                return protocol + '://' + (url.indexOf('//') === 0 ? url.substring(2) : url);
            },

            getPoint: function(evt, srid) {
                var pt = null;

                if (evt.mapPoint) {
                    pt = evt.mapPoint;
                }
                else if (evt.geometry) {
                    if (evt.geometry.getCentroid) {
                        pt = evt.geometry.getCentroid();
                    }
                    else if (evt.geometry.type === 'polyline') {
                        pt = evt.geometry.getExtent().getCenter();
                    }
                    else if (evt.geometry.type === 'point') {
                        pt = evt.geometry;
                    }
                }

                return ProjectionManager.getInstance().projectPoint(pt.spatialReference.wkid, srid,
                    pt.x, pt.y);
            },

            _clearHandlers: function() {
                array.forEach(this._toggleHandlers, function(t) {
                    t.remove();
                });

                this._toggleHandlers = [];
            },

            _removeHandlers: function(node) {
                if (node == null) {
                    return;
                }

                if (node.mouseEnterHandler) {
                    node.mouseEnterHandler.remove();
                    node.mouseEnterHandler = null;
                }

                if (node.mouseLeaveHandler) {
                    if (node.feature) {
                        this.spwViewer.get('spwMap').removeFeature(node.feature);
                    }

                    node.mouseLeaveHandler.remove();
                    node.mouseLeaveHandler = null;
                }
            },

            _addHandlers: function(opts) {
                if (opts == null || opts.node == null) {
                    return;
                }

                var node = opts.node;

                this._removeHandlers(node);

                if (opts.feature == null) {
                    return;
                }

                this.own(
                    node.mouseEnterHandler = on(node, mouse.enter, lang.hitch(this, function() {
                        this.spwViewer.get('spwMap').highlightFeature(opts.feature);
                    })),

                    node.mouseLeaveHandler = on(node, mouse.leave, lang.hitch(this, function() {
                        this.spwViewer.get('spwMap').removeFeature(opts.feature);
                    }))
                );

                node.feature = opts.feature;
            },

            _addZoomIcon: function(opts) {
                if (opts == null || opts.node == null) {
                    return;
                }

                var node = opts.node;

                if (node.zoomHandler) {
                    node.zoomHandler.remove();
                    node.zoomHandler = null;
                }

                var zoomIcon = node.zoomIcon;

                if (zoomIcon == null) {
                    zoomIcon = domConstruct.create('div', {
                        'class': 'zoomImg'
                    }, node, 'last');

                    node.zoomIcon = zoomIcon;
                }

                if (opts.feature == null) {
                    domStyle.set(node.zoomIcon, 'display', 'none');
                    return;
                }

                domStyle.set(node.zoomIcon, 'display', opts.inline ? 'inline-block' : '');

                var timeout = null;
                this.own(
                    node.zoomHandler = on(zoomIcon, touch.press, lang.hitch(this, function(event) {
                        this.spwViewer.get('spwMap').zoomToFeature(opts.feature);
                        if (opts.highlight) {
                            this.spwViewer.get('spwMap').highlightFeature(opts.feature);
                            clearTimeout(timeout);
                            timeout = setTimeout(lang.hitch(this, function() {
                                this.spwViewer.get('spwMap').unhighlightFeature(opts.feature);
                                this.spwViewer.get('spwMap').get('esriMap').graphics.remove(opts.feature);
                            }), 2000);
                        }

                        event.stopPropagation();
                    }))
                );
            },

            replaceNullsFunc: function(val) {
                if (!this.replaceNulls) {
                    return val;
                }

                if (val == null || (typeof(val) === 'string' && val.toLowerCase() === 'null')) {
                    val = '';
                }

                return val;
            }

        });
    });