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 __());
};
/************************************************
Web Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var WebGadget = (function (_super) {
            __extends(WebGadget, _super);
            function WebGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.WebGadgetRender();
                _super.call(this, gadgetOptions);
            }
            WebGadget.CreateInstance = function (gadgetOptions) {
                var instance = new WebGadget(gadgetOptions);
                return instance;
            };
            WebGadget.TypeId = "078f791f-df7e-46e0-9bee-6e25532d2235";
            return WebGadget;
        }(Models.Gadget));
        Models.WebGadget = WebGadget;
        var WebGadget;
        (function (WebGadget) {
            (function (ViewPortDisplayMode) {
                ViewPortDisplayMode[ViewPortDisplayMode["Portal"] = 1] = "Portal";
                ViewPortDisplayMode[ViewPortDisplayMode["Crop"] = 0] = "Crop";
            })(WebGadget.ViewPortDisplayMode || (WebGadget.ViewPortDisplayMode = {}));
            var ViewPortDisplayMode = WebGadget.ViewPortDisplayMode;
        })(WebGadget = Models.WebGadget || (Models.WebGadget = {}));
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var WebGadgetRender = (function (_super) {
            __extends(WebGadgetRender, _super);
            function WebGadgetRender() {
                _super.call(this);
                this.uriValue = '';
                this.refreshIntervalPointer = null;
            }
            /* Render Overrides
            ******************************************************************************/
            WebGadgetRender.prototype.refresh = function () {
                var handler = this.getHandler();
                this.webGadgetHelper.refresh(handler);
            };
            WebGadgetRender.prototype.render = function () {
                var _this = this;
                // Remove default status text class
                Views.Gadget.RemoveStatusMessage(this.$gadgetProper);
                if (!this.$gadgetProper)
                    return; // We have been destroyed!
                var handler = this.getHandler();
                this.setUriValue(handler);
                if (this.uriValue && this.uriValue.length > 0) {
                    this.clearForRender();
                    TemplateUtil.LocAndLoad(WebGadgetRender.RenderTemplateUri, new Core.CallBack(function (template) {
                        _this.$gadgetProper.empty();
                        _this.$gadgetProper.html(template);
                        _this.$gadgetIframe = _this.$gadgetProper.find('iframe');
                        _this.webGadgetHelper = new Dashboards.Models.WebGadgetHelper(_this.$gadgetIframe, _this.$gadgetProper.find('.webGadgetProper'), _this.$gadgetProper.find('.viewPort'));
                        _this.refresh();
                        _this.$gadgetIframe.load(_this, function () {
                            if (!_this.$gadgetProper)
                                return;
                            _this.setupAutoRefresh();
                        });
                        // IE10+ does not load the scrollbars in "portal" mode 
                        // unless we set the src twice. No idea why this happens,
                        // but this is the easiest fix I could find.
                        _this.$gadgetIframe.attr('src', '');
                        _this.$gadgetIframe.attr('src', _this.uriValue);
                        _this.gadget.gadgetInitialized = true;
                    }, this));
                }
                else {
                    this.$gadgetProper.empty();
                    Views.Gadget.RenderStatusMessage(this.$gadgetProper, i18n.loc.unencoded("Gadgets", "WebGadget_InsecureUri"));
                }
            };
            WebGadgetRender.prototype.destroy = function () {
                if (this.refreshIntervalPointer) {
                    clearInterval(this.refreshIntervalPointer);
                    this.refreshIntervalPointer = null;
                }
                if (this.webGadgetHelper) {
                    this.webGadgetHelper.destroy();
                    this.webGadgetHelper = null;
                }
                _super.prototype.destroy.call(this);
            };
            /* Private methods
            ******************************************************************************/
            WebGadgetRender.prototype.setUriValue = function (handler) {
                var uri = handler.byName(Settings.Gadget.Uri);
                uri = Dashboards.SetupControl.ViewPort.CleanUriValue(uri);
                // Check the URI and abort load if not valid
                if (!Dashboards.SetupControl.ViewPort.IsValidUri(uri))
                    return;
                this.uriValue = uri;
            };
            WebGadgetRender.prototype.setupAutoRefresh = function () {
                var _this = this;
                var refreshIntervalValue = this.getHandler().byName(Settings.Gadget.RefreshIntervalSeconds);
                var refreshIntervalMs = parseInt(refreshIntervalValue) * 1000;
                if (this.refreshIntervalPointer) {
                    clearInterval(this.refreshIntervalPointer);
                }
                if (refreshIntervalMs > 0) {
                    this.refreshIntervalPointer = setInterval(function () {
                        if (_this.refreshIntervalPointer) {
                            _this.render();
                        }
                    }, refreshIntervalMs);
                }
            };
            WebGadgetRender.RenderTemplateUri = 'Content/Gadgets/WebGadget/WebGadgetTemplate.html';
            return WebGadgetRender;
        }(Views.Gadget));
        Views.WebGadgetRender = WebGadgetRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.WebGadget.TypeId,
    create: Dashboards.Models.WebGadget.CreateInstance,
    resourceSet: 'Gadgets'
});
//# sourceMappingURL=WebGadget.js.map