angular.js/src/Browser.js

148 lines
4.1 KiB
JavaScript
Raw Normal View History

2010-04-04 00:04:36 +00:00
//////////////////////////////
// Browser
//////////////////////////////
2010-07-22 18:18:32 +00:00
function Browser(location, document, head) {
2010-05-30 23:34:59 +00:00
this.delay = 50;
2010-04-06 03:53:33 +00:00
this.expectedUrl = location.href;
this.urlListeners = [];
this.hoverListener = noop;
2010-05-07 19:09:14 +00:00
this.isMock = false;
2010-05-26 22:21:58 +00:00
this.outstandingRequests = { count: 0, callbacks:[]};
2010-04-06 03:53:33 +00:00
2010-04-21 21:29:05 +00:00
this.XHR = window.XMLHttpRequest || function () {
2010-04-06 03:53:33 +00:00
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.");
};
2010-04-04 00:04:36 +00:00
this.setTimeout = function(fn, delay) {
window.setTimeout(fn, delay);
};
2010-04-06 03:53:33 +00:00
this.location = location;
2010-07-22 18:18:32 +00:00
this.document = document;
this.head = head;
this.idCounter = 0;
2010-04-04 00:04:36 +00:00
}
Browser.prototype = {
2010-04-06 03:53:33 +00:00
bind: function() {
var self = this;
self.document.bind("mouseover", function(event){
2010-04-21 01:14:13 +00:00
self.hoverListener(jqLite(msie ? event.srcElement : event.target), true);
2010-04-06 03:53:33 +00:00
return true;
});
self.document.bind("mouseleave mouseout click dblclick keypress keyup", function(event){
self.hoverListener(jqLite(event.target), false);
return true;
});
},
hover: function(hoverListener) {
this.hoverListener = hoverListener;
},
addCss: function(url) {
2010-04-21 01:14:13 +00:00
var doc = this.document[0],
head = jqLite(doc.getElementsByTagName('head')[0]),
link = jqLite(doc.createElement('link'));
link.attr('rel', 'stylesheet');
link.attr('type', 'text/css');
2010-04-06 03:53:33 +00:00
link.attr('href', url);
head.append(link);
},
xhr: function(method, url, post, callback){
2010-04-30 19:22:07 +00:00
if (isFunction(post)) {
callback = post;
post = _null;
2010-04-30 19:22:07 +00:00
}
2010-07-22 18:18:32 +00:00
if (lowercase(method) == 'json') {
var callbackId = "angular_" + Math.random() + '_' + (this.idCounter++);
callbackId = callbackId.replace(/\d\./, '');
var script = this.document[0].createElement('script');
script.type = 'text/javascript';
script.src = url.replace('JSON_CALLBACK', callbackId);
this.head.append(script);
window[callbackId] = function(data){
window[callbackId] = _undefined;
2010-07-22 18:18:32 +00:00
callback(200, data);
};
} else {
var xhr = new this.XHR(),
self = this;
xhr.open(method, url, true);
2010-08-03 23:53:27 +00:00
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
2010-07-22 18:18:32 +00:00
this.outstandingRequests.count ++;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
try {
callback(xhr.status || 200, xhr.responseText);
} finally {
self.outstandingRequests.count--;
self.processRequestCallbacks();
}
2010-05-26 22:21:58 +00:00
}
2010-07-22 18:18:32 +00:00
};
xhr.send(post || '');
}
2010-04-05 18:46:53 +00:00
},
2010-05-26 22:21:58 +00:00
processRequestCallbacks: function(){
if (this.outstandingRequests.count === 0) {
while(this.outstandingRequests.callbacks.length) {
try {
this.outstandingRequests.callbacks.pop()();
} catch (e) {
}
}
}
},
notifyWhenNoOutstandingRequests: function(callback){
if (this.outstandingRequests.count === 0) {
callback();
} else {
this.outstandingRequests.callbacks.push(callback);
}
},
2010-04-04 00:04:36 +00:00
watchUrl: function(fn){
2010-04-06 03:53:33 +00:00
this.urlListeners.push(fn);
2010-04-04 00:04:36 +00:00
},
startUrlWatcher: function() {
var self = this;
(function pull () {
if (self.expectedUrl !== self.location.href) {
2010-04-06 03:53:33 +00:00
foreach(self.urlListeners, function(listener){
2010-04-05 18:46:53 +00:00
try {
listener(self.location.href);
} catch (e) {
error(e);
}
2010-04-04 00:04:36 +00:00
});
self.expectedUrl = self.location.href;
}
self.setTimeout(pull, self.delay);
})();
},
setUrl: function(url) {
var existingURL = this.location.href;
2010-04-16 21:01:29 +00:00
if (!existingURL.match(/#/)) existingURL += '#';
if (!url.match(/#/)) url += '#';
if (existingURL != url) {
this.location.href = this.expectedUrl = url;
}
2010-04-04 00:04:36 +00:00
},
getUrl: function() {
return this.location.href;
}
};