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