-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtsp.js
190 lines (172 loc) · 5.57 KB
/
tsp.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
'use strict';
var sticker = {};
function getColorsFromUrl() {
var colors = {}, hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
var face = hash[0];
var color = hash[1];
if (face == 'U' || face == 'D' || face == 'L' || face == 'F' || face == 'R' || face == 'B') {
colors[face] = color.replace("%20", " ");
}
}
if (Object.keys(colors).length === 0) {
return defaultColor;
} else {
for (var j = 0; j < faceList.length; j++) {
if (typeof(colors[faceList[j]]) == "undefined") {
colors[faceList[j]] = "";
}
}
return colors;
}
}
function getBodyColorFromUrl() {
var colors = {}, hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
if (hash[0] == 'bc') {
if (bodyColorList.indexOf(hash[1]) >= 0) {
return hash[1];
} else {
return "black";
}
}
}
return "black"
}
function parseInput() {
var input = $.trim($("#stickerInput").val());
var splitedInput = input.split("\n");
var colors = {};
for (var i = 0, len = splitedInput.length; i < len; i++) {
var colorPair = splitedInput[i].split(":").map($.trim);
var face = colorPair[0];
var color = colorPair[1];
colors[face] = color;
}
return colors
}
function applyColors(colors) {
for (var face in colors) {
var color = colors[face];
var rgba = colorList[color]['rgba'];
sticker[face].css("background-color", "rgba(" + rgba[0] + "," + rgba[1] + "," + rgba[2] + "," + rgba[3] + ") ");
}
}
function changeUrl(colors, bc) {
var query = "?" +
"U=" + colors["U"] + "&" +
"D=" + colors["D"] + "&" +
"L=" + colors["L"] + "&" +
"F=" + colors["F"] + "&" +
"R=" + colors["R"] + "&" +
"B=" + colors["B"] + "&" +
"bc=" + bc;
history.pushState(null, null, query);
$("#twitterShare").html('<a href="https://twitter.com/share" class="twitter-share-button" data-url="' + location.href + '" data-size="large" data-count="none" data-hashtags="triboxStickersPreview">Tweet</a>');
twttr.widgets.load()
}
function validateColors(colors) {
try {
if (typeof(colors["U"]) == "undefined" ||
typeof(colors["D"]) == "undefined" ||
typeof(colors["L"]) == "undefined" ||
typeof(colors["F"]) == "undefined" ||
typeof(colors["R"]) == "undefined" ||
typeof(colors["B"]) == "undefined") {
return false;
}
for (var face in colors) {
var color = colors[face];
if (typeof(colorList[color]) == "undefined") {
return false;
}
}
} catch (e) {
return false;
}
return true;
}
function changeInputForm(colors, bc) {
var text = "";
for (var i = 0; i < faceList.length; i++) {
var face = faceList[i];
text += face + ": " + colors[face] + "\n"
}
$("#stickerInput").val($.trim(text));
$("#bodyColorSelect").val(bc);
}
function toggleInvalidNotice(isValid) {
if (isValid) {
$("#previewButton").prop("disabled", false);
$("#invalid-notice").hide();
} else {
$("#previewButton").prop("disabled", true);
$("#invalid-notice").show();
}
}
function applyBodyColor(bc) {
for (var i = 0, len = bodyColorList.length; i < len; i++) {
$("g-cube").removeClass(bodyColorList[i]);
}
$('g-cube').addClass(bc);
}
$(function () {
$("#previewButton").click(function () {
var colors = parseInput();
var bc = $("#bodyColorSelect").val();
if (validateColors(colors)) {
changeUrl(colors, bc);
changeInputForm(colors, bc);
$('g-cube').fadeOut(150, function () {
applyColors(colors);
applyBodyColor(bc);
});
$('g-cube').fadeIn(150);
}
});
sticker["U"] = $(".sticker.orange");
sticker["D"] = $(".sticker.red");
sticker["L"] = $(".sticker.green");
sticker["F"] = $(".sticker.white");
sticker["R"] = $(".sticker.blue");
sticker["B"] = $(".sticker.yellow");
$("#g-cube-container").css("height", $("#g-cube-container").width());
var colors = getColorsFromUrl();
var bc = getBodyColorFromUrl();
changeInputForm(colors, bc);
var isValid = validateColors(colors);
toggleInvalidNotice(isValid);
if (isValid) {
applyColors(colors);
applyBodyColor(bc);
}
$("#stickerInput").keyup(function () {
var colors = parseInput();
var isValid = validateColors(colors);
toggleInvalidNotice(isValid);
});
});
// for iOS Safari
$(window).load(function () {
if (sticker["U"].length == 0) {
sticker["U"] = $(".sticker.orange");
sticker["D"] = $(".sticker.red");
sticker["L"] = $(".sticker.green");
sticker["F"] = $(".sticker.white");
sticker["R"] = $(".sticker.blue");
sticker["B"] = $(".sticker.yellow");
var colors = getColorsFromUrl();
var bc = getBodyColorFromUrl();
changeInputForm(colors, bc);
var isValid = validateColors(colors);
toggleInvalidNotice(isValid);
if (isValid) {
applyColors(colors);
applyBodyColor(bc);
}
}
});