adding first pass at global utility functions #5

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+35 −0
Split
View
@@ -0,0 +1,35 @@
+var guardian = {};
+
+/* utility functions for low-level dom manipulation */
+guardian.util = {
+
+ /* native method to add a class */
+ addClass: function(elm, classname) {
+ var re = new RegExp(classname, 'g');
+ if(!elm.className.match(re)){
+ elm.className += ' ' + classname;
+ }
+ },
+
+ /* native method to remove a class */
+ removeClass: function(elm, classname) {
+ var re = new RegExp(classname, 'g');
+ elm.className = elm.className.replace(re, '');
+ },
+
+ /* convenience method to swap one class for another */
+ swapClass: function(elm, classToRemove, classToAdd) {
+ this.removeClass(elm, classToRemove);
+ this.addClass(elm, classToAdd);
+ }
+
+};
+
+// shim function for console logging, prevents IE errors
+window.log = function() {
+ log.history = log.history || [];
+ log.history.push(arguments);
+ if (this.console) {
+ console.log(Array.prototype.slice.call(arguments));
+ }
+};