-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathtooltipster-SVG.js
133 lines (98 loc) · 3.46 KB
/
tooltipster-SVG.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
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
var pluginName = 'tooltipster.SVG';
$.tooltipster._plugin({
name: pluginName,
core: {
__init: function() {
$.tooltipster._on('init', function(event) {
var win = $.tooltipster._env.window;
if ( win.SVGElement
&& event.origin instanceof win.SVGElement
) {
// auto-activation of the plugin on the instance
event.instance._plug(pluginName);
}
});
}
},
instance: {
__init: function(instance) {
var self = this;
//list of instance variables
self.__hadTitleTag = false;
self.__instance = instance;
// jQuery < v3.0's addClass and hasClass do not work on SVG elements.
// However, $('.tooltipstered') does find elements having the class.
if (!self.__instance._$origin.hasClass('tooltipstered')) {
var c = self.__instance._$origin.attr('class') || '';
if (c.indexOf('tooltipstered') == -1) {
self.__instance._$origin.attr('class', c + ' tooltipstered');
}
}
// if there is no content yet, let's look for a <title> child element
if (self.__instance.content() === null) {
// TODO: when there are several <title> tags (not supported in
// today's browsers yet though, still an RFC draft), pick the right
// one based on its "lang" attribute
var $title = self.__instance._$origin.find('>title');
if ($title[0]) {
var title = $title.text();
self.__hadTitleTag = true;
self.__instance._$origin.data('tooltipster-initialTitle', title);
self.__instance.content(title);
$title.remove();
}
}
// rectify the geometry if SVG.js and its screenBBox plugin have been included
self.__instance
._on('geometry.'+ self.namespace, function(event) {
var win = $.tooltipster._env.window;
// SVG coordinates may need fixing but we need svg.screenbox.js
// to provide it. SVGElement is IE8+
if (win.SVG.svgjs) {
if (!win.SVG.parser) {
win.SVG.prepare();
}
var svgEl = win.SVG.adopt(event.origin);
// not all figures need (and have) screenBBox
if (svgEl && svgEl.screenBBox) {
var bbox = svgEl.screenBBox();
event.edit({
height: bbox.height,
left: bbox.x,
top: bbox.y,
width: bbox.width
});
}
}
})
// if jQuery < v3.0, we have to remove the class ourselves
._on('destroy.'+ self.namespace, function() {
self.__destroy();
});
},
__destroy: function() {
var self = this;
if (!self.__instance._$origin.hasClass('tooltipstered')) {
var c = self.__instance._$origin.attr('class').replace('tooltipstered', '');
self.__instance._$origin.attr('class', c);
}
self.__instance._off('.'+ self.namespace);
// if the content was provided as a title tag, we may need to restore it
if (self.__hadTitleTag) {
// this must happen after Tooltipster restored (or not) the title attr
self.__instance.one('destroyed', function() {
// if a title attribute was restored, we just need to replace it with a tag
var title = self.__instance._$origin.attr('title');
if (title) {
// must be namespaced to work
$(document.createElementNS('http://www.w3.org/2000/svg', 'title'))
.text(title)
.appendTo(self.__instance._$origin);
self.__instance._$origin.removeAttr('title');
}
});
}
}
}
});
/* a build task will add "return $;" here */