-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathGlobalCard.js
More file actions
286 lines (242 loc) · 12.2 KB
/
GlobalCard.js
File metadata and controls
286 lines (242 loc) · 12.2 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2017 Euan Ong
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/*
global
_, hideOnClickOutside, updateCheckboxes, ClipboardJS
*/
/*
exported
GlobalCard, copyURLToClipboard
*/
class GlobalCard {
constructor(Planet) {
this.Planet = Planet;
this.ProjectData = null;
this.id = null;
this.likeTimeout = null;
this.PlaceholderMBImage = "images/mbgraphic.png";
this.PlaceholderTBImage = "images/tbgraphic.png";
this.renderData = `
<div class="col no-margin-left s12 m6 l4">
<div class="card" style="height:95%;">
<div class="card-image">
<img class="project-image project-card-image" id="global-project-image-{ID}" src="images/planetgraphic.png">
</div>
<div class="card-content">
<span class="card-title global-title grey-text text-darken-4" id="global-project-title-{ID}"></span>
<div id="global-project-tags-{ID}">
</div>
</div>
<div class="card-action">
<div class="flexcontainer">
<a class="project-icon tooltipped" data-position="bottom" data-delay="50" data-tooltip="${_(
"More Details"
)}" id="global-project-more-details-{ID}"><i class="material-icons">info</i></a>
<a class="project-icon tooltipped" data-position="bottom" data-delay="50" data-tooltip="${_(
"Open in Music Blocks"
)}" id="global-project-open-{ID}"><i class="material-icons">launch</i></a>
<a class="project-icon tooltipped" data-position="bottom" data-delay="50" data-tooltip="${_(
"Merge with current project"
)}" id="global-project-merge-{ID}"><i class="material-icons">merge_type</i></a>
<a class="project-icon tooltipped" data-position="bottom" data-delay="50" data-tooltip="${`${
Planet.ProjectStorage.isLiked(this.id) ? "Unlike" : "Like"
} project`}"><i class="material-icons"id="global-like-icon-{ID}"></i><span class="likes-count" id="global-project-likes-{ID}"></span></a>
<div id="global-share-{ID}">
<a class="project-icon tooltipped" data-position="bottom" data-delay="50" data-tooltip="${_(
"Share project"
)}" id="global-project-share-{ID}"><i class="material-icons">share</i></a>
<div class="card share-card" id="global-sharebox-{ID}" style="display:none;">
<div class="card-content shareurltext">
<div class="shareurltitle">${_("Share")}</div>
<input type="text" name="shareurl" class="shareurlinput" data-originalurl="https://musicblocks.sugarlabs.org/index.html?id={ID}">
<a class="copyshareurl tooltipped" onclick="copyURLToClipboard()" data-clipboard-text="https://musicblocks.sugarlabs.org/index.html?id={ID}&run=True" data-delay="50" data-tooltip="${_(
"Copy link to clipboard"
)}"><i class="material-icons"alt="Copy!">file_copy</i></a>
<div class="shareurl-advanced" id="global-advanced-{ID}">
<div class="shareurltitle">${_("Flags")}</div>
<div><input type="checkbox" name="run" id="global-checkboxrun-{ID}" checked><label for="global-checkboxrun-{ID}">${_(
"Run project on startup."
)}</label></div>
<div><input type="checkbox" name="show" id="global-checkboxshow-{ID}"><label for="global-checkboxshow-{ID}">${_(
"Show code blocks on startup."
)}</label></div>
<div><input type="checkbox" name="collapse" id="global-checkboxcollapse-{ID}"><label for="global-checkboxcollapse-{ID}">${_(
"Collapse code blocks on startup."
)}</label></div>
</div>
</div>
<div class="card-action">
<a onclick="toggleExpandable('global-advanced-{ID}','shareurl-advanced');">${_(
"Advanced Options"
)}</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
}
render() {
const Planet = this.Planet;
const html = this.renderData.replace(new RegExp("{ID}", "g"), this.id);
const frag = document.createRange().createContextualFragment(html);
// set image
let source;
const projectImage = this.ProjectData.ProjectImage;
if (projectImage !== null && projectImage !== "") source = projectImage;
else {
source =
this.ProjectData.ProjectIsMusicBlocks === 1
? this.PlaceholderMBImage
: this.PlaceholderTBImage;
}
const imageId = `global-project-image-${this.id}`;
frag.getElementById(imageId).src = source;
// set tags
const tagcontainer = frag.getElementById(`global-project-tags-${this.id}`);
tagcontainer.classList.add("global-tags-container");
for (let i = 0; i < this.ProjectData.ProjectTags.length; i++) {
const chip = document.createElement("div");
chip.classList.add("chipselect");
chip.textContent = _(Planet.TagsManifest[this.ProjectData.ProjectTags[i]].TagName);
tagcontainer.appendChild(chip);
}
// set title text
frag.getElementById(`global-project-title-${this.id}`).textContent =
this.ProjectData.ProjectName;
// set number of likes
frag.getElementById(`global-project-likes-${this.id}`).textContent =
this.ProjectData.ProjectLikes.toString();
// set view button listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-project-more-details-${this.id}`).addEventListener(
"click",
evt => {
Planet.GlobalPlanet.ProjectViewer.open(this.id);
}
);
// set open button listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-project-open-${this.id}`).addEventListener("click", evt => {
Planet.GlobalPlanet.openGlobalProject(this.id);
});
// set image listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-project-image-${this.id}`).addEventListener("click", evt => {
Planet.GlobalPlanet.ProjectViewer.open(this.id);
});
// set merge modify listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-project-merge-${this.id}`).addEventListener("click", evt => {
Planet.GlobalPlanet.mergeGlobalProject(this.id);
});
// set share button listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-project-share-${this.id}`).addEventListener("click", evt => {
const s = document.getElementById(`global-sharebox-${this.id}`);
if (s.style.display == "none") {
s.style.display = "initial";
hideOnClickOutside(
[document.getElementById(`global-share-${this.id}`)],
`global-sharebox-${this.id}`
);
} else s.style.display = "none";
});
// set share checkbox listener
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-checkboxrun-${this.id}`).addEventListener("click", evt => {
updateCheckboxes(`global-sharebox-${this.id}`);
});
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-checkboxshow-${this.id}`).addEventListener("click", evt => {
updateCheckboxes(`global-sharebox-${this.id}`);
});
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-checkboxcollapse-${this.id}`).addEventListener("click", evt => {
updateCheckboxes(`global-sharebox-${this.id}`);
});
// set like icon
const likeIconId = `global-like-icon-${this.id}`;
frag.getElementById(likeIconId).textContent = Planet.ProjectStorage.isLiked(this.id)
? "favorite"
: "favorite_border";
// eslint-disable-next-line no-unused-vars
frag.getElementById(`global-like-icon-${this.id}`).addEventListener("click", evt => {
this.like();
});
document.getElementById("global-projects").appendChild(frag);
updateCheckboxes(`global-sharebox-${this.id}`);
}
like() {
const Planet = this.Planet;
clearTimeout(this.likeTimeout);
let like = true;
if (Planet.ProjectStorage.isLiked(this.id)) like = false;
this.likeTimeout = setTimeout(() => {
Planet.ServerInterface.likeProject(this.id, like, data => {
this.afterLike(data, like);
});
}, 500);
}
afterLike(data, like) {
!data.success && data.error === "ERROR_ACTION_NOT_PERMITTED"
? this.setLike(like)
: this.setLike(like);
}
setLike(like) {
this.Planet.ProjectStorage.like(this.id, like);
let incr = 1;
let text = "favorite";
if (!like) {
incr = -1;
text = "favorite_border";
}
const l = document.getElementById(`global-project-likes-${this.id}`);
l.textContent = (parseInt(l.textContent) + incr).toString();
document.getElementById(`global-like-icon-${this.id}`).textContent = text;
}
init(id) {
this.id = id;
this.ProjectData = this.Planet.GlobalPlanet.cache[id];
}
}
function showTemporaryCopyMessage(target, message) {
// Remove existing feedback if present
const existing = target.parentNode.querySelector(".copy-feedback");
if (existing) {
existing.remove();
}
const msg = document.createElement("span");
msg.textContent = message;
msg.className = "copy-feedback";
msg.style.marginLeft = "8px";
msg.style.fontSize = "12px";
msg.style.color = "#d32f2f"; // red for failure
target.parentNode.appendChild(msg);
setTimeout(() => {
msg.remove();
}, 2000);
}
function copyURLToClipboard() {
const clipboard = new ClipboardJS(".copyshareurl");
clipboard.on("success", e => {
// eslint-disable-next-line no-console
console.info("Copied:", e.text);
e.clearSelection();
});
clipboard.on("error", e => {
showTemporaryCopyMessage(e.trigger, "Failed to copy");
// eslint-disable-next-line no-console
console.error("Failed to copy:", e.action);
});
}