3 export interface ISmartTooltipScope extends ng.IScope {
7 export class SmartTooltipDirective implements ng.IDirective {
9 constructor(private $compile:ng.ICompileService) {
12 public replace = false;
13 public restrict = 'A';
14 public transclude = false;
16 public link = (scope:ISmartTooltipScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {
18 if ($elem[0].hasAttribute('style') === false) {
19 $elem[0].setAttribute("style", "overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
21 let styles = $elem.attr('style');
22 $elem[0].setAttribute("style", styles + ";overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
25 $elem.bind('mouseenter', () => {
26 if ($elem[0].offsetWidth < $elem[0].scrollWidth && !$elem.attr('tooltips')) {
27 $attrs.$set('tooltips', 'tooltips');
28 if ($attrs['sdcSmartTooltip'] && $attrs['sdcSmartTooltip'].length > 0) {
29 $elem.attr('tooltip-content', $attrs['sdcSmartTooltip']);
31 $attrs.$set('tooltip-content', $elem.text());
34 //One possible problem arises when the ngIf is placed on the root element of the template.
35 //ngIf removes the node and places a comment in it's place. Then it watches over the expression and adds/removes the actual HTML element as necessary.
36 //The problem seems to be that if it is placed on the root element of the template, then a single comment is what is left from the
37 //whole template (even if only temporarily), which gets ignored (I am not sure if this is browser-specific behaviour), resulting in an empty template.
39 // Remove ng-if attribute and its value (if we reach here, we pass ng-if (ng-if===true), so we can remove it).
40 $elem.removeAttr('ng-if');
41 $elem.removeAttr('data-ng-if');
43 // Remove me (the directive from the element)
44 let template = $elem[0].outerHTML;
45 template = template.replace('sdc-smart-tooltip=""', '');
46 template = template.replace('sdc-smart-tooltip="' + $elem.text() + '"', '');
47 //console.log(template);
49 let el = this.$compile(template)(scope);
51 $elem.replaceWith(el);
56 public static factory = ($compile:ng.ICompileService)=> {
57 return new SmartTooltipDirective($compile);
61 SmartTooltipDirective.factory.$inject = ['$compile'];