/************************************************
Web Gadget Helper
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var WebGadgetHelper = (function () {
            function WebGadgetHelper($gadgetIframe, $webGadgetProper, $viewPort) {
                this.uriValue = '';
                this.$gadgetIframe = $gadgetIframe;
                this.$webGadgetProper = $webGadgetProper;
                this.$viewPort = $viewPort;
            }
            WebGadgetHelper.prototype.refresh = function (handler) {
                this.getGadgetSize();
                this.sizeIframe(handler);
                this.sizeViewPort(handler);
                this.applyOffsets(handler);
            };
            // Completely remove any DOM content or other resources created by this gadget.
            WebGadgetHelper.prototype.destroy = function () {
                this.$gadgetIframe = null;
                this.$webGadgetProper = null;
                this.$viewPort = null;
            };
            WebGadgetHelper.prototype.getGadgetSize = function () {
                this.webGadgetWidth = Math.floor(this.$webGadgetProper.width());
                this.webGadgetHeight = Math.floor(this.$webGadgetProper.height());
            };
            WebGadgetHelper.prototype.sizeIframe = function (handler) {
                this.$webGadgetProper.css({
                    transform: ''
                });
                var displayMode = parseInt(handler.byName(Settings.Gadget.ViewPortDisplayMode));
                // The display mode code is used before the calculations to force
                // the gadget to view in a certain mode.
                if (displayMode == Models.WebGadget.ViewPortDisplayMode.Portal) {
                    // "Portal" mode basically makes the gadget act like a browser
                    // window. Set the frame values to 0 so they will be auto set
                    // later in the code.
                    this.frameWidth = 0;
                    this.frameHeight = 0;
                    this.$gadgetIframe.attr('scrolling', 'yes');
                    this.$gadgetIframe.css('overflow', 'auto');
                    this.$gadgetIframe.css('-webkit-overflow-scrolling', 'touch');
                }
                else {
                    // Set the iFrame height to the height of the viewport, plus the scroll amount.
                    this.frameWidth = parseInt(handler.byName(Settings.Gadget.FrameWidth));
                    var viewPortHeight = parseInt(handler.byName(Settings.Gadget.ViewPortHeight));
                    var viewPortYScroll = parseInt(handler.byName(Settings.Gadget.OffsetY));
                    this.frameHeight = viewPortHeight + viewPortYScroll;
                    // Disable scrolling for this mode.
                    this.$gadgetIframe.attr('scrolling', 'no');
                    this.$gadgetIframe.css('overflow', 'hidden');
                }
                if (isNaN(this.frameWidth))
                    this.frameWidth = 0;
                if (this.frameWidth == 0) {
                    this.frameWidth = this.webGadgetWidth;
                }
                if (isNaN(this.frameHeight))
                    this.frameHeight = 0;
                if (this.frameHeight == 0) {
                    this.frameHeight = this.webGadgetHeight;
                }
                this.$gadgetIframe.attr('style', StringUtil.format('width:{0}px;height:{1}px', this.frameWidth, this.frameHeight));
            };
            WebGadgetHelper.prototype.sizeViewPort = function (handler) {
                var displayMode = parseInt(handler.byName(Settings.Gadget.ViewPortDisplayMode));
                // The display mode code is used before the calculations to force
                // the gadget to view in a certain mode.
                if (displayMode == Models.WebGadget.ViewPortDisplayMode.Portal) {
                    // "Portal" mode basically makes the gadget act like a browser
                    // window. Set the viewport values to 0 so they will be auto
                    // set later in the code.
                    this.viewPortWidth = 0;
                    this.viewPortHeight = 0;
                }
                else {
                    this.viewPortWidth = parseInt(handler.byName(Settings.Gadget.ViewPortWidth));
                    this.viewPortHeight = parseInt(handler.byName(Settings.Gadget.ViewPortHeight));
                }
                // Get View Port settings
                if (isNaN(this.viewPortWidth))
                    this.viewPortHeight = 0;
                this.viewPortWidthIgnorable = false;
                if (this.viewPortWidth == 0) {
                    this.viewPortWidthIgnorable = true;
                    this.viewPortWidth = this.frameWidth;
                }
                // Ensure view port is never larger than Frame
                if (this.viewPortWidth > this.frameWidth) {
                    this.viewPortWidth = this.frameWidth;
                }
                if (isNaN(this.viewPortHeight))
                    this.viewPortHeight = 0;
                this.viewPortHeightIgnorable = false;
                if (this.viewPortHeight == 0) {
                    this.viewPortHeightIgnorable = true;
                    this.viewPortHeight = this.frameHeight;
                }
                // Ensure view port is never larger than Frame
                if (this.viewPortHeight > this.frameHeight) {
                    this.viewPortHeight = this.frameHeight;
                }
                // Apply size
                this.$viewPort.height(this.viewPortHeight);
                this.$viewPort.width(this.viewPortWidth);
                this.scaleViewPort();
            };
            WebGadgetHelper.prototype.scaleViewPort = function () {
                var scale = 1;
                var horizontalRatio = this.webGadgetWidth / this.viewPortWidth;
                var verticalRatio = this.webGadgetHeight / this.viewPortHeight;
                if (this.viewPortWidthIgnorable) {
                    scale = verticalRatio;
                }
                else if (this.viewPortHeightIgnorable) {
                    scale = horizontalRatio;
                }
                else if (horizontalRatio < verticalRatio) {
                    scale = horizontalRatio;
                }
                else {
                    scale = verticalRatio;
                }
                if (scale == 1) {
                    this.$webGadgetProper.css({
                        transform: ''
                    });
                }
                else {
                    this.$webGadgetProper.css({
                        transform: StringUtil.format('scale({0})', scale)
                    });
                }
            };
            WebGadgetHelper.prototype.applyOffsets = function (handler) {
                var offsetYSetting = handler.byName(Settings.Gadget.OffsetY);
                var offsetYPx = parseInt(offsetYSetting);
                var offsetXSetting = handler.byName(Settings.Gadget.OffsetX);
                var offsetXPx = parseInt(offsetXSetting);
                if (isNaN(offsetYPx))
                    offsetYPx = 0;
                if (isNaN(offsetXPx))
                    offsetXPx = 0;
                this.$viewPort.scrollTop(offsetYPx);
                this.$viewPort.scrollLeft(offsetXPx);
            };
            return WebGadgetHelper;
        }());
        Models.WebGadgetHelper = WebGadgetHelper;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
//# sourceMappingURL=WebGadgetHelper.js.map