'use strict'; /** * Licensed Materials - Property of IBM IBM Cognos Products: BI Cloud (C) * * Copyright IBM Corp. 2016, 2020 * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../lib/@waca/dashboard-common/dist/ui/SearchableListView', '../../lib/@waca/core-client/js/core-client/utils/Deferred', '../../app/nls/StringResources', 'underscore', 'doT', 'text!./templates/CustomWidgetListItem.html', 'text!./templates/EmptyWidgetList.html'], function (BaseView, Deferred, stringResources, _, dot, itemTemplate, emptyTemplate) { var WidgetListView = BaseView.extend({ itemTemplate: itemTemplate, events: { 'clicktap .listitem': 'onItemClick', 'mousedown .listitem': 'onItemStartDrag', 'drag .listitem': 'onItemStartDrag' }, init: function init(options) { WidgetListView.inherited('init', this, arguments); this.options = options || {}; this.glassContext = options.glassContext; this.services = options.services; this.dashboardApi = options.dashboardApi; this.whenIsReadyDfd = new Deferred(); this._initialize(); }, whenIsReady: function whenIsReady() { return this.whenIsReadyDfd.promise; }, accepts: function accepts() { return true; }, remove: function remove() { if (this._dropZone) { this._dropZone.remove(); this._dropZone = null; } WidgetListView.inherited('remove', this, arguments); }, render: function render() { var dfd = WidgetListView.inherited('render', this); dfd.then(function () { if (!this.list || this.list.length === 0) { var empty = dot.template(emptyTemplate); this.$el.find('.list').append(empty({ text: stringResources.get('noCustomWidgets') })); } }.bind(this)); return dfd; }, _initialize: function _initialize() { var DndManager = this.dashboardApi.getFeature('DashboardDnd.internal'); this.whenIsReadyDfd.resolve(); this._dropZone = DndManager.addDropTarget(this.$el[0], { accepts: this.accepts.bind(this) }); }, _getEntry: function _getEntry(ev) { var target = this.getTarget(ev.target, 'listitem'); var dataId = target.getAttribute('data-id'); // lookup with the id return this.widgetMap[dataId]; }, onItemClick: function onItemClick(ev) { if (this.options.onItemClick) { var entry = this._getEntry(ev); this.options.onItemClick(entry, ev); if (ev && ev.gesture) { ev.gesture.preventDefault(); } } }, onItemStartDrag: function onItemStartDrag(ev) { if (this.options.onItemStartDrag) { var entry = this._getEntry(ev); this.options.onItemStartDrag(entry, ev); } }, /** * Override to provide the list of all custom widgets */ getListItems: function getListItems() { var _this = this; this.$el.empty(); return this.options.getEntries({ glassContext: this.options.glassContext }).then(function (content) { _this.list = content.list; // create the lookup map _this.widgetMap = _.object(_.map(content.list, function (item) { return [item.id, item]; })); return _this.list; }); }, /** * Override to enable search */ getCustomRenderProperties: function getCustomRenderProperties() { return { searchText: stringResources.get('find_label') }; }, /** * Override to provide the searchable custom widgets */ _getSearchableItems: function _getSearchableItems() { return this.list; }, /** * Override to provide the search property of a custom widget */ _getSearchableFieldValue: function _getSearchableFieldValue(value) { return value.title; } }); return WidgetListView; }); //# sourceMappingURL=CustomWidgetListView.js.map