|
| 1 | +/* global document */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +angular.module('bootstrap.angular.validation').factory('tooltipMessageService', ['$uibPosition', 'BsValidationService', |
| 6 | +function($uibPosition, validationService) { |
| 7 | + |
| 8 | + var iconMarkup = '<i class="fa fa-exclamation-triangle fa-fw"></i>'; |
| 9 | + |
| 10 | + function getElementID($element) { |
| 11 | + var id = $element.attr('id'); |
| 12 | + |
| 13 | + if (id) { |
| 14 | + return id; |
| 15 | + } |
| 16 | + |
| 17 | + id = 'bs-' + (Math.floor(Math.random() * 10000)) + '-' + Math.floor(Math.random() * 10000); |
| 18 | + $element.attr('id', id); |
| 19 | + return id; |
| 20 | + } |
| 21 | + |
| 22 | + function getErrorTooltipID($element) { |
| 23 | + return 'bs-error-' + getElementID($element); |
| 24 | + } |
| 25 | + |
| 26 | + function getErrorTooltip($element) { |
| 27 | + var tooltipID = getErrorTooltipID($element); |
| 28 | + return angular.element(document.getElementById(tooltipID)); |
| 29 | + } |
| 30 | + |
| 31 | + return { |
| 32 | + hideErrorMessage: function($element, $formGroupElement) { |
| 33 | + validationService.removeErrorClass($formGroupElement); |
| 34 | + getErrorTooltip($element).removeClass('in'); |
| 35 | + }, |
| 36 | + |
| 37 | + resolveMessage: function($element, $attr, key) { |
| 38 | + return validationService.resolveMessage($element, $attr, key); |
| 39 | + }, |
| 40 | + |
| 41 | + showErrorMessage: function($element, $attr, ngModelController, $formGroupElement) { |
| 42 | + var firstErrorKey = Object.keys(ngModelController.$error)[0]; |
| 43 | + var message = this.resolveMessage($element, $attr, firstErrorKey); |
| 44 | + |
| 45 | + var tooltipID = getErrorTooltipID($element); |
| 46 | + var $errorTooltip = getErrorTooltip($element); |
| 47 | + |
| 48 | + if (!$errorTooltip || !$errorTooltip.length) { |
| 49 | + angular.element('body').append('<div class="tooltip has-error" id="' + tooltipID + '" role="tooltip"' + |
| 50 | + '><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'); |
| 51 | + |
| 52 | + $errorTooltip = getErrorTooltip($element); |
| 53 | + } |
| 54 | + |
| 55 | + var ttPosition = $uibPosition.positionElements($element, $errorTooltip, 'bottom-left', true); |
| 56 | + $errorTooltip.css({ top: ttPosition.top + 'px', left: ttPosition.left + 'px' }); |
| 57 | + $errorTooltip.findOne('.tooltip-inner').html(message); |
| 58 | + $errorTooltip.addClass('in').addClass(ttPosition.placement); |
| 59 | + } |
| 60 | + }; |
| 61 | +}]); |
0 commit comments