forked from zeroclipboard/zeroclipboard
/
event.js
172 lines (141 loc) · 4.52 KB
/
event.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
/*
* Bridge from the flash object back to the javascript
*
* returns nothing
*/
ZeroClipboard.dispatch = function (eventName, args) {
// receive event from flash movie, send to client
ZeroClipboard.prototype._singleton.receiveEvent(eventName, args);
};
/*
* Add an event to the client.
*
* returns nothing
*/
ZeroClipboard.prototype.on = function (eventName, func) {
// add user event listener for event
// event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
var events = eventName.toString().split(/\s/g);
for (var i = 0; i < events.length; i++) {
eventName = events[i].toLowerCase().replace(/^on/, '');
if (!this.handlers[eventName]) this.handlers[eventName] = func;
}
// If we don't have flash, tell an adult
if (this.handlers.noflash && !ZeroClipboard.detectFlashSupport()) {
this.receiveEvent("onNoFlash", null);
}
};
// shortcut to old stuff
ZeroClipboard.prototype.addEventListener = ZeroClipboard.prototype.on;
/*
* Remove an event from the client.
*
* returns nothing
*/
ZeroClipboard.prototype.off = function (eventName, func) {
// remove user event listener for event
var events = eventName.toString().split(/\s/g);
for (var i = 0; i < events.length; i++) {
eventName = events[i].toLowerCase().replace(/^on/, "");
for (var event in this.handlers) {
if (event === eventName && this.handlers[event] === func) {
delete this.handlers[event];
}
}
}
};
// shortcut to old stuff
ZeroClipboard.prototype.removeEventListener = ZeroClipboard.prototype.off;
/*
* Receive an event for a specific client.
*
* returns nothing
*/
ZeroClipboard.prototype.receiveEvent = function (eventName, args) {
// receive event from flash
eventName = eventName.toString().toLowerCase().replace(/^on/, '');
var element = currentElement;
var performCallbackAsync = true;
// special behavior for certain events
switch (eventName) {
case 'load':
// If the flash version is less than 10, throw event.
if (args && parseFloat(args.flashVersion.replace(",", ".").replace(/[^0-9\.]/gi, '')) < 10) {
this.receiveEvent("onWrongFlash", { flashVersion: args.flashVersion });
return;
}
this.htmlBridge.setAttribute("data-clipboard-ready", true);
break;
case 'mouseover':
_addClass(element, this.options.hoverClass);
break;
case 'mouseout':
_removeClass(element, this.options.hoverClass);
this.resetBridge();
break;
case 'mousedown':
_addClass(element, this.options.activeClass);
break;
case 'mouseup':
_removeClass(element, this.options.activeClass);
break;
case 'datarequested':
var targetId = element.getAttribute('data-clipboard-target'),
targetEl = !targetId ? null : document.getElementById(targetId);
if (targetEl) {
var textContent = targetEl.value || targetEl.textContent || targetEl.innerText;
if (textContent) this.setText(textContent);
}
else {
var defaultText = element.getAttribute('data-clipboard-text');
if (defaultText) this.setText(defaultText);
}
performCallbackAsync = false;
break;
case 'complete':
this.options.text = null;
break;
} // switch eventName
if (this.handlers[eventName]) {
var func = this.handlers[eventName];
if (typeof(func) == 'function') {
// actual function reference
_dispatchCallback(func, element, this, args, performCallbackAsync);
}
else if (typeof(func) == 'string') {
_dispatchCallback(window[func], element, this, args, performCallbackAsync);
}
} // user defined handler for event
};
/*
/*
* Register new element(s) to the object.
*
* returns nothing
*/
ZeroClipboard.prototype.glue = function (elements) {
elements = _prepGlue(elements);
for (var i = 0; i < elements.length ; i++) {
// if the element has not been glued
if (_inArray(elements[i], gluedElements) == -1) {
// push to glued elements
gluedElements.push(elements[i]);
_addEventHandler(elements[i], "mouseover", _elementMouseOver);
}
}
};
/*
* Unregister the clipboard actions of an element on the page
*
* returns nothing
*/
ZeroClipboard.prototype.unglue = function (elements) {
elements = _prepGlue(elements);
for (var i = 0; i < elements.length; i++) {
_removeEventHandler(elements[i], "mouseover", _elementMouseOver);
// get the index of the item
var arrayIndex = _inArray(elements[i], gluedElements);
// if the index is not -1, remove from array
if (arrayIndex != -1) gluedElements.splice(arrayIndex, 1);
}
};