PropertiesUtils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Business Analytics (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class DashboardState
  10. * @hideconstructor
  11. * @classdesc This class provides APIs for dashboard avtivity state
  12. */
  13. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../app/nls/StringResources', './DomainValidator', '../../../dashboard/widgets/staticwidget/media/MediaResolver'], function (APIFactory, resources, DomainValidator, MediaResolver) {
  14. /**
  15. * this class is used in VisPropertyProvider.
  16. */
  17. var PropertyUtils = function () {
  18. function PropertyUtils(options) {
  19. _classCallCheck(this, PropertyUtils);
  20. void APIFactory;
  21. this.transaction = options.features['Transaction'];
  22. this.dashboardApi = options.features.API;
  23. }
  24. PropertyUtils.prototype.getAPI = function getAPI() {
  25. // we have to return implementation here.
  26. return this;
  27. };
  28. PropertyUtils.prototype.initialize = function initialize() {
  29. var _this = this;
  30. var VALID_DOMAIN_LIST_CONFIGURATION_PARAMETER = 'ClientValidDomainList';
  31. return this.dashboardApi.getGlassCoreSvc('.Config').getConfigValue(VALID_DOMAIN_LIST_CONFIGURATION_PARAMETER).then(function (configValueStr) {
  32. _this.domainValidator = new DomainValidator(configValueStr);
  33. });
  34. };
  35. /**
  36. * Restore the crosstab format to default
  37. */
  38. PropertyUtils.prototype.setProperties = function setProperties(_content, props) {
  39. var transactionToken = this.transaction.startTransaction();
  40. var propNames = Object.keys(props);
  41. propNames.forEach(function (propName) {
  42. var propVal = props[propName];
  43. _content.setPropertyValue(propName, propVal, transactionToken);
  44. });
  45. this.transaction.endTransaction(transactionToken);
  46. };
  47. /** Validates a URL.
  48. * @param sUrl - the url to validate.
  49. * @param errorData - if the URL is invalid, it will contain a message describing that fact.
  50. * @returns - returns true if the URL is valid, false otherwise.
  51. */
  52. PropertyUtils.prototype.validateImageInput = function validateImageInput(sUrl) {
  53. var imagePattern = /(^https?:\/\/.+\.(jpeg|jpg|gif|png)$)|(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
  54. var localImageRegex = /(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
  55. var ret = {
  56. valid: true
  57. };
  58. if (!imagePattern.test(sUrl)) {
  59. ret.message = resources.get('imgUrlValidationError');
  60. ret.valid = false;
  61. } else if (!this.domainValidator.isAllowedDomain(sUrl) && !localImageRegex.test(sUrl)) {
  62. ret.message = resources.get('imgUrlInvalidDomainError', {
  63. domains: this.domainValidator.getAllowedDomains()
  64. });
  65. ret.valid = false;
  66. }
  67. return ret;
  68. };
  69. /** Callback provided to WidgetPropertiesManager for URL validation.
  70. * @param sUrl - the URI to test.
  71. * @param errorData - Will be filled with a message saying the URI is invalid, if it is invalid.
  72. * @returns - true if it is valid, false otherwise.
  73. */
  74. PropertyUtils.prototype.validateWebpageLink = function validateWebpageLink(sUrl) {
  75. var ret = {
  76. valid: true
  77. };
  78. if (!sUrl) {
  79. ret.message = resources.get('webpageMissingUrl');
  80. ret.valid = false;
  81. } else if (!this.domainValidator.isAllowedProtocol(sUrl)) {
  82. ret.message = resources.get('webpageHttpValidationError');
  83. ret.valid = false;
  84. } else if (!this.domainValidator.isAllowedDomain(sUrl)) {
  85. ret.message = resources.get('webpageInvalidDomainError', {
  86. domains: this.domainValidator.getAllowedDomains()
  87. });
  88. ret.valid = false;
  89. }
  90. return ret;
  91. };
  92. /** Validates the specified URL.
  93. * @param url - the URL to validate.
  94. * @param errorData - if the URL is invalid, this will contain an error message indicating the reason.
  95. * @returns true if the URL is valid, false otherwise.
  96. */
  97. PropertyUtils.prototype.validateMediaInput = function validateMediaInput(url) {
  98. var ret = {
  99. valid: true
  100. };
  101. if (!url) {
  102. ret.message = resources.get('mediaUrlValidationError');
  103. ret.valid = false;
  104. } else if (!this.domainValidator.isAllowedProtocol(url)) {
  105. ret.message = resources.get('mediaHttpValidationError');
  106. ret.valid = false;
  107. } else if (!this.domainValidator.isAllowedDomain(url)) {
  108. ret.message = resources.get('mediaInvalidDomainError', {
  109. domains: this.domainValidator.getAllowedDomains()
  110. });
  111. ret.valid = false;
  112. } else {
  113. var media = MediaResolver.loadFromUrl(url);
  114. if (!media) {
  115. ret.message = resources.get('mediaUnknownValidationError');
  116. ret.valid = false;
  117. } else {
  118. ret.media = media;
  119. }
  120. }
  121. return ret;
  122. };
  123. return PropertyUtils;
  124. }();
  125. return PropertyUtils;
  126. });
  127. //# sourceMappingURL=PropertiesUtils.js.map