// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var widget_1 = require('./widget');
var phosphor_widget_1 = require('phosphor-widget');
var _ = require('underscore');
var ButtonModel = (function (_super) {
    __extends(ButtonModel, _super);
    function ButtonModel() {
        _super.apply(this, arguments);
    }
    ButtonModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            description: '',
            tooltip: '',
            disabled: false,
            icon: '',
            button_style: '',
            _view_name: 'ButtonView',
            _model_name: 'ButtonModel'
        });
    };
    return ButtonModel;
}(widget_1.DOMWidgetModel));
exports.ButtonModel = ButtonModel;
var ButtonPWidget = (function (_super) {
    __extends(ButtonPWidget, _super);
    function ButtonPWidget() {
        _super.apply(this, arguments);
    }
    ButtonPWidget.createNode = function () {
        return document.createElement('button');
    };
    return ButtonPWidget;
}(phosphor_widget_1.Widget));
exports.ButtonPWidget = ButtonPWidget;
var ButtonView = (function (_super) {
    __extends(ButtonView, _super);
    function ButtonView() {
        _super.apply(this, arguments);
    }
    ButtonView.createPhosphorWidget = function () {
        return new ButtonPWidget();
    };
    /**
     * Called when view is rendered.
     */
    ButtonView.prototype.render = function () {
        this.pWidget.addClass('jupyter-widgets');
        this.pWidget.addClass('widget-button');
        this.listenTo(this.model, 'change:button_style', this.update_button_style);
        this.update_button_style();
        this.update(); // Set defaults.
    };
    /**
     * Update the contents of this view
     *
     * Called when the model is changed. The model may have been
     * changed by another view or by a state update from the back-end.
     */
    ButtonView.prototype.update = function () {
        this.el.disabled = this.model.get('disabled');
        this.el.setAttribute('title', this.model.get('tooltip'));
        var description = this.model.get('description');
        var icon = this.model.get('icon');
        if (description.trim().length || icon.trim().length) {
            this.el.textContent = '';
            if (icon.trim().length) {
                var i = document.createElement('i');
                i.classList.add('fa');
                i.classList.add('fa-' + icon);
                this.el.appendChild(i);
            }
            this.el.appendChild(document.createTextNode(description));
        }
        return _super.prototype.update.call(this);
    };
    ButtonView.prototype.update_button_style = function () {
        var class_map = {
            primary: ['mod-primary'],
            success: ['mod-success'],
            info: ['mod-info'],
            warning: ['mod-warning'],
            danger: ['mod-danger']
        };
        this.update_mapped_classes(class_map, 'button_style');
    };
    /**
     * Dictionary of events and handlers
     */
    ButtonView.prototype.events = function () {
        // TODO: return typing not needed in Typescript later than 1.8.x
        // See http://stackoverflow.com/questions/22077023/why-cant-i-indirectly-return-an-object-literal-to-satisfy-an-index-signature-re and https://github.com/Microsoft/TypeScript/pull/7029
        return { 'click': '_handle_click' };
    };
    /**
     * Handles when the button is clicked.
     */
    ButtonView.prototype._handle_click = function (event) {
        event.preventDefault();
        this.send({ event: 'click' });
    };
    return ButtonView;
}(widget_1.DOMWidgetView));
exports.ButtonView = ButtonView;
