'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['jquery'], function ($) { return function () { function TemplateHelper(options) { _classCallCheck(this, TemplateHelper); this._model = options.model; this._layoutController = options.layoutController; this._model.on('insert:item', this._onNewItem, this); this._model.on('remove:item', this._onRemoveItem, this); if (this._model.items) { for (var i = 0; i < this._model.items.length; i++) { this._setChildMonitors(this._model.items[i]); } } } /** * Called when the view is destroyed */ TemplateHelper.prototype.destroy = function destroy() { this._model.off('insert:item', this._onNewItem, this); this._model.off('remove:item', this._onRemoveItem, this); if (this._model.items) { for (var i = 0; i < this._model.items.length; i++) { this._setChildMonitors(this._model.items[i], 'off'); } } }; TemplateHelper.prototype.setPreferredLocation = function setPreferredLocation(options) { var locationSet = false; var view = this._layoutController.getLayoutView(this._model.id); var emptyDropZones = view.$el.find('.pagetemplateDropZone.empty'); if (emptyDropZones.length > 0) { emptyDropZones[0]._layout.setPreferredLocation(options); locationSet = true; } return locationSet; }; /** * Set or Remove the event listeners to monitor when the layout model changes. * @param childModel * @param {String} sOff (Optional) Set to 'off' to remove listeners. */ TemplateHelper.prototype._setChildMonitors = function _setChildMonitors(childModel, sOff) { var sOnOrOff = sOff === 'off' ? 'off' : 'on'; childModel[sOnOrOff]('change:style', this._onChildModelStyleChange, this); // childModel[sOnOrOff]('prechange:style', this._onChildModelStylePreChange, this); childModel[sOnOrOff]('change:parent', this._onChildModelUpdateParent, this); }; /** * Called when a new layout model is added to this layout. * */ TemplateHelper.prototype._onNewItem = function _onNewItem(payload) { var model = this._model.findModel(payload.value.parameter.model.id); this._setChildMonitors(model); this._updateDropZonesState(model, payload.data); }; /** * Called when a child layout is removed * */ TemplateHelper.prototype._onRemoveItem = function _onRemoveItem(payload) { // Update the drop zone that this layout is snapped to var view = this._layoutController.getLayoutView(payload.modelId); var dropZones = view.$el.children('.pagetemplateDropZone'); var id = payload.value.parameter; for (var i = 0; i < dropZones.length; i++) { dropZones[i]._layout.removeRelatedModel(id, payload); } }; TemplateHelper.prototype._clearDropZoneState = function _clearDropZoneState(model, payloadData) { var view = this._layoutController.getLayoutView(model.id); var siblingDropZones = view && view.$el ? view.$el.children('.pagetemplateDropZone') : []; var dz = void 0; for (var i = 0; i < siblingDropZones.length; i++) { dz = siblingDropZones[i]; if (dz._layout) { var layout = dz._layout; layout.removeRelatedModel(model.id, payloadData); } } model.set({ relatedLayouts: '' }, { payloadData: payloadData.data }); }; /** * Called when the parent of a child model has changed. */ TemplateHelper.prototype._onChildModelUpdateParent = function _onChildModelUpdateParent(payload) { if (payload.data && payload.data.hint !== 'updateStyleToPercent' && payload.sender !== 'UndoRedoController') { var model = this._layoutController.topLayoutModel.findModel(payload.modelId); this._clearDropZoneState(model, payload.data); this._setChildMonitors(model, 'off'); } }; /** * Called when the layout style has changed */ TemplateHelper.prototype._onChildModelStyleChange = function _onChildModelStyleChange(payload) { var model = this._layoutController.topLayoutModel.findModel(payload.modelId); this._updateDropZonesState(model, payload.data); }; /** * Go over all drop zones and find the drop zone that the given model is snapped to. If one is found, then we will update the dropzone layout to indicate this relationship * */ TemplateHelper.prototype._updateDropZonesState = function _updateDropZonesState(model, payloadData) { var _this = this; // Make sure the layout is ready before accessing the node. // This function was historically wrapped in a setTimeout. It appears to be required because the widget may not be available in the domNode // when layoutReady returns (See RTC 241861). setTimeout(function () { _this._layoutController.layoutReady(model.id).then(function () { var content = _this._layoutController.dashboardApi.getCanvas().getContent(model.id); var view = _this._layoutController.getLayoutView(_this._model.id); var $node = $('#' + _this._layoutController.modelIdToNodeId(model.id), view.$el); // Check if the layout is snapped to a drop zone. If so, then we mark it as related var siblingDropZones = $node.siblings('.pagetemplateDropZone'); var dz = void 0; var foundDropZone = null; for (var i = 0; i < siblingDropZones.length; i++) { dz = siblingDropZones[i]; if (dz._layout) { (function () { var layout = dz._layout; if (layout.isLayoutRelatedToDropZone($node[0])) { foundDropZone = layout; foundDropZone.addRelatedModel(model.id, payloadData); } else { var childrenGroup = content.findContent(); if (childrenGroup && childrenGroup.length != 0) { childrenGroup.forEach(function (child) { layout.removeRelatedModel(child.getId(), payloadData); }); } layout.removeRelatedModel(model.id, payloadData); } })(); } } model.set({ relatedLayouts: foundDropZone ? foundDropZone.model.id : '' }, { payloadData: payloadData.data }); }); }, 10); }; return TemplateHelper; }(); }); //# sourceMappingURL=TemplateHelper.js.map