aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrmr19932014-12-18 11:56:53 +0000
committermrmr19932014-12-18 11:56:53 +0000
commit9c9c48598534c2a0cd8aec28a4a806d74f28e090 (patch)
treee81d488340f8bb3106382908a3eefbdb58f2646a
parent3132ae601b2de787f9cddd3fd77b36767e2e467e (diff)
downloadvimium-9c9c48598534c2a0cd8aec28a4a806d74f28e090.tar.bz2
Move rect functions to their own file
-rw-r--r--content_scripts/link_hints.coffee2
-rw-r--r--lib/dom_utils.coffee3
-rw-r--r--lib/rect.coffee64
-rw-r--r--lib/utils.coffee60
-rw-r--r--manifest.json1
-rw-r--r--tests/dom_tests/dom_tests.html1
6 files changed, 68 insertions, 63 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee
index 57b46e6b..27402250 100644
--- a/content_scripts/link_hints.coffee
+++ b/content_scripts/link_hints.coffee
@@ -198,7 +198,7 @@ LinkHints =
while visibleElement = visibleElements.pop()
rects = [visibleElement.rect]
for {rect: negativeRect} in visibleElements
- rects = Array::concat.apply [], (rects.map (rect) -> Utils.subtractRect rect, negativeRect)
+ rects = Array::concat.apply [], (rects.map (rect) -> Rect.subtract rect, negativeRect)
if rects.length > 0
nonOverlappingElements.push {element: visibleElement.element, rect: rects[0]}
else
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index 7e19a7fc..ebbed006 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -95,7 +95,6 @@ DomUtils =
else
rect
-
getClientRectsForAreas: (imgClientRect, areas) ->
rects = []
for area in areas
@@ -114,7 +113,7 @@ DomUtils =
# something more sophisticated, but likely not worth the effort.
[x1, y1, x2, y2] = coords
- rect = Utils.shiftRect (Utils.createRect x1, y1, x2, y2), imgClientRect.left, imgClientRect.top
+ rect = Rect.translate (Rect.create x1, y1, x2, y2), imgClientRect.left, imgClientRect.top
rect = @cropRectToVisible rect
rects.push {element: area, rect: rect} unless not rect or isNaN rect.top
diff --git a/lib/rect.coffee b/lib/rect.coffee
new file mode 100644
index 00000000..67c9de7c
--- /dev/null
+++ b/lib/rect.coffee
@@ -0,0 +1,64 @@
+# Commands for manipulating rects.
+Rect =
+ # Create a rect given the top left and bottom right corners.
+ create: (x1, y1, x2, y2) ->
+ bottom: y2
+ top: y1
+ left: x1
+ right: x2
+ width: x2 - x1
+ height: y2 - y1
+
+ # Translate a rect by x horizontally and y vertically.
+ translate: (rect, x, y) ->
+ bottom: rect.bottom + y
+ top: rect.top + y
+ left: rect.left + x
+ right: rect.right + x
+ width: rect.width
+ height: rect.height
+
+ # Subtract rect2 from rect1, returning an array of rects which are in rect1 but not rect2.
+ subtract: (rect1, rect2_) ->
+ # Bound rect2 by rect1
+ rect2 = {}
+ rect2 = @create(
+ Math.max(rect1.left, rect2_.left),
+ Math.max(rect1.top, rect2_.top),
+ Math.min(rect1.right, rect2_.right),
+ Math.min(rect1.bottom, rect2_.bottom)
+ )
+
+ # If bounding rect2 has made the width or height negative, rect1 does not contain rect2.
+ return [rect1] if rect2.width < 0 or rect2.height < 0
+
+ #
+ # All the possible rects, in the order
+ # +-+-+-+
+ # |1|2|3|
+ # +-+-+-+
+ # |4| |5|
+ # +-+-+-+
+ # |6|7|8|
+ # +-+-+-+
+ # where the outer rectangle is rect1 and the inner rectangle is rect 2. Note that the rects may be of
+ # width or height 0.
+ #
+ rects = [
+ # Top row.
+ @create rect1.left, rect1.top, rect2.left, rect2.top
+ @create rect2.left, rect1.top, rect2.right, rect2.top
+ @create rect2.right, rect1.top, rect1.right, rect2.top
+ # Middle row.
+ @create rect1.left, rect2.top, rect2.left, rect2.bottom
+ @create rect2.right, rect2.top, rect1.right, rect2.bottom
+ # Bottom row.
+ @create rect1.left, rect2.bottom, rect2.left, rect1.bottom
+ @create rect2.left, rect2.bottom, rect2.right, rect1.bottom
+ @create rect2.right, rect2.bottom, rect1.right, rect1.bottom
+ ]
+
+ rects.filter (rect) -> rect.height > 0 and rect.width > 0
+
+root = exports ? window
+root.Rect = Rect
diff --git a/lib/utils.coffee b/lib/utils.coffee
index 6cc45f32..b7f8731a 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -136,66 +136,6 @@ Utils =
# locale-sensitive uppercase detection
hasUpperCase: (s) -> s.toLowerCase() != s
- # Create a rect given the top left and bottom right corners.
- createRect: (x1, y1, x2, y2) ->
- bottom: y2
- top: y1
- left: x1
- right: x2
- width: x2 - x1
- height: y2 - y1
-
- # Translate a rect by x horizontally and y vertically.
- shiftRect: (rect, x, y) ->
- bottom: rect.bottom + y
- top: rect.top + y
- left: rect.left + x
- right: rect.right + x
- width: rect.width
- height: rect.height
-
- # Subtract rect2 from rect1, returning an array of rects which are in rect1 but not rect2.
- subtractRect: (rect1, rect2_) ->
- # Bound rect2 by rect1
- rect2 = {}
- rect2 = @createRect(
- Math.max(rect1.left, rect2_.left),
- Math.max(rect1.top, rect2_.top),
- Math.min(rect1.right, rect2_.right),
- Math.min(rect1.bottom, rect2_.bottom)
- )
-
- # If bounding rect2 has made the width or height negative, rect1 does not contain rect2.
- return [rect1] if rect2.width < 0 or rect2.height < 0
-
- #
- # All the possible rects, in the order
- # +-+-+-+
- # |1|2|3|
- # +-+-+-+
- # |4| |5|
- # +-+-+-+
- # |6|7|8|
- # +-+-+-+
- # where the outer rectangle is rect1 and the inner rectangle is rect 2. Note that the rects may be of
- # width or height 0.
- #
- rects = [
- # Top row.
- @createRect rect1.left, rect1.top, rect2.left, rect2.top
- @createRect rect2.left, rect1.top, rect2.right, rect2.top
- @createRect rect2.right, rect1.top, rect1.right, rect2.top
- # Middle row.
- @createRect rect1.left, rect2.top, rect2.left, rect2.bottom
- @createRect rect2.right, rect2.top, rect1.right, rect2.bottom
- # Bottom row.
- @createRect rect1.left, rect2.bottom, rect2.left, rect1.bottom
- @createRect rect2.left, rect2.bottom, rect2.right, rect1.bottom
- @createRect rect2.right, rect2.bottom, rect1.right, rect1.bottom
- ]
-
- rects.filter (rect) -> rect.height > 0 and rect.width > 0
-
# This creates a new function out of an existing function, where the new function takes fewer arguments. This
# allows us to pass around functions instead of functions + a partial list of arguments.
Function::curry = ->
diff --git a/manifest.json b/manifest.json
index 3cd88d1e..e5271692 100644
--- a/manifest.json
+++ b/manifest.json
@@ -35,6 +35,7 @@
"js": ["lib/utils.js",
"lib/keyboard_utils.js",
"lib/dom_utils.js",
+ "lib/rect.js",
"lib/handler_stack.js",
"lib/clipboard.js",
"content_scripts/link_hints.js",
diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html
index feddafac..d8232892 100644
--- a/tests/dom_tests/dom_tests.html
+++ b/tests/dom_tests/dom_tests.html
@@ -32,6 +32,7 @@
<script type="text/javascript" src="../../lib/utils.js"></script>
<script type="text/javascript" src="../../lib/keyboard_utils.js"></script>
<script type="text/javascript" src="../../lib/dom_utils.js"></script>
+ <script type="text/javascript" src="../../lib/rect.js"></script>
<script type="text/javascript" src="../../lib/handler_stack.js"></script>
<script type="text/javascript" src="../../lib/clipboard.js"></script>
<script type="text/javascript" src="../../content_scripts/link_hints.js"></script>