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 __()); }; /// <reference path="../../base/BaseClass.ts" /> /// <reference path="./FallbackConfig.ts" /> /// <reference path="../../templating/Template.ts" /> /// <reference path="./IWidgetConfigOptions.ts" /> var econda; (function (econda) { var recengine; (function (recengine) { var widget; (function (widget_1) { /** * Handles what happens if a widget could not be rendered, e.g. because we got not products in recommendation response or cross sell server is down. * @class econda.recengine.widget.FallbackHandler * @experimental */ var FallbackHandler = (function (_super) { __extends(FallbackHandler, _super); function FallbackHandler(cfg) { _super.call(this); /** * Target element * @cfg {HTMLElement} element * @accessor */ this._element = null; /** * Fallback configuration * @cfg {econda.recengine.widget.FallbackConfig} config * @accessor */ this._config = null; /** * Set to false to disable cross sell ajax requests. It makes no sense to call cross sell again, if we've got an * ajax error before. * @cfg {Boolean} allowCrossSellRequests * @accessor */ this._allowCrossSellRequests = true; /** * Function to call after fallback was processed. Argument is an object, containing information if we could * render content to target element (success: true) or not (success: false). * @cfg {Function} callback * @accessor */ this._callback = null; if (cfg instanceof FallbackHandler) { return cfg; } this.initConfig(cfg); } FallbackHandler.prototype.getElement = function () { return this._element; }; FallbackHandler.prototype.setElement = function (element) { this._element = element; return this; }; FallbackHandler.prototype.getConfig = function () { return this._config; }; FallbackHandler.prototype.setConfig = function (config) { this._config = config; return this; }; FallbackHandler.prototype.getAllowCrossSellRequests = function () { return this._allowCrossSellRequests; }; FallbackHandler.prototype.setAllowCrossSellRequests = function (enabled) { this._allowCrossSellRequests = enabled; return this; }; FallbackHandler.prototype.getCallback = function () { return this._callback; }; FallbackHandler.prototype.setCallback = function (callback) { this._callback = callback; return this; }; /** * Execute configured fallbacks. */ FallbackHandler.prototype.execute = function () { // Order: widget, template, image this._tryRenderFallbackWidget(); }; /// @private FallbackHandler.prototype._tryRenderFallbackWidget = function () { var cmp = this; var c = this._config; if (c.getWidget() !== null && this._allowCrossSellRequests) { var widgetConfig = c.getWidget(); var widget = new econda.recengine.Widget(widgetConfig); widget.setElement(this.getElement()); widget.setAfterRender(function (widget, success) { if (success === false) { cmp._tryRenderFallbackTemplate(); } else { cmp._exitStatusSuccess(); } }); widget.render(); } else { this._tryRenderFallbackTemplate(); } }; /// @private FallbackHandler.prototype._tryRenderFallbackTemplate = function () { var _this = this; var c = this._config; if (c.getTemplate() !== null) { var templateConfig = c.getTemplate(); var template = new econda.templating.Template(templateConfig); template.render({}, function (html) { econda.util.DomHelper.update(_this.getElement(), html); _this._exitStatusSuccess(); }, function (err) { _this._tryRenderFallbackImage(); }); } else { this._tryRenderFallbackImage(); } }; /// @private FallbackHandler.prototype._tryRenderFallbackImage = function () { var c = this._config; var target; if (c.getImage() !== null && (target = this.getElement())) { econda.util.DomHelper.empty(target); var imgNode = document.createElement('img'); imgNode.src = this._config.getImage().toString(); target.appendChild(imgNode); this._exitStatusSuccess(); } else { this._exitStatusFailure(); } }; FallbackHandler.prototype._exitStatusSuccess = function () { if (typeof this._callback == 'function') { this._callback({ success: true }); } }; FallbackHandler.prototype._exitStatusFailure = function () { if (typeof this._callback == 'function') { this._callback({ success: false }); } }; return FallbackHandler; }(econda.base.BaseClass)); widget_1.FallbackHandler = FallbackHandler; })(widget = recengine.widget || (recengine.widget = {})); })(recengine = econda.recengine || (econda.recengine = {})); })(econda || (econda = {}));