1$oc = OpenCities = $.extend(true, typeof($oc) === 'object' ? $oc : (typeof(OpenCities) === 'object' ? OpenCities : { }), { 2 Ext : (function(){ 3 var readyQueue = []; 4 return function(namespace, objFactory) 5 { 6 var space = namespace.split('.'); 7 var currentObj = this; 8 var prevObj = null; 9 var currentObjWasCreated = false; 10 for (var i = 0; i < space.length; i++) { 11 var level = space[i]; 12 if(currentObj.hasOwnProperty(level)) 13 { 14 var oType = typeof(currentObj); 15 if(oType !== 'object') 16 { 17 throw '\'OpenCities.' + (space.slice(0,i+1).join('.')) + '\' is a \''+oType+'\'. Only \'object\' types may be extended.'; 18 } 19 } 20 else 21 { 22 currentObj[level] = {}; 23 currentObjWasCreated = true; 24 } 25 prevObj = currentObj; 26 currentObj = currentObj[level]; 27 } 28 var extObj = (typeof(objFactory) === 'function') ? objFactory.call(currentObj, this) : objFactory; 29 30 if(typeof(extObj) === 'function') 31 { 32 //The factory has returned a function, so we should try to set it on the last level of the namespace 33 //Example call with function factory: $oc.Ext('Path.To.Namespace.functionName', function(){ return function(){console.log('Call me on $oc.Path.To.Namespace.functionName()');} }); 34 if(currentObjWasCreated) 35 { 36 //The current object was created in this function call (it wasn't pre-existing) so we can safely replace with the function 37 prevObj[space[space.length - 1]] = extObj; 38 } 39 else 40 { 41 //The current object already existed and was created outside this function call. We can't replace it. 42 throw '\'OpenCities.' + (space.join('.')) + '\' already exists as an \'object\' and cannot be replaced with a \'function\'.'; 43 44 } 45 } 46 else 47 { 48 //The factory has returned an object, so extend with the currentObj 49 //Example call with object literal: $oc.Ext('Path.To.Namespace', {'functionName':function(){console.log('Call me on $oc.Path.To.Namespace.functionName()')}); 50 //Example call with object factory: $oc.Ext('Path.To.Namespace', function(){ return {'functionName':function(){console.log('Call me on $oc.Path.To.Namespace.functionName()')}}}); 51 $.extend(true, currentObj, extObj); 52 } 53 54 if($oc.hasOwnProperty('__setReady')) 55 { 56 while(readyQueue.length > 0) 57 { 58 //loop through all the ready events for when __setReady was unavailable 59 $oc.__setReady(readyQueue.shift()); 60 } 61 $oc.__setReady(namespace); 62 } else { 63 readyQueue.push(namespace); 64 } 65 } 66 })() 67}); 68 69 70$oc.Ext('Ready', function(ocObj){ 71 var objs = {}; 72 var objOrder = []; 73 var mainName = '__main_ready'; 74 75 ocObj.Ext('__setReady', function(ocObj){ 76 return function(name) 77 { 78 if(arguments.length === 0) 79 { 80 //Called as $oc.__setReady() which is the general "all ready" function 81 //So we need to loop over all objects and run their ready functions 82 //and then run the main ready function. 83 for (var i = 0; i < objOrder.length; i++) { 84 var name = objOrder[i]; 85 if(spaceExists(name)) 86 { 87 doReady(name); 88 } else { 89 console.warn('$oc.' + name + ' object does not exist. $oc.Ready() functions relying on it may not run.') 90 } 91 } 92 doReady(); 93 94 } else { 95 //Called as $oc.__setReady('Path.To.Namespace') 96 //So just run the ready function for that 97 doReady(name); 98 } 99 } 100 }); 101 102 function spaceExists(namespace) 103 { 104 return getSpace(namespace) !== null; 105 } 106 107 function getSpace(namespace) 108 { 109 var space = namespace.split('.'); 110 var currentObj = ocObj; 111 for (var i = 0; i < space.length; i++) { 112 var level = space[i]; 113 if(!currentObj.hasOwnProperty(level)) 114 { 115 return null; 116 } 117 currentObj = currentObj[level]; 118 } 119 return currentObj; 120 } 121 122 function doReady(name) 123 { 124 var obj = getReadyObj(name); 125 var thisScope = getNamespaceScope(obj); 126 obj.isReady = true; 127 while(obj.callbacks.length > 0) 128 { 129 obj.callbacks.shift().call(thisScope, ocObj); 130 } 131 } 132 133 function getNamespaceScope(obj) 134 { 135 return (obj.namespace !== mainName) ? getSpace(obj.namespace) : ocObj; 136 } 137 138 function getReadyObj(name) 139 { 140 name = getReadyName(name); 141 if(!objs.hasOwnProperty(name)) 142 { 143 objs[name] = createReadyObj(name); 144 if(name !== mainName) 145 { 146 objOrder.push(name); 147 } 148 } 149 return objs[name]; 150 } 151 152 function createReadyObj(name) 153 { 154 return {'namespace' : name, 'isReady' : false, 'callbacks' : []}; 155 } 156 157 function getReadyName(name) 158 { 159 return name || mainName; 160 } 161 162 163 //Usage: 164 //$oc.Ready('Path.To.Namespace', function(ocObj){}); //Called immediately after $oc.Path.To.Namespace is created by $oc.Ext('Path.To.Namespace', ...) 165 //$oc.Ready(function(ocObj){}); //Called on OC framework ready 166 return function() 167 { 168 var obj, callback; 169 170 if(arguments.length === 1) 171 { 172 obj = getReadyObj(); 173 callback = arguments[0]; 174 } 175 else if (arguments.length > 1) 176 { 177 obj = getReadyObj(arguments[0]); 178 callback = arguments[1] 179 } 180 181 182 if(obj.isReady) 183 { 184 var thisScope = getNamespaceScope(obj); 185 callback.call(thisScope, ocObj); 186 } 187 else 188 { 189 obj.callbacks.push(callback); 190 } 191 } 192});
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.