This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
/
class.js
203 lines (166 loc) · 5.31 KB
/
class.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
/*
* Core plugin implementation
*/
function QTip(target, options, id, attr) {
// Elements and ID
this.id = id;
this.target = target;
this.tooltip = NULL;
this.elements = elements = { target: target };
// Internal constructs
this._id = NAMESPACE + '-' + id;
this.timers = { img: {} };
this.options = options;
this.plugins = {};
// Cache object
this.cache = cache = {
event: {},
target: $(),
disabled: FALSE,
attr: attr,
onTooltip: FALSE,
lastClass: ''
};
// Set the initial flags
this.rendered = this.destroyed = this.disabled = this.waiting =
this.hiddenDuringWait = this.positioning = this.triggering = FALSE;
}
PROTOTYPE = QTip.prototype;
PROTOTYPE.render = function(show) {
if(this.rendered || this.destroyed) { return this; } // If tooltip has already been rendered, exit
var self = this,
options = this.options,
cache = this.cache,
elements = this.elements,
text = options.content.text,
title = options.content.title,
button = options.content.button,
posOptions = options.position,
namespace = '.'+this._id+' ',
deferreds = [];
// Add ARIA attributes to target
$.attr(this.target[0], 'aria-describedby', this._id);
// Create tooltip element
this.tooltip = elements.tooltip = tooltip = $('<div/>', {
'id': this._id,
'class': [ NAMESPACE, CLASS_DEFAULT, options.style.classes, NAMESPACE + '-pos-' + options.position.my.abbrev() ].join(' '),
'width': options.style.width || '',
'height': options.style.height || '',
'tracking': posOptions.target === 'mouse' && posOptions.adjust.mouse,
/* ARIA specific attributes */
'role': 'alert',
'aria-live': 'polite',
'aria-atomic': FALSE,
'aria-describedby': this._id + '-content',
'aria-hidden': TRUE
})
.toggleClass(CLASS_DISABLED, this.disabled)
.attr(ATTR_ID, this.id)
.data(NAMESPACE, this)
.appendTo(posOptions.container)
.append(
// Create content element
elements.content = $('<div />', {
'class': NAMESPACE + '-content',
'id': this._id + '-content',
'aria-atomic': TRUE
})
);
// Set rendered flag and prevent redundant reposition calls for now
this.rendered = -1;
this.positioning = TRUE;
// Create title...
if(title) {
this._createTitle();
// Update title only if its not a callback (called in toggle if so)
if(!$.isFunction(title)) {
deferreds.push( this._updateTitle(title, FALSE) );
}
}
// Create button
if(button) { this._createButton(); }
// Set proper rendered flag and update content if not a callback function (called in toggle)
if(!$.isFunction(text)) {
deferreds.push( this._updateContent(text, FALSE) );
}
this.rendered = TRUE;
// Setup widget classes
this._setWidget();
// Assign passed event callbacks (before plugins!)
$.each(options.events, function(name, callback) {
$.isFunction(callback) && tooltip.bind(
(name === 'toggle' ? ['tooltipshow','tooltiphide'] : ['tooltip'+name])
.join(namespace)+namespace, callback
);
});
// Initialize 'render' plugins
$.each(PLUGINS, function(name) {
var instance;
if(this.initialize === 'render' && (instance = this(self))) {
self.plugins[name] = instance;
}
});
// Assign events
this._assignEvents();
// When deferreds have completed
$.when.apply($, deferreds).then(function() {
// tooltiprender event
self._trigger('render');
// Reset flags
self.positioning = FALSE;
// Show tooltip if not hidden during wait period
if(!self.hiddenDuringWait && (options.show.ready || show)) {
self.toggle(TRUE, cache.event, FALSE);
}
self.hiddenDuringWait = FALSE;
});
// Expose API
QTIP.api[this.id] = this;
return this;
};
PROTOTYPE.destroy = function(immediate) {
// Set flag the signify destroy is taking place to plugins
// and ensure it only gets destroyed once!
if(this.destroyed) { return this.target; }
function process() {
if(this.destroyed) { return; }
this.destroyed = TRUE;
var target = this.target,
title = target.attr(oldtitle);
// Destroy tooltip if rendered
if(this.rendered) {
this.tooltip.stop(1,0).find('*').remove().end().remove();
}
// Destroy all plugins
$.each(this.plugins, function(name) {
this.destroy && this.destroy();
});
// Clear timers and remove bound events
clearTimeout(this.timers.show);
clearTimeout(this.timers.hide);
this._unassignEvents();
// Remove api object and ARIA attributes
target.removeData(NAMESPACE).removeAttr(ATTR_ID)
.removeAttr('aria-describedby');
// Reset old title attribute if removed
if(this.options.suppress && title) {
target.attr('title', title).removeAttr(oldtitle);
}
// Remove qTip events associated with this API
this._unbind(target);
// Remove ID from used id objects, and delete object references
// for better garbage collection and leak protection
this.options = this.elements = this.cache = this.timers =
this.plugins = this.mouse = NULL;
// Delete epoxsed API object
delete QTIP.api[this.id];
}
// If an immediate destory is needed
if(immediate !== TRUE && this.rendered) {
tooltip.one('tooltiphidden', $.proxy(process, this));
!this.triggering && this.hide();
}
// If we're not in the process of hiding... process
else { process.call(this); }
return this.target;
};