/// class ProspectDirector.Web.UI.Canvas.Charts.EditValueField
/// using ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand

/**
@class EditValueField
@constructor
@namespace Web.UI.Canvas.Charts
@extends Web.UI.Command
**/
ProspectDirector.Web.UI.Canvas.Charts.EditValueField = ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    execute: function () {

        var selection = this.get_editor().get_selection().get_selections().getAtIndex(0),
            context = this.get_feature().get_featureSet().get_data().context;

        ProspectDirector.Web.UI.FeatureSets.JsonTargetEditorDialog.open({
            config: {
                title: "Value Field - " + selection.columnName,
                height: 450,
                width: 500
            },
            data: {
                targetType: "ProspectDirector.Web.UI.Canvas.Elements.Charts.ValueField",
                targetData: selection,
                attributes: {
                    chart: {
                        value: context,
                        valueType: "ProspectDirector.Web.UI.FeatureSets.TargetContext"
                    }
                },
                layoutType: ProspectDirector.Web.UI.FeatureSets.Layouts.TabStripLayout
            },
            callbacks: {
                ok: $.proxy(this._dialog_onOk, this)
            }
        });

    },

    //===================================
    // EVENT HANDLERS
    //===================================

    _dialog_onOk: function (sender, e) {

        var index = 0,
            fields = this.get_feature().getValue("valueFields");

        for (index; index < fields.length; index++) {
            if (e.target.id === fields[index].id) {
                fields[index] = $.extend({}, e.target);
            }
        }

        this.get_feature().setValue("valueFields", fields);

        this.get_editor().refresh();

    }

});