CreateFromTemplateActionHandler.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: dashboard-core
  6. *| (C) Copyright IBM Corp. 2019, 2020
  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(['underscore', '../../../app/nls/StringResources', '../../../app/util/ErrorUtils', '../../../lib/@waca/core-client/js/core-client/utils/UniqueId', '../../../lib/@waca/core-client/js/core-client/ui/core/Class'], function (_, stringResources, Utils, UniqueId, BaseClass) {
  13. var ActionHandler = BaseClass.extend({
  14. /**
  15. * Public API to indicate if the Action should be shown
  16. */
  17. isItemVisible: function isItemVisible(options) {
  18. return this.canExecute(options);
  19. },
  20. /**
  21. * Public API to indicate if the Action can be executed
  22. */
  23. canExecute: function canExecute(options) {
  24. var selection = options.target && options.target.activeObject && options.target.activeObject.aSelectedContext;
  25. var isSelectionAllowed = selection.length === 1 && this._isSelectionValid(options, true);
  26. return Utils.hasCapability(options.glassContext, 'canAuthorDashboard') && isSelectionAllowed;
  27. },
  28. /**
  29. * Public API to perform the action
  30. */
  31. doAction: function doAction(context) {
  32. return this._openDashboardView(context);
  33. },
  34. /**
  35. * Handle the create from template action
  36. */
  37. onSelectItem: function onSelectItem(context) {
  38. return this._openDashboardView(context);
  39. },
  40. _openDashboardView: function _openDashboardView(context) {
  41. var _this = this;
  42. var selection = context.target && context.target.activeObject && context.target.activeObject.aSelectedContext;
  43. var id = selection.length && selection[0].id;
  44. return this._getTemplateSpec(context, id).then(function (spec) {
  45. // replace the template's name with the default new dashboard name
  46. spec = JSON.parse(spec);
  47. Object.assign(spec, { name: stringResources.get('defaultName') });
  48. return _this._getTargetPerspective().then(function (perspective) {
  49. return context.glassContext.appController.openAppView(perspective, {
  50. content: {
  51. id: UniqueId.get('dashboard_'),
  52. boardSpec: spec,
  53. isAuthoringMode: true
  54. }
  55. }).then(function (view) {
  56. return view.onViewRendered();
  57. }).then(function (view) {
  58. view.currentContentView.getDashboardApi().getFeature('DashboardState').setDirty(true);
  59. view.currentContentView.openDatasetpane(false);
  60. });
  61. });
  62. });
  63. },
  64. _getTemplateSpec: function _getTemplateSpec(context, id) {
  65. return context.glassContext.getSvc('.Content').then(function (contentSvc) {
  66. var url = contentSvc.getBaseObjectsURL() + '/' + id;
  67. var options = {
  68. dataType: 'json',
  69. type: 'GET',
  70. data: {
  71. fields: 'specification'
  72. }
  73. };
  74. return contentSvc.get(url, options).then(function (response) {
  75. return Promise.resolve(response && response.data && response.data.length && response.data[0].specification);
  76. });
  77. });
  78. },
  79. _getTargetPerspective: function _getTargetPerspective() {
  80. return Promise.resolve('dashboard');
  81. },
  82. _hasValidTags: function _hasValidTags(item) {
  83. var tags = item.tags || [];
  84. return tags.indexOf('dashboard_template') !== -1;
  85. },
  86. _isSelectionValid: function _isSelectionValid(options, defaultValue) {
  87. var selection = options.target && options.target.activeObject && options.target.activeObject.aSelectedContext;
  88. if (selection) {
  89. for (var _iterator = selection, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  90. var _ref;
  91. if (_isArray) {
  92. if (_i >= _iterator.length) break;
  93. _ref = _iterator[_i++];
  94. } else {
  95. _i = _iterator.next();
  96. if (_i.done) break;
  97. _ref = _i.value;
  98. }
  99. var item = _ref;
  100. if (!this._hasValidTags(item)) {
  101. return false;
  102. }
  103. var isDashboardTemplatesEnabled = !options.glassContext.getCoreSvc('.FeatureChecker').checkValue('dashboard', 'dashboardTemplates', 'disabled');
  104. if (!isDashboardTemplatesEnabled) {
  105. return false;
  106. }
  107. if (item.type !== 'exploration') {
  108. return false;
  109. }
  110. var aPerm = ['read', 'execute'];
  111. if (_.intersection(item.permissions, aPerm).length < aPerm.length) {
  112. return false;
  113. }
  114. if (item.disabled && item.disabled.toString().toLowerCase() === 'true') {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. return defaultValue || false;
  121. }
  122. });
  123. return ActionHandler;
  124. });
  125. //# sourceMappingURL=CreateFromTemplateActionHandler.js.map