-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
346 lines (282 loc) · 11.9 KB
/
index.html
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<html>
<head>
<title>Watch Face Editor</title>
<!-- https://github.com/jdorn/json-editor -->
<script src="js/jsoneditor.js"></script>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap3-typeahead.min.js"></script>
<script src="js/jszip.min.js"></script>
<script src="js/jszip-utils.min.js"></script>
<script src="js/filereader.js"></script>
<script src="js/jquery.xml2json.js"></script>
<script src="js/json2xml.js"></script>
<script src="js/FileSaver.min.js"></script>
<script src="js/XMLWriter-1.0.0-min.js"></script>
<script src="js/interact-1.2.0.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.css"/>
<link rel="stylesheet" href="css/font-awesome.min.css"/>
<link rel="stylesheet" href="css/main.css"/>
</head>
<body id="body">
<div class="container">
<div class="row">
<h1>Watch Face Editor</h1>
<div class="col-md-7">
<div id='editor_holder'></div>
</div>
<div class="col-md-5">
<span id="valid_indicator" class="alert"></span>
<br/><br/><br/>
<br/>
<form id="file-form" method="post" enctype="multipart/form-data">
<input type="hidden" name="extra-data" multiple="">
<input type="file" id="file-input" name="file-input" multiple="">
</form>
<div id="dropzone" style="width: 150px; height: 50px; border: 1px #000000 dashed">Drop .watch or .face file here</div>
<br/>
<button class="btn btn-success" id="download">Download</button>
<br/><br/>
<div id="watch"></div>
</div>
</div>
</div>
<script>
var currentWatchFile;
var currentFileName;
var faceHeight = 520;
var faceWidth = 520;
var scale = 100;
var editor;
var selectedType;
var options = {
dragClass: "drag",
accept: false,
readAsMap: {},
readAsDefault: 'ArrayBuffer',
on: {
beforestart: function (e, file) {
// return false if you want to skip this file
},
loadstart: function (e, file) { /* Native ProgressEvent */
},
progress: function (e, file) { /* Native ProgressEvent */
},
load: function (e, file) {
// file loaded
try {
// unzip file
currentWatchFile = new JSZip(e.target.result);
currentFileName = file.name;
} catch (e) {
alert('unable to unzip file, is it a .watch or .face file? Is the zip protected?');
}
var json;
if (currentWatchFile.file("watch.xml")) {
// watchmaker
selectedType = 'watchmaker';
// convert watchmaker faces to json
json = $.xml2json(currentWatchFile.file("watch.xml").asText());
} else if (currentWatchFile.file("watchface.json")) {
// facer
selectedType = 'facer';
json = JSON.parse(currentWatchFile.file("watchface.json").asText());
} else {
// draw empty face, error?
}
// draw watch face
drawWatch(json, selectedType);
// load editor
loadEditor(json);
},
error: function (e, file) { /* Native ProgressEvent */
},
loadend: function (e, file) { /* Native ProgressEvent */
},
abort: function (e, file) { /* Native ProgressEvent */
},
skip: function (e, file) {
// Called when a file is skipped. This happens when:
// 1) A file doesn't match the accept option
// 2) false is returned in the beforestart callback
},
groupstart: function (group) {
// Called when a 'group' (a single drop / copy / select that may
// contain multiple files) is receieved.
// You can ignore this event if you don't care about groups
},
groupend: function (group) {
// Called when a 'group' is finished.
// You can ignore this event if you don't care about groups
}
}
};
// init
$(document).ready(function() {
$("#file-input, #dropzone").fileReaderJS(options);
$('#watch').css('width', faceWidth + 'px').css('height', faceHeight + 'px');
$("#download").click(function() {
packageZip();
});
});
function drawWatch(watch, type) {
// TODO: cleanup, find a better way to display watch face
$('#watch').html('');
if (type == 'watchmaker') {
$.each(watch.Layer, function (key, lyr) {
//console.log(lyr);
var tag;
if (lyr.type == 'image') {
tag = $(genImageTag(lyr));
}
$('#watch').append(tag);
if (tag) {
tag.attr('style', 'z-index:1' + key + '0; width:' + lyr.width + "px; height:" + lyr.height + "px ;position:absolute; top:" +
(((faceHeight / 2) - (parseInt(lyr.height) / 2)) + parseInt(lyr.y)) +
"px; left:" + (((faceWidth / 2) - (parseInt(lyr.width) / 2)) + parseInt(lyr.x)) + "px;");
}
});
} else if (type == 'facer') {
$.each(watch, function (key, lyr) {
console.log(lyr);
var tag;
if (lyr.type == 'image') {
tag = $(genImageTag(lyr));
}
$('#watch').append(tag);
if (tag) {
tag.attr('style', 'z-index:1' + key + '0; width:' + lyr.width + "px; height:" + lyr.height + "px ;position:absolute; top:" +
(((faceHeight / 2) - (parseInt(lyr.height) / 2)) + parseInt(lyr.y)) +
"px; left:" + (((faceWidth / 2) - (parseInt(lyr.width) / 2)) + parseInt(lyr.x)) + "px;");
}
});
}
}
function packageZip() {
var json = editor.getValue();
// main watch config file
if (selectedType == 'watchmaker') {
currentWatchFile.file("watch.xml", createToWatchmakerXML(json));
} else if (selectedType == 'facer') {
currentWatchFile.file("watchface.json", json);
}
// deal with images
// deal with fonts
var content = currentWatchFile.generate({type:"blob"});
// send file to browser
//saveAs(content, currentFileName);
}
function createToWatchmakerXML(json) {
var watchXML = new XMLWriter('UTF-8', '1.0');
watchXML.writeStartDocument(true);
watchXML.writeStartElement('Watch');
watchXML.writeAttributeString('name',json.name);
watchXML.writeAttributeString('bg_color',json.bg_color);
watchXML.writeAttributeString('ind_bg',json.ind_bg);
watchXML.writeAttributeString('ind_loc',json.ind_loc);
watchXML.writeAttributeString('hotword_loc',json.hotword_loc);
watchXML.writeAttributeString('hotword_bg',json.hotword_bg);
$.each(json.Layer, function(key, layer) {
watchXML.writeStartElement('Layer');
for (var property in layer ) {
watchXML.writeAttributeString(property, layer[property]);
}
watchXML.writeEndElement();
});
watchXML.writeEndElement();
watchXML.writeEndDocument();
// TODO: header needs to be removed, deal with html entities
console.log(watchXML.flush());
return watchXML.flush();
}
function genImageTag(image) {
// TODO: clean this up
var img = new Image();
var arrayBuffer = currentWatchFile.file("images/" + image.path).asArrayBuffer();
var bytes = new Uint8Array(arrayBuffer);
var blob = new Blob([bytes.buffer]);
var reader = new FileReader();
reader.onload = function (e) {
img.src = e.target.result;
};
reader.readAsDataURL(blob);
return img;
}
/*
function clock() {
var hour = document.getElementById("hour"),
min = document.getElementById("min"),
sec = document.getElementById("sec");
//set up a loop
(function loop() {
requestAnimFrame(loop);
draw();
})();
//position the hands
function draw() {
var now = new Date(),//now
then = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0),//midnight
diffInMil = (now.getTime() - then.getTime()),// difference in milliseconds
h = (diffInMil / (1000 * 60 * 60)),//hours
m = (h * 60),//minutes
s = (m * 60);//seconds
//rotate the hands accordingly
sec.style.webkitTransform = "rotate(" + (s * 6) + "deg)";
hour.style.webkitTransform = "rotate(" + (h * 30 + (h / 2)) + "deg)";
min.style.webkitTransform = "rotate(" + (m * 6) + "deg)";
}
}
*/
function loadEditor(data) {
// destroy if it exists
if (editor) {
editor.destroy();
}
// Initialize the editor
editor = new JSONEditor(document.getElementById('editor_holder'), {
// Enable fetching schemas via ajax
ajax: true,
// The schema for the editor
schema: {
"$ref": "schemas/" + selectedType + ".json"
},
startval: data,
// Disable additional properties
no_additional_properties: true,
// Require all properties by default
required_by_default: true,
disable_edit_json: true,
disable_properties: true,
theme: "bootstrap3"
});
// Hook up the validation indicator to update its
// status whenever the editor changes
editor.on('change', function () {
validate();
});
}
/**
* Valid JSON and update watch face
*/
function validate() {
var json = editor.getValue();
// Get an array of errors from the validator
var errors = editor.validate();
var indicator = $('#valid_indicator');
// Not valid
if (errors.length || json.length == 0) {
indicator.removeClass('alert-success');
indicator.addClass('alert-danger');
indicator.html('<i class="fa fa-times"></i> Configuration is <strong>not valid</strong>');
}
// Valid
else {
indicator.removeClass('alert-danger');
indicator.addClass('alert-success');
indicator.html('<i class="fa fa-check"></i> Configuration is <strong>valid</strong>');
drawWatch(json, selectedType);
}
}
</script>
</body>
</html>