Credit.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import DOMPurify from "../ThirdParty/dompurify.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. let nextCreditId = 0;
  6. const creditToId = {};
  7. /**
  8. * A credit contains data pertaining to how to display attributions/credits for certain content on the screen.
  9. * @param {String} html An string representing an html code snippet
  10. * @param {Boolean} [showOnScreen=false] If true, the credit will be visible in the main credit container. Otherwise, it will appear in a popover
  11. *
  12. * @alias Credit
  13. * @constructor
  14. *
  15. * @exception {DeveloperError} html is required.
  16. *
  17. * @example
  18. * //Create a credit with a tooltip, image and link
  19. * const credit = new Cesium.Credit('<a href="https://cesium.com/" target="_blank"><img src="/images/cesium_logo.png" title="Cesium"/></a>');
  20. */
  21. function Credit(html, showOnScreen) {
  22. //>>includeStart('debug', pragmas.debug);
  23. Check.typeOf.string("html", html);
  24. //>>includeEnd('debug');
  25. let id;
  26. const key = html;
  27. if (defined(creditToId[key])) {
  28. id = creditToId[key];
  29. } else {
  30. id = nextCreditId++;
  31. creditToId[key] = id;
  32. }
  33. showOnScreen = defaultValue(showOnScreen, false);
  34. // Credits are immutable so generate an id to use to optimize equal()
  35. this._id = id;
  36. this._html = html;
  37. this._showOnScreen = showOnScreen;
  38. this._element = undefined;
  39. }
  40. Object.defineProperties(Credit.prototype, {
  41. /**
  42. * The credit content
  43. * @memberof Credit.prototype
  44. * @type {String}
  45. * @readonly
  46. */
  47. html: {
  48. get: function () {
  49. return this._html;
  50. },
  51. },
  52. /**
  53. * @memberof Credit.prototype
  54. * @type {Number}
  55. * @readonly
  56. *
  57. * @private
  58. */
  59. id: {
  60. get: function () {
  61. return this._id;
  62. },
  63. },
  64. /**
  65. * Whether the credit should be displayed on screen or in a lightbox
  66. * @memberof Credit.prototype
  67. * @type {Boolean}
  68. */
  69. showOnScreen: {
  70. get: function () {
  71. return this._showOnScreen;
  72. },
  73. set: function (value) {
  74. this._showOnScreen = value;
  75. },
  76. },
  77. /**
  78. * Gets the credit element
  79. * @memberof Credit.prototype
  80. * @type {HTMLElement}
  81. * @readonly
  82. */
  83. element: {
  84. get: function () {
  85. if (!defined(this._element)) {
  86. const html = DOMPurify.sanitize(this._html);
  87. const div = document.createElement("div");
  88. div._creditId = this._id;
  89. div.style.display = "inline";
  90. div.innerHTML = html;
  91. const links = div.querySelectorAll("a");
  92. for (let i = 0; i < links.length; i++) {
  93. links[i].setAttribute("target", "_blank");
  94. }
  95. this._element = div;
  96. }
  97. return this._element;
  98. },
  99. },
  100. });
  101. /**
  102. * Returns true if the credits are equal
  103. *
  104. * @param {Credit} left The first credit
  105. * @param {Credit} right The second credit
  106. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  107. */
  108. Credit.equals = function (left, right) {
  109. return (
  110. left === right ||
  111. (defined(left) &&
  112. defined(right) &&
  113. left._id === right._id &&
  114. left._showOnScreen === right._showOnScreen)
  115. );
  116. };
  117. /**
  118. * Returns true if the credits are equal
  119. *
  120. * @param {Credit} credit The credit to compare to.
  121. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  122. */
  123. Credit.prototype.equals = function (credit) {
  124. return Credit.equals(this, credit);
  125. };
  126. /**
  127. * @private
  128. * @param attribution
  129. * @return {Credit}
  130. */
  131. Credit.getIonCredit = function (attribution) {
  132. const showOnScreen =
  133. defined(attribution.collapsible) && !attribution.collapsible;
  134. const credit = new Credit(attribution.html, showOnScreen);
  135. credit._isIon = credit.html.indexOf("ion-credit.png") !== -1;
  136. return credit;
  137. };
  138. /**
  139. * Duplicates a Credit instance.
  140. *
  141. * @param {Credit} [credit] The Credit to duplicate.
  142. * @returns {Credit} A new Credit instance that is a duplicate of the one provided. (Returns undefined if the credit is undefined)
  143. */
  144. Credit.clone = function (credit) {
  145. if (defined(credit)) {
  146. return new Credit(credit.html, credit.showOnScreen);
  147. }
  148. };
  149. export default Credit;