33'use strict' ;
44
55angular . module ( 'bootstrap.angular.validation' ) . factory ( 'tooltipMessageService' , [ '$injector' , 'BsValidationService' ,
6- 'bsValidationConfig' , function ( $injector , validationService , validationConfig ) {
6+ '$interpolate' , '$templateCache' , 'bsValidationConfig' ,
7+ function ( $injector , validationService , $interpolate , $templateCache , validationConfig ) {
78
89 function getElementID ( $element ) {
910 var id = $element . attr ( 'id' ) ;
10-
1111 if ( id ) {
1212 return id ;
1313 }
@@ -17,43 +17,64 @@ angular.module('bootstrap.angular.validation').factory('tooltipMessageService',
1717 return id ;
1818 }
1919
20- function getErrorTooltipID ( $element ) {
21- return 'bs-error-' + getElementID ( $element ) ;
22- }
23-
2420 function getErrorTooltip ( $element ) {
25- var tooltipID = getErrorTooltipID ( $element ) ;
21+ var tooltipID = 'bs-error-' + getElementID ( $element ) ;
22+ var tooltipElement = document . getElementById ( tooltipID ) ;
23+
24+ if ( tooltipElement ) {
25+ return angular . element ( tooltipElement ) ;
26+ }
27+
28+ var data = { errorClass : validationConfig . errorClass , tooltipID : tooltipID } ;
29+ var html = $templateCache . get ( 'bav/template/tooltip.html' ) ;
30+ angular . element ( 'body' ) . append ( $interpolate ( html ) ( data ) ) ;
31+
2632 return angular . element ( document . getElementById ( tooltipID ) ) ;
2733 }
2834
35+ function getTooltipPlacement ( $element ) {
36+ var attributeName = 'bs-tooltip-placement' ;
37+ if ( $element . attr ( attributeName ) ) {
38+ return $element . attr ( attributeName ) ;
39+ }
40+
41+ var parentForm = $element . parents ( 'form' ) ;
42+ if ( parentForm && parentForm . attr ( attributeName ) ) {
43+ return parentForm . attr ( attributeName ) ;
44+ }
45+
46+ // Use the global config
47+ return validationConfig . getTooltipPlacement ( ) ;
48+ }
49+
2950 return {
3051 hideErrorMessage : function ( $element , $formGroupElement ) {
3152 validationService . removeErrorClass ( $formGroupElement ) ;
3253 getErrorTooltip ( $element ) . removeClass ( 'in' ) ;
3354 } ,
3455
35- resolveMessage : function ( $element , $attr , key ) {
36- return validationService . resolveMessage ( $element , $attr , key ) ;
37- } ,
38-
3956 showErrorMessage : function ( $element , $attr , ngModelController ) {
40- var firstErrorKey = Object . keys ( ngModelController . $error ) [ 0 ] ;
41- var message = validationConfig . getErrorMessagePrefix ( ) + this . resolveMessage ( $element , $attr , firstErrorKey ) ;
42-
43- var tooltipID = getErrorTooltipID ( $element ) ;
57+ var message = validationService . getErrorMessage ( $element , $attr , ngModelController ) ;
4458 var $errorTooltip = getErrorTooltip ( $element ) ;
59+ var placement = getTooltipPlacement ( $element ) ;
4560
46- if ( ! $errorTooltip || ! $errorTooltip . length ) {
47- angular . element ( 'body' ) . append ( '<div class="tooltip has-error" id="' + tooltipID + '" role="tooltip"' +
48- '><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' ) ;
61+ $errorTooltip . findOne ( '.tooltip-inner' ) . html ( message ) ;
4962
50- $errorTooltip = getErrorTooltip ( $element ) ;
63+ var $position = $injector . get ( '$uibPosition' ) ;
64+ var ttPosition = $position . positionElements ( $element , $errorTooltip , placement , true ) ;
65+ $errorTooltip . css ( { top : ttPosition . top + 'px' , left : ttPosition . left + 'px' } ) ;
66+ $errorTooltip . addClass ( 'in' ) ;
67+
68+ if ( ! $errorTooltip . hasClass ( ttPosition . placement . split ( '-' ) [ 0 ] ) ) {
69+ $errorTooltip . addClass ( ttPosition . placement . split ( '-' ) [ 0 ] ) ;
5170 }
5271
53- var ttPosition = $injector . get ( '$uibPosition' ) . positionElements ( $element , $errorTooltip , 'bottom-left' , true ) ;
54- $errorTooltip . css ( { top : ttPosition . top + 'px' , left : ttPosition . left + 'px' } ) ;
55- $errorTooltip . findOne ( '.tooltip-inner' ) . html ( message ) ;
56- $errorTooltip . addClass ( 'in' ) . addClass ( ttPosition . placement ) ;
72+ $position . positionArrow ( $errorTooltip , ttPosition . placement ) ;
5773 }
5874 } ;
5975} ] ) ;
76+
77+ angular . module ( 'bootstrap.angular.validation' ) . run ( [ '$templateCache' , function ( $templateCache ) {
78+ $templateCache . put ( 'bav/template/tooltip.html' , '<div class="tooltip {{errorClass}}" id="{{tooltipID}}"' +
79+ ' role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' ) ;
80+ } ] ) ;
0 commit comments