|
1 | 1 | (function(angular) {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
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; |
5 | 12 |
|
6 |
| - module.provider('$tooltip', function() { |
| 13 | + module.provider('$Tooltip', function() { |
7 | 14 | // Default template for tooltips.
|
8 | 15 | var defaultTemplateUrl = 'template/ng-tooltip.html'
|
9 | 16 | this.setDefaultTemplateUrl = function(templateUrl) {
|
10 | 17 | defaultTemplateUrl = templateUrl;
|
11 | 18 | };
|
12 | 19 |
|
| 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 | + |
13 | 99 | /**
|
14 | 100 | * Returns a factory function for building a directive for tooltips.
|
15 | 101 | *
|
16 | 102 | * @param {String} name - The name of the directive.
|
17 | 103 | */
|
18 |
| - this.$get = function($compile, $templateCache, $animate) { |
| 104 | + this.$get = function($Tooltip) { |
19 | 105 | return function(name, options) {
|
20 |
| - options = options || {}; |
21 |
| - |
22 |
| - var templateUrl = options.templateUrl || defaultTemplateUrl; |
23 |
| - |
24 | 106 | return {
|
25 | 107 | restrict: 'EA',
|
26 | 108 | scope: {
|
27 | 109 | content: '@' + name,
|
28 | 110 | tether: '=?' + name + 'Tether'
|
29 | 111 | },
|
30 | 112 | 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)); |
34 | 117 |
|
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); |
86 | 120 |
|
87 | 121 | /**
|
88 | 122 | * Toggle the tooltip.
|
89 | 123 | */
|
90 | 124 | elem.hover(function() {
|
91 |
| - scope.$apply(enter); |
| 125 | + scope.$apply(open); |
92 | 126 | }, function() {
|
93 |
| - scope.$apply(leave); |
| 127 | + scope.$apply(close); |
94 | 128 | });
|
95 |
| - |
96 |
| - /** |
97 |
| - * Destroy the tether when this tooltip is removed. |
98 |
| - */ |
99 |
| - scope.$on('$destroy', leave); |
100 | 129 | }
|
101 | 130 | };
|
102 | 131 | };
|
103 | 132 | };
|
104 | 133 | });
|
105 | 134 |
|
106 |
| - module.directive('ngTooltip', function($tooltip) { |
107 |
| - return $tooltip('ngTooltip'); |
| 135 | + module.directive('ngTooltip', function($tooltipDirective) { |
| 136 | + return $tooltipDirective('ngTooltip'); |
108 | 137 | });
|
109 | 138 |
|
110 | 139 | module.run(function($templateCache) {
|
|
0 commit comments