ThumbnailStore.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../../lib/@waca/core-client/js/core-client/ui/core/Events'], function (Events) {
  8. var ThumbnailStore = Events.extend({
  9. /**
  10. * local cache
  11. * @type {Object}
  12. */
  13. _items: null,
  14. /**
  15. * Constructor
  16. *
  17. * @param {Object} [items={}] - store data
  18. */
  19. init: function init() {
  20. var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  21. ThumbnailStore.inherited('init', this, []);
  22. this._items = items;
  23. },
  24. /**
  25. * Check whether a thumbnail id exists in the store
  26. *
  27. * @param {string} id - thumbnail id
  28. *
  29. * @return {boolean} TRUE if exists in store and FALSE if not
  30. */
  31. exists: function exists(id) {
  32. return this._items[id] !== undefined;
  33. },
  34. /**
  35. * Set data
  36. *
  37. * @param {string} id - thumbnail id
  38. * @param {any} data - thumbnail data
  39. *
  40. * @return {ThumbnailStore} self; current instance
  41. */
  42. set: function set(id, _ref) {
  43. var data = _ref.data,
  44. cacheId = _ref.cacheId;
  45. var item = {
  46. data: data,
  47. cacheId: cacheId
  48. };
  49. this._items[id] = item;
  50. this.trigger('thumbnailStore:set', { id: id, item: item });
  51. return this;
  52. },
  53. /**
  54. * Set error on the thumbnail
  55. *
  56. * @param {string} id - thumbnail id
  57. * @param {error} error - error to store on the object
  58. */
  59. setError: function setError(id, _ref2) {
  60. var message = _ref2.message;
  61. var item = void 0;
  62. if (this.exists(id)) {
  63. item = this.getLocal(id);
  64. item.error = {
  65. message: message
  66. };
  67. } else {
  68. item = {
  69. error: {
  70. message: message
  71. }
  72. };
  73. this._items[id] = item;
  74. }
  75. this.trigger('thumbnailStore:set', { id: id, item: item });
  76. return this;
  77. },
  78. /**
  79. * Set warning on the thumbnail
  80. *
  81. * @param {string} id - thumbnail id
  82. * @param {warning} warning - warning to store on the object
  83. */
  84. setWarning: function setWarning(id, _ref3) {
  85. var message = _ref3.message;
  86. var item = void 0;
  87. if (this.exists(id)) {
  88. item = this.getLocal(id);
  89. item.warning = {
  90. message: message
  91. };
  92. } else {
  93. item = {
  94. warning: {
  95. message: message
  96. }
  97. };
  98. this._items[id] = item;
  99. }
  100. this.trigger('thumbnailStore:set', { id: id, item: item });
  101. return this;
  102. },
  103. /**
  104. * Get data locally, from the cache
  105. *
  106. * @param {string} id - thumbnail id
  107. *
  108. * @return {any} thumbnail data
  109. */
  110. getLocal: function getLocal(id) {
  111. return this._items[id];
  112. },
  113. /**
  114. * Get all data locally, from the cache
  115. *
  116. * @return {Object[]} array of thumbnail data
  117. */
  118. getAllLocal: function getAllLocal() {
  119. var _this = this;
  120. return Object.keys(this._items).map(function (id) {
  121. var item = _this._items[id];
  122. return {
  123. id: id,
  124. item: item
  125. };
  126. });
  127. },
  128. /**
  129. * Get data
  130. *
  131. * @param {string} id - thumbnail id
  132. * @param {string} type - vis type
  133. *
  134. * @return {any} thumbnail data
  135. */
  136. get: function get(id, type) {
  137. var result = void 0;
  138. if (this.exists(id)) {
  139. result = Promise.resolve(this.getLocal(id));
  140. } else {
  141. result = this.getVisTypeDefault(type);
  142. }
  143. return result;
  144. },
  145. /**
  146. * Delete data
  147. *
  148. * @param {string} id - thumbnail id
  149. *
  150. * @return {ThumbnailStore} self; current instance
  151. */
  152. delete: function _delete(id) {
  153. delete this._items[id];
  154. this.trigger('thumbnailStore:delete', { id: id });
  155. return this;
  156. },
  157. /**
  158. * Get default thumbnail for a specific vis type
  159. *
  160. * @param {string} type - vis type
  161. *
  162. * @return {string} default thumbnail
  163. */
  164. getVisTypeDefault: function getVisTypeDefault() /* type */{
  165. return Promise.resolve();
  166. }
  167. });
  168. return ThumbnailStore;
  169. });
  170. //# sourceMappingURL=ThumbnailStore.js.map