Commit ee7d4c61 authored by Solly Ross's avatar Solly Ross

Cleanup: Webutil code

File: webutil.js
Tests Added: False
Changes:
- Fixed JSHint Errors (global "use strict", spaces)
- added some newline characters when appropriate for readability
- moved variable declarations to the places they were actually used
  for readability
parent 31f169e8
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
* See README.md for usage and integration instructions. * See README.md for usage and integration instructions.
*/ */
"use strict"; /*jslint bitwise: false, white: false, browser: true, devel: true */
/*jslint bitwise: false, white: false */
/*global Util, window, document */ /*global Util, window, document */
// Globals defined here // Globals defined here
...@@ -31,45 +30,47 @@ if (!window.$D) { ...@@ -31,45 +30,47 @@ if (!window.$D) {
} }
/* /*
* ------------------------------------------------------ * ------------------------------------------------------
* Namespaced in WebUtil * Namespaced in WebUtil
* ------------------------------------------------------ * ------------------------------------------------------
*/ */
// init log level reading the logging HTTP param // init log level reading the logging HTTP param
WebUtil.init_logging = function(level) { WebUtil.init_logging = function (level) {
"use strict";
if (typeof level !== "undefined") { if (typeof level !== "undefined") {
Util._log_level = level; Util._log_level = level;
} else { } else {
Util._log_level = (document.location.href.match( var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
/logging=([A-Za-z0-9\._\-]*)/) || Util._log_level = (param || ['', Util._log_level])[1];
['', Util._log_level])[1];
} }
Util.init_logging(); Util.init_logging();
}; };
WebUtil.dirObj = function (obj, depth, parent) { WebUtil.dirObj = function (obj, depth, parent) {
var i, msg = "", val = ""; "use strict";
if (! depth) { depth=2; } if (! depth) { depth = 2; }
if (! parent) { parent= ""; } if (! parent) { parent = ""; }
// Print the properties of the passed-in object // Print the properties of the passed-in object
for (i in obj) { var msg = "";
if ((depth > 1) && (typeof obj[i] === "object")) { for (var i in obj) {
if ((depth > 1) && (typeof obj[i] === "object")) {
// Recurse attributes that are objects // Recurse attributes that are objects
msg += WebUtil.dirObj(obj[i], depth-1, parent + "." + i); msg += WebUtil.dirObj(obj[i], depth - 1, parent + "." + i);
} else { } else {
//val = new String(obj[i]).replace("\n", " "); //val = new String(obj[i]).replace("\n", " ");
var val = "";
if (typeof(obj[i]) === "undefined") { if (typeof(obj[i]) === "undefined") {
val = "undefined"; val = "undefined";
} else { } else {
val = obj[i].toString().replace("\n", " "); val = obj[i].toString().replace("\n", " ");
} }
if (val.length > 30) { if (val.length > 30) {
val = val.substr(0,30) + "..."; val = val.substr(0, 30) + "...";
} }
msg += parent + "." + i + ": " + val + "\n"; msg += parent + "." + i + ": " + val + "\n";
} }
} }
...@@ -77,7 +78,8 @@ WebUtil.dirObj = function (obj, depth, parent) { ...@@ -77,7 +78,8 @@ WebUtil.dirObj = function (obj, depth, parent) {
}; };
// Read a query string variable // Read a query string variable
WebUtil.getQueryVar = function(name, defVal) { WebUtil.getQueryVar = function (name, defVal) {
"use strict";
var re = new RegExp('.*[?&]' + name + '=([^&#]*)'), var re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
match = document.location.href.match(re); match = document.location.href.match(re);
if (typeof defVal === 'undefined') { defVal = null; } if (typeof defVal === 'undefined') { defVal = null; }
...@@ -94,42 +96,50 @@ WebUtil.getQueryVar = function(name, defVal) { ...@@ -94,42 +96,50 @@ WebUtil.getQueryVar = function(name, defVal) {
*/ */
// No days means only for this browser session // No days means only for this browser session
WebUtil.createCookie = function(name,value,days) { WebUtil.createCookie = function (name, value, days) {
var date, expires, secure; "use strict";
var date, expires;
if (days) { if (days) {
date = new Date(); date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000)); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires="+date.toGMTString(); expires = "; expires=" + date.toGMTString();
} else { } else {
expires = ""; expires = "";
} }
var secure;
if (document.location.protocol === "https:") { if (document.location.protocol === "https:") {
secure = "; secure"; secure = "; secure";
} else { } else {
secure = ""; secure = "";
} }
document.cookie = name+"="+value+expires+"; path=/"+secure; document.cookie = name + "=" + value + expires + "; path=/" + secure;
}; };
WebUtil.readCookie = function(name, defaultValue) { WebUtil.readCookie = function (name, defaultValue) {
var i, c, nameEQ = name + "=", ca = document.cookie.split(';'); "use strict";
for(i=0; i < ca.length; i += 1) { var nameEQ = name + "=",
c = ca[i]; ca = document.cookie.split(';');
while (c.charAt(0) === ' ') { c = c.substring(1,c.length); }
if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); } for (var i = 0; i < ca.length; i += 1) {
var c = ca[i];
while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
} }
return (typeof defaultValue !== 'undefined') ? defaultValue : null; return (typeof defaultValue !== 'undefined') ? defaultValue : null;
}; };
WebUtil.eraseCookie = function(name) { WebUtil.eraseCookie = function (name) {
WebUtil.createCookie(name,"",-1); "use strict";
WebUtil.createCookie(name, "", -1);
}; };
/* /*
* Setting handling. * Setting handling.
*/ */
WebUtil.initSettings = function(callback) { WebUtil.initSettings = function (callback /*, ...callbackArgs */) {
"use strict";
var callbackArgs = Array.prototype.slice.call(arguments, 1); var callbackArgs = Array.prototype.slice.call(arguments, 1);
if (window.chrome && window.chrome.storage) { if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.get(function (cfg) { window.chrome.storage.sync.get(function (cfg) {
...@@ -148,7 +158,8 @@ WebUtil.initSettings = function(callback) { ...@@ -148,7 +158,8 @@ WebUtil.initSettings = function(callback) {
}; };
// No days means only for this browser session // No days means only for this browser session
WebUtil.writeSetting = function(name, value) { WebUtil.writeSetting = function (name, value) {
"use strict";
if (window.chrome && window.chrome.storage) { if (window.chrome && window.chrome.storage) {
//console.log("writeSetting:", name, value); //console.log("writeSetting:", name, value);
if (WebUtil.settings[name] !== value) { if (WebUtil.settings[name] !== value) {
...@@ -160,7 +171,8 @@ WebUtil.writeSetting = function(name, value) { ...@@ -160,7 +171,8 @@ WebUtil.writeSetting = function(name, value) {
} }
}; };
WebUtil.readSetting = function(name, defaultValue) { WebUtil.readSetting = function (name, defaultValue) {
"use strict";
var value; var value;
if (window.chrome && window.chrome.storage) { if (window.chrome && window.chrome.storage) {
value = WebUtil.settings[name]; value = WebUtil.settings[name];
...@@ -177,7 +189,8 @@ WebUtil.readSetting = function(name, defaultValue) { ...@@ -177,7 +189,8 @@ WebUtil.readSetting = function(name, defaultValue) {
} }
}; };
WebUtil.eraseSetting = function(name) { WebUtil.eraseSetting = function (name) {
"use strict";
if (window.chrome && window.chrome.storage) { if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.remove(name); window.chrome.storage.sync.remove(name);
delete WebUtil.settings[name]; delete WebUtil.settings[name];
...@@ -189,9 +202,12 @@ WebUtil.eraseSetting = function(name) { ...@@ -189,9 +202,12 @@ WebUtil.eraseSetting = function(name) {
/* /*
* Alternate stylesheet selection * Alternate stylesheet selection
*/ */
WebUtil.getStylesheets = function() { var i, links, sheets = []; WebUtil.getStylesheets = function () {
links = document.getElementsByTagName("link"); "use strict";
for (i = 0; i < links.length; i += 1) { var links = document.getElementsByTagName("link");
var sheets = [];
for (var i = 0; i < links.length; i += 1) {
if (links[i].title && if (links[i].title &&
links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) { links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
sheets.push(links[i]); sheets.push(links[i]);
...@@ -202,14 +218,16 @@ WebUtil.getStylesheets = function() { var i, links, sheets = []; ...@@ -202,14 +218,16 @@ WebUtil.getStylesheets = function() { var i, links, sheets = [];
// No sheet means try and use value from cookie, null sheet used to // No sheet means try and use value from cookie, null sheet used to
// clear all alternates. // clear all alternates.
WebUtil.selectStylesheet = function(sheet) { WebUtil.selectStylesheet = function (sheet) {
var i, link, sheets = WebUtil.getStylesheets(); "use strict";
if (typeof sheet === 'undefined') { if (typeof sheet === 'undefined') {
sheet = 'default'; sheet = 'default';
} }
for (i=0; i < sheets.length; i += 1) {
link = sheets[i]; var sheets = WebUtil.getStylesheets();
if (link.title === sheet) { for (var i = 0; i < sheets.length; i += 1) {
var link = sheets[i];
if (link.title === sheet) {
Util.Debug("Using stylesheet " + sheet); Util.Debug("Using stylesheet " + sheet);
link.disabled = false; link.disabled = false;
} else { } else {
......
...@@ -24,13 +24,13 @@ describe('Utils', function() { ...@@ -24,13 +24,13 @@ describe('Utils', function() {
describe('push16', function () { describe('push16', function () {
it('should push two bytes on to the array', function () { it('should push two bytes on to the array', function () {
var arr = [1]; var arr = [1];
arr.push16(0xABCD); arr.push16(0xABCD);
expect(arr).to.deep.equal([1, 0xAB, 0xCD]); expect(arr).to.deep.equal([1, 0xAB, 0xCD]);
}); });
it('should only use the two least significant bytes of any number passed in', function () { it('should only use the two least significant bytes of any number passed in', function () {
var arr = [1]; var arr = [1];
arr.push16(0xABCDEF); arr.push16(0xABCDEF);
expect(arr).to.deep.equal([1, 0xCD, 0xEF]); expect(arr).to.deep.equal([1, 0xCD, 0xEF]);
}); });
...@@ -38,19 +38,19 @@ describe('Utils', function() { ...@@ -38,19 +38,19 @@ describe('Utils', function() {
describe('push32', function () { describe('push32', function () {
it('should push four bytes on to the array', function () { it('should push four bytes on to the array', function () {
var arr = [1]; var arr = [1];
arr.push32(0xABCDEF12); arr.push32(0xABCDEF12);
expect(arr).to.deep.equal([1, 0xAB, 0xCD, 0xEF, 0x12]); expect(arr).to.deep.equal([1, 0xAB, 0xCD, 0xEF, 0x12]);
}); });
it('should only use the four least significant bytes of any number passed in', function () { it('should only use the four least significant bytes of any number passed in', function () {
var arr = [1]; var arr = [1];
arr.push32(0xABCDEF1234); arr.push32(0xABCDEF1234);
expect(arr).to.deep.equal([1, 0xCD, 0xEF, 0x12, 0x34]); expect(arr).to.deep.equal([1, 0xCD, 0xEF, 0x12, 0x34]);
}); });
}); });
}); });
describe('logging functions', function () { describe('logging functions', function () {
beforeEach(function () { beforeEach(function () {
sinon.spy(console, 'log'); sinon.spy(console, 'log');
...@@ -98,7 +98,7 @@ describe('Utils', function() { ...@@ -98,7 +98,7 @@ describe('Utils', function() {
// TODO(directxman12): test decodeUTF8 // TODO(directxman12): test decodeUTF8
// TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent) // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
// TODO(directxman12): figure out a good way to test getPosition and getEventPosition // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
// TODO(directxman12): figure out how to test the browser detection functions properly // TODO(directxman12): figure out how to test the browser detection functions properly
// (we can't really test them against the browsers, except for Gecko // (we can't really test them against the browsers, except for Gecko
// via PhantomJS, the default test driver) // via PhantomJS, the default test driver)
// TODO(directxman12): figure out how to test Util.Flash // TODO(directxman12): figure out how to test Util.Flash
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment