Credit.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import DOMPurify from "../ThirdParty/purify.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. var nextCreditId = 0;
  6. var 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. * var 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. var id;
  26. var 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. * @readonly
  69. */
  70. showOnScreen: {
  71. get: function () {
  72. return this._showOnScreen;
  73. },
  74. },
  75. /**
  76. * Gets the credit element
  77. * @memberof Credit.prototype
  78. * @type {HTMLElement}
  79. * @readonly
  80. */
  81. element: {
  82. get: function () {
  83. if (!defined(this._element)) {
  84. var html = DOMPurify.sanitize(this._html);
  85. var div = document.createElement("div");
  86. div._creditId = this._id;
  87. div.style.display = "inline";
  88. div.innerHTML = html;
  89. var links = div.querySelectorAll("a");
  90. for (var i = 0; i < links.length; i++) {
  91. links[i].setAttribute("target", "_blank");
  92. }
  93. this._element = div;
  94. }
  95. return this._element;
  96. },
  97. },
  98. });
  99. /**
  100. * Returns true if the credits are equal
  101. *
  102. * @param {Credit} left The first credit
  103. * @param {Credit} right The second credit
  104. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  105. */
  106. Credit.equals = function (left, right) {
  107. return (
  108. left === right ||
  109. (defined(left) && defined(right) && left._id === right._id)
  110. );
  111. };
  112. /**
  113. * Returns true if the credits are equal
  114. *
  115. * @param {Credit} credit The credit to compare to.
  116. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  117. */
  118. Credit.prototype.equals = function (credit) {
  119. return Credit.equals(this, credit);
  120. };
  121. /**
  122. * @private
  123. * @param attribution
  124. * @return {Credit}
  125. */
  126. Credit.getIonCredit = function (attribution) {
  127. var showOnScreen =
  128. defined(attribution.collapsible) && !attribution.collapsible;
  129. var credit = new Credit(attribution.html, showOnScreen);
  130. credit._isIon = credit.html.indexOf("ion-credit.png") !== -1;
  131. return credit;
  132. };
  133. /**
  134. * Duplicates a Credit instance.
  135. *
  136. * @param {Credit} [credit] The Credit to duplicate.
  137. * @returns {Credit} A new Credit instance that is a duplicate of the one provided. (Returns undefined if the credit is undefined)
  138. */
  139. Credit.clone = function (credit) {
  140. if (defined(credit)) {
  141. return new Credit(credit.html, credit.showOnScreen);
  142. }
  143. };
  144. export default Credit;