2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor Position class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
/**
|
|
|
|
* Pixel position.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* This can also support an optional bottom field, to represent a vertical line, such as a cursor.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {Number} left Horizontal position
|
|
|
|
* @param {Number} top Vertical top position
|
|
|
|
* @param {Number} bottom Vertical bottom position of bottom (optional, default: top)
|
|
|
|
* @property {Number} left Horizontal position
|
|
|
|
* @property {Number} top Vertical top position
|
|
|
|
* @property {Number} bottom Vertical bottom position of bottom
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-09-06 23:15:55 +00:00
|
|
|
ve.Position = function VePosition( left, top, bottom ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.left = left || 0;
|
|
|
|
this.top = top || 0;
|
|
|
|
this.bottom = bottom || this.top;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates position object from the page position of an element.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {jQuery} $element Element to get offset from
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Position} Position with element data applied
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.newFromElementPagePosition = function ( $element ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
var offset = $element.offset();
|
2012-02-06 23:50:56 +00:00
|
|
|
return new ve.Position( offset.left, offset.top );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates position object from the layer position of an element.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {jQuery} $element Element to get position from
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Position} Position with element data applied
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.newFromElementLayerPosition = function ( $element ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
var position = $element.position();
|
2012-02-06 23:50:56 +00:00
|
|
|
return new ve.Position( position.left, position.top );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates position object from the screen position data in an Event object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {Event} event Event to get position data from
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Position} Position with event data applied
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.newFromEventScreenPosition = function ( event ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
return new ve.Position( event.screenX, event.screenY );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates position object from the page position data in an Event object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {Event} event Event to get position data from
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Position} Position with event data applied
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.newFromEventPagePosition = function ( event ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
return new ve.Position( event.pageX, event.pageY );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates position object from the layer position data in an Event object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {Event} event Event to get position data from
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Position} Position with event data applied
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.newFromEventLayerPosition = function ( event ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
return new ve.Position( event.layerX, event.layerY );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the values of a given position to this one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to add values from
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.add = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.top += position.top;
|
|
|
|
this.bottom += position.bottom;
|
|
|
|
this.left += position.left;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts the values of a given position to this one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to subtract values from
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.subtract = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.top -= position.top;
|
|
|
|
this.bottom -= position.bottom;
|
|
|
|
this.left -= position.left;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is the same as another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If positions have the same left and top values
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.at = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.left === position.left && this.top === position.top;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position perpendicular with another one, sharing either a top or left value.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If positions share a top or a left value
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.perpendicularWith = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.left === position.left || this.top === position.top;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is level with another one, having the same top value.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If positions have the same top value
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.levelWith = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.top === position.top;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is plumb with another one, having the same left value.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If positions have the same left value
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.plumbWith = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.left === position.left;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is nearby another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* Distance is measured radially.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
|
|
|
* @param {Number} radius Pixel distance from this position to consider "near-by"
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If positions are near-by each other
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.near = function ( position, radius ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return Math.sqrt(
|
|
|
|
Math.pow( this.left - position.left, 2 ),
|
|
|
|
Math.pow( this.top - position.top )
|
|
|
|
) <= radius;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is above another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* This method utilizes the bottom property.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If this position is above the other
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.above = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.bottom < position.top;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is below another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* This method utilizes the bottom property.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If this position is below the other
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.below = function ( position ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.top > position.bottom;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is to the left of another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If this position is the left the other
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.leftOf = function ( left ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.left < left;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this position is to the right of another one.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {ve.Position} position Position to compare with
|
2011-11-02 21:00:55 +00:00
|
|
|
* @returns {Boolean} If this position is the right the other
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Position.prototype.rightOf = function ( left ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.left > left;
|
|
|
|
};
|