| 1 | /* |
| 2 | |
| 3 | Drag: A Really Simple Drag Handler |
| 4 | |
| 5 | */ |
| 6 | Drag = { |
| 7 | _move: null, |
| 8 | _down: null, |
| 9 | |
| 10 | start: function(e) { |
| 11 | e.stop(); |
| 12 | |
| 13 | // We need to remember what we're dragging. |
| 14 | Drag._target = e.target(); |
| 15 | |
| 16 | /* |
| 17 | There's no cross-browser way to get offsetX and offsetY, so we |
| 18 | have to do it ourselves. For performance, we do this once and |
| 19 | cache it. |
| 20 | */ |
| 21 | Drag._offset = Drag._diff( |
| 22 | e.mouse().page, |
| 23 | getElementPosition(Drag._target)); |
| 24 | |
| 25 | Drag._move = connect(document, 'onmousemove', Drag._drag); |
| 26 | Drag._down = connect(document, 'onmouseup', Drag._stop); |
| 27 | }, |
| 28 | |
| 29 | _offset: null, |
| 30 | _target: null, |
| 31 | |
| 32 | _diff: function(lhs, rhs) { |
| 33 | return new MochiKit.Style.Coordinates(lhs.x - rhs.x, lhs.y - rhs.y); |
| 34 | }, |
| 35 | |
| 36 | _drag: function(e) { |
| 37 | e.stop(); |
| 38 | setElementPosition( |
| 39 | Drag._target, |
| 40 | Drag._diff(e.mouse().page, Drag._offset)); |
| 41 | }, |
| 42 | |
| 43 | _stop: function(e) { |
| 44 | disconnect(Drag._move); |
| 45 | disconnect(Drag._down); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | connect(window, 'onload', |
| 50 | function() { |
| 51 | /* |
| 52 | Find all DIVs tagged with the draggable class, and connect them to |
| 53 | the Drag handler. |
| 54 | */ |
| 55 | var d = getElementsByTagAndClassName('DIV', 'draggable'); |
| 56 | forEach(d, |
| 57 | function(elem) { |
| 58 | connect(elem, 'onmousedown', Drag.start); |
| 59 | }); |
| 60 | |
| 61 | }); |
| 62 | |
| 63 | connect(window, 'onload', |
| 64 | function() { |
| 65 | var elems = getElementsByTagAndClassName("A", "view-source"); |
| 66 | var page = "draggable/"; |
| 67 | for (var i = 0; i < elems.length; i++) { |
| 68 | var elem = elems[i]; |
| 69 | var href = elem.href.split(/\//).pop(); |
| 70 | elem.target = "_blank"; |
| 71 | elem.href = "../view-source/view-source.html#" + page + href; |
| 72 | } |
| 73 | }); |