Spaces:
Running
Running
function clone(obj) { | |
if(obj == null || typeof(obj) != 'object') | |
return obj; | |
var temp = obj.constructor(); | |
for(var key in obj) | |
temp[key] = clone(obj[key]); | |
return temp; | |
} | |
function getParameterByName(name) { | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
results = regex.exec(location.search); | |
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
Array.prototype.remove = function(item) { | |
// TODO Update to use Array.prototype.indexOf | |
for(var i = this.length; i--;) { | |
if(this[i] === item) { | |
this.splice(i, 1); | |
} | |
} | |
} | |
Array.prototype.unique = function() { | |
var a = this.concat(); | |
for(var i=0; i<a.length; ++i) { | |
for(var j=i+1; j<a.length; ++j) { | |
if(a[i] === a[j]) | |
a.splice(j--, 1); | |
} | |
} | |
return a; | |
}; | |
// Distance from point to line | |
// http://stackoverflow.com/a/6853926/2608804 | |
function pDistance(x, y, x1, y1, x2, y2) { | |
var A = x - x1; | |
var B = y - y1; | |
var C = x2 - x1; | |
var D = y2 - y1; | |
var dot = A * C + B * D; | |
var len_sq = C * C + D * D; | |
var param = dot / len_sq; | |
var xx, yy; | |
if (param < 0 || (x1 == x2 && y1 == y2)) { | |
xx = x1; | |
yy = y1; | |
} | |
else if (param > 1) { | |
xx = x2; | |
yy = y2; | |
} | |
else { | |
xx = x1 + param * C; | |
yy = y1 + param * D; | |
} | |
var dx = x - xx; | |
var dy = y - yy; | |
return Math.sqrt(dx * dx + dy * dy); | |
} | |
/** | |
* jQuery.fn.sortElements | |
* | |
* from http://james.padolsey.com/javascript/sorting-elements-with-jquery | |
* -------------- | |
* @param Function comparator: | |
* Exactly the same behaviour as [1,2,3].sort(comparator) | |
* | |
* @param Function getSortable | |
* A function that should return the element that is | |
* to be sorted. The comparator will run on the | |
* current collection, but you may want the actual | |
* resulting sort to occur on a parent or another | |
* associated element. | |
* | |
* E.g. $('td').sortElements(comparator, function(){ | |
* return this.parentNode; | |
* }) | |
* | |
* The <td>'s parent (<tr>) will be sorted instead | |
* of the <td> itself. | |
*/ | |
jQuery.fn.sortElements = (function(){ | |
var sort = [].sort; | |
return function(comparator, getSortable) { | |
getSortable = getSortable || function(){return this;}; | |
var placements = this.map(function(){ | |
var sortElement = getSortable.call(this), | |
parentNode = sortElement.parentNode, | |
// Since the element itself will change position, we have | |
// to have some way of storing its original position in | |
// the DOM. The easiest way is to have a 'flag' node: | |
nextSibling = parentNode.insertBefore( | |
document.createTextNode(''), | |
sortElement.nextSibling | |
); | |
return function() { | |
if (parentNode === this) { | |
throw new Error( | |
"You can't sort elements if any one is a descendant of another." | |
); | |
} | |
// Insert before flag: | |
parentNode.insertBefore(this, nextSibling); | |
// Remove flag: | |
parentNode.removeChild(nextSibling); | |
}; | |
}); | |
return sort.call(this, comparator).each(function(i){ | |
placements[i].call(getSortable.call(this)); | |
}); | |
}; | |
})(); | |
// http://stackoverflow.com/a/20095486/2608804 | |
function isNewerVersion(v1, v2) { | |
"use strict"; | |
var v1parts = v1.split('.'), v2parts = v2.split('.'); | |
var maxLen = Math.max(v1parts.length, v2parts.length); | |
var part1, part2; | |
var cmp = 0; | |
for(var i = 0; i < maxLen && !cmp; i++) { | |
part1 = parseInt(v1parts[i], 10) || 0; | |
part2 = parseInt(v2parts[i], 10) || 0; | |
if(part1 < part2) | |
cmp = 1; | |
if(part1 > part2) | |
cmp = -1; | |
} | |
return (0 > cmp); | |
} | |