summaryrefslogtreecommitdiff
path: root/static/js/src/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/js/src/util.js')
-rw-r--r--static/js/src/util.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/static/js/src/util.js b/static/js/src/util.js
new file mode 100644
index 0000000..fdfd1c9
--- /dev/null
+++ b/static/js/src/util.js
@@ -0,0 +1,80 @@
+// Utils
+
+emptyFunc = function(){};
+falseFunc = function(){ return false };
+
+isImgBroken = function(img){
+ return (img.height == 0 || img.width == 0 || img.height == NaN || img.width == NaN)
+}
+
+isEmptyObject = function(obj) {
+ for (key in obj) {
+ if (obj.hasOwnProperty(key)) return false;
+ }
+ return true
+}
+
+String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }
+
+function isCSSPropertySupported(prop){ return prop in document.body.style }
+
+function track(group, name) {
+ if (typeof pageTracker !== 'undefined') {
+ pageTracker._trackEvent(group, name,
+ typeof Nick !== 'undefined' ? Nick : 'anon');
+ }
+}
+
+// generate a new CSS rule and apply it immediately
+// (more persistent than dumping a style tag)
+window.cssRule = function (selector, declaration) {
+ var x = document.styleSheets,y=x.length-1;
+ x[y].insertRule(selector+"{"+declaration+"}",x[y].cssRules.length);
+ $(selector).css(declaration.split(": "));
+};
+
+
+
+var Log = {
+ "Levels": ['info', 'warn', 'error'],
+ "AjaxSubmitLevels": ['warn', 'error'],
+ "AjaxSubmitPath": "/logerror",
+
+ "SupplementalInfo": function() {
+ return { 'user': UserInfo && UserInfo.nick };
+ },
+
+ "ajaxSubmit": function(level, component, msg) {
+ var info = Log.SupplementalInfo();
+ var data = { 'level': level, 'component': component, 'msg': msg };
+ $.extend(info, data);
+
+ $.ajax({type: 'POST',
+ timeout: 5000,
+ url: Log.AjaxSubmitPath,
+ data: info
+ });
+ },
+
+ "initialize": function() {
+ $.each(Log.Levels, function(i, level) {
+ Log[level] = function(component, msg) {
+ if (window.console && window.console[level])
+ window.console[level](args);
+ if (Log.AjaxSubmitLevels.indexOf(level) != -1)
+ Log.ajaxSubmit(level, args);
+ };
+ });
+ }
+};
+
+Log.initialize();
+
+
+// Time how long a function takes to complete
+function timeFunc(f){
+ var start = new Date().getTime();
+ var res = f();
+// console.log((new Date().getTime()) - start + " msecs");
+ return res;
+}