CustomWidgetListView.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM IBM Cognos Products: BI Cloud (C)
  4. *
  5. * Copyright IBM Corp. 2016, 2020
  6. *
  7. * US Government Users Restricted Rights - Use, duplication or disclosure
  8. * restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. 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) {
  11. var WidgetListView = BaseView.extend({
  12. itemTemplate: itemTemplate,
  13. events: {
  14. 'clicktap .listitem': 'onItemClick',
  15. 'mousedown .listitem': 'onItemStartDrag',
  16. 'drag .listitem': 'onItemStartDrag'
  17. },
  18. init: function init(options) {
  19. WidgetListView.inherited('init', this, arguments);
  20. this.options = options || {};
  21. this.glassContext = options.glassContext;
  22. this.services = options.services;
  23. this.dashboardApi = options.dashboardApi;
  24. this.whenIsReadyDfd = new Deferred();
  25. this._initialize();
  26. },
  27. whenIsReady: function whenIsReady() {
  28. return this.whenIsReadyDfd.promise;
  29. },
  30. accepts: function accepts() {
  31. return true;
  32. },
  33. remove: function remove() {
  34. if (this._dropZone) {
  35. this._dropZone.remove();
  36. this._dropZone = null;
  37. }
  38. WidgetListView.inherited('remove', this, arguments);
  39. },
  40. render: function render() {
  41. var dfd = WidgetListView.inherited('render', this);
  42. dfd.then(function () {
  43. if (!this.list || this.list.length === 0) {
  44. var empty = dot.template(emptyTemplate);
  45. this.$el.find('.list').append(empty({
  46. text: stringResources.get('noCustomWidgets')
  47. }));
  48. }
  49. }.bind(this));
  50. return dfd;
  51. },
  52. _initialize: function _initialize() {
  53. var DndManager = this.dashboardApi.getFeature('DashboardDnd.internal');
  54. this.whenIsReadyDfd.resolve();
  55. this._dropZone = DndManager.addDropTarget(this.$el[0], {
  56. accepts: this.accepts.bind(this)
  57. });
  58. },
  59. _getEntry: function _getEntry(ev) {
  60. var target = this.getTarget(ev.target, 'listitem');
  61. var dataId = target.getAttribute('data-id');
  62. // lookup with the id
  63. return this.widgetMap[dataId];
  64. },
  65. onItemClick: function onItemClick(ev) {
  66. if (this.options.onItemClick) {
  67. var entry = this._getEntry(ev);
  68. this.options.onItemClick(entry, ev);
  69. if (ev && ev.gesture) {
  70. ev.gesture.preventDefault();
  71. }
  72. }
  73. },
  74. onItemStartDrag: function onItemStartDrag(ev) {
  75. if (this.options.onItemStartDrag) {
  76. var entry = this._getEntry(ev);
  77. this.options.onItemStartDrag(entry, ev);
  78. }
  79. },
  80. /**
  81. * Override to provide the list of all custom widgets
  82. */
  83. getListItems: function getListItems() {
  84. var _this = this;
  85. this.$el.empty();
  86. return this.options.getEntries({ glassContext: this.options.glassContext }).then(function (content) {
  87. _this.list = content.list;
  88. // create the lookup map
  89. _this.widgetMap = _.object(_.map(content.list, function (item) {
  90. return [item.id, item];
  91. }));
  92. return _this.list;
  93. });
  94. },
  95. /**
  96. * Override to enable search
  97. */
  98. getCustomRenderProperties: function getCustomRenderProperties() {
  99. return {
  100. searchText: stringResources.get('find_label')
  101. };
  102. },
  103. /**
  104. * Override to provide the searchable custom widgets
  105. */
  106. _getSearchableItems: function _getSearchableItems() {
  107. return this.list;
  108. },
  109. /**
  110. * Override to provide the search property of a custom widget
  111. */
  112. _getSearchableFieldValue: function _getSearchableFieldValue(value) {
  113. return value.title;
  114. }
  115. });
  116. return WidgetListView;
  117. });
  118. //# sourceMappingURL=CustomWidgetListView.js.map