Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2107eaf

Browse files
committed
added hover service
1 parent 1c670b2 commit 2107eaf

9 files changed

+102
-25
lines changed

scenario/widgets.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
33
<head>
44
<link rel="stylesheet" type="text/css" href="style.css"></link>
5+
<!--<script type="text/javascript" src="../lib/jquery/jquery-1.4.2.js"></script>-->
56
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
67
</head>
78
<body ng-init="$window.$scope = this">
89
<table>
910
<tr>
10-
<th>Description</th>
11+
<th width="330">Description</th>
1112
<th>Test</th>
1213
<th>Result</th>
1314
</tr>

src/Angular.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,10 @@ function merge(src, dst) {
313313

314314
function compile(element, parentScope, overrides) {
315315
var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
316-
$element = jqLite(element);
317-
return compiler.compile($element)($element, parentScope, overrides);
316+
$element = jqLite(element),
317+
parent = extend({}, parentScope);
318+
parent.$element = $element;
319+
return compiler.compile($element)($element, parent, overrides);
318320
}
319321
/////////////////////////////////////////////////
320322

@@ -340,6 +342,8 @@ function toKeyValue(obj) {
340342

341343
function angularInit(config){
342344
if (config.autobind) {
343-
compile(window.document, null, {'$config':config}).$init();
345+
var scope = compile(window.document, null, {'$config':config});
346+
scope.$browser.addCss('../css/angular.css');
347+
scope.$init();
344348
}
345349
}

src/AngularPublic.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
var browserSingleton;
22
angularService('$browser', function browserFactory(){
33
if (!browserSingleton) {
4-
var XHR = XMLHttpRequest;
5-
if (isUndefined(XHR)) {
6-
XHR = function () {
7-
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
8-
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {}
9-
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
10-
throw new Error("This browser does not support XMLHttpRequest.");
11-
};
12-
}
13-
browserSingleton = new Browser(window.location, XHR);
4+
browserSingleton = new Browser(window.location, window.document);
145
browserSingleton.startUrlWatcher();
6+
browserSingleton.bind();
157
}
168
return browserSingleton;
179
});

src/Browser.js

+41-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,52 @@
33
// Browser
44
//////////////////////////////
55

6-
function Browser(location, XHR) {
7-
this.location = location;
6+
function Browser(location, document) {
87
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+
};
1018
this.setTimeout = function(fn, delay) {
1119
window.setTimeout(fn, delay);
1220
};
13-
this.expectedUrl = location.href;
14-
this.listeners = [];
21+
22+
this.location = location;
23+
this.document = jqLite(document);
24+
this.body = jqLite(document.body);
1525
}
1626

1727
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+
1852
xhr: function(method, url, callback){
1953
var xhr = new this.XHR();
2054
xhr.open(method, url, true);
@@ -27,14 +61,14 @@ Browser.prototype = {
2761
},
2862

2963
watchUrl: function(fn){
30-
this.listeners.push(fn);
64+
this.urlListeners.push(fn);
3165
},
3266

3367
startUrlWatcher: function() {
3468
var self = this;
3569
(function pull () {
3670
if (self.expectedUrl !== self.location.href) {
37-
foreach(self.listeners, function(listener){
71+
foreach(self.urlListeners, function(listener){
3872
try {
3973
listener(self.location.href);
4074
} catch (e) {

src/angular-bootstrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
addScript("/Parser.js");
4848
addScript("/Resource.js");
4949
addScript("/Browser.js");
50-
addScript("/~AngularPublic.js");
50+
addScript("/AngularPublic.js");
5151

5252
// Extension points
5353
addScript("/apis.js");

src/jqLite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ JQLite.prototype = {
7878
bind[type] = eventHandler = function(event) {
7979
var bubbleEvent = false;
8080
foreach(eventHandler.fns, function(fn){
81-
bubbleEvent = bubbleEvent || fn.apply(self, arguments);
81+
bubbleEvent = bubbleEvent || fn.call(self, event);
8282
});
8383
if (!bubbleEvent) {
8484
event.preventDefault();

src/services.js

+43
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,46 @@ angularService("$location", function(browser){
4444
return location;
4545
}, {inject: ['$browser']});
4646

47+
angularService("$hover", function(browser) {
48+
var tooltip, self = this, error, width = 300, arrowWidth = 10;
49+
browser.hover(function(element, show){
50+
if (show && (error = element.attr('ng-error'))) {
51+
if (!tooltip) {
52+
tooltip = {
53+
callout: jqLite('<div id="ng-callout"></div>'),
54+
arrow: jqLite('<div></div>'),
55+
title: jqLite('<div class="ng-title"></div>'),
56+
content: jqLite('<div class="ng-content"></div>')
57+
};
58+
tooltip.callout.append(tooltip.arrow);
59+
tooltip.callout.append(tooltip.title);
60+
tooltip.callout.append(tooltip.content);
61+
self.$browser.body.append(tooltip.callout);
62+
}
63+
var docRect = self.$browser.body[0].getBoundingClientRect(),
64+
elementRect = element[0].getBoundingClientRect(),
65+
leftSpace = docRect.right - elementRect.right - arrowWidth;
66+
tooltip.title.text(element.hasClass("ng-exception") ? "EXCEPTION:" : "Validation error...");
67+
tooltip.content.text(error);
68+
if (leftSpace < width) {
69+
tooltip.arrow.addClass('ng-arrow-right');
70+
tooltip.arrow.css({left: (width + 1)+'px'});
71+
tooltip.callout.css({
72+
left: (elementRect.left - arrowWidth - width - 4) + "px",
73+
top: (elementRect.top - 3) + "px",
74+
width: width + "px"
75+
});
76+
} else {
77+
tooltip.arrow.addClass('ng-arrow-left');
78+
tooltip.callout.css({
79+
left: (elementRect.right + arrowWidth) + "px",
80+
top: (elementRect.top - 3) + "px",
81+
width: width + "px"
82+
});
83+
}
84+
} else if (tooltip) {
85+
tooltip.callout.remove();
86+
tooltip = null;
87+
}
88+
});
89+
}, {inject:['$browser']});

test/BrowserTest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ BrowserTest = TestCase('BrowserTest');
33
BrowserTest.prototype.testUrlWatcher = function () {
44
expectAsserts(2);
55
var location = {href:"http://server", hash:""};
6-
var watcher = new Browser(location);
6+
var watcher = new Browser(location, {});
77
watcher.delay = 1;
88
watcher.watchUrl(function(url){
99
assertEquals('http://getangular.test', url);

test/angular-mocks.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function MockBrowser() {
88
var expect = expectations[method] || {};
99
var response = expect[url];
1010
if (!response) {
11-
throw "Unexepected request for mothod '" + method + "' and url '" + url + "'.";
11+
throw "Unexepected request for method '" + method + "' and url '" + url + "'.";
1212
}
1313
requests.push(function(){
1414
callback(200, response);
@@ -32,6 +32,9 @@ function MockBrowser() {
3232
}
3333
MockBrowser.prototype = {
3434

35+
hover: function(onHover) {
36+
},
37+
3538
getUrl: function(){
3639
return this.url;
3740
},

0 commit comments

Comments
 (0)