|
| 1 | +/** |
| 2 | + * STOPSHIP(kamens) |
| 3 | +*/ |
| 4 | +(function($) { |
| 5 | + $.fn.menuAim = function(opts) { |
| 6 | + |
| 7 | + // STOPSHIP |
| 8 | + var $menu = $(this), |
| 9 | + activeRow = null, |
| 10 | + mouseLocs = [], |
| 11 | + lastDelayLoc = null, |
| 12 | + timeoutId = null, |
| 13 | + options = $.extend({ |
| 14 | + tolerance: 50, // STOPSHIP |
| 15 | + enter: $.noop, |
| 16 | + exit: $.noop, |
| 17 | + activate: $.noop, |
| 18 | + deactivate: $.noop |
| 19 | + }, opts); |
| 20 | + |
| 21 | + var MOUSE_LOCS_TRACKED = 4, // STOPSHIP |
| 22 | + DELAY = 300; // STOPSHIP |
| 23 | + |
| 24 | + /** |
| 25 | + * STOPSHIP |
| 26 | + */ |
| 27 | + var mousemove = function(e) { |
| 28 | + mouseLocs.push({x: e.pageX, y: e.pageY}); |
| 29 | + |
| 30 | + if (mouseLocs.length > MOUSE_LOCS_TRACKED) { |
| 31 | + // STOPSHIP |
| 32 | + mouseLocs.shift(); |
| 33 | + } |
| 34 | + }, |
| 35 | + mouseenter = function() { |
| 36 | + // STOPSHIP remove? |
| 37 | + }, |
| 38 | + mouseleave = function() { |
| 39 | + // STOPSHIP remove? |
| 40 | + if (timeoutId) { |
| 41 | + clearTimeout(timeoutId); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * STOPSHIP |
| 47 | + */ |
| 48 | + var mouseenterRow = function() { |
| 49 | + if (timeoutId) { |
| 50 | + clearTimeout(timeoutId); |
| 51 | + } |
| 52 | + |
| 53 | + options.enter(this); |
| 54 | + possiblyActivate(this); |
| 55 | + }, |
| 56 | + mouseleaveRow = function() { |
| 57 | + options.exit(this); |
| 58 | + }; |
| 59 | + |
| 60 | + /** |
| 61 | + * STOPSHIP |
| 62 | + */ |
| 63 | + var activate = function(row) { |
| 64 | + if (activeRow) { |
| 65 | + options.deactivate(activeRow); |
| 66 | + } |
| 67 | + |
| 68 | + options.activate(row); |
| 69 | + activeRow = row; |
| 70 | + }, |
| 71 | + possiblyActivate = function(row) { |
| 72 | + var delay = activationDelay(); |
| 73 | + |
| 74 | + if (delay) { |
| 75 | + timeoutId = setTimeout(function() { |
| 76 | + possiblyActivate(row) |
| 77 | + }, delay); |
| 78 | + } else { |
| 79 | + activate(row); |
| 80 | + } |
| 81 | + }, |
| 82 | + activationDelay = function() { |
| 83 | + var offset = $menu.offset(), |
| 84 | + upperRight = { |
| 85 | + x: offset.left + $menu.outerWidth(), |
| 86 | + y: offset.top - options.tolerance |
| 87 | + }, |
| 88 | + lowerRight = { |
| 89 | + x: offset.left + $menu.outerWidth(), |
| 90 | + y: offset.top + $menu.outerHeight() + options.tolerance |
| 91 | + }, |
| 92 | + loc = mouseLocs[mouseLocs.length - 1], |
| 93 | + prevLoc = mouseLocs[0]; |
| 94 | + |
| 95 | + if (!loc) { |
| 96 | + // STOPSHIP |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + if (!prevLoc) { |
| 101 | + // STOPSHIP |
| 102 | + prevLoc = loc; |
| 103 | + } |
| 104 | + |
| 105 | + if (lastDelayLoc && |
| 106 | + loc.x == lastDelayLoc.x && loc.y == lastDelayLoc.y) { |
| 107 | + // STOPSHIP |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + // STOPSHIP -- how all this works |
| 112 | + function slope(a, b) { |
| 113 | + return (b.y - a.y) / (b.x - a.x); |
| 114 | + }; |
| 115 | + |
| 116 | + var upperSlope = slope(loc, upperRight), |
| 117 | + lowerSlope = slope(loc, lowerRight), |
| 118 | + prevUpperSlope = slope(prevLoc, upperRight), |
| 119 | + prevLowerSlope = slope(prevLoc, lowerRight); |
| 120 | + |
| 121 | + if (upperSlope < prevUpperSlope && |
| 122 | + lowerSlope > prevLowerSlope) { |
| 123 | + // STOPSHIP |
| 124 | + lastDelayLoc = loc; |
| 125 | + return DELAY; |
| 126 | + } |
| 127 | + |
| 128 | + lastDelayLoc = null; |
| 129 | + return 0; |
| 130 | + }; |
| 131 | + |
| 132 | + /** |
| 133 | + * STOPSHIP |
| 134 | + */ |
| 135 | + var init = function() { |
| 136 | + $menu |
| 137 | + .mouseenter(mouseenter) |
| 138 | + .mouseleave(mouseleave) |
| 139 | + .find(options.rowSelector) |
| 140 | + .mouseenter(mouseenterRow) |
| 141 | + .mouseleave(mouseleaveRow); |
| 142 | + |
| 143 | + $(document).mousemove(mousemove); |
| 144 | + }; |
| 145 | + |
| 146 | + init(); |
| 147 | + return this; |
| 148 | + }; |
| 149 | +})(jQuery); |
| 150 | + |
0 commit comments