2017-06-08 07:29:03 +00:00
|
|
|
import {module} from '../module';
|
|
|
|
import './style.css';
|
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
tooltip.$inject = ['$document', '$compile', '$interpolate', '$sce', '$templateCache', '$http', '$q'];
|
|
|
|
function tooltip($document, $compile, $interpolate, $sce, $templateCache, $http, $q) {
|
|
|
|
var promise;
|
|
|
|
|
|
|
|
function _getTemplate(tooltipTemplateUrl) {
|
2017-06-14 07:54:00 +00:00
|
|
|
var template = $templateCache.get(tooltipTemplateUrl);
|
|
|
|
if (typeof template === 'undefined') {
|
|
|
|
template = $http.get(tooltipTemplateUrl).then(function onGetTemplateSuccess(response) {
|
2017-06-15 05:45:01 +00:00
|
|
|
if (promise) {
|
|
|
|
promise.resolve(response.data);
|
|
|
|
}
|
|
|
|
promise = undefined;
|
2017-06-14 07:54:00 +00:00
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
$templateCache.put(tooltipTemplateUrl, template);
|
|
|
|
}
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
function getTemplate(tooltipTemplateUrl) {
|
|
|
|
var _promise = $q.defer();
|
|
|
|
var template = _getTemplate(tooltipTemplateUrl);
|
|
|
|
if (template) {
|
|
|
|
_promise.resolve(template);
|
|
|
|
} else {
|
|
|
|
promise = _promise;
|
|
|
|
}
|
|
|
|
return _promise.promise;
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:29:03 +00:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2017-06-14 07:54:00 +00:00
|
|
|
priority: -1,
|
2017-06-08 07:29:03 +00:00
|
|
|
link: function(scope, element, attrs) {
|
2017-09-21 07:33:02 +00:00
|
|
|
var tipHtml = '<div class="mdl-shadow--2dp" ng-class="tipClass"><div class="tooltip-text" translate>{{text}}</div><div ng-if="isHtmlContent" ng-bind-html="htmlContent"></div><div class="tooltip-arrow"></div></div>';
|
2017-06-15 05:45:01 +00:00
|
|
|
var tip;
|
2017-06-08 07:29:03 +00:00
|
|
|
var tipClassName = 'tooltip';
|
|
|
|
var tipActiveClassName = 'tooltip-show';
|
|
|
|
|
|
|
|
scope.tipClass = [tipClassName];
|
2017-06-14 07:54:00 +00:00
|
|
|
scope.text = attrs.vnTooltip || '';
|
|
|
|
|
|
|
|
if (attrs.tooltipHtml) {
|
|
|
|
scope.isHtmlContent = true;
|
|
|
|
scope.htmlContent = $sce.trustAsHtml(attrs.tooltipHtml);
|
2017-06-15 05:45:01 +00:00
|
|
|
_compileTip();
|
2017-06-14 07:54:00 +00:00
|
|
|
} else if (attrs.tooltipTemplate) {
|
2017-06-15 05:45:01 +00:00
|
|
|
getTemplate(attrs.tooltipTemplate).then(template => {
|
|
|
|
scope.isHtmlContent = true;
|
|
|
|
scope.htmlContent = $sce.trustAsHtml($interpolate(template)(scope));
|
|
|
|
_compileTip();
|
|
|
|
});
|
2017-06-14 07:54:00 +00:00
|
|
|
} else {
|
|
|
|
scope.isHtmlContent = false;
|
|
|
|
scope.htmlContent = null;
|
2017-06-15 05:45:01 +00:00
|
|
|
_compileTip();
|
2017-06-14 07:54:00 +00:00
|
|
|
}
|
2017-06-08 07:29:03 +00:00
|
|
|
|
|
|
|
if (attrs.tooltipPosition) {
|
|
|
|
scope.tipClass.push('tooltip-' + attrs.tooltipPosition);
|
|
|
|
} else {
|
|
|
|
scope.tipClass.push('tooltip-down');
|
|
|
|
}
|
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
function _compileTip() {
|
|
|
|
tip = $compile(tipHtml)(scope);
|
|
|
|
$document.find('body').append(tip);
|
|
|
|
_bindEvents();
|
|
|
|
}
|
2017-06-08 07:29:03 +00:00
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
function _bindEvents() {
|
|
|
|
element.bind('mouseover', function(e) {
|
|
|
|
tip.addClass(tipActiveClassName);
|
2017-06-08 07:29:03 +00:00
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
let pos = e.target.getBoundingClientRect();
|
|
|
|
let tipPos = tip[0].getBoundingClientRect();
|
|
|
|
let offset = {top: 0, left: 0};
|
|
|
|
let tipWidth = tipPos.width || tipPos.right - tipPos.left;
|
|
|
|
let tipHeight = tipPos.height || tipPos.bottom - tipPos.top;
|
|
|
|
let elWidth = pos.width || pos.right - pos.left;
|
|
|
|
let elHeight = pos.height || pos.bottom - pos.top;
|
|
|
|
let tipOffset = 10;
|
2017-06-08 07:29:03 +00:00
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
if (tip.hasClass('tooltip-right')) {
|
|
|
|
offset.top = pos.top - (tipHeight / 2) + (elHeight / 2);
|
|
|
|
offset.left = pos.right + tipOffset;
|
|
|
|
} else if (tip.hasClass('tooltip-left')) {
|
|
|
|
offset.top = pos.top - (tipHeight / 2) + (elHeight / 2);
|
|
|
|
offset.left = pos.left - tipWidth - tipOffset;
|
|
|
|
} else if (tip.hasClass('tooltip-down')) {
|
|
|
|
offset.top = pos.top + elHeight + tipOffset;
|
|
|
|
offset.left = pos.left - (tipWidth / 2) + (elWidth / 2);
|
|
|
|
} else {
|
|
|
|
offset.top = pos.top - tipHeight - tipOffset;
|
|
|
|
offset.left = pos.left - (tipWidth / 2) + (elWidth / 2);
|
|
|
|
}
|
2017-06-08 07:29:03 +00:00
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
tip.css('top', offset.top + 'px');
|
|
|
|
tip.css('left', offset.left + 'px');
|
|
|
|
});
|
2017-06-08 07:29:03 +00:00
|
|
|
|
2017-06-15 05:45:01 +00:00
|
|
|
element.on('mouseout', function() {
|
|
|
|
tip.removeClass(tipActiveClassName);
|
|
|
|
});
|
|
|
|
|
|
|
|
tip.on('mouseover', function() {
|
|
|
|
tip.addClass(tipActiveClassName);
|
|
|
|
});
|
|
|
|
|
|
|
|
tip.on('mouseout', function() {
|
|
|
|
tip.removeClass(tipActiveClassName);
|
|
|
|
});
|
|
|
|
|
|
|
|
element.on('$destroy', function() {
|
|
|
|
tip.remove();
|
|
|
|
});
|
|
|
|
}
|
2017-06-08 07:29:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.directive('vnTooltip', tooltip);
|