-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleTooltip.js
More file actions
235 lines (196 loc) · 7.22 KB
/
simpleTooltip.js
File metadata and controls
235 lines (196 loc) · 7.22 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Created by jcarrell on 10/7/18.
*/
/**
* To use simpleTooltip...
*
* Requires: Bootstrap, jQuery
*
* Add to any HTML element the "data-toggle-stt" attribute. No value is necessary.
*
* To specify the content for the tooltip add the content as the value of the "stt-title" attribute.
*
* TODO This documentation and the functionality itself is a WIP...
*/
(function()
{
"use strict";
// On mouseover, create the tooltip
$(document).on("mouseover", "[data-toggle-stt]", function(event)
{
const jqElement = $(event.currentTarget);
if (jqElement.attr("data-original-title") === undefined)
{
const title = jqElement.attr("stt-title");
const useFancy = jqElement.attr("data-toggle-stt-use-fancy") !== undefined;
if (title !== undefined && title.length > 0)
{
console.debug("Applying simpleTooltip functionality to element", jqElement);
const dataset = jqElement[0].dataset;
const dataSetKeys = Object.keys(dataset);
const sttDatasetKeys = dataSetKeys.filter((key) => key.startsWith("sttoption"));
const tooltipOptions = {
trigger: "hover"
};
for (const sttDatasetKey of sttDatasetKeys)
{
let value = dataset[sttDatasetKey];
try
{
value = JSON.parse(value);
}
catch(e){}
tooltipOptions[sttDatasetKey.replace("sttoption", "").toLowerCase()] = value;
}
tooltipOptions.title = title;
let tooltipFunction;
if (useFancy)
tooltipFunction = "fancyTooltip";
else
tooltipFunction = "tooltip";
jqElement[tooltipFunction](tooltipOptions);
jqElement[tooltipFunction]("show");
}
}
});
// On mouseleave destroy the tooltip
$(document).on("mouseleave", "[data-toggle-stt]", function(event)
{
$(event.currentTarget)
.tooltip("destroy")
.removeAttr("data-original-title");
});
})();
function getNewJQElement(tagName)
{
/*
* Return a new, blank jQuery-wrapped element of tag name 'tagName'
*
* :param: tagName: (String) The tag name to generate.
*
* :return: jQuery-wrapped element of tag name 'tagName'
*/
return $(`<${tagName}></${tagName}>`);
}
$.fn.fancyTooltip = function(options={}, type="info", excludeCustomCSS=false)
{
/**
* Enable a "fancy" (by John Carrell's definition) tool tip on the jQuery selected element(s).
*
* :param: options: (object) Custom options to pass directly to 'tooltip()'.
* Caveat: If 'options' contains the key "title"
* The value of that key will be used for the content of the tooltip
* then the "title" key will be deleted. This is done in order to prepend the glyphicon.
*
* Default options automatically applied but overridden by 'options' are:
* html: true,
* animation: false
* placement: "auto right"
*
* See BootstrapTooltip docs for details of these and other options.
*
* NOTE: if the first argument is a string it will be passed directly to .tooltip()
* This enables the same "command" syntax that the .tooltip() interface exposes.
*
* :param: type: (str) An indication of how the dialog should be colored and which icon should be used.
* The supported values for 'type' are:
* "info"
* "success"
* "warning"
* "danger"
*
* All coloring uses standard Bootstrap hex codes.
* If any other value is supplied (such as "none"), no icon will appear and there will be no background color.
*
* :param: excludeCustomCSS: (boolean) If true, will refrain from applying custom CSS (this is what makes it fancy!)
* In order to apply your own CSS know that the following HTML structure exists:
* <div class="customTooltip">
* <div class="customTooltipInner">
* <span class="glyphicon ..."></span> [ content ]
* </div>
* </div>
*
* Use the two "customTooltip[Inner]" classes to control the style.
*
* :return: None
*/
if (typeof options === "string")
{
this.tooltip(options);
}
else
{
const templateWrapperDOM = $(getNewJQElement("div"));
const customTooltipDOM = getNewJQElement("div")
.addClass("tooltip customTooltip");
const customTooltipInnerDOM = getNewJQElement("div")
.addClass("tooltip-inner customToolTipInner");
let iconClass, iconColor, backgroundColor;
switch (type)
{
case "info":
iconClass = "glyphicon-info-sign";
iconColor = "#31708f";
backgroundColor = "#d9edf7";
break;
case "success":
iconClass = "glyphicon-ok-sign";
iconColor = "#3c763d";
backgroundColor = "#dff0d8";
break;
case "warning":
iconClass = "glyphicon-exclamation-sign";
iconColor = "#8a6d3b";
backgroundColor = "#fcf8e3";
break;
case "danger":
iconClass = "glyphicon-remove-sign";
iconColor = "#a94442";
backgroundColor = "#f2dede";
break;
default:
backgroundColor = "#FFF";
}
let icon = "";
if (iconClass !== undefined)
{
icon = `<span class="columnMenuItemInfoIcon glyphicon ${iconClass}" style="color: ${iconColor}"></span> `;
}
let title = icon;
if ("title" in options)
{
title += options.title;
delete options.title;
}
else if (this.attr("title") !== undefined)
{
title += this.attr("title");
}
if (!excludeCustomCSS)
{
customTooltipDOM.css("opacity", 1);
customTooltipInnerDOM
.css("width", "max-content")
.css("width", "-moz-max-content")
.css("font-size", "14px")
.css("max-width", "300px")
.css("color", "black")
.css("border", "1px solid black")
.css("box-shadow", "0 0 5px 3px white");
if (backgroundColor !== undefined)
customTooltipInnerDOM
.css("background-color", backgroundColor);
}
customTooltipDOM.append(customTooltipInnerDOM);
templateWrapperDOM.append(customTooltipDOM);
const defaultOptions = {
title: title,
template: templateWrapperDOM.html(),
html: true,
animation: false,
placement: "auto right"
};
const finalOptions = $.extend(true, defaultOptions, options);
this.tooltip(finalOptions);
}
};