-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (95 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//binding for showing tooltip only when the element text has ellipsis
(function( factory ) {
"use strict";
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'knockout', 'mario'], function ( $, ko, mario ) {
return factory( $, ko, mario);
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function () {
var jq = $;
if(!jq) {
jq = require('jquery');
}
var ko = require('knockout'),
mario = require('mario');
return factory( jq, ko, mario );
};
}
else {
// Browser
if(!ko || !mario || !jQuery) {
throw new Error('Dependencies not found. [jQuery, ko, mario] required as global variables if not using AMD');
}
factory( jQuery, ko, mario);
}
}
(function ($, ko, mario) {
ko.bindingHandlers.ellipsisTooltip = {
init: function (element, valueAccessor, allBindingsAccessor) {
var text = allBindingsAccessor().text,
value = valueAccessor(),
$element = $(element);
if (value && isEllipsisPresent(element)) {
$element.attr('title', $element.text());
} else {
$element.removeAttr('title');
}
//Make sure ellipsisTooltip is applied after text binding is updated in case its an observable
if (ko.isObservable(text)) {
text.subscribe(function (newValue) {
$element.ellipsisTooltip();
$element.trigger('ellipsisTooltip:updated');
});
}
},
update: function (element, valueAccessor) {
var value = valueAccessor(),
$element = $(element);
if (value && isEllipsisPresent(element)) {
$element.attr('title', $element.text());
} else {
$element.removeAttr('title');
}
}
};
function isEllipsisPresent(element) {
if (mario.chrome || mario.safari) {
return (element.offsetWidth < element.scrollWidth);
}
// In IE and FF in some cases element.offsetWidth and element.scrollWidth are same even though there is ellipsis.
// So we make a clone div with same text and width = 'auto' and compare its width
var $element = $(element),
isEllipsisPresent = false,
clone = document.createElement('div'),
$clone = $(clone).text($element.text());
clone.style.cssText = getComputedStyleCssText(element);
$clone
.css({
position: 'absolute',
top: '0px',
left: '0px',
visibility: 'hidden',
width: 'auto'})
.appendTo('body');
if ($clone.width() > $element.width()) {
isEllipsisPresent = true;
}
$clone.remove();
return isEllipsisPresent;
}
function getComputedStyleCssText(element) {
var style = window.getComputedStyle(element), cssText;
if (style.cssText !== '') {
return style.cssText;
}
cssText = '';
for (var i = 0; i < style.length; i++) {
cssText += style[i] + ': ' + style.getPropertyValue(style[i]) + '; ';
}
return cssText;
}
}));