PropertyCallbacks.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, 2020
  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 activity state
  12. */
  13. define(['../../../app/nls/StringResources', './DomainValidator', '../../../dashboard/widgets/staticwidget/media/MediaResolver'], function (resources, DomainValidator, MediaResolver) {
  14. /**
  15. * this class is used in VisPropertyProvider.
  16. */
  17. var PropertyCallbacks = function () {
  18. function PropertyCallbacks(options) {
  19. _classCallCheck(this, PropertyCallbacks);
  20. this._features = options.features;
  21. }
  22. PropertyCallbacks.prototype.getAPI = function getAPI() {
  23. // we have to return implementation here.
  24. return this;
  25. };
  26. /**
  27. * Restore the crosstab format to default
  28. */
  29. PropertyCallbacks.prototype.setProperties = function setProperties(_content, props) {
  30. var transaction = this._features['Transaction'];
  31. var transactionToken = transaction.startTransaction();
  32. var propNames = Object.keys(props);
  33. propNames.forEach(function (propName) {
  34. var propVal = props[propName];
  35. _content.setPropertyValue(propName, propVal, transactionToken);
  36. });
  37. transaction.endTransaction(transactionToken);
  38. };
  39. /** Validates a URL.
  40. * @param sUrl - the url to validate.
  41. * @param errorData - if the URL is invalid, it will contain a message describing that fact.
  42. * @returns - returns true if the URL is valid, false otherwise.
  43. */
  44. PropertyCallbacks.prototype.validateImageInput = function validateImageInput(_content, sUrl) {
  45. var imagePattern = /(^https?:\/\/.+\.(jpeg|jpg|gif|png)$)|(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
  46. var localImageRegex = /(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
  47. var ret = {
  48. isValid: true
  49. };
  50. if (!imagePattern.test(sUrl)) {
  51. ret.message = resources.get('imgUrlValidationError');
  52. ret.isValid = false;
  53. } else {
  54. var domainValidator = this._getDomainValidator();
  55. if (!domainValidator.isAllowedDomain(sUrl) && !localImageRegex.test(sUrl)) {
  56. ret.message = resources.get('imgUrlInvalidDomainError', {
  57. domains: domainValidator.getAllowedDomains()
  58. });
  59. ret.isValid = false;
  60. }
  61. }
  62. return ret;
  63. };
  64. /** Callback provided to WidgetPropertiesManager for URL validation.
  65. * @param sUrl - the URI to test.
  66. * @param errorData - Will be filled with a message saying the URI is invalid, if it is invalid.
  67. * @returns - true if it is valid, false otherwise.
  68. */
  69. PropertyCallbacks.prototype.validateWebpageLink = function validateWebpageLink(_content, sUrl) {
  70. var ret = {
  71. isValid: true
  72. };
  73. if (!sUrl) {
  74. ret.message = resources.get('webpageMissingUrl');
  75. ret.isValid = false;
  76. } else {
  77. var domainValidator = this._getDomainValidator();
  78. if (!domainValidator.isAllowedProtocol(sUrl)) {
  79. ret.message = resources.get('webpageHttpValidationError');
  80. ret.isValid = false;
  81. } else if (!domainValidator.isAllowedDomain(sUrl)) {
  82. ret.message = resources.get('webpageInvalidDomainError', {
  83. domains: domainValidator.getAllowedDomains()
  84. });
  85. ret.isValid = false;
  86. }
  87. }
  88. return ret;
  89. };
  90. /** Validates the specified URL.
  91. * @param url - the URL to validate.
  92. * @param errorData - if the URL is invalid, this will contain an error message indicating the reason.
  93. * @returns true if the URL is valid, false otherwise.
  94. */
  95. PropertyCallbacks.prototype.validateMediaInput = function validateMediaInput(_content, url) {
  96. var ret = {
  97. isValid: true
  98. };
  99. if (!url) {
  100. ret.message = resources.get('mediaUrlValidationError');
  101. ret.isValid = false;
  102. } else {
  103. var domainValidator = this._getDomainValidator();
  104. if (!domainValidator.isAllowedProtocol(url)) {
  105. ret.message = resources.get('mediaHttpValidationError');
  106. ret.isValid = false;
  107. } else if (!domainValidator.isAllowedDomain(url)) {
  108. ret.message = resources.get('mediaInvalidDomainError', {
  109. domains: domainValidator.getAllowedDomains()
  110. });
  111. ret.isValid = false;
  112. } else {
  113. var media = MediaResolver.loadFromUrl(url);
  114. if (!media) {
  115. ret.message = resources.get('mediaUnknownValidationError');
  116. ret.isValid = false;
  117. } else {
  118. ret.media = media;
  119. }
  120. }
  121. }
  122. return ret;
  123. };
  124. PropertyCallbacks.prototype._getDomainValidator = function _getDomainValidator() {
  125. if (!this._domainValidator) {
  126. var domainList = this._features.UserProfile.getValidDomainList();
  127. this._domainValidator = new DomainValidator(domainList);
  128. }
  129. return this._domainValidator;
  130. };
  131. return PropertyCallbacks;
  132. }();
  133. return PropertyCallbacks;
  134. });
  135. //# sourceMappingURL=PropertyCallbacks.js.map