2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
23 export interface ISmartTooltipScope extends ng.IScope {
27 export class SmartTooltipDirective implements ng.IDirective {
29 constructor(private $compile:ng.ICompileService) {
32 public replace = false;
33 public restrict = 'A';
34 public transclude = false;
36 public link = (scope:ISmartTooltipScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {
38 if ($elem[0].hasAttribute('style') === false) {
39 $elem[0].setAttribute("style", "overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
41 let styles = $elem.attr('style');
42 $elem[0].setAttribute("style", styles + ";overflow: hidden; white-space: nowrap; text-overflow: ellipsis;");
45 $elem.bind('mouseenter', () => {
46 if ((<HTMLElement>$elem[0]).offsetWidth < $elem[0].scrollWidth && !$elem.attr('tooltips')) {
47 $attrs.$set('tooltips', 'tooltips');
48 if ($attrs['sdcSmartTooltip'] && $attrs['sdcSmartTooltip'].length > 0) {
49 $elem.attr('tooltip-content', $attrs['sdcSmartTooltip']);
51 $attrs.$set('tooltip-content', $elem.text());
54 //One possible problem arises when the ngIf is placed on the root element of the template.
55 //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.
56 //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
57 //whole template (even if only temporarily), which gets ignored (I am not sure if this is browser-specific behaviour), resulting in an empty template.
59 // Remove ng-if attribute and its value (if we reach here, we pass ng-if (ng-if===true), so we can remove it).
60 $elem.removeAttr('ng-if');
61 $elem.removeAttr('data-ng-if');
63 // Remove me (the directive from the element)
64 let template = $elem[0].outerHTML;
65 template = template.replace('sdc-smart-tooltip=""', '');
66 template = template.replace('sdc-smart-tooltip="' + $elem.text() + '"', '');
67 //console.log(template);
69 let el = this.$compile(template)(scope);
71 $elem.replaceWith(el);
76 public static factory = ($compile:ng.ICompileService)=> {
77 return new SmartTooltipDirective($compile);
81 SmartTooltipDirective.factory.$inject = ['$compile'];