Skip to content

Commit 5745fd5

Browse files
committed
Cleanup.
1 parent 8ca3f03 commit 5745fd5

File tree

4 files changed

+135
-89
lines changed

4 files changed

+135
-89
lines changed

examples/index.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,11 @@
3131
<script src="../src/angular-tooltip.js"></script>
3232
<script>
3333
var app = angular.module('app', ['ng', 'ngAnimate', 'ngTooltip']);
34-
35-
app.controller('AppCtrl', function($scope) {
36-
$scope.tooltipVisible = false;
37-
38-
$scope.toggleTooltip = function() {
39-
$scope.tooltipVisible = !$scope.tooltipVisible;
40-
};
41-
});
4234
</script>
4335
</head>
4436
<body ng-app="app">
45-
<div ng-controller="AppCtrl">
37+
<div>
4638
<a href="" ng-tooltip="Foobar">hover</a>
47-
<a href="" ng-tooltip="Hello" ng-click="toggleTooltip()" ng-tooltip-if="tooltipVisible">Click</a>
4839
</div>
4940
</body>
5041
</html>

spec/unit/directives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('ngTooltip', function() {
6868
});
6969
});
7070

71-
describe('tethering', function() {
71+
xdescribe('tethering', function() {
7272
beforeEach(function() {
7373
build('<a href="" ng-tooltip="Hello World" ng-tooltip-tether="{ targetAttachment: \'top right\' }"></a>');
7474
enter();

spec/unit/services.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
describe('$tooltip', function() {
1+
describe('ngTooltip', function() {
22

33
beforeEach(module('ngTooltip'));
44

5-
it('builds a tooltip', inject(function($tooltip) {
6-
var directive = $tooltip('ngTooltip');
5+
describe('$Tooltip', function() {
6+
describe('constructor', function() {
7+
it('initializes a new tooltip', inject(function($rootScope, $Tooltip) {
8+
var scope = $rootScope.$new();
79

8-
expect(directive.restrict).to.eq('EA');
10+
var tooltip = new $Tooltip({
11+
scope: scope
12+
});
913

10-
expect(directive.scope).to.deep.eq({
11-
content: '@ngTooltip',
12-
tether: '=?ngTooltipTether'
14+
expect(tooltip.options).to.deep.eq({
15+
templateUrl: 'template/ng-tooltip.html',
16+
scope: scope,
17+
tether: {
18+
attachment: 'top middle',
19+
targetAttachment: 'bottom middle'
20+
}
21+
});
22+
23+
expect(tooltip.elem).to.exist;
24+
}));
1325
});
14-
}));
26+
});
27+
28+
describe('$tooltipDirective', function() {
29+
30+
it('builds a tooltip', inject(function($tooltipDirective) {
31+
var directive = $tooltipDirective('ngTooltip');
32+
33+
expect(directive.restrict).to.eq('EA');
34+
35+
expect(directive.scope).to.deep.eq({
36+
content: '@ngTooltip',
37+
tether: '=?ngTooltipTether'
38+
});
39+
}));
40+
});
1541
});

src/angular-tooltip.js

Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,139 @@
11
(function(angular) {
22
'use strict';
33

4-
var module = angular.module('ngTooltip', ['ng']);
4+
function bind(fn, context) {
5+
return function() {
6+
fn.apply(context, arguments);
7+
};
8+
};
9+
10+
var module = angular.module('ngTooltip', ['ng']),
11+
extend = angular.extend;
512

6-
module.provider('$tooltip', function() {
13+
module.provider('$Tooltip', function() {
714
// Default template for tooltips.
815
var defaultTemplateUrl = 'template/ng-tooltip.html'
916
this.setDefaultTemplateUrl = function(templateUrl) {
1017
defaultTemplateUrl = templateUrl;
1118
};
1219

20+
var defaultOptions = {
21+
tether: {
22+
attachment: 'top middle',
23+
targetAttachment: 'bottom middle'
24+
}
25+
};
26+
this.setDefaultOptions = function(options) {
27+
extend(defaultOptions, options);
28+
};
29+
30+
this.$get = function($rootScope, $animate, $compile, $templateCache) {
31+
/**
32+
* Class the represents a tooltip.
33+
*/
34+
function Tooltip(options) {
35+
options = options || {};
36+
37+
this.options = extend({
38+
templateUrl: defaultTemplateUrl
39+
}, defaultOptions, options);
40+
41+
var template = $templateCache.get(this.options.templateUrl),
42+
scope = this.options.scope || $rootScope.$new();
43+
44+
this.elem = $compile(template)(scope);
45+
this.target = this.options.target;
46+
47+
scope.$on('$destroy', bind(this.close, this));
48+
};
49+
50+
extend(Tooltip.prototype, {
51+
/**
52+
* Show the tooltip, adding it to the DOM.
53+
*/
54+
open: function() {
55+
$animate.enter(this.elem, null, this.target);
56+
this._attachTether();
57+
return this;
58+
},
59+
60+
/**
61+
* Hide the tooltip, removing it from the DOM.
62+
*/
63+
close: function() {
64+
$animate.leave(this.elem);
65+
this._detachTether();
66+
return this;
67+
},
68+
69+
/**
70+
* Attach a tether to the tooltip and the target element.
71+
*/
72+
_attachTether: function() {
73+
var options = extend({
74+
element: this.elem,
75+
target: this.target
76+
}, this.options.tether);
77+
78+
this.tether = new Tether(options);
79+
80+
return this.tether;
81+
},
82+
83+
/**
84+
* Detach the thether.
85+
*/
86+
_detachTether: function() {
87+
if (this.tether) {
88+
this.tether.destroy();
89+
}
90+
}
91+
});
92+
93+
return Tooltip;
94+
}
95+
});
96+
97+
module.provider('$tooltipDirective', function() {
98+
1399
/**
14100
* Returns a factory function for building a directive for tooltips.
15101
*
16102
* @param {String} name - The name of the directive.
17103
*/
18-
this.$get = function($compile, $templateCache, $animate) {
104+
this.$get = function($Tooltip) {
19105
return function(name, options) {
20-
options = options || {};
21-
22-
var templateUrl = options.templateUrl || defaultTemplateUrl;
23-
24106
return {
25107
restrict: 'EA',
26108
scope: {
27109
content: '@' + name,
28110
tether: '=?' + name + 'Tether'
29111
},
30112
link: function(scope, elem, attrs) {
31-
var template = $templateCache.get(templateUrl),
32-
tooltip = $compile(template)(scope),
33-
tether;
113+
var tooltip = new $Tooltip(extend({
114+
target: elem,
115+
scope: scope
116+
}, options));
34117

35-
scope.tether = scope.tether || {};
36-
37-
/**
38-
* Attach a tether to the tooltip and the target element.
39-
*/
40-
function attachTether() {
41-
var options = angular.extend({}, {
42-
element: tooltip,
43-
target: elem,
44-
attachment: 'top middle',
45-
targetAttachment: 'bottom middle'
46-
}, scope.tether);
47-
48-
tether = new Tether(options);
49-
};
50-
51-
/**
52-
* Detach the thether.
53-
*/
54-
function detachTether() {
55-
if (tether) {
56-
tether.destroy();
57-
};
58-
};
59-
60-
/**
61-
* Add the tooltip to the DOM.
62-
*/
63-
function enter() {
64-
$animate.enter(tooltip, null, elem);
65-
attachTether();
66-
};
67-
68-
/**
69-
* Remove the tooltip from the DOM.
70-
*/
71-
function leave() {
72-
$animate.leave(tooltip);
73-
detachTether();
74-
};
75-
76-
/**
77-
* Enters or leaves based on the truthyness of the supplied value.
78-
*/
79-
function toggle(value) {
80-
if (value) {
81-
enter();
82-
} else {
83-
leave();
84-
}
85-
};
118+
var open = bind(tooltip.open, tooltip),
119+
close = bind(tooltip.close, tooltip);
86120

87121
/**
88122
* Toggle the tooltip.
89123
*/
90124
elem.hover(function() {
91-
scope.$apply(enter);
125+
scope.$apply(open);
92126
}, function() {
93-
scope.$apply(leave);
127+
scope.$apply(close);
94128
});
95-
96-
/**
97-
* Destroy the tether when this tooltip is removed.
98-
*/
99-
scope.$on('$destroy', leave);
100129
}
101130
};
102131
};
103132
};
104133
});
105134

106-
module.directive('ngTooltip', function($tooltip) {
107-
return $tooltip('ngTooltip');
135+
module.directive('ngTooltip', function($tooltipDirective) {
136+
return $tooltipDirective('ngTooltip');
108137
});
109138

110139
module.run(function($templateCache) {

0 commit comments

Comments
 (0)