SourcesCollection.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['../../../lib/@waca/dashboard-common/dist/core/Collection', './SourceModel'], function (Collection, SourceModel) {
  13. 'use strict';
  14. /**
  15. * Wrapper model class around the moserJS module to allow us to extend the boardModel
  16. */
  17. var SourcesCollection = Collection.extend({
  18. // The class of objects that this collection manages
  19. modelClass: SourceModel,
  20. destroy: function destroy() {
  21. this.getModels().forEach(function (sourceModel) {
  22. sourceModel.destroy();
  23. });
  24. },
  25. /**
  26. * Adds a SourceModel to the collection
  27. * @param {string} sourceInfo.assetId the CM id of the source
  28. * @param {string} sourceInfo.name the name of the source
  29. * @param {string} sourceInfo.type the type of the source
  30. * @param {string} sourceInfo.searchPath the CM searchPath of the source
  31. * @param {object} options These options will be passed to the collection add method. Look at Collection.js for description.
  32. * allowDuplicateAssetIds option to enable adding multiple source with the same asset Id.
  33. * @return {string} The id of the source that was added
  34. */
  35. addSource: function addSource(sourceInfo, options) {
  36. var preventDuplicateAssetIdSources = !(options && options.allowDuplicateAssetIds);
  37. // If the source already exists don't do anything
  38. var existingSource = this._getExistingSource(sourceInfo.assetId);
  39. if (existingSource && preventDuplicateAssetIdSources) {
  40. return existingSource.id;
  41. }
  42. if (!sourceInfo.shaping) {
  43. sourceInfo.shaping = {}; // Need this since we want a default ShapingModel created
  44. }
  45. // Since we're adding a source we already have the localizedName - saves another CM call
  46. sourceInfo.localizedName = sourceInfo.name;
  47. sourceInfo.cmObjectExists = true;
  48. var newSourceModel = new SourceModel(sourceInfo);
  49. this.add(newSourceModel, options);
  50. return newSourceModel.id;
  51. },
  52. /**
  53. * Given an assetId (storeId) find the source in the collection
  54. * @param {String} assetId The storeId
  55. * @return {Object} The source Model or null if not found
  56. */
  57. _getExistingSource: function _getExistingSource(assetId) {
  58. var models = this.getModels();
  59. for (var i = 0; i < models.length; i++) {
  60. // Only match on sources that weren't added because of pinning
  61. if (assetId && models[i].get('assetId') === assetId && !models[i].get('pinSourceName')) {
  62. return models[i];
  63. }
  64. }
  65. return null;
  66. }
  67. });
  68. return SourcesCollection;
  69. });
  70. //# sourceMappingURL=SourcesCollection.js.map