From 7d44853da7f06cada71f0f3776463d954a041694 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 14 Feb 2012 07:51:44 -0600 Subject: [PATCH] Pull include/util.js from noVNC. Add map to arrays for IE9. IE9 still doesn't support [].map() so add it to the prototype in include/util.js. --- include/util.js | 140 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 35 deletions(-) diff --git a/include/util.js b/include/util.js index 2527f52..ddc1914 100644 --- a/include/util.js +++ b/include/util.js @@ -1,6 +1,6 @@ /* - * from noVNC: HTML5 VNC client - * Copyright (C) 2010 Joel Martin + * noVNC: HTML5 VNC client + * Copyright (C) 2011 Joel Martin * Licensed under LGPL-3 (see LICENSE.txt) * * See README.md for usage and integration instructions. @@ -11,7 +11,7 @@ /*global window, console, document, navigator, ActiveXObject */ // Globals defined here -var Util = {}, $D; +var Util = {}; /* @@ -33,6 +33,30 @@ Array.prototype.push32 = function (num) { (num ) & 0xFF ); }; +// IE does not support map (even in IE9) +//This prototype is provided by the Mozilla foundation and +//is distributed under the MIT license. +//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license +if (!Array.prototype.map) +{ + Array.prototype.map = function(fun /*, thisp*/) + { + var len = this.length; + if (typeof fun != "function") + throw new TypeError(); + + var res = new Array(len); + var thisp = arguments[1]; + for (var i = 0; i < len; i++) + { + if (i in this) + res[i] = fun.call(thisp, this[i], i, this); + } + + return res; + }; +} + /* * ------------------------------------------------------ * Namespaced in Util @@ -78,46 +102,88 @@ Util.init_logging = function (level) { }; Util.get_logging = function () { return Util._log_level; -} +}; // Initialize logging level Util.init_logging(); -// Set defaults for Crockford style function namespaces -Util.conf_default = function(cfg, api, v, type, defval, desc) { - // Description - api['get_' + v + '_desc'] = desc; - // Default getter +// Set configuration default for Crockford style function namespaces +Util.conf_default = function(cfg, api, defaults, v, mode, type, defval, desc) { + var getter, setter; + + // Default getter function + getter = function (idx) { + if ((type in {'arr':1, 'array':1}) && + (typeof idx !== 'undefined')) { + return cfg[v][idx]; + } else { + return cfg[v]; + } + }; + + // Default setter function + setter = function (val, idx) { + if (type in {'boolean':1, 'bool':1}) { + if ((!val) || (val in {'0':1, 'no':1, 'false':1})) { + val = false; + } else { + val = true; + } + } else if (type in {'integer':1, 'int':1}) { + val = parseInt(val, 10); + } else if (type === 'func') { + if (!val) { + val = function () {}; + } + } + if (typeof idx !== 'undefined') { + cfg[v][idx] = val; + } else { + cfg[v] = val; + } + }; + + // Set the description + api[v + '_description'] = desc; + + // Set the getter function if (typeof api['get_' + v] === 'undefined') { - api['get_' + v] = function () { - return cfg[v]; - }; - } - // Default setter - if (typeof api['set_' + v] === 'undefined') { - api['set_' + v] = function (val) { - if (type in {'boolean':1, 'bool':1}) { - if ((!val) || (val in {'0':1, 'no':1, 'false':1})) { - val = false; - } else { - val = true; - } - } else if (type in {'integer':1, 'int':1}) { - val = parseInt(val, 10); - } - cfg[v] = val; - }; + api['get_' + v] = getter; } - if (typeof cfg[v] === 'undefined') { - // Set to default - api['set_' + v](defval); - } else { - // Coerce existing setting to the right type - api['set_' + v](cfg[v]); + // Set the setter function with extra sanity checks + if (typeof api['set_' + v] === 'undefined') { + api['set_' + v] = function (val, idx) { + if (mode in {'RO':1, 'ro':1}) { + throw(v + " is read-only"); + } else if ((mode in {'WO':1, 'wo':1}) && + (typeof cfg[v] !== 'undefined')) { + throw(v + " can only be set once"); + } + setter(val, idx); + }; } + + // Set the default value + if (typeof defaults[v] !== 'undefined') { + defval = defaults[v]; + } else if ((type in {'arr':1, 'array':1}) && + (! (defval instanceof Array))) { + defval = []; + } + // Coerce existing setting to the right type + //Util.Debug("v: " + v + ", defval: " + defval + ", defaults[v]: " + defaults[v]); + setter(defval); }; +// Set group of configuration defaults +Util.conf_defaults = function(cfg, api, defaults, arr) { + var i; + for (i = 0; i < arr.length; i++) { + Util.conf_default(cfg, api, defaults, arr[i][0], arr[i][1], + arr[i][2], arr[i][3], arr[i][4]); + } +}; /* @@ -142,6 +208,7 @@ Util.getEventPosition = function (e, obj, scale) { var evt, docX, docY, pos; //if (!e) evt = window.event; evt = (e ? e : window.event); + evt = (evt.changedTouches ? evt.changedTouches[0] : evt.touches ? evt.touches[0] : evt); if (evt.pageX || evt.pageY) { docX = evt.pageX; docY = evt.pageY; @@ -197,8 +264,11 @@ Util.stopEvent = function(e) { Util.Features = {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)}; Util.Engine = { - 'presto': (function() { - return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); }()), + // Version detection break in Opera 11.60 (errors on arguments.callee.caller reference) + //'presto': (function() { + // return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); }()), + 'presto': (function() { return (!window.opera) ? false : true; }()), + 'trident': (function() { return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4); }()), 'webkit': (function() {