////////////////////////////// // Browser ////////////////////////////// var XHR = window.XMLHttpRequest || function () { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {} throw new Error("This browser does not support XMLHttpRequest."); }; function Browser(location, document, head, XHR, $log) { var self = this; self.isMock = false; ////////////////////////////////////////////////////////////// // XHR API ////////////////////////////////////////////////////////////// var idCounter = 0; var outstandingRequestCount = 0; var outstandingRequestCallbacks = []; self.xhr = function(method, url, post, callback){ if (isFunction(post)) { callback = post; post = _null; } if (lowercase(method) == 'json') { var callbackId = "angular_" + Math.random() + '_' + (idCounter++); callbackId = callbackId.replace(/\d\./, ''); var script = document[0].createElement('script'); script.type = 'text/javascript'; script.src = url.replace('JSON_CALLBACK', callbackId); head.append(script); window[callbackId] = function(data){ window[callbackId] = _undefined; callback(200, data); }; } else { var xhr = new XHR(); xhr.open(method, url, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Accept", "application/json, text/plain, */*"); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); outstandingRequestCount ++; xhr.onreadystatechange = function() { if (xhr.readyState == 4) { try { callback(xhr.status || 200, xhr.responseText); } finally { outstandingRequestCount--; if (outstandingRequestCount === 0) { while(outstandingRequestCallbacks.length) { try { outstandingRequestCallbacks.pop()(); } catch (e) { } } } } } }; xhr.send(post || ''); } }; self.notifyWhenNoOutstandingRequests = function(callback){ if (outstandingRequestCount === 0) { callback(); } else { outstandingRequestCallbacks.push(callback); } }; ////////////////////////////////////////////////////////////// // Poll Watcher API ////////////////////////////////////////////////////////////// var pollFns = []; function poll(){ foreach(pollFns, function(pollFn){ pollFn(); }); } self.poll = poll; /** * Adds a function to the list of functions that poller periodically executes * @return {Function} the added function */ self.addPollFn = function(/**Function*/fn){ pollFns.push(fn); return fn; }; /** * Configures the poller to run in the specified intervals, using the specified setTimeout fn and * kicks it off. */ self.startPoller = function(/**number*/interval, /**Function*/setTimeout){ (function check(){ poll(); setTimeout(check, interval); })(); }; ////////////////////////////////////////////////////////////// // URL API ////////////////////////////////////////////////////////////// self.setUrl = function(url) { var existingURL = location.href; if (!existingURL.match(/#/)) existingURL += '#'; if (!url.match(/#/)) url += '#'; location.href = url; }; self.getUrl = function() { return location.href; }; ////////////////////////////////////////////////////////////// // Cookies API ////////////////////////////////////////////////////////////// var rawDocument = document[0]; var lastCookies = {}; var lastCookieString = ''; /** * The cookies method provides a 'private' low level access to browser cookies. It is not meant to * be used directly, use the $cookie service instead. * * The return values vary depending on the arguments that the method was called with as follows: *