-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decaffeinate: Convert coordinates.coffee and 7 other files to JS
- Loading branch information
1 parent
c3268b5
commit 6dcab4a
Showing
8 changed files
with
1,801 additions
and
1,573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,204 +1,224 @@ | ||
$window = require("./window") | ||
/* | ||
* decaffeinate suggestions: | ||
* DS102: Remove unnecessary code created because of implicit returns | ||
* DS205: Consider reworking code to avoid use of IIFEs | ||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md | ||
*/ | ||
const $window = require("./window"); | ||
|
||
getElementAtPointFromViewport = (doc, x, y) -> | ||
doc.elementFromPoint(x, y) | ||
const getElementAtPointFromViewport = (doc, x, y) => doc.elementFromPoint(x, y); | ||
|
||
getElementPositioning = ($el) -> | ||
el = $el[0] | ||
const getElementPositioning = function($el) { | ||
const el = $el[0]; | ||
|
||
win = $window.getWindowByElement(el) | ||
const win = $window.getWindowByElement(el); | ||
|
||
## properties except for width / height | ||
## are relative to the top left of the viewport | ||
rect = el.getBoundingClientRect() | ||
//# properties except for width / height | ||
//# are relative to the top left of the viewport | ||
const rect = el.getBoundingClientRect(); | ||
|
||
center = getCenterCoordinates(rect) | ||
const center = getCenterCoordinates(rect); | ||
|
||
## add the center coordinates | ||
## because its useful to any caller | ||
topCenter = center.y | ||
leftCenter = center.x | ||
//# add the center coordinates | ||
//# because its useful to any caller | ||
const topCenter = center.y; | ||
const leftCenter = center.x; | ||
|
||
return { | ||
scrollTop: el.scrollTop | ||
scrollLeft: el.scrollLeft | ||
width: rect.width | ||
height: rect.height | ||
scrollTop: el.scrollTop, | ||
scrollLeft: el.scrollLeft, | ||
width: rect.width, | ||
height: rect.height, | ||
fromViewport: { | ||
top: rect.top | ||
left: rect.left | ||
right: rect.right | ||
bottom: rect.bottom | ||
topCenter | ||
top: rect.top, | ||
left: rect.left, | ||
right: rect.right, | ||
bottom: rect.bottom, | ||
topCenter, | ||
leftCenter | ||
} | ||
}, | ||
fromWindow: { | ||
top: rect.top + win.pageYOffset | ||
left: rect.left + win.pageXOffset | ||
topCenter: topCenter + win.pageYOffset | ||
top: rect.top + win.pageYOffset, | ||
left: rect.left + win.pageXOffset, | ||
topCenter: topCenter + win.pageYOffset, | ||
leftCenter: leftCenter + win.pageXOffset | ||
} | ||
} | ||
|
||
getCoordsByPosition = (left, top, xPosition = "center", yPosition = "center") -> | ||
left = switch xPosition | ||
when "left" then Math.ceil(left) | ||
when "center" then Math.floor(left) | ||
when "right" then Math.floor(left) - 1 | ||
|
||
top = switch yPosition | ||
when "top" then Math.ceil(top) | ||
when "center" then Math.floor(top) | ||
when "bottom" then Math.floor(top) - 1 | ||
|
||
## returning x/y here because this is | ||
## about the target position we want | ||
## to fire the event at based on what | ||
## the desired xPosition and yPosition is | ||
}; | ||
}; | ||
|
||
const getCoordsByPosition = function(left, top, xPosition = "center", yPosition = "center") { | ||
left = (() => { switch (xPosition) { | ||
case "left": return Math.ceil(left); | ||
case "center": return Math.floor(left); | ||
case "right": return Math.floor(left) - 1; | ||
} })(); | ||
|
||
top = (() => { switch (yPosition) { | ||
case "top": return Math.ceil(top); | ||
case "center": return Math.floor(top); | ||
case "bottom": return Math.floor(top) - 1; | ||
} })(); | ||
|
||
//# returning x/y here because this is | ||
//# about the target position we want | ||
//# to fire the event at based on what | ||
//# the desired xPosition and yPosition is | ||
return { | ||
x: left | ||
x: left, | ||
y: top | ||
} | ||
|
||
getTopLeftCoordinates = (rect) -> | ||
x = rect.left | ||
y = rect.top | ||
getCoordsByPosition(x, y, "left", "top") | ||
|
||
getTopCoordinates = (rect) -> | ||
x = rect.left + rect.width / 2 | ||
y = rect.top | ||
getCoordsByPosition(x, y, "center", "top") | ||
|
||
getTopRightCoordinates = (rect) -> | ||
x = rect.left + rect.width | ||
y = rect.top | ||
getCoordsByPosition(x, y, "right", "top") | ||
|
||
getLeftCoordinates = (rect) -> | ||
x = rect.left | ||
y = rect.top + rect.height / 2 | ||
getCoordsByPosition(x, y, "left", "center") | ||
|
||
getCenterCoordinates = (rect) -> | ||
x = rect.left + rect.width / 2 | ||
y = rect.top + rect.height / 2 | ||
getCoordsByPosition(x, y, "center", "center") | ||
|
||
getRightCoordinates = (rect) -> | ||
x = rect.left + rect.width | ||
y = rect.top + rect.height / 2 | ||
getCoordsByPosition(x, y, "right", "center") | ||
|
||
getBottomLeftCoordinates = (rect) -> | ||
x = rect.left | ||
y = rect.top + rect.height | ||
getCoordsByPosition(x, y, "left", "bottom") | ||
|
||
getBottomCoordinates = (rect) -> | ||
x = rect.left + rect.width / 2 | ||
y = rect.top + rect.height | ||
getCoordsByPosition(x, y, "center", "bottom") | ||
|
||
getBottomRightCoordinates = (rect) -> | ||
x = rect.left + rect.width | ||
y = rect.top + rect.height | ||
getCoordsByPosition(x, y, "right", "bottom") | ||
|
||
getElementCoordinatesByPositionRelativeToXY = ($el, x, y) -> | ||
positionProps = getElementPositioning($el) | ||
|
||
{ fromViewport, fromWindow } = positionProps | ||
|
||
fromViewport.left += x | ||
fromViewport.top += y | ||
|
||
fromWindow.left += x | ||
fromWindow.top += y | ||
|
||
viewportTargetCoords = getTopLeftCoordinates(fromViewport) | ||
windowTargetCoords = getTopLeftCoordinates(fromWindow) | ||
|
||
fromViewport.x = viewportTargetCoords.x | ||
fromViewport.y = viewportTargetCoords.y | ||
|
||
fromWindow.x = windowTargetCoords.x | ||
fromWindow.y = windowTargetCoords.y | ||
|
||
return positionProps | ||
|
||
getElementCoordinatesByPosition = ($el, position = "center") -> | ||
positionProps = getElementPositioning($el) | ||
|
||
## get the coordinates from the window | ||
## but also from the viewport so | ||
## whoever is calling us can use it | ||
## however they'd like | ||
{ width, height, fromViewport, fromWindow } = positionProps | ||
|
||
## dynamically call the function by transforming the name | ||
## bottom -> getBottomCoordinates | ||
## topLeft -> getTopLeftCoordinates | ||
capitalizedPosition = position.charAt(0).toUpperCase() + position.slice(1) | ||
|
||
fnName = "get" + capitalizedPosition + "Coordinates" | ||
|
||
fn = calculations[fnName] | ||
|
||
## get the desired x/y coords based on | ||
## what position we're trying to target | ||
viewportTargetCoords = fn({ | ||
width | ||
height | ||
top: fromViewport.top | ||
}; | ||
}; | ||
|
||
const getTopLeftCoordinates = function(rect) { | ||
const x = rect.left; | ||
const y = rect.top; | ||
return getCoordsByPosition(x, y, "left", "top"); | ||
}; | ||
|
||
const getTopCoordinates = function(rect) { | ||
const x = rect.left + (rect.width / 2); | ||
const y = rect.top; | ||
return getCoordsByPosition(x, y, "center", "top"); | ||
}; | ||
|
||
const getTopRightCoordinates = function(rect) { | ||
const x = rect.left + rect.width; | ||
const y = rect.top; | ||
return getCoordsByPosition(x, y, "right", "top"); | ||
}; | ||
|
||
const getLeftCoordinates = function(rect) { | ||
const x = rect.left; | ||
const y = rect.top + (rect.height / 2); | ||
return getCoordsByPosition(x, y, "left", "center"); | ||
}; | ||
|
||
var getCenterCoordinates = function(rect) { | ||
const x = rect.left + (rect.width / 2); | ||
const y = rect.top + (rect.height / 2); | ||
return getCoordsByPosition(x, y, "center", "center"); | ||
}; | ||
|
||
const getRightCoordinates = function(rect) { | ||
const x = rect.left + rect.width; | ||
const y = rect.top + (rect.height / 2); | ||
return getCoordsByPosition(x, y, "right", "center"); | ||
}; | ||
|
||
const getBottomLeftCoordinates = function(rect) { | ||
const x = rect.left; | ||
const y = rect.top + rect.height; | ||
return getCoordsByPosition(x, y, "left", "bottom"); | ||
}; | ||
|
||
const getBottomCoordinates = function(rect) { | ||
const x = rect.left + (rect.width / 2); | ||
const y = rect.top + rect.height; | ||
return getCoordsByPosition(x, y, "center", "bottom"); | ||
}; | ||
|
||
const getBottomRightCoordinates = function(rect) { | ||
const x = rect.left + rect.width; | ||
const y = rect.top + rect.height; | ||
return getCoordsByPosition(x, y, "right", "bottom"); | ||
}; | ||
|
||
const getElementCoordinatesByPositionRelativeToXY = function($el, x, y) { | ||
const positionProps = getElementPositioning($el); | ||
|
||
const { fromViewport, fromWindow } = positionProps; | ||
|
||
fromViewport.left += x; | ||
fromViewport.top += y; | ||
|
||
fromWindow.left += x; | ||
fromWindow.top += y; | ||
|
||
const viewportTargetCoords = getTopLeftCoordinates(fromViewport); | ||
const windowTargetCoords = getTopLeftCoordinates(fromWindow); | ||
|
||
fromViewport.x = viewportTargetCoords.x; | ||
fromViewport.y = viewportTargetCoords.y; | ||
|
||
fromWindow.x = windowTargetCoords.x; | ||
fromWindow.y = windowTargetCoords.y; | ||
|
||
return positionProps; | ||
}; | ||
|
||
const getElementCoordinatesByPosition = function($el, position = "center") { | ||
const positionProps = getElementPositioning($el); | ||
|
||
//# get the coordinates from the window | ||
//# but also from the viewport so | ||
//# whoever is calling us can use it | ||
//# however they'd like | ||
const { width, height, fromViewport, fromWindow } = positionProps; | ||
|
||
//# dynamically call the function by transforming the name | ||
//# bottom -> getBottomCoordinates | ||
//# topLeft -> getTopLeftCoordinates | ||
const capitalizedPosition = position.charAt(0).toUpperCase() + position.slice(1); | ||
|
||
const fnName = `get${capitalizedPosition}Coordinates`; | ||
|
||
const fn = calculations[fnName]; | ||
|
||
//# get the desired x/y coords based on | ||
//# what position we're trying to target | ||
const viewportTargetCoords = fn({ | ||
width, | ||
height, | ||
top: fromViewport.top, | ||
left: fromViewport.left | ||
}) | ||
|
||
## get the desired x/y coords based on | ||
## what position we're trying to target | ||
windowTargetCoords = fn({ | ||
width | ||
height | ||
top: fromWindow.top | ||
}); | ||
|
||
//# get the desired x/y coords based on | ||
//# what position we're trying to target | ||
const windowTargetCoords = fn({ | ||
width, | ||
height, | ||
top: fromWindow.top, | ||
left: fromWindow.left | ||
}) | ||
}); | ||
|
||
fromViewport.x = viewportTargetCoords.x | ||
fromViewport.y = viewportTargetCoords.y | ||
fromViewport.x = viewportTargetCoords.x; | ||
fromViewport.y = viewportTargetCoords.y; | ||
|
||
fromWindow.x = windowTargetCoords.x | ||
fromWindow.y = windowTargetCoords.y | ||
fromWindow.x = windowTargetCoords.x; | ||
fromWindow.y = windowTargetCoords.y; | ||
|
||
## return an object with both sets | ||
## of normalized coordinates for both | ||
## the window and the viewport | ||
//# return an object with both sets | ||
//# of normalized coordinates for both | ||
//# the window and the viewport | ||
return { | ||
width | ||
height | ||
fromViewport | ||
width, | ||
height, | ||
fromViewport, | ||
fromWindow | ||
} | ||
|
||
calculations = { | ||
getTopCoordinates | ||
getTopLeftCoordinates | ||
getTopRightCoordinates | ||
getLeftCoordinates | ||
getCenterCoordinates | ||
getRightCoordinates | ||
getBottomLeftCoordinates | ||
getBottomCoordinates | ||
}; | ||
}; | ||
|
||
var calculations = { | ||
getTopCoordinates, | ||
getTopLeftCoordinates, | ||
getTopRightCoordinates, | ||
getLeftCoordinates, | ||
getCenterCoordinates, | ||
getRightCoordinates, | ||
getBottomLeftCoordinates, | ||
getBottomCoordinates, | ||
getBottomRightCoordinates | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getCoordsByPosition | ||
getCoordsByPosition, | ||
|
||
getElementPositioning | ||
getElementPositioning, | ||
|
||
getElementAtPointFromViewport | ||
getElementAtPointFromViewport, | ||
|
||
getElementCoordinatesByPosition | ||
getElementCoordinatesByPosition, | ||
|
||
getElementCoordinatesByPositionRelativeToXY | ||
} | ||
}; |
Oops, something went wrong.