// 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 ImageModel = (function (_super) {
    __extends(ImageModel, _super);
    function ImageModel() {
        _super.apply(this, arguments);
    }
    ImageModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _model_name: 'ImageModel',
            _view_name: 'ImageView',
            format: 'png',
            width: '',
            height: '',
            _b64value: ''
        });
    };
    return ImageModel;
}(widget_1.DOMWidgetModel));
exports.ImageModel = ImageModel;
var ImagePWidget = (function (_super) {
    __extends(ImagePWidget, _super);
    function ImagePWidget() {
        _super.apply(this, arguments);
    }
    ImagePWidget.createNode = function () {
        return document.createElement('img');
    };
    return ImagePWidget;
}(phosphor_widget_1.Widget));
exports.ImagePWidget = ImagePWidget;
var ImageView = (function (_super) {
    __extends(ImageView, _super);
    function ImageView() {
        _super.apply(this, arguments);
    }
    ImageView.createPhosphorWidget = function () {
        return new ImagePWidget();
    };
    ImageView.prototype.render = function () {
        /**
         * Called when view is rendered.
         */
        this.pWidget.addClass('jupyter-widgets');
        this.pWidget.addClass('widget-image');
        this.update(); // Set defaults.
    };
    ImageView.prototype.update = function () {
        /**
         * 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.
         */
        var image_src = 'data:image/' + this.model.get('format') + ';base64,' + this.model.get('_b64value');
        this.el.setAttribute('src', image_src);
        var width = this.model.get('width');
        if (width !== undefined && width.length > 0) {
            this.el.setAttribute('width', width);
        }
        else {
            this.el.removeAttribute('width');
        }
        var height = this.model.get('height');
        if (height !== undefined && height.length > 0) {
            this.el.setAttribute('height', height);
        }
        else {
            this.el.removeAttribute('height');
        }
        return _super.prototype.update.call(this);
    };
    return ImageView;
}(widget_1.DOMWidgetView));
exports.ImageView = ImageView;
