-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
247 lines (189 loc) · 7.77 KB
/
script.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var watchface = {};
var zoom = 4;
var editor;
function positionToggle() {
$("#position").show();
$("#jsonEditor").hide();
$("#size").hide();
editor.disable();
$("#watchface-position").css("width", 144 * zoom);
$("#watchface-position").css("height", 168 * zoom);
$("#watchface-position").css("background-color", "#eee");
drawWatchface(watchface, "-position");
}
function sizeToggle() {
$("#position").hide();
$("#jsonEditor").hide();
$("#size").show();
editor.disable();
$("#watchface-size").css("width", 144 * zoom);
$("#watchface-size").css("height", 168 * zoom);
$("#watchface-size").css("background-color", "#eee");
drawWatchface(watchface, "-size");
}
function jsonEditorToggle() {
$("#position").hide();
$("#jsonEditor").show();
$("#size").hide(); editor.disable();
editor.enable();
editor.setValue(watchface);
}
function initEditor() {
// Initialize the editor
editor = new JSONEditor(document.getElementById('editor_holder'), {
// Enable fetching schemas via ajax
ajax: true,
// The schema for the editor
schema: {
$ref: "schema.json",
format: "grid"
},
// Seed the form with a starting value
startval: {}
});
editor.on('change', function () {
// Get an array of errors from the validator
var errors = editor.validate();
var indicator = document.getElementById('valid_indicator');
watchface = editor.getValue();
// Not valid
if (errors.length) {
indicator.className = 'label alert';
indicator.textContent = 'not valid';
}
// Valid
else {
indicator.className = 'label success';
indicator.textContent = 'valid';
}
});
}
function loadFile() {
var input, file, fr;
if (typeof window.FileReader !== 'function') {
alert("The file API isn't supported on this browser yet.");
return;
}
input = document.getElementById('fileinput');
if (!input) {
alert("Um, couldn't find the fileinput element.");
}
else if (!input.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
file = input.files[0];
fr = new FileReader();
fr.onload = receivedText;
fr.readAsText(file);
}
function receivedText(e) {
json = e.target.result;
watchface = JSON.parse(json);
console.log(watchface);
zoom = $('#zoom').val();
$("#watchface-position").css("width", 144 * zoom);
$("#watchface-position").css("height", 168 * zoom);
$("#watchface-position").css("background-color", "#eee");
if ($("#watchface-position").is(":visible")) {
drawWatchface(watchface, "-position");
}
if ($("#watchface-size").is(":visible")) {
drawWatchface(watchface, "-size");
}
if ($("#jsonEditor").is(":visible")) {
editor.setValue(watchface);
}
}
}
function saveFile() {
var json_watchface = JSON.stringify(watchface);
var url = 'data:text/json;charset=utf8,' + encodeURIComponent(json_watchface);
window.open(url, '_blank');
window.focus();
}
function drawWatchface(data, sufix) {
$("#watchface" + sufix).html("");
$("#controlsList" + sufix).html("");
if (!!data.data.screens.length > 0) {
for (var i = 0; i < data.data.screens[0].controls.length; i++) {
drawControl(data.data.screens[0].controls[i], i, sufix);
}
}
}
function drawControl(controlData, index, sufix) {
var control = $("<div class=\"draggable\" id=\"control_" + index + "\"/>")
if (controlData.type !== undefined) {
$("#watchface" + sufix).append(control);
if (controlData.type == "imageFromSet" || controlData.type == "number") {
control.css("width", controlData.style.width * zoom);
control.css("height", controlData.style.height * zoom);
}
if (controlData.type == "text") {
control.css("width", controlData.size.width * zoom);
control.css("height", controlData.size.height * zoom);
}
control.css("position", "absolute");
control.css("box-shadow", " 0px 0px 1px black inset");
control.css("left", controlData.position.x * zoom);
control.css("top", controlData.position.y * zoom);
var color = ["rgba(255,0,0,0.1)", "rgba(0,255,0,0.1)", "rgba(0,0,255,0.1)", "rgba(255,255,0,0.1)", "rgba(255,0,255,0.1)", "rgba(0,255,255,0.1)"];
var random = color[index % color.length];
control.css("background-color", random);
control.append($("<span>x:" + control[0].offsetLeft / zoom + " y:" + control[0].offsetTop / zoom + "</span>"));
control.append($("<br /><span>w:" + control[0].offsetWidth / zoom + " h:" + control[0].offsetHeight / zoom + "</span>"));
control.prop("title", controlData.type + "_" + index);
control.prop("controlId", index);
if ($("#watchface-position").is(":visible")) {
control.draggable({
containment: "parent",
grid: [zoom, zoom],
stop: function (event, ui) {
this.childNodes[0].innerText = "";
this.childNodes[0].innerText = "x:" + control[0].offsetLeft / zoom + " y:" + control[0].offsetTop / zoom;
watchface.data.screens[0].controls[control[0].controlId].position.x = control[0].offsetLeft / zoom;
watchface.data.screens[0].controls[control[0].controlId].position.y = control[0].offsetTop / zoom;
}
});
}
if ($("#watchface-size").is(":visible")) {
control.resizable({
containment: "parent",
grid: [4, 4],
stop: function (event, ui) {
this.childNodes[2].innerText = "";
this.childNodes[2].innerText = "w:" + control[0].offsetWidth / zoom + " h:" + control[0].offsetHeight / zoom;
if (watchface.data.screens[0].controls[control[0].controlId].type == "imageFromSet" || watchface.data.screens[0].controls[control[0].controlId].type == "number") {
watchface.data.screens[0].controls[control[0].controlId].style.width = control[0].offsetWidth / zoom;
watchface.data.screens[0].controls[control[0].controlId].style.height = control[0].offsetHeight / zoom;
}
if (watchface.data.screens[0].controls[control[0].controlId].type == "text") {
watchface.data.screens[0].controls[control[0].controlId].size.width = control[0].offsetWidth / zoom;
watchface.data.screens[0].controls[control[0].controlId].size.height = control[0].offsetHeight / zoom;
}
}
});
}
control.tooltip();
var controlsList = $("<div id=\"controlsList_" + index + "\"/>");
var coltrolsListCheckbox = $("<input type=\"checkbox\" name=\"my-checkbox\" id=\"checkbox_" + index + "\"/>");
$("#controlsList" + sufix).append(controlsList);
controlsList.append(coltrolsListCheckbox);
controlsList.css("height", "50px");
coltrolsListCheckbox.bootstrapSwitch({
labelText: controlData.type + "_" + index,
labelWidth: 200,
state: "true",
handleWidth: 50,
onText: "displayed",
offText: "hidden",
size: "mini",
onSwitchChange: function (event, state) {
control.toggle();
}
});
}
}