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