Source: widgets/SpwEditRelatedAttribute.js

Retour à la documentation
/**
 * @class spw.widgets.SpwEditRelatedAttribute
 */
define([
    'dojo/_base/declare', 'dojo/_base/lang', 'dojo/_base/array', 'dojo/Evented',
    'spw/api/SpwBaseTemplatedWidget',
    'dojo/on', 'dojo/dom-construct', 'esri/request',
    'dijit/form/ComboBox',
    'esri/tasks/QueryTask', 'esri/tasks/query',
    'spw/api/MessageManager', 'dojo/store/Memory', 'esri/graphic',
    'dijit/TooltipDialog', 'dijit/popup', 'esri/layers/FeatureLayer'
], function(declare, lang, array, Evented,
            _TemplatedWidget,
            on, domConstruct, request,
            ComboBox,
            QueryTask, Query,
            MessageManager, Memory, Graphic,
            TooltipDialog, popup, FeatureLayer) {

    return declare([_TemplatedWidget, Evented], {

        templateString: '<div></div>',

        layerURI: null,

        labelTemplate: '{ID_EVENEMENT} - {NOM_COMMUNE}',

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

            this.editAttributeWidget = new require('spw/widgets/SpwEditAttributes')({
                widgetTitle: "Édition des attributs",
                activated: false,
                position: "none",
                width: "360px",
                height: "auto",
                top: "30%",
                left: "40%",
                spwViewer: this.spwViewer,
                readOnlyAttributes: this.readOnlyAttributes,
                fieldsOrder: this.fieldsOrder,
                ignoreFields: this.ignoreFields
            });

            this._currentLayer = new FeatureLayer(this.layerURI, {
                mode: FeatureLayer.MODE_SELECTION
            });

            var queryTask = new QueryTask(this.layerURI);
            var query = new Query();

            query.where = '1=1';
            query.returnGeometry = false;
            query.outFields = ['*'];

            queryTask.execute(query, lang.hitch(this, function(featureSet) {

                var data = array.map(featureSet.features, lang.hitch(this, function(feature) {

                    return lang.mixin(feature.attributes, {
                        id: feature.attributes.OBJECTID,
                        label: lang.replace(this.labelTemplate, feature.attributes)
                    });

                }));

                var store = new Memory({
                    data: data,
                    idProperty: 'id'
                });

                this.comboBox = new ComboBox({
                    store: store,
                    labelAttr: 'label',
                    searchAttr: 'label',
                    required: this.required,
                    disabled: this.disabled,
                    style: 'width: calc(100% - 3em); display: inline-block;'
                }, domConstruct.create('div', null, this.domNode));

                if (this.value) {
                    this.comboBox.set('item', store.get(this.value));
                }

                this.comboBox.on('change', lang.hitch(this, function() {
                    if (this.get('value') == null) {
                        this.editButton.innerHTML = '&#43;'
                    }
                    else {
                        this.editButton.innerHTML = '&#9998;';
                    }

                    this.emit('change', null);
                }));

                if (this.disabled !== true) {
                    this.editButton = domConstruct.create('div', {
                        'class': 'related-attribute-button',
                        innerHTML: '&#43;'
                    }, this.domNode);

                    on(this.editButton, 'click', lang.hitch(this, this.onEditButtonClicked));
                }

            }), lang.hitch(this, function(err) {
                console.error(err);
                MessageManager.getInstance().notifyError('Impossible de récupérer les informations du service lié');
            }));
        },

        close: function() {
            this.displayErrorMsg(); // fermeture tooltipdialog
        },

        validate: function() {
            this.comboBox._hasBeenBlurred = true;
            return this.comboBox.validate();
        },

        _getValueAttr: function() {
            var item = this.comboBox.get('item');

            if (item == null) {
                return null;
            }

            return item.OBJECTID;
        },

        onEditButtonClicked: function() {
            this.displayErrorMsg();

            this.tooltipDialog = new TooltipDialog({
                content: this.editAttributeWidget,
                style: "width: 300px;"
            });

            if (this.comboBox.get('item') == null) {
                // TODO: ajouter
                console.log('ajout');

                this.editAttributeWidget.edit(this.layerURI, null, lang.hitch(this, function(item) {

                    var graphic = new Graphic(null, null, item);

                    this._currentLayer.applyEdits([graphic], null, null, lang.hitch(this, function(added) {

                        if (added && added.length === 1 && added[0].success) {
                            var toAdd = lang.mixin(item, {
                                id: added[0].objectId,
                                label: lang.replace(this.labelTemplate, item)
                            });

                            this.comboBox.get('store').add(toAdd);
                        }
                        else {
                            this.displayErrorMsg(item);
                        }

                        popup.close(this.tooltipDialog);

                    }), lang.hitch(this, this.displayErrorMsg));

                }), lang.hitch(this, this.displayErrorMsg));
            }
            else {
                // TODO: éditer...
                console.log('edition');

                this.editAttributeWidget.edit(this.layerURI, this.comboBox.get('item'), lang.hitch(this, function(item) {

                    var graphic = new Graphic(null, null, item);

                    this._currentLayer.applyEdits(null, [graphic], null, lang.hitch(this, function(added) {

                        if (added && added.length === 1 && added[0].success) {
                            var toUpdate = lang.mixin(item, {
                                id: item.OBJECTID,
                                label: lang.replace(this.labelTemplate, item)
                            });

                            this.comboBox.get('store').put(toUpdate);
                        }
                        else {
                            this.displayErrorMsg(item);
                        }

                        popup.close(this.tooltipDialog);

                    }), lang.hitch(this, this.displayErrorMsg));

                }), lang.hitch(this, this.displayErrorMsg));
            }

            popup.open({
                popup: this.tooltipDialog,
                around: this.editButton
            });
        },

        displayErrorMsg: function(err) {
            popup.close(this.tooltipDialog);

            if (err) {
                console.error(err);
                MessageManager.getInstance().notifyError('Impossible de sauvegarder l\'élément');
            }
        }

    });

});