Created
August 13, 2014 15:51
-
-
Save moimikey/889a032b47dce8851c9c to your computer and use it in GitHub Desktop.
Firebug lite's useful helper methods condensed
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
this.arrayInsert = function (array, index, other) { | |
for (var i = 0; i < other.length; ++i) { | |
array.splice(i + index, 0, other[i]) | |
} | |
return array | |
}; | |
this.createStyleSheet = function (doc, url) { | |
var style = this.createElement("link"); | |
style.setAttribute("charset", "utf-8"); | |
style.setAttribute("rel", "stylesheet"); | |
style.setAttribute("type", "text/css"); | |
style.setAttribute("href", url); | |
return style | |
}; | |
this.addStyleSheet = function (doc, style) { | |
var heads = doc.getElementsByTagName("head"); | |
if (heads.length) { | |
heads[0].appendChild(style) | |
} else { | |
doc.documentElement.appendChild(style) | |
} | |
}; | |
this.getStyle = function (el, name) { | |
return el.currentStyle[name] || el.style[name] || undefined | |
} | |
: | |
function (el, name) { | |
return el.ownerDocument.defaultView.getComputedStyle(el, null)[name] || el.style[name] || undefined | |
}; | |
this.escapeNewLines = function (value) { | |
return value.replace(/\r/g, "\\r").replace(/\n/g, "\\n") | |
}; | |
this.stripNewLines = function (value) { | |
return typeof(value) == "string" ? value.replace(/[\r\n]/g, " ") : value | |
}; | |
this.escapeJS = function (value) { | |
return value.replace(/\r/g, "\\r").replace(/\n/g, "\\n").replace('"', '\\"', "g") | |
}; | |
this.getChildByClass = function (node) { | |
for (var i = 1; i < arguments.length; ++i) { | |
var className = arguments[i]; | |
var child = node.firstChild; | |
node = null; | |
for (; child; child = child.nextSibling) { | |
if (this.hasClass(child, className)) { | |
node = child; | |
break | |
} | |
} | |
} | |
return node | |
}; | |
this.getAncestorByClass = function (node, className) { | |
for (var parent = node; parent; parent = parent.parentNode) { | |
if (this.hasClass(parent, className)) { | |
return parent | |
} | |
} | |
return null | |
}; | |
this.getElementsByClass = function (node, className) { | |
var result = []; | |
for (var child = node.firstChild; child; child = child.nextSibling) { | |
if (this.hasClass(child, className)) { | |
result.push(child) | |
} | |
} | |
return result | |
}; | |
this.getElementByClass = function (node, className) { | |
var args = cloneArray(arguments); | |
args.splice(0, 1); | |
for (var child = node.firstChild; child; child = child.nextSibling) { | |
var args1 = cloneArray(args); | |
args1.unshift(child); | |
if (FBL.hasClass.apply(null, args1)) { | |
return child | |
} else { | |
var found = FBL.getElementByClass.apply(null, args1); | |
if (found) { | |
return found | |
} | |
} | |
} | |
return null | |
}; | |
this.isAncestor = function (node, potentialAncestor) { | |
for (var parent = node; parent; parent = parent.parentNode) { | |
if (parent == potentialAncestor) { | |
return true | |
} | |
} | |
return false | |
}; | |
this.getNextElement = function (node) { | |
while (node && node.nodeType != 1) { | |
node = node.nextSibling | |
} | |
return node | |
}; | |
this.getPreviousElement = function (node) { | |
while (node && node.nodeType != 1) { | |
node = node.previousSibling | |
} | |
return node | |
}; | |
this.isElement = function (o) { | |
try { | |
return o && this.instanceOf(o, "Element") | |
} catch (ex) { | |
return false | |
} | |
}; | |
this.isLeftClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 1 : event.button == 0) && this.noKeyModifiers(event) | |
}; | |
this.isMiddleClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 4 : event.button == 1) && this.noKeyModifiers(event) | |
}; | |
this.isRightClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 2 : event.button == 2) && this.noKeyModifiers(event) | |
}; | |
this.noKeyModifiers = function (event) { | |
return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey | |
}; | |
this.isControlClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 1 : event.button == 0) && this.isControl(event) | |
}; | |
this.isShiftClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 1 : event.button == 0) && this.isShift(event) | |
}; | |
this.isControl = function (event) { | |
return (event.metaKey || event.ctrlKey) && !event.shiftKey && !event.altKey | |
}; | |
this.isAlt = function (event) { | |
return event.altKey && !event.ctrlKey && !event.shiftKey && !event.metaKey | |
}; | |
this.isAltClick = function (event) { | |
return (this.isIE && event.type != "click" && event.type != "dblclick" ? event.button == 1 : event.button == 0) && this.isAlt(event) | |
}; | |
this.isControlShift = function (event) { | |
return (event.metaKey || event.ctrlKey) && event.shiftKey && !event.altKey | |
}; | |
this.isShift = function (event) { | |
return event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey | |
}; | |
this.addEvent = function (object, name, handler, useCapture) { | |
if (object.addEventListener) { | |
object.addEventListener(name, handler, useCapture) | |
} else { | |
object.attachEvent("on" + name, handler) | |
} | |
}; | |
this.removeEvent = function (object, name, handler, useCapture) { | |
try { | |
if (object.removeEventListener) { | |
object.removeEventListener(name, handler, useCapture) | |
} else { | |
object.detachEvent("on" + name, handler) | |
} | |
} catch (e) { | |
} | |
}; | |
this.cancelEvent = function (e, preventDefault) { | |
if (!e) { | |
return | |
} | |
if (preventDefault) { | |
if (e.preventDefault) { | |
e.preventDefault() | |
} else { | |
e.returnValue = false | |
} | |
} | |
if (e.stopPropagation) { | |
e.stopPropagation() | |
} else { | |
e.cancelBubble = true | |
} | |
}; | |
this.getFileExtension = function (url) { | |
if (!url) { | |
return null | |
} | |
var queryString = url.indexOf("?"); | |
if (queryString != -1) { | |
url = url.substr(0, queryString) | |
} | |
var lastDot = url.lastIndexOf("."); | |
return url.substr(lastDot + 1) | |
}; | |
this.isDataURL = function (url) { | |
return (url && url.substr(0, 5) == "data:") | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment