1// Prototypical Object Oriented Programming - POOP 2// 3// Turns out using Object.prototype.* is bad for those of 4// thus that iterate over object literals :-(. Using Object 5// to get around this and Object.extend({}) for creating 6 7// Allows for easy mixin support 8Object.mixin = function(mixin) { 9 return $.extend(true, this, mixin); 10}; 11 12// Allow for easier prototypical OO programming 13// Optional takes list of objects in which to mixin 14// see examples. 15Object.extend = function () { 16 var F = function() {}; 17 F.prototype = this; 18 var O = new F(); 19 $.each(arguments, function(i, arg) { 20 typeof arg == "object" && O.mixin(arg); 21 }); 22 // CHROME NEEDED THIS FOR DEBUGGING? 23 // "Function.prototype.toString is not generic" 24 this.toString = function() {return null;} 25 // typeof O.init == "function" && O.init(); 26 return O; 27}; 28 29// Helper functions to added to $ for namespacing 30(function( o ){ 31 // Optimization for the iPad 32 if (navigator.userAgent.match(/iPad/i) != null) { 33 window.ontouchstart = function(e) { 34 o.dragging = false; 35 o.touchHelpOrigX = e.pageX; 36 o.touchHelpOrigY = e.pageY; 37 }; 38 39 window.ontouchmove = function(e) { 40 var deltaX = Math.abs(o.touchHelpOrigX - e.pageX); 41 var deltaY = Math.abs(o.touchHelpOrigY - e.pageY); 42 o.dragging = (deltaX > 25 || deltaY > 25); 43 } 44 45 o.fn.click = function (data, fn) { 46 if (fn == null) { 47 fn = data; 48 data = null; 49 } 50 51 // We alter the handler fn 52 var fn1 = fn; 53 fn = function(e) { 54 if(!o.dragging && (o.touchHelpOrigElement == null || e.target.innerHTML == o.touchHelpOrigElement)) { 55 fn1.apply(this, arguments); 56 } 57 o.dragging = false; 58 }; 59 60 name = 'touchend'; 61 if(arguments.length > 0) { 62 return this.bind(name,data,fn); 63 } else { 64 o.dragging = false; 65 return this.trigger(name); 66 } 67 }; 68 var origUnbind = o.fn.unbind; 69 o.fn.unbind = function(type,fn) { 70 if(type == 'click') type = 'touchend'; 71 return origUnbind.call(this,type,fn); 72 } 73 } 74 75 // Handy function for converting from dict style object to array 76 o.dictToArray = function(dict) { 77 var array = []; 78 $.each(dict, function(k, v) {array.push(v);}); 79 return array; 80 }; 81 // Same as above just adds in the key as _id 82 // because we need that in many cases too 83 o.dictToArrayAddKey = function(dict) { 84 var array = []; 85 $.each(dict, function(k, v) {v._id = k; array.push(v);}); 86 return array; 87 }; 88 // Take {key1:value1,key2:value2} 89 // and return [[key1,value1],[key2,value2]] 90 o.dictToArrayPair = function(dict) { 91 var array = []; 92 $.each(dict, function(k, v) {array.push([k,v]);}); 93 return array; 94 }; 95 // Take $.serializeArray fmt [{name:key1,value:value1},{name:key1,value:value1}] 96 // and return {key1:value1,key2:value2} 97 o.serializedArrayToDict = function(serialized) { 98 var dict = {}; 99 var indexed = /^(.+)\[(.*)\]$/; 100 $.each(serialized, function() { 101 var key = this.name; 102 var value = this.value; 103 104 var match = indexed.exec(key); 105 if (match) { 106 key = match[1]; 107 // [] and [1] are both numeric, while [arg] is not 108 var numeric = match[2] == "" || (!isNaN(match[2]) && +match[2] == match[2]); 109 // get or create the proper array 110 var value_array = key in dict ? dict[key] : numeric ? [] : {}; 111 112 var index = numeric ? +match[2] : match[2]; 113 // pick the right index for the value array 114 index = index ? index : index === 0 ? 0 : value_array.length; 115 116 // add the value into the array 117 value_array[index] = value; 118 119 value = value_array; 120 } 121 122 dict[key] = value; 123 }); 124 125 return dict; 126 }; 127 o.itemToDict = function(item, key) { 128 // Convert a single item (commonly something from the REST API) 129 // into a dictionary style Object 130 var dict = {}; 131 dict[key] = item; 132 return dict; 133 }; 134 // Take a dict style object and turn it into two arrays [[keys...], [arrays...]] 135 o.unzip = function(dict) { 136 var unzipped = [[],[]]; 137 $.each(dict, function(k, v) { 138 unzipped[0].push(k); 139 unzipped[1].push(v); 140 }); 141 return unzipped; 142 }; 143 // While "arguments" appears to be an array, it is not. This is a 144 // pain. This makes an "arguments" instance an array.
145 o.argumentsToArray = function(args) { 146 var a = []; 147 for (var i = 0; i < args.length; i++) 148 a.push(args[i]); 149 return a; 150 }; 151 o.seq = function(start, stop, interval) { 152 var s = []; 153 interval = interval ? interval : 1; 154 for(var i = start; i <= stop; i+=interval) {s.push(i);} 155 return s; 156 }; 157 // better wrapper for console.log 158 o.con = function() { 159 // var filter = ['[RECEIPT]', '[DATA]']; 160 window.console != undefined && $.isFunction(window.console.log) && window.console.log.apply(console, arguments); 161 }; 162 o.isoDateToJs = function(isoDate) { 163 return Admin.isoDateToJs(isoDate); 164 }; 165 o.getMonthAbbreviation = function(date) { 166 var m = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 167 return m[date.getMonth()]; 168 }; 169 o.capitalize = function(str) { 170 return str.charAt(0).toUpperCase() + str.slice(1); 171 }; 172 // Turn 1000000 into 1,000,000 173 o.addCommas = function(nStr, thousands, decimal) { 174 if (thousands == null) { thousands = ','; } 175 if (decimal == null) { decimal = '.'; } 176 nStr += ''; 177 x = nStr.split('.'); 178 x1 = x[0]; 179 x2 = x.length > 1 ? decimal + x[1] : ''; 180 var rgx = /(\d+)(\d{3})/; 181 while (rgx.test(x1)) { 182 x1 = x1.replace(rgx, '$1' + thousands + '$2'); 183 } 184 return x1 + x2; 185 } 186})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.