Source: widgets/SpwCoordinatesSearch.js

Retour à la documentation
/**
 * @class spw.widgets.SpwCoordinatesSearch
 */
define([
    "dojo/_base/declare",
    "dojo/on",
    "spw/api/SpwBaseTemplatedWidget",
    "dojo/text!./templates/SpwCoordinatesSearch.html",
    "dojo/dom-construct",
    "dojo/dom-class",
    "dijit/form/NumberTextBox",
    "dijit/form/RadioButton",
    "dojo/query",
    "dojo/_base/lang",
    "dijit/form/Button",
    "esri/geometry/Point",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/graphic",
    "esri/symbols/SimpleLineSymbol",
    "dojo/_base/Color",
    "spw/api/ProjectionManager",
    "dojo/has",
    "esri/SpatialReference",
    "dojo/i18n!./nls/SpwCoordinatesSearch",
    "dojo/_base/array",
    "dijit/popup",

    "dojo/_base/sniff",
    "dijit/form/DropDownButton",
    "dijit/TooltipDialog",
    "dijit/layout/ContentPane",
    "dijit/form/Form",
    "dijit/form/ValidationTextBox"

], function (declare, on, SpwBaseTemplatedWidget, template, domConstruct, domClass, NumberTextBox, RadioButton,
             query, lang, Button, Point, SimpleMarkerSymbol, Graphic, SimpleLineSymbol,
             Color, ProjectionManager, has, SpatialReference, labels, array, popup) {

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

        templateString: template,
        labels: labels,
        currentGraphic: null,
        selectedProjection: null,

        /** config widget */
        widgetTitle: "Localiser des coordonnées",
        // helpContent: "http://geoportail.wallonie.be/aideWalOnMap",
        projections: [
            {
                "SRID": 31370,
                "name": "lambert72",
                "type": "decimal",
                "exampleX": "263873,6",
                "exampleY": "143206,8",
                "checked": true
            },
            {
                "SRID": 3812,
                "name": "lambert2008",
                "type": "decimal",
                "exampleX": "763875,1",
                "exampleY": "643212,2"
            },
            {
                "SRID": 4326,
                "name": "wg84",
                "type": "decimal",
                "exampleX": "5,977",
                "exampleY": "50,588"
            },
            {
                "SRID": 4326,
                "name": "dms",
                "type": "dms",
                "exampleX": "5° 58' 36,839\"E",
                "exampleY": "50° 35' 17,998\"N"
            }
        ],
        style: {
            "width": "500px"
        },

        patterns: {
            decimal: '-?(0|([1-9]\\d*))(,\\d+)?',
            dms: '\\s*([+-]?\\d{1,3}°?\\s+\\d{1,2}\'?\\s+\\d{1,2}(,\\d+)?"?[NSEW]?|\\d{1,3}(:\\d{2}){2}\\.\\d[NSEW]\\s*){1,2}'
        },

        postCreate: function () {
            this.inherited(arguments);

            this.own(
                this._formNode.on('submit', lang.hitch(this, this._goOnCoordinates)),
                this._formNode.on('reset', lang.hitch(this, this.onReset))
            );
        },

        onReset: function () {
        },

        buildRendering: function () {
            this.inherited(arguments);

            array.forEach(this.projections, lang.hitch(this, function (proj) {

                var div = domConstruct.create('div', null, this.radioNode);

                var radio = new RadioButton({
                    value: proj.SRID,
                    name: 'coordinateChoice',
                    checked: proj.checked,
                    style: 'margin-bottom: 5px;'
                }, domConstruct.create('span', null, div));

                var span = domConstruct.place('<span style="cursor: pointer;">' +
                    labels[proj.name + 'Label'] +
                    '</span>', div, 'last');

                this.own(
                    radio.on('change', lang.hitch(this, function (isChecked) {
                        if (isChecked) {
                            this._projSelected(proj);
                        }
                    })),
                    on(span, 'click', function () {
                        radio.set('checked', true)
                    })
                );

                if (proj.checked) {
                    this._projSelected(proj);
                }

            }));
        },

        _projSelected: function (proj) {
            this.labelXNode.innerHTML = labels[proj.name + 'XLabel'];
            this.labelYNode.innerHTML = labels[proj.name + 'YLabel'];

            this.inputXNode.invalidMessage = labels.invalidCoord;
            this.inputYNode.invalidMessage = labels.invalidCoord;

            this.xExampleNode.innerHTML = labels.example + ' : ' + proj.exampleX;
            this.yExampleNode.innerHTML = labels.example + ' : ' + proj.exampleY;

            this.inputXNode.pattern = lang.getObject(proj.type, false, this.patterns);
            this.inputYNode.pattern = lang.getObject(proj.type, false, this.patterns);

            if (proj.type === 'decimal') {
                this.inputXNode.invalidMessage += labels.invalidDecimal;
                this.inputYNode.invalidMessage += labels.invalidDecimal;
            }

            this.selectedProjection = proj;
        },

        _goOnCoordinates: function () {
            if (!this.selectedProjection) {
                return false;
            }

            if (!this.inputXNode.validate() || !this.inputYNode.validate()) {
                return false;
            }

            var map = this.spwViewer.get('spwMap');
            var sRef = this.spwViewer.get('spatialReference').wkid;

            if (this.currentGraphic) {
                map.removeFeature(this.currentGraphic);
            }

            var x = this.inputXNode.get('value');
            var y = this.inputYNode.get('value');

            if (this.selectedProjection.type === 'decimal') {
                x = parseFloat(x.replace(',', '.'));
                y = parseFloat(y.replace(',', '.'));
            }
            else if (this.selectedProjection.type === 'dms') {
                // DMS -> lat/long
                var xs = array.map(x.replace(',', '.').split(' '), function (v) {
                    return parseFloat(v);
                });
                var ys = array.map(y.replace(',', '.').split(' '), function (v) {
                    return parseFloat(v);
                });

                if (x.indexOf('S') > -1) {
                    xs[0] = -xs[0];
                }

                if (y.indexOf('W') > -1) {
                    ys[0] = -ys[0];
                }

                x = xs[0] + (xs[1] ? xs[1] / 60 : 0) + (xs[2] ? xs[2] / 3600 : 0);
                y = ys[0] + (ys[1] ? ys[1] / 60 : 0) + (ys[2] ? ys[2] / 3600 : 0);
            }

            var pointLambert = ProjectionManager.getInstance().projectPoint(
                this.selectedProjection.SRID,
                sRef,
                x,
                y
            );

            var pt = new Point(pointLambert.x, pointLambert.y, new SpatialReference({wkid: sRef}));
            var symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 12,
                new SimpleLineSymbol(), new Color([255, 0, 0]));
            this.currentGraphic = new Graphic(pt, symbol);

            map.showFeature(this.currentGraphic);
            map.zoomToFeature(this.currentGraphic);

            return false;
        },

        onDeactivate: function () {
            this.inherited(arguments);

            if (this.currentGraphic) {
                this.spwViewer.get('spwMap').removeFeature(this.currentGraphic);
            }
        },

        closeWidget: function () {
            var p = this.getParent();

            if (p && p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
                if (p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
                    p._button.closeDropDown();
                }
            }
        }

    });
});