/
Credit.js
158 lines (141 loc) · 4 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
/*global define*/
define([
'./defined',
'./defineProperties',
'./DeveloperError'
], function(
defined,
defineProperties,
DeveloperError) {
"use strict";
var nextCreditId = 0;
var creditToId = {};
/**
* A credit contains data pertaining to how to display attributions/credits for certain content on the screen.
*
* @param {String} [text] The text to be displayed on the screen if no imageUrl is specified.
* @param {String} [imageUrl] The source location for an image
* @param {String} [link] A URL location for which the credit will be hyperlinked
*
* @alias Credit
* @constructor
*
* @example
* //Create a credit with a tooltip, image and link
* var credit = new Cesium.Credit('Cesium', '/images/cesium_logo.png', 'http://cesiumjs.org/');
*/
function Credit(text, imageUrl, link) {
var hasLink = (defined(link));
var hasImage = (defined(imageUrl));
var hasText = (defined(text));
//>>includeStart('debug', pragmas.debug);
if (!hasText && !hasImage && !hasLink) {
throw new DeveloperError('text, imageUrl or link is required');
}
//>>includeEnd('debug');
if (!hasText && !hasImage) {
text = link;
}
this._text = text;
this._imageUrl = imageUrl;
this._link = link;
this._hasLink = hasLink;
this._hasImage = hasImage;
// Credits are immutable so generate an id to use to optimize equal()
var id;
var key = JSON.stringify([text, imageUrl, link]);
if (defined(creditToId[key])) {
id = creditToId[key];
} else {
id = nextCreditId++;
creditToId[key] = id;
}
this._id = id;
}
defineProperties(Credit.prototype, {
/**
* The credit text
* @memberof Credit.prototype
* @type {String}
* @readonly
*/
text : {
get : function() {
return this._text;
}
},
/**
* The source location for the image.
* @memberof Credit.prototype
* @type {String}
* @readonly
*/
imageUrl : {
get : function() {
return this._imageUrl;
}
},
/**
* A URL location for the credit hyperlink
* @memberof Credit.prototype
* @type {String}
* @readonly
*/
link : {
get : function() {
return this._link;
}
},
/**
* @memberof Credit.prototype
* @type {Number}
* @readonly
*
* @private
*/
id : {
get : function() {
return this._id;
}
}
});
/**
* Returns true if the credit has an imageUrl
*
* @returns {Boolean}
*/
Credit.prototype.hasImage = function() {
return this._hasImage;
};
/**
* Returns true if the credit has a link
*
* @returns {Boolean}
*/
Credit.prototype.hasLink = function() {
return this._hasLink;
};
/**
* 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));
};
/**
* Returns true if the credits are equal
*
* @param {Credit} credits 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);
};
return Credit;
});