3
3
// Browser
4
4
//////////////////////////////
5
5
6
- function Browser ( location , XHR ) {
7
- this . location = location ;
6
+ function Browser ( location , document ) {
8
7
this . delay = 25 ;
9
- this . XHR = XHR ;
8
+ this . expectedUrl = location . href ;
9
+ this . urlListeners = [ ] ;
10
+ this . hoverListener = noop ;
11
+
12
+ this . XHR = XMLHttpRequest || function ( ) {
13
+ try { return new ActiveXObject ( "Msxml2.XMLHTTP.6.0" ) ; } catch ( e1 ) { }
14
+ try { return new ActiveXObject ( "Msxml2.XMLHTTP.3.0" ) ; } catch ( e2 ) { }
15
+ try { return new ActiveXObject ( "Msxml2.XMLHTTP" ) ; } catch ( e3 ) { }
16
+ throw new Error ( "This browser does not support XMLHttpRequest." ) ;
17
+ } ;
10
18
this . setTimeout = function ( fn , delay ) {
11
19
window . setTimeout ( fn , delay ) ;
12
20
} ;
13
- this . expectedUrl = location . href ;
14
- this . listeners = [ ] ;
21
+
22
+ this . location = location ;
23
+ this . document = jqLite ( document ) ;
24
+ this . body = jqLite ( document . body ) ;
15
25
}
16
26
17
27
Browser . prototype = {
28
+
29
+ bind : function ( ) {
30
+ var self = this ;
31
+ self . document . bind ( "mouseover" , function ( event ) {
32
+ self . hoverListener ( jqLite ( event . target ) , true ) ;
33
+ return true ;
34
+ } ) ;
35
+ self . document . bind ( "mouseleave mouseout click dblclick keypress keyup" , function ( event ) {
36
+ self . hoverListener ( jqLite ( event . target ) , false ) ;
37
+ return true ;
38
+ } ) ;
39
+ } ,
40
+
41
+ hover : function ( hoverListener ) {
42
+ this . hoverListener = hoverListener ;
43
+ } ,
44
+
45
+ addCss : function ( url ) {
46
+ var head = jqLite ( this . document [ 0 ] . getElementsByTagName ( 'head' ) [ 0 ] ) ,
47
+ link = jqLite ( '<link rel="stylesheet" type="text/css"></link>' ) ;
48
+ link . attr ( 'href' , url ) ;
49
+ head . append ( link ) ;
50
+ } ,
51
+
18
52
xhr : function ( method , url , callback ) {
19
53
var xhr = new this . XHR ( ) ;
20
54
xhr . open ( method , url , true ) ;
@@ -27,14 +61,14 @@ Browser.prototype = {
27
61
} ,
28
62
29
63
watchUrl : function ( fn ) {
30
- this . listeners . push ( fn ) ;
64
+ this . urlListeners . push ( fn ) ;
31
65
} ,
32
66
33
67
startUrlWatcher : function ( ) {
34
68
var self = this ;
35
69
( function pull ( ) {
36
70
if ( self . expectedUrl !== self . location . href ) {
37
- foreach ( self . listeners , function ( listener ) {
71
+ foreach ( self . urlListeners , function ( listener ) {
38
72
try {
39
73
listener ( self . location . href ) ;
40
74
} catch ( e ) {
0 commit comments