Merge "[1707-OS] Updated license text according to the"
[sdc.git] / catalog-ui / src / app / directives / utils / smart-tooltip / smart-tooltip.ts
1 'use strict';
2
3 export interface ISmartTooltipScope extends ng.IScope {
4     sdcSmartToolip;
5 }
6
7 export class SmartTooltipDirective implements ng.IDirective {
8
9     constructor(private $compile:ng.ICompileService) {
10     }
11
12     public replace = false;
13     public restrict = 'A';
14     public transclude = false;
15
16     public link = (scope:ISmartTooltipScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {
17
18         if ($elem[0].hasAttribute('style') === false) {
19             $elem[0].setAttribute("style", "overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
20         } else {
21             let styles = $elem.attr('style');
22             $elem[0].setAttribute("style", styles + ";overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
23         }
24
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']);
30                 } else {
31                     $attrs.$set('tooltip-content', $elem.text());
32                 }
33
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.
38
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');
42
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);
48
49                 let el = this.$compile(template)(scope);
50                 console.log(el);
51                 $elem.replaceWith(el);
52             }
53         });
54     };
55
56     public static factory = ($compile:ng.ICompileService)=> {
57         return new SmartTooltipDirective($compile);
58     };
59 }
60
61 SmartTooltipDirective.factory.$inject = ['$compile'];