-
Notifications
You must be signed in to change notification settings - Fork 3
/
medallion.js
225 lines (181 loc) · 6.16 KB
/
medallion.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Renders a link to a This Is My Jam user's profile, with an image, text or both.
* If the user has no active jam, renders a simple 'follow me' link.
*
* Usage
* -----
*
* <script src="http://www.thisismyjam.com/includes/js/medallion.js"></script>
* <script>Jam.Medallion.insert({username: "..."})</script>
*
* You can pass several options to Jam.Medallion.insert:
*
* - text: Set to false to stop showing the 'My current jam is:' tag.
* - image: Set to false to stop showing the jam avatar.
* - imageSize: "small" (default), "medium" or "large".
*
* Markup
* ------
*
* The generated markup will look something like this:
*
* <div class="jam-medallion">
* <a class="jam-image" href="...">
* <img class="jam-jamvatar" src="...">
* </a>
*
* <p class="jam-text">
* My current jam is <a href="...">...</a>.
* </p>
* </div>
*
* A "jam-loading" class is added to the outer div while the jam data is being
* loaded from the API, so that you can style it appropriately.
*/
// Setup + utilities
Jam = window.Jam || {};
Jam.escapeHTML = function(text) {
var tmpEl = document.createElement('div');
("innerText" in tmpEl) ? (tmpEl.innerText=text) : (tmpEl.textContent=text);
return tmpEl.innerHTML;
}
Jam.getJSONP = function (url, callback) {
var callbackName = '_'+Math.floor(Math.random()*1000000);
Jam.callbacks = Jam.callbacks || {};
Jam.callbacks[callbackName] = callback;
var script = document.createElement('script');
script.src = url + window.encodeURIComponent('Jam.callbacks.' + callbackName);
document.getElementsByTagName('head')[0].appendChild(script);
}
// Constructor
Jam.Medallion = function(options) {
this.setOptions(options);
};
// Class methods
Jam.Medallion.insert = function(options) {
var medallion = new Jam.Medallion(options);
medallion.insertElement();
medallion.fetch();
}
// Accessors
Jam.Medallion.prototype.setOptions = function(options) {
this.options = options;
this.username = options.username;
};
Jam.Medallion.prototype.getOption = function(name, defaultValue) {
if (this.options.hasOwnProperty(name)) {
return this.options[name];
} else {
return defaultValue;
}
};
Jam.Medallion.prototype.hasActiveJam = function() {
return this.json && this.json.hasOwnProperty('jam');
};
Jam.Medallion.prototype.hasImage = function() {
return this.getOption('image', true);
};
Jam.Medallion.prototype.hasText = function() {
return this.getOption('text', true);
};
Jam.Medallion.prototype.imageSize = function() {
return this.getOption('imageSize', 'small');
};
Jam.Medallion.prototype.setJSON = function(json) {
this.json = json;
};
// Initialization
Jam.Medallion.prototype.insertElement = function() {
var id = 'jam-medallion-'+Math.floor(Math.random()*1000000);
document.write('<div class="jam-medallion jam-loading" id="'+id+'"></div>');
this.element = document.getElementById(id);
};
Jam.Medallion.prototype.fetch = function(element) {
var medallion = this;
Jam.getJSONP(
'https://api.thisismyjam.com/1/' + this.username + '.json?callback=',
function(json) {
medallion.setJSON(json);
medallion.render();
}
);
};
// Rendering
Jam.Medallion.prototype.render = function() {
if (!this.element || !this.json) return;
this.element.className = this.element.className.replace(/\bjam-loading\b/, '');
this.element.innerHTML = '';
if (this.hasActiveJam()) {
if (this.hasImage()) {
this.element.appendChild(this.createImageElement());
}
if (this.hasText()) {
this.element.appendChild(this.createTextElement());
}
} else {
this.element.className += ' jam-inactive';
this.element.appendChild(this.createNoJamTextElement());
}
};
Jam.Medallion.prototype.typeSlug = function() {
var components = [];
if (this.hasActiveJam()) {
if (this.hasText()) {
components.push('text');
}
if (this.hasImage()) {
components.push('image');
components.push(this.imageSize());
}
} else {
components.push('inactive');
}
return components.join('-');
};
Jam.Medallion.prototype.createImageElement = function() {
var imageSize = this.imageSize();
var imageKey = 'jamvatar' + imageSize[0].toUpperCase() + imageSize.substring(1);
var imageElement = document.createElement('img');
imageElement.className = 'jam-jamvatar';
imageElement.src = this.json.jam[imageKey].replace("api.thisismyjam","thisismyjam");
if (imageKey == 'jamvatarSmall') {
imageElement.setAttribute('height', '80'); imageElement.setAttribute('width', '80');
}
else if (imageKey == 'jamvatarMedium') {
imageElement.setAttribute('height', '185'); imageElement.setAttribute('width', '185');
}
else if (imageKey == 'jamvatarLarge') {
imageElement.setAttribute('height', '395'); imageElement.setAttribute('width', '395');
}
var linkElement = this.createLinkElement('image');
linkElement.className = 'jam-image';
linkElement.appendChild(imageElement);
return linkElement;
};
Jam.Medallion.prototype.createTextElement = function() {
var textElement = document.createElement('p');
textElement.className = 'jam-text';
textElement.innerHTML = Jam.escapeHTML('My current jam is ');
var linkElement = this.createLinkElement('text');
linkElement.innerHTML += '“' + Jam.escapeHTML(this.json.jam.title) + '”';
linkElement.innerHTML += Jam.escapeHTML(' by ' + this.json.jam.artist);
textElement.appendChild(linkElement);
return textElement;
};
Jam.Medallion.prototype.createNoJamTextElement = function() {
var textElement = document.createElement('p');
textElement.className = 'jam-text';
var linkElement = this.createLinkElement('text');
linkElement.innerHTML = Jam.escapeHTML('Follow me on This Is My Jam');
textElement.appendChild(linkElement);
return textElement;
};
Jam.Medallion.prototype.createLinkElement = function(linkType) {
var linkElement = document.createElement('a');
linkElement.href = this.linkURL(linkType);
linkElement.target = '_blank';
return linkElement;
};
Jam.Medallion.prototype.linkURL = function(linkType) {
return 'http://www.thisismyjam.com/'+this.username+'?utm_source=user&utm_medium=medallion&utm_campaign='+this.typeSlug()+'&utm_content='+linkType;
}