-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
357 lines (297 loc) · 9.34 KB
/
index.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Module dependencies.
*/
var classes = require('classes');
var inherit = require('inherit');
var Emitter = require('emitter');
/**
* Module exports.
*/
exports = module.exports = Client;
exports.swf = swf;
/**
* URL to the "ZeroClipboard.swf" file.
*/
var swfPath = 'ZeroClipboard.swf';
/**
* Get/set the SWF path.
*/
function swf(path){
if (0 === arguments.length) {
return swfPath;
} else {
swfPath = path;
return Client;
}
}
/**
* Get absolute coordinates for dom element.
* XXX: this probably belongs in a more focused component ("position"?)
*
* @api private
*/
function getDOMObjectPosition(obj, stopObj){
var info = {
left: 0,
top: 0,
width: obj.width ? obj.width : obj.offsetWidth,
height: obj.height ? obj.height : obj.offsetHeight
};
while (obj && (obj != stopObj)) {
info.left += obj.offsetLeft;
info.top += obj.offsetTop;
obj = obj.offsetParent;
}
return info;
}
/**
* Dispatches an event from the ZeroClipboard SWF Flash environment.
*/
function dispatch(id, eventName, args){
var client = window.ZeroClipboard.clients[id];
if (client) {
client.receiveEvent(eventName, args);
}
}
/**
* Ensure that the ZeroClipboard-required global variables are set.
*/
if (!window.ZeroClipboard) window.ZeroClipboard = {};
if (!window.ZeroClipboard.nextId) window.ZeroClipboard.nextId = 1;
if (!window.ZeroClipboard.clients) window.ZeroClipboard.clients = {};
if (!window.ZeroClipboard.dispatch) window.ZeroClipboard.dispatch = dispatch;
/**
* The Client constructor. Turns a DOM node into a "Copy to Clipboard" button.
* To simulate ":hover" and ":active" styling on "node", define ".hover" and
* ".active" CSS classes.
*
* @param node The DOM node to turn into a "Copy to Clipboard" button.
* @param parent (optional) The parent DOM node of "node" that "has layout".
* @api public
*/
function Client(node, parent){
if (!(this instanceof Client)) {
return new Client(node, parent);
}
Emitter.call(this);
this.loaded = false; // "true" when the SWF movie has loaded
this._text = ''; // the text to copy to the clipboard. set with "text()".
this._cursor = true; // whether to show the hand cursor on mouse-over
this.zIndex = 99; // default z-index of the movie object
// unique ID
this.id = window.ZeroClipboard.nextId++;
this.movieId = 'ZeroClipboardMovie_' + this.id;
// register client with ZeroClipboard global to receive flash events
window.ZeroClipboard.clients[this.id] = this;
// create movie
if (node) this.render(node, parent);
}
/**
* Inherits from `Emitter.prototype`.
*/
inherit(Client, Emitter);
/**
* Render the SWF movie on top of the "elem" DOM element.
*
* @param elem DOMNode The DOM node that will be "converted"
* @param appendElem DOMNode (optional) The DOM node that the SWF will be inserted into
* @api public
*/
Client.prototype.glue =
Client.prototype.render = function(elem, appendElem) {
this.domElement = elem;
// float just above object, or default zIndex if dom element isn't set
if (this.domElement.style.zIndex) {
this.zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
}
if (typeof(appendElem) == 'string') {
appendElem = appendElem;
}
else if (typeof(appendElem) == 'undefined') {
appendElem = document.getElementsByTagName('body')[0];
}
this.appendElement = appendElem;
// find X/Y position of domElement
var box = getDOMObjectPosition(this.domElement, appendElem);
// create floating DIV above element
this.div = document.createElement('div');
var style = this.div.style;
style.position = 'absolute';
style.left = '' + box.left + 'px';
style.top = '' + box.top + 'px';
style.width = '' + box.width + 'px';
style.height = '' + box.height + 'px';
style.zIndex = this.zIndex;
appendElem.appendChild(this.div);
this.div.innerHTML = this.getHTML(box.width, box.height);
};
/**
* Generate the HTML for SWF embed.
*
* @api private
*/
Client.prototype.getHTML = function(width, height){
var html = '';
var flashvars = 'id=' + this.id + '&width=' + width + '&height=' + height;
if (navigator.userAgent.match(/MSIE/)) {
// IE gets an OBJECT tag
var protocol = /^https/i.test(location.href) ? 'https://' : 'http://';
html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="'+this.movieId+'" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+swfPath+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><param name="wmode" value="transparent"/></object>';
} else {
// all other browsers get an EMBED tag
html += '<embed id="'+this.movieId+'" src="'+swfPath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+this.movieId+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
}
return html;
};
/**
* temporarily hide floater offscreen
*/
Client.prototype.hide = function(){
if (this.div) {
this.div.style.left = '-2000px';
}
};
/**
* show ourselves after a call to hide()
*/
Client.prototype.show = function(){
this.reposition();
};
/**
* destroy control and floater
*/
Client.prototype.destroy = function(){
if (this.domElement && this.div) {
this.hide();
this.div.innerHTML = '';
var body = document.getElementsByTagName('body')[0];
try { body.removeChild( this.div ); } catch(e) {}
this.domElement = null;
this.div = null;
}
};
/**
* Reposition our floating div, optionally to new container.
*
* @api private
*/
Client.prototype.reposition = function(elem){
// warning: container CANNOT change size, only position
if (elem) {
this.domElement = elem;
if (!this.domElement) this.hide();
}
if (this.domElement && this.div) {
var box = getDOMObjectPosition(this.domElement, this.appendElement);
var style = this.div.style;
style.left = '' + box.left + 'px';
style.top = '' + box.top + 'px';
}
};
/**
* Set the text that will be copied to clipboard upon a user click.
*
* @param text String the text that will be copied to the clipboard on-click
* @api public
*/
Client.prototype.text =
Client.prototype.setText = function(text){
if (0 === arguments.length) {
return this._text;
} else {
this._text = text;
if (this.loaded) this.movie.setText(text);
}
};
/**
* Enable hand cursor (true), or default arrow cursor (false).
*
* @param enabled Boolean true enabled the cursor hand, false enables the arrow
* @api public
*/
Client.prototype.cursor =
Client.prototype.setHandCursor = function(enabled){
if (0 === arguments.length) {
return this._cursor;
} else {
var e = !!enabled;
this._cursor = e;
if (this.loaded) this.movie.setHandCursor(e);
}
};
/**
* Receive event from flash. Process the event and possibly relay the event to
* this client instance using "emit()".
*
* @api private
*/
Client.prototype.receiveEvent = function(eventName, args){
eventName = eventName.toString().toLowerCase().replace(/^on/, '');
var domClasses;
if (this.domElement) {
domClasses = classes(this.domElement);
}
// special behavior for certain events
switch (eventName) {
case 'load':
// movie claims it is loaded, but in IE this isn't always the case...
// bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
this.movie = document.getElementById(this.movieId);
if (!this.movie) {
var self = this;
setTimeout(function(){ self.receiveEvent('load', null); }, 1);
return;
}
// Firefox on Windows needs a "kick" in order to set these in certain cases
if (!this.loaded && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
var self = this;
setTimeout(function(){ self.receiveEvent('load', null); }, 100);
this.loaded = true;
return;
}
this.loaded = true;
this.movie.setText(this._text);
this.movie.setHandCursor(this._cursor);
break;
case 'mouseover':
if (domClasses) {
domClasses.add('hover');
if (this.recoverActive) {
domClasses.add('active');
}
}
break;
case 'mouseout':
if (domClasses) {
this.recoverActive = false;
if (domClasses.has('active')) {
domClasses.remove('active');
this.recoverActive = true;
}
domClasses.remove('hover');
}
break;
case 'mousedown':
if (domClasses) {
domClasses.add('active');
}
break;
case 'mouseup':
if (domClasses) {
domClasses.remove('active');
this.recoverActive = false;
}
break;
}
// emit the event
var a;
if ('string' == typeof args) {
a = [eventName, args]; // "args" is only a string
} else if (args) {
a = args.slice(0); // "args" is actually an array
a.unshift(eventName);
} else {
a = [eventName];
}
this.emit.apply(this, a);
};