OLD | NEW |
1 /** | 1 /** |
2 * Common JS that talks XHR back to the server and runs the code and receives | 2 * Common JS that talks XHR back to the server and runs the code and receives |
3 * the results. | 3 * the results. |
4 */ | 4 */ |
5 | 5 |
6 /** | 6 /** |
7 * A polyfill for HTML Templates. | 7 * A polyfill for HTML Templates. |
8 * | 8 * |
9 * This just adds in the content attribute, it doesn't stop scripts | 9 * This just adds in the content attribute, it doesn't stop scripts |
10 * from running nor does it stop other side-effects. | 10 * from running nor does it stop other side-effects. |
11 */ | 11 */ |
12 (function polyfillTemplates() { | 12 (function polyfillTemplates() { |
13 if('content' in document.createElement('template')) { | 13 if('content' in document.createElement('template')) { |
14 return false; | 14 return false; |
15 } | 15 } |
16 | 16 |
17 var templates = document.getElementsByTagName('template'); | 17 var templates = document.getElementsByTagName('template'); |
18 for (var i=0; i<templates.length; i++) { | 18 for (var i=0; i<templates.length; i++) { |
19 var content = document.createDocumentFragment(); | 19 var content = document.createDocumentFragment(); |
20 while (templates[i].firstChild) { | 20 while (templates[i].firstChild) { |
21 content.appendChild(templates[i].firstChild); | 21 content.appendChild(templates[i].firstChild); |
22 } | 22 } |
23 templates[i].content = content; | 23 templates[i].content = content; |
24 } | 24 } |
25 })(); | 25 })(); |
26 | 26 |
27 /** | 27 /** |
| 28 * Enable zooming for any images with a class of 'zoom'. |
| 29 */ |
| 30 (function () { |
| 31 var clientX = 0; |
| 32 var clientY = 0; |
| 33 var lastClientX = 0; |
| 34 var lastClientY = 0; |
| 35 var ctx = null; // The 2D canvas context of the zoom. |
| 36 var currentImage = null; // The img node we are zooming for, otherwise null. |
| 37 |
| 38 function zoomMove(e) { |
| 39 clientX = e.clientX; |
| 40 clientY = e.clientY; |
| 41 } |
| 42 |
| 43 function zoomMouseDown(e) { |
| 44 e.preventDefault(); |
| 45 // Only do zooming on the primary mouse button. |
| 46 if (e.button != 0) { |
| 47 return |
| 48 } |
| 49 currentImage = e.target; |
| 50 clientX = e.clientX; |
| 51 clientY = e.clientY; |
| 52 lastClientX = clientX-1; |
| 53 lastClientY = clientY-1; |
| 54 document.body.style.cursor = 'crosshair'; |
| 55 canvas = document.createElement('canvas'); |
| 56 canvas.width=256; |
| 57 canvas.height=256; |
| 58 canvas.classList.add('zoomCanvas'); |
| 59 ctx = canvas.getContext('2d'); |
| 60 ctx.imageSmoothingEnabled = false; |
| 61 this.parentNode.insertBefore(canvas, this); |
| 62 |
| 63 document.body.addEventListener('mousemove', zoomMove, true); |
| 64 document.body.addEventListener('mouseup', zoomFinished); |
| 65 document.body.addEventListener('mouseleave', zoomFinished); |
| 66 |
| 67 // Kick off the drawing. |
| 68 setTimeout(drawZoom, 1); |
| 69 } |
| 70 |
| 71 function drawZoom() { |
| 72 if (currentImage) { |
| 73 // Only draw if the mouse has moved from the last time we drew. |
| 74 if (lastClientX != clientX || lastClientY != clientY) { |
| 75 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 76 ctx.drawImage(currentImage, |
| 77 clientX - currentImage.x, clientY - currentImage.y, // src zero |
| 78 32, 32, // src dimensio
ns |
| 79 0, 0, // dst zero |
| 80 ctx.canvas.width, ctx.canvas.height); // dst dimensio
ns |
| 81 lastClientX = clientX; |
| 82 lastClientY = clientY; |
| 83 } |
| 84 setTimeout(drawZoom, 1000/30); |
| 85 } |
| 86 } |
| 87 |
| 88 function zoomFinished() { |
| 89 currentImage = null; |
| 90 document.body.style.cursor = 'default'; |
| 91 ctx.canvas.parentNode.removeChild(ctx.canvas); |
| 92 document.body.removeEventListener('mousemove', zoomMove, true); |
| 93 document.body.removeEventListener('mouseup', zoomFinished); |
| 94 document.body.removeEventListener('mouseleave', zoomFinished); |
| 95 } |
| 96 |
| 97 this.addEventListener('DOMContentLoaded', function() { |
| 98 var zoomables = document.body.querySelectorAll('.zoom'); |
| 99 for (var i=0; i<zoomables.length; i++) { |
| 100 zoomables[i].addEventListener('mousedown', zoomMouseDown); |
| 101 } |
| 102 }); |
| 103 })(); |
| 104 |
| 105 |
| 106 /** |
28 * All the functionality is wrapped up in this anonymous closure, but we need | 107 * All the functionality is wrapped up in this anonymous closure, but we need |
29 * to be told if we are on the workspace page or a normal try page, so the | 108 * to be told if we are on the workspace page or a normal try page, so the |
30 * workspaceName is passed into the closure, it must be set in the global | 109 * workspaceName is passed into the closure, it must be set in the global |
31 * namespace. If workspaceName is the empty string then we know we aren't | 110 * namespace. If workspaceName is the empty string then we know we aren't |
32 * running on a workspace page. | 111 * running on a workspace page. |
33 * | 112 * |
34 * If we are on a workspace page we also look for a 'history' | 113 * If we are on a workspace page we also look for a 'history' |
35 * variable in the global namespace which contains the list of tries | 114 * variable in the global namespace which contains the list of tries |
36 * that are included in this workspace. That variable is used to | 115 * that are included in this workspace. That variable is used to |
37 * populate the history list. | 116 * populate the history list. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 273 |
195 | 274 |
196 // Add the images to the history if we are on a workspace page. | 275 // Add the images to the history if we are on a workspace page. |
197 if (tryHistory && history) { | 276 if (tryHistory && history) { |
198 for (var i=0; i<history.length; i++) { | 277 for (var i=0; i<history.length; i++) { |
199 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); | 278 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); |
200 } | 279 } |
201 } | 280 } |
202 | 281 |
203 })(workspaceName); | 282 })(workspaceName); |
OLD | NEW |