/******************************************************************************
This class is responsible for all aspects of managing the Data Series Settings
dialog.
*****************************************************************************/
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var Application = Dashboards.Views.Application;
        var PredefinedEquivalencySetupControlDialog = (function () {
            function PredefinedEquivalencySetupControlDialog(equivalencies, okCallback) {
                if (okCallback === void 0) { okCallback = Core.CallBackPayload.None; }
                this.okWasClicked = false;
                this.$dialog = null;
                this.$list = null;
                this.$rowEntryTemplate = null;
                this.$equivalencies = equivalencies;
                this.okCallback = okCallback;
                this.$template = $('<div/>');
            }
            PredefinedEquivalencySetupControlDialog.ShowDialog = function (equivalencies, okCallback) {
                if (okCallback === void 0) { okCallback = Core.CallBackPayload.None; }
                var dialog = new PredefinedEquivalencySetupControlDialog(equivalencies, okCallback);
                dialog.showDialog();
                return dialog;
            };
            PredefinedEquivalencySetupControlDialog.prototype.showDialog = function () {
                var _this = this;
                TemplateUtil.LocAndLoad(PredefinedEquivalencySetupControlDialog.SetupControlDialogTemplateUri, new Core.CallBack(function (template) {
                    _this.$template.html(template);
                    _this.$dialog = _this.$template.find('.gadgetPredefinedEquivalencyDialog');
                    _this.$list = _this.$template.find('.equivalencyTable');
                    _this.$rowEntryTemplate = _this.$template.find('.equivalencyRowEntryTemplate');
                    _this.populateList();
                    _this.renderDialog();
                }), this);
            };
            // Called after the Template has been loaded
            PredefinedEquivalencySetupControlDialog.prototype.renderDialog = function ($parentDialog) {
                var _this = this;
                this.bindEventHandlers();
                // Center on parent
                var pos = { my: "center", at: "center", of: window };
                if ($parentDialog) {
                    pos = { my: "top", at: "top+20", of: $parentDialog };
                }
                this.$dialog.dialog({
                    modal: true,
                    width: 500,
                    height: 450,
                    position: pos,
                    title: i18n.loc.unencoded('EnergyGadgets', 'PredefinedEquivalencies'),
                    autoResize: false,
                    resizable: false,
                    closeText: i18n.loc.s('General', 'Close'),
                    buttons: [],
                    open: function () { },
                    close: function () {
                        _this.okCallback.call(_this.okWasClicked, _this.lastSelected);
                        _this.$dialog.remove();
                    }
                });
                // if we don't do this, the focus is set to the "X" (close icon) on the form, because there
                // is no other focus on the page. We don't want this, so change the focus elsewhere
                this.$dialog.find('.dialog-okbutton').removeAttr('disabled').focus().attr('disabled', 'disabled');
            };
            PredefinedEquivalencySetupControlDialog.prototype.populateList = function () {
                var _this = this;
                this.$list.empty();
                _.each(this.$equivalencies.getEquivalencies(), function (equivalency) {
                    var $entryTemplate = _this.$rowEntryTemplate.clone();
                    var $populatedRowEntry = $(TemplateUtil.ApplyLocalModel('equivalencyRowEntryTemplate', $entryTemplate.html(), equivalency));
                    $populatedRowEntry.data('equivalency', equivalency);
                    $populatedRowEntry.click(function (eventObj) {
                        var equivalencyRow = $(eventObj.delegateTarget);
                        _this.setUIState(equivalencyRow);
                    });
                    $populatedRowEntry.dblclick(function (eventObj) {
                        $populatedRowEntry.click(eventObj);
                        _this.$dialog.find('.dialog-okbutton').click();
                    });
                    _this.$list.append($populatedRowEntry);
                });
            };
            // Bind all static event handlers.  Should only ever be called once.
            PredefinedEquivalencySetupControlDialog.prototype.bindEventHandlers = function () {
                var _this = this;
                this.$dialog.find('.dialog-okbutton').click(function () {
                    if (_this.isValid()) {
                        _this.okWasClicked = true;
                        _this.$dialog.dialog('close');
                    }
                });
                this.$dialog.find('.CancelLink').click(function () {
                    _this.okWasClicked = false;
                    _this.$dialog.dialog('close');
                });
            };
            PredefinedEquivalencySetupControlDialog.prototype.closeDialog = function () {
                var _this = this;
                (function () {
                    _this.$dialog.dialog('close');
                });
            };
            /************************ Settings Management ****************************/
            // Perform validation and save the series from UI settings.
            // - If validation fails, then the dialog will not close and return false
            // - Else it will close and update the seriesOptions with the updated series settings.
            PredefinedEquivalencySetupControlDialog.prototype.isValid = function () {
                // there is nothing to validate at this time, but leaving this in case
                // there is a need in the future.
                ///****************  Reset Validation and start validating
                this.clearValidation();
                return true;
            };
            /************************ State Management *******************************/
            PredefinedEquivalencySetupControlDialog.prototype.clearValidation = function () {
                // Reset Validation on Data Series Setup
                this.$dialog.find('.dialogValidationErrorMessage').html('');
                this.$dialog.find('.validationError').removeClass('validationError');
            };
            /************************ End State Management ***************************/
            PredefinedEquivalencySetupControlDialog.prototype.setUIState = function ($equivalencyRow) {
                var $lastSelected = this.$list.find('.' + Application.ClassCustomListItemSelected);
                if ($lastSelected) {
                    $lastSelected.removeClass(Application.ClassCustomListItemSelected);
                }
                $equivalencyRow.addClass(Application.ClassCustomListItemSelected);
                this.lastSelected = $equivalencyRow.data('equivalency');
                // enable OK button if item selected
                this.$dialog.find('.dialog-okbutton').removeAttr('disabled');
            };
            PredefinedEquivalencySetupControlDialog.SetupControlDialogTemplateUri = 'Content/Gadgets/EnergyEquivalency/PredefinedEquivalencySetupControlDialogTemplate.html';
            return PredefinedEquivalencySetupControlDialog;
        }());
        SetupControl.PredefinedEquivalencySetupControlDialog = PredefinedEquivalencySetupControlDialog;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
//# sourceMappingURL=PredefinedEquivalencySetupControlDialog.js.map