mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
fixing tab spaces and some documentation
This commit is contained in:
@@ -9,33 +9,37 @@
|
||||
;(function($, window, document, undefined){
|
||||
|
||||
var defaults = {
|
||||
colliders_context: document.body,
|
||||
on_overlap: function(collider_data){},
|
||||
on_overlap_start : function(collider_data){
|
||||
// console.log('the element START being a collider', collider_data);
|
||||
},
|
||||
on_overlap_stop : function(collider_data){
|
||||
// console.log('the element STOP being a collider', collider_data);
|
||||
}
|
||||
colliders_context: document.body
|
||||
// ,on_overlap: function(collider_data){},
|
||||
// on_overlap_start : function(collider_data){},
|
||||
// on_overlap_stop : function(collider_data){}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Collision
|
||||
*
|
||||
* @class Collision
|
||||
*
|
||||
* Detects collisions between a DOM element against other DOM elements or
|
||||
* Coords objects.
|
||||
*
|
||||
* @uses Coords
|
||||
* @param {HTMLElement} element An Attribute name or object property path
|
||||
* @param {String|HTMLElement|Array} colliders An Attribute name or object property path
|
||||
* @param {Object} [options] An Attribute name or object property path
|
||||
* @param {Function} [options.on_overlap] An Attribute name or object property path
|
||||
* @param {Function} [options.on_overlap_start] An Attribute name or object property path
|
||||
* @param {Function} [options.on_overlap_stop] An Attribute name or object property path
|
||||
* @return {Object} dasdasdadasd
|
||||
* @param {HTMLElement} el The jQuery wrapped HTMLElement.
|
||||
* @param {HTMLElement|Array} colliders Can be a jQuery collection
|
||||
* of HTMLElements or an Array of Coords instances.
|
||||
* @param {Object} [options] An Object with all options you want to
|
||||
* overwrite:
|
||||
* @param {Function} [options.on_overlap_start] Executes a function the first
|
||||
* time each `collider ` is overlapped.
|
||||
* @param {Function} [options.on_overlap_stop] Executes a function when a
|
||||
* `collider` is no longer collided.
|
||||
* @param {Function} [options.on_overlap] Executes a function when the
|
||||
* mouse is moved during the collision.
|
||||
* @return {Object} Collision instance.
|
||||
* @constructor
|
||||
*/
|
||||
function Collision(element, colliders, options) {
|
||||
function Collision(el, colliders, options) {
|
||||
this.options = $.extend(defaults, options);
|
||||
this.$element = element;
|
||||
this.$element = el;
|
||||
this.last_colliders = [];
|
||||
this.last_colliders_coords = [];
|
||||
if (typeof colliders === 'string' || colliders instanceof jQuery) {
|
||||
@@ -48,12 +52,15 @@
|
||||
this.init();
|
||||
}
|
||||
|
||||
|
||||
var fn = Collision.prototype;
|
||||
|
||||
|
||||
fn.init = function() {
|
||||
this.find_collisions();
|
||||
};
|
||||
|
||||
|
||||
fn.overlaps = function(a, b) {
|
||||
var x = false;
|
||||
var y = false;
|
||||
@@ -71,6 +78,7 @@
|
||||
return (x && y);
|
||||
};
|
||||
|
||||
|
||||
fn.detect_overlapping_region = function(a, b){
|
||||
var regionX = '';
|
||||
var regionY = '';
|
||||
@@ -83,6 +91,7 @@
|
||||
return (regionX + regionY) || 'C';
|
||||
};
|
||||
|
||||
|
||||
fn.calculate_overlapped_area_coords = function(a, b){
|
||||
var x1 = Math.max(a.x1, b.x1);
|
||||
var y1 = Math.max(a.y1, b.y1);
|
||||
@@ -97,10 +106,12 @@
|
||||
}).coords().get();
|
||||
};
|
||||
|
||||
|
||||
fn.calculate_overlapped_area = function(coords){
|
||||
return (coords.width * coords.height);
|
||||
};
|
||||
|
||||
|
||||
fn.manage_colliders_start_stop = function(new_colliders_coords, start_callback, stop_callback){
|
||||
var last = this.last_colliders_coords;
|
||||
|
||||
@@ -118,16 +129,19 @@
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
fn.find_collisions = function(player_data_coords){
|
||||
var self = this;
|
||||
var colliders_coords = [];
|
||||
var colliders_data = [];
|
||||
var $colliders = (this.colliders || this.$colliders);
|
||||
var count = $colliders.length;
|
||||
var player_coords = self.$element.coords().update(player_data_coords || false).get();
|
||||
var player_coords = self.$element.coords()
|
||||
.update(player_data_coords || false).get();
|
||||
|
||||
while(count--){
|
||||
var $collider = self.$colliders ? $($colliders[count]) : $colliders[count];
|
||||
var $collider = self.$colliders ?
|
||||
$($colliders[count]) : $colliders[count];
|
||||
var $collider_coords_ins = ($collider.isCoords) ?
|
||||
$collider : $collider.coords();
|
||||
var collider_coords = $collider_coords_ins.get();
|
||||
@@ -137,9 +151,10 @@
|
||||
continue;
|
||||
}
|
||||
|
||||
var region = self.detect_overlapping_region(player_coords,
|
||||
collider_coords);
|
||||
//todo: make this if customizable
|
||||
var region = self.detect_overlapping_region(
|
||||
player_coords, collider_coords);
|
||||
|
||||
//todo: make this an option
|
||||
if (region === 'C'){
|
||||
var area_coords = self.calculate_overlapped_area_coords(
|
||||
player_coords, collider_coords);
|
||||
@@ -153,15 +168,18 @@
|
||||
el: $collider
|
||||
};
|
||||
|
||||
if (self.options.on_overlap) {
|
||||
self.options.on_overlap.call(this, collider_data);
|
||||
}
|
||||
colliders_coords.push($collider_coords_ins);
|
||||
colliders_data.push(collider_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (self.options.on_overlap_stop || self.options.on_overlap_start) {
|
||||
this.manage_colliders_start_stop(colliders_coords,
|
||||
self.options.on_overlap_stop, self.options.on_overlap_start);
|
||||
}
|
||||
|
||||
this.last_colliders_coords = colliders_coords;
|
||||
|
||||
@@ -173,7 +191,6 @@
|
||||
var colliders = this.find_collisions(player_data_coords);
|
||||
var min_area = 100;
|
||||
colliders.sort(function(a, b){
|
||||
|
||||
if (a.area <= min_area) {
|
||||
return 1;
|
||||
}
|
||||
@@ -198,6 +215,7 @@
|
||||
return colliders;
|
||||
};
|
||||
|
||||
|
||||
//jQuery adapter
|
||||
$.fn.collision = function(collider, options) {
|
||||
return new Collision( this, collider, options );
|
||||
|
Reference in New Issue
Block a user