This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 465
/
jquery_methods.js
232 lines (187 loc) · 7.16 KB
/
jquery_methods.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
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
// Initialization method
function init(elem, id, opts)
{
var obj, posOptions, attr, config, title,
// Setup element references
docBody = $(document.body),
// Use document body instead of document element if needed
newTarget = elem[0] === document ? docBody : elem,
// Grab metadata from element if plugin is present
metadata = (elem.metadata) ? elem.metadata(opts.metadata) : NULL,
// If metadata type if HTML5, grab 'name' from the object instead, or use the regular data object otherwise
metadata5 = opts.metadata.type === 'html5' && metadata ? metadata[opts.metadata.name] : NULL,
// Grab data from metadata.name (or data-qtipopts as fallback) using .data() method,
html5 = elem.data(opts.metadata.name || 'qtipopts');
// If we don't get an object returned attempt to parse it manualyl without parseJSON
try { html5 = typeof html5 === 'string' ? $.parseJSON(html5) : html5; } catch(e) {}
// Merge in and sanitize metadata
config = $.extend(TRUE, {}, QTIP.defaults, opts,
typeof html5 === 'object' ? sanitizeOptions(html5) : NULL,
sanitizeOptions(metadata5 || metadata));
// Re-grab our positioning options now we've merged our metadata and set id to passed value
posOptions = config.position;
config.id = id;
// Setup missing content if none is detected
if('boolean' === typeof config.content.text) {
attr = elem.attr(config.content.attr);
// Grab from supplied attribute if available
if(config.content.attr !== FALSE && attr) { config.content.text = attr; }
// No valid content was found, abort render
else { return FALSE; }
}
// Setup target options
if(!posOptions.container.length) { posOptions.container = docBody; }
if(posOptions.target === FALSE) { posOptions.target = newTarget; }
if(config.show.target === FALSE) { config.show.target = newTarget; }
if(config.show.solo === TRUE) { config.show.solo = posOptions.container.closest('body'); }
if(config.hide.target === FALSE) { config.hide.target = newTarget; }
if(config.position.viewport === TRUE) { config.position.viewport = posOptions.container; }
// Ensure we only use a single container
posOptions.container = posOptions.container.eq(0);
// Convert position corner values into x and y strings
posOptions.at = new CORNER(posOptions.at, TRUE);
posOptions.my = new CORNER(posOptions.my);
// Destroy previous tooltip if overwrite is enabled, or skip element if not
if(elem.data(NAMESPACE)) {
if(config.overwrite) {
elem.qtip('destroy');
}
else if(config.overwrite === FALSE) {
return FALSE;
}
}
// Add has-qtip attribute
elem.attr(ATTR_HAS, id);
// Remove title attribute and store it if present
if(config.suppress && (title = elem.attr('title'))) {
// Final attr call fixes event delegatiom and IE default tooltip showing problem
elem.removeAttr('title').attr(oldtitle, title).attr('title', '');
}
// Initialize the tooltip and add API reference
obj = new QTip(elem, config, id, !!attr);
elem.data(NAMESPACE, obj);
// Catch remove/removeqtip events on target element to destroy redundant tooltip
elem.one('remove.qtip-'+id+' removeqtip.qtip-'+id, function() {
var api; if((api = $(this).data(NAMESPACE))) { api.destroy(); }
});
return obj;
}
// jQuery $.fn extension method
QTIP = $.fn.qtip = function(options, notation, newValue)
{
var command = ('' + options).toLowerCase(), // Parse command
returned = NULL,
args = $.makeArray(arguments).slice(1),
event = args[args.length - 1],
opts = this[0] ? $.data(this[0], NAMESPACE) : NULL;
// Check for API request
if((!arguments.length && opts) || command === 'api') {
return opts;
}
// Execute API command if present
else if('string' === typeof options)
{
this.each(function()
{
var api = $.data(this, NAMESPACE);
if(!api) { return TRUE; }
// Cache the event if possible
if(event && event.timeStamp) { api.cache.event = event; }
// Check for specific API commands
if((command === 'option' || command === 'options') && notation) {
if($.isPlainObject(notation) || newValue !== undefined) {
api.set(notation, newValue);
}
else {
returned = api.get(notation);
return FALSE;
}
}
// Execute API command
else if(api[command]) {
api[command].apply(api, args);
}
});
return returned !== NULL ? returned : this;
}
// No API commands. validate provided options and setup qTips
else if('object' === typeof options || !arguments.length)
{
opts = sanitizeOptions($.extend(TRUE, {}, options));
// Bind the qTips
return QTIP.bind.call(this, opts, event);
}
};
// $.fn.qtip Bind method
QTIP.bind = function(opts, event)
{
return this.each(function(i) {
var options, targets, events, namespace, api, id;
// Find next available ID, or use custom ID if provided
id = $.isArray(opts.id) ? opts.id[i] : opts.id;
id = !id || id === FALSE || id.length < 1 || QTIP.api[id] ? QTIP.nextid++ : id;
// Setup events namespace
namespace = '.qtip-'+id+'-create';
// Initialize the qTip and re-grab newly sanitized options
api = init($(this), id, opts);
if(api === FALSE) { return TRUE; }
else { QTIP.api[id] = api; }
options = api.options;
// Initialize plugins
$.each(PLUGINS, function() {
if(this.initialize === 'initialize') { this(api); }
});
// Determine hide and show targets
targets = { show: options.show.target, hide: options.hide.target };
events = {
show: $.trim('' + options.show.event).replace(/ /g, namespace+' ') + namespace,
hide: $.trim('' + options.hide.event).replace(/ /g, namespace+' ') + namespace
};
/*
* Make sure hoverIntent functions properly by using mouseleave as a hide event if
* mouseenter/mouseout is used for show.event, even if it isn't in the users options.
*/
if(/mouse(over|enter)/i.test(events.show) && !/mouse(out|leave)/i.test(events.hide)) {
events.hide += ' mouseleave' + namespace;
}
/*
* Also make sure initial mouse targetting works correctly by caching mousemove coords
* on show targets before the tooltip has rendered.
*
* Also set onTarget when triggered to keep mouse tracking working
*/
targets.show.bind('mousemove'+namespace, function(event) {
api._storeMouse(event);
api.cache.onTarget = TRUE;
});
// Define hoverIntent function
function hoverIntent(event) {
function render() {
// Cache mouse coords,render and render the tooltip
api.render(typeof event === 'object' || options.show.ready);
// Unbind show and hide events
targets.show.add(targets.hide).unbind(namespace);
}
// Only continue if tooltip isn't disabled
if(api.disabled) { return FALSE; }
// Cache the event data
api.cache.event = $.extend({}, event);
api.cache.target = event ? $(event.target) : [undefined];
// Start the event sequence
if(options.show.delay > 0) {
clearTimeout(api.timers.show);
api.timers.show = setTimeout(render, options.show.delay);
if(events.show !== events.hide) {
targets.hide.bind(events.hide, function() { clearTimeout(api.timers.show); });
}
}
else { render(); }
}
// Bind show events to target
targets.show.bind(events.show, hoverIntent);
// Prerendering is enabled, create tooltip now
if(options.show.ready || options.prerender) { hoverIntent(event); }
});
};
// Populated in render method
QTIP.api = {};