-
Notifications
You must be signed in to change notification settings - Fork 3
/
linker.html
491 lines (455 loc) · 14.8 KB
/
linker.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css" media="screen">
body {
margin: 0;
background-color: #ddd;
font-family: Verdana;
cursor: crosshair;
}
div.linking {
position: absolute;
outline: 1px dashed red;
pointer-events: none;
}
div.link {
position: absolute;
background-color: #fcc; opacity: .5;
outline: 1px solid red;
cursor: pointer;
}
div.selected {
background-color: #966;
}
#upform {
position: fixed; clear: both;
width: 100%;
margin: 0; padding: 6px;
bottom: 0; border: none; border-top: 1px solid #666;
background-color: white; z-index: 10; opacity: .8;
}
#upbox {
position: absolute;
float: left;
width: 220px;
border: 1px inset;
padding: 3px;
background-color: #bbb;
font-size: 12px;
}
#upfile { opacity: 0; }
#url {
display: none; float: right;
margin-right: 20px; width: 300px;
}
.guide {
position: absolute; background-color: #666;
pointer-events: none;
}
#v-guide { width: 1px; height: 2000px; top: 0; }
#h-guide { width: 2000px; height: 1px; left: 0; }
</style>
<title>Linker</title>
<img id="background">
<form id="upform">
<input id="url" class="text_input" placeholder="Enter URL or page number here">
<div id="upbox">Click or drop file here</div>
<input id="upfile" type="file">
<input type="submit" id="prev" class="nav" value="< Prev">
<input type="submit" id="next" class="nav" value="Next >">
</form>
<div id="v-guide" class="guide"></div>
<div id="h-guide" class="guide"></div>
<script type="text/javascript" charset="utf-8">
$('.nav').prop('disabled', true);
window.moveTo(0, 0);
window.resizeTo(768, window.outerHeight);
$('#background').css( 'margin-bottom', $('#upform').outerHeight() );
var lw = open_links_window();
var ld = lw.document;
self.focus();
var pages = [];
var mouseX = -1;
var mouseY = -1;
var linking;
var n_links = 0;
var changed;
var fromServer;
// EVENTS
String.prototype.sprintf = function () {
var args = arguments, i = 0;
return this.replace( /\%s/g, function () { return args[i++] } );
}
$('#background').click( function (e) {
if ( linking ) {
var dims = '{{%s,%s},{%s,%s}}'.sprintf(
linking.offset().left, linking.offset().top,
linking.width(), linking.height()
);
var url = 'http://domain.tld';
if ( e.shiftKey ) url = '000';
$('#links', ld).append( $('<div id="link-' + n_links + '">').html( linkXML(dims, url) ) );
changed = true;
$('.link').removeClass('selected');
linking.removeClass().addClass('link selected').css( 'pointer-events', 'auto' );
$('#url').val( $(this).data('url') ).show();
linking = null;
} else {
linking = $('<div id="link-' + ++n_links + '" class="linking"></div>').css( {
left: mouseX + 1, top: mouseY,
width: 0, height: 0
} );
$('body').append( linking );
}
} );
$('body').on( {
click: function (e) {
if ( e.altKey ) {
$('#' + this.id, ld).remove();
$(this).remove();
changed = true;
if ( linking ) linking.remove();
e.stopPropagation();
} else if ( e.shiftKey ) {
$(this).toggleClass('selected');
} else {
$('.link').removeClass('selected');
$(this).addClass('selected');
$('#url').val( $(this).data('url') ).show();
}
},
dblclick: function () {
$(this).addClass('selected');
$('#url').val( $(this).data('url') ).show().focus();
},
mouseenter: function (e) {
$('#' + this.id, ld).css( 'color', 'red' );
},
mouseleave: function (e) {
$('#' + this.id, ld).css( 'color', 'black' );
}
}, 'div.link' )
.on( 'mousemove', function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
if ( ! e.altKey ) {
// Snapping to link edges
$('div.link').each( function () {
var link = $(this);
var l = link.offset().left
var t = link.offset().top;
var r = l + link.width();
var b = t + link.height();
if ( Math.abs(mouseX - l) < 4 ) { mouseX = l - 1 }
if ( Math.abs(mouseX - r) < 4 ) { mouseX = r }
if ( Math.abs(mouseY - t) < 4 ) { mouseY = t - 1 }
if ( Math.abs(mouseY - b) < 4 ) { mouseY = b }
} );
}
if ( linking ) {
var lo = linking.offset();
linking.css( { width: mouseX - lo.left, height: mouseY - lo.top } );
}
$('#v-guide').css( { left: mouseX } );
$('#h-guide').css( { top: mouseY } );
} );
var filename;
$('#upfile').change( function (e) {
filename = $('#upfile').val();
filename = filename.split('\\').pop();
update_image(filename);
} ).click( function (e) { e.stopPropagation() } );
$('#prev').click( function (e) {
var n = filename.match( /(\d+)\.jpg/ )[1];
if ( n > 0 ) {
filename = filename.replace( /\d+\.jpg/, Number(n) - 1 + '.jpg' );
update_image();
}
return false;
} );
$('#next').click( function (e) {
var n = filename.match( /(\d+)\.jpg/ )[1];
filename = filename.replace( /\d+\.jpg/, Number(n) + 1 + '.jpg' );
update_image();
return false;
} );
$(document).keydown( function (e) {
var distance = 1;
if (e.altKey) distance = 10;
if (e.which == 37) { // left arrow
if (e.metaKey) { $('#prev').click(); return false; }
if (e.shiftKey) {
$('.selected').each( function () { $(this).width( $(this).width() - distance ) } );
} else {
$('.selected').each( function () {
var lo = $(this).offset(); lo.left -= distance; $(this).offset( lo );
} );
}
changed = true;
} else if (e.which == 39) { // right arrow
if (e.metaKey) { $('#next').click(); return false; }
if (e.shiftKey) {
$('.selected').each( function () { $(this).width( $(this).width() + distance ) } );
} else {
$('.selected').each( function () {
var lo = $(this).offset(); lo.left += distance; $(this).offset( lo );
} );
}
changed = true;
} else if (e.which == 38) { // up arrow
if (e.shiftKey) {
$('.selected').each( function () { $(this).height( $(this).height() - distance ) } );
} else {
$('.selected').each( function () {
var lo = $(this).offset(); lo.top -= distance; $(this).offset( lo );
e.preventDefault();
} );
}
changed = true;
} else if (e.which == 40) { // down arrow
if (e.shiftKey) {
$('.selected').each( function () { $(this).height( $(this).height() + distance ) } );
} else {
$('.selected').each( function () {
var lo = $(this).offset(); lo.top += distance; $(this).offset( lo );
e.preventDefault();
} );
}
changed = true;
} else if (e.which == 8) { // backspace
if ( $(document.activeElement).hasClass('text_input') ) return;
if ( $('.selected').length > 1 && ! confirm( 'Are you sure you want to delete those links?' ) ) {
return false;
}
$('.selected').each( function () {
$('#' + this.id, ld).remove();
$(this).remove();
changed = true;
} );
$('#url').hide();
return false;
} else if (e.which == 27) { // escape
if ( linking ) linking.remove();
linking = null;
return false;
}
} );
$('#url').keydown( function (e) {
if (e.which == 13) {
var url = $(this).val();
var urlType = 'url', urlElementType = 'string';
if ( ! url.match(/\D/) ) { urlType = 'page-number'; urlElementType = 'integer'; }
$('.selected').each( function () {
$(this).data( 'url', url );
$('#' + this.id + ' .urlType', ld).text(urlType);
$('#' + this.id + ' .urlElementType', ld).text(urlElementType);
$('#' + this.id + ' .url', ld).text(url);
} )
$(this).blur();
e.preventDefault();
changed = true;
return false;
}
} );
// FUNCTIONS
window.scrollBarWidth = function() {
document.body.style.overflow = 'hidden';
var width = document.body.clientWidth;
document.body.style.overflow = 'scroll';
width -= document.body.clientWidth;
if(!width) width = document.body.offsetWidth - document.body.clientWidth;
document.body.style.overflow = '';
return width;
}
function linkXML (dims, url) {
var urlType = 'url', urlElementType = 'string';
if ( ! url.match(/\D/) ) { urlType = 'page-number'; urlElementType = 'integer'; }
return [
'<dict>',
'\t<key>view-type</key>',
'\t<string>link</string>',
'\t<key>final-coordinates</key>',
'\t<string>[span class="dims"]%s[/span]</string>',
'\t<key>[span class="urlType"]%s[/span]</key>',
'\t<[span class="urlElementType"]%s[/span]>[span class="url"]%s[/span]</[span class="urlElementType"]%s[/span]>',
'</dict>'
].join('\n').replace(/</g, '<').replace(/\[/g, '<').replace(/\]/g, '>').sprintf(
dims, urlType, urlElementType, url, urlElementType
);
}
function update_image () {
localStorage.setItem( 'page_filename', filename );
$('#background').prop( 'src', encodeURI('pages/' + filename) ).width(768);
setTimeout( function () {
if ( $('#background').height() > 1004 ) {
$('#background').height(1004).width('auto');
}
setTimeout( function () {
$('#v-guide').height( $('#background').height() ).show();
$('#h-guide').width( $('#background').width() ).show();
var window_width = $('#background').width() + window.scrollBarWidth();
window.resizeTo( window_width, window.screen.availHeight );
lw.moveTo(window_width + 1, 0);
}, 50 );
}, 50 );
$('.link').remove();
$('#upbox').text( filename );
$('.nav').prop('disabled', false);
linking = null;
update_plist(filename);
}
var xs = new XMLSerializer;
var plist_xml, dict_array_xml;
function update_plist (filename) {
if ( fromServer && changed ) { save_plist(); changed = false; }
$('#links', ld).empty();
var plistname = pages[filename.match( /(\d+)\.jpg/ )[1]] + '.plist';
$('#filename', ld).text( plistname );
$.ajax( {
url: plistname,
tries: 0,
dataType: 'xml',
success: function (xml) {
n_links = 0;
plist_xml = xml;
dict_array_xml = $(xml).find('array');
dict_array_xml.children().each( function () {
var dims, url;
$(this).children().each( function () {
switch ( $(this).text() ) {
case 'final-coordinates':
dims = $(this).next().text();
case 'url':
case 'page-number':
url = $(this).next().text();
}
} );
$('#links', ld).append(
$('<div id="link-' + ++n_links + '"></div').html( linkXML( dims, url ) )
);
var m = dims.match( /{{(\d+),(\d+)},{(\d+),(\d+)}}/ );
if ( m ) {
var link = $('<div id="link-' + n_links + '" class="link linked"></div>');
$('body').append(link);
link.css( { left: m[1], top: m[2], width: m[3], height: m[4] } );
}
} );
},
error: function(x,e){
if(x.status==404){
console.error('Requested URL not found.');
}else if(x.status==500){
console.error('Internal Server Error.');
}else if(e=='parsererror'){
console.error('Response parsing failed.');
}else if(e=='timeout'){
console.error('Request Time out.');
}else {
console.error('Unknown Error.\n'+x.responseText);
}
if( this.tries++ > 2 ) return;
this.url = this.url.replace( /(\d+\.plist)/, '0$&' );
$.ajax(this);
}
} );
}
function save_plist () {
$(dict_array_xml).empty().append( $( $('#links', ld).text() ) );
$.ajax( {
url: '/' + $('#filename', ld).text(),
type: 'PUT',
data: xs.serializeToString(plist_xml),
error: function (jqXHR, type, error) {
console.error('Could not save plist, error type ' + type);
}
} );
}
function open_links_window () {
var win = window.open(
'', 'links_window',
'location=0,status=0,scrollbars=1,width=400,height=800'
);
var d = win.document;
d.writeln( '<style type="text/css" media="screen">' );
d.writeln( ' body { font-family: Verdana; font-size: 12px; margin: 10px; }' );
d.writeln( ' h2 { font-size: 14px; }' );
d.writeln( ' #filename { font-weight: bold; margin-bottom: 3px; }' );
d.writeln( ' #upform {' );
d.writeln( ' position: fixed; clear: both;' );
d.writeln( ' width: 100%;' );
d.writeln( ' margin: 0; padding: 6px; margin-left: -10px;' );
d.writeln( ' bottom: 0; border: none; border-top: 1px solid #666;' );
d.writeln( ' background-color: white; opacity: .8;' );
d.writeln( ' }' );
d.writeln( ' #links {' );
d.writeln( ' padding: 6px;' );
d.writeln( ' border-width: 1px;' );
d.writeln( ' border-style: inset;' );
d.writeln( ' background-color: #eee;' );
d.writeln( ' margin-bottom: 1em;' );
d.writeln( ' }' );
d.writeln( ' #links div { margin: 6px; white-space: pre; }' );
d.writeln( ' span { width: 150px; }' );
d.writeln( '</style>' );
d.writeln( '' );
d.writeln( '<title>Links</title>' );
d.writeln( '<div id="filename"></div>' );
d.writeln( '<div id="links"></div>' );
if ( fromServer ) {
d.writeln( '<input type="submit" id="save" value="Save">' );
}
d.writeln( '<div>' );
d.writeln( ' <h2>How to use this</h2>' );
if ( ! fromServer ) {
d.writeln( ' <p>Put this HTML file in the same directory as the issue plists.</p>' );
}
d.writeln( ' <p>Define link areas by clicking first on the top left corner and then the bottom right corner. Hold down the option key to turn off the guide snap.</p>' );
d.writeln( ' <p>Click another link to select it. You can shift-click to select multiple links. Use the arrow keys to move the selected links around. Hold the shift key to resize instead, and hold the option or Alt key to move or resize by 10px instead of 1px.</p>' );
d.writeln( ' <p>When you have links selected, you can edit their URLs, then press Enter to update the XML above.</p>' );
if ( fromServer ) {
d.writeln( ' <p>When everything looks right, click the "Save" button in this window, or click the "Next" button to save this page and load the next one.</p>' );
} else {
d.writeln( ' <p>When everything looks right, copy the XML text and paste it into the page\'s plist.</p>' );
d.writeln( ' <p>Finally you can click the "Next" button to load the next page.</p>' );
}
d.writeln( '</div>' );
d.close();
$('#links', d).on( {
mouseenter: function (e) {
$('#' + this.id).css( 'outline-width', '5px' );
},
mouseleave: function (e) {
$('#' + this.id).css( 'outline-width', '1px' );
},
dblclick: function (e) {
$(this).css( 'background-color', 'white' ).prop( 'contenteditable', true );
changed = true;
},
keydown: function (e) {
if ( e.which == 13 ) {
$(this).css( 'background-color', 'inherit' ).blur();
$(this).prop( 'contenteditable', false ).each( function () {
var m = $(this).text().match( /{{(\d+),(\d+)},{(\d+),(\d+)}}/ );
$('#' + this.id).css( { left: m[1], top: m[2], width: m[3], height: m[4] } );
} );
}
}
}, 'div' );
$('#save', d).click( save_plist );
return win;
}
// OPEN INITIAL PAGE
$.ajax( {
url: 'Replica.plist',
success: function (xml) {
$(xml).find('array').find('string').each( function (index, page) {
pages.push( page.textContent );
} );
filename = localStorage.getItem( 'page_filename' ) ||
pages[0].replace( /\d+$/, function (n) { return --n } ) + '.jpg';
update_image();
},
error: function () {
alert( 'No Replica.plist found. Please put this file in the issue directory.')
}
} );
</script>