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