1 |
// Simple Set Clipboard System |
2 |
// Author: Joseph Huckaby |
3 |
|
4 |
var ZeroClipboard_TableTools = { |
5 |
|
6 |
version: "1.0.4-TableTools2", |
7 |
clients: {}, // registered upload clients on page, indexed by id |
8 |
moviePath: '', // URL to movie |
9 |
nextId: 1, // ID of next movie |
10 |
|
11 |
$: function(thingy) { |
12 |
// simple DOM lookup utility function |
13 |
if (typeof(thingy) == 'string') thingy = document.getElementById(thingy); |
14 |
if (!thingy.addClass) { |
15 |
// extend element with a few useful methods |
16 |
thingy.hide = function() { this.style.display = 'none'; }; |
17 |
thingy.show = function() { this.style.display = ''; }; |
18 |
thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; }; |
19 |
thingy.removeClass = function(name) { |
20 |
this.className = this.className.replace( new RegExp("\\s*" + name + "\\s*"), " ").replace(/^\s+/, '').replace(/\s+$/, ''); |
21 |
}; |
22 |
thingy.hasClass = function(name) { |
23 |
return !!this.className.match( new RegExp("\\s*" + name + "\\s*") ); |
24 |
} |
25 |
} |
26 |
return thingy; |
27 |
}, |
28 |
|
29 |
setMoviePath: function(path) { |
30 |
// set path to ZeroClipboard.swf |
31 |
this.moviePath = path; |
32 |
}, |
33 |
|
34 |
dispatch: function(id, eventName, args) { |
35 |
// receive event from flash movie, send to client |
36 |
var client = this.clients[id]; |
37 |
if (client) { |
38 |
client.receiveEvent(eventName, args); |
39 |
} |
40 |
}, |
41 |
|
42 |
register: function(id, client) { |
43 |
// register new client to receive events |
44 |
this.clients[id] = client; |
45 |
}, |
46 |
|
47 |
getDOMObjectPosition: function(obj) { |
48 |
// get absolute coordinates for dom element |
49 |
var info = { |
50 |
left: 0, |
51 |
top: 0, |
52 |
width: obj.width ? obj.width : obj.offsetWidth, |
53 |
height: obj.height ? obj.height : obj.offsetHeight |
54 |
}; |
55 |
|
56 |
if ( obj.style.width != "" ) |
57 |
info.width = obj.style.width.replace("px",""); |
58 |
|
59 |
if ( obj.style.height != "" ) |
60 |
info.height = obj.style.height.replace("px",""); |
61 |
|
62 |
while (obj) { |
63 |
info.left += obj.offsetLeft; |
64 |
info.top += obj.offsetTop; |
65 |
obj = obj.offsetParent; |
66 |
} |
67 |
|
68 |
return info; |
69 |
}, |
70 |
|
71 |
Client: function(elem) { |
72 |
// constructor for new simple upload client |
73 |
this.handlers = {}; |
74 |
|
75 |
// unique ID |
76 |
this.id = ZeroClipboard_TableTools.nextId++; |
77 |
this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id; |
78 |
|
79 |
// register client with singleton to receive flash events |
80 |
ZeroClipboard_TableTools.register(this.id, this); |
81 |
|
82 |
// create movie |
83 |
if (elem) this.glue(elem); |
84 |
} |
85 |
}; |
86 |
|
87 |
ZeroClipboard_TableTools.Client.prototype = { |
88 |
|
89 |
id: 0, // unique ID for us |
90 |
ready: false, // whether movie is ready to receive events or not |
91 |
movie: null, // reference to movie object |
92 |
clipText: '', // text to copy to clipboard |
93 |
fileName: '', // default file save name |
94 |
action: 'copy', // action to perform |
95 |
handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor |
96 |
cssEffects: true, // enable CSS mouse effects on dom container |
97 |
handlers: null, // user event handlers |
98 |
sized: false, |
99 |
|
100 |
glue: function(elem, title) { |
101 |
// glue to DOM element |
102 |
// elem can be ID or actual DOM element object |
103 |
this.domElement = ZeroClipboard_TableTools.$(elem); |
104 |
|
105 |
// float just above object, or zIndex 99 if dom element isn't set |
106 |
var zIndex = 99; |
107 |
if (this.domElement.style.zIndex) { |
108 |
zIndex = parseInt(this.domElement.style.zIndex) + 1; |
109 |
} |
110 |
|
111 |
// find X/Y position of domElement |
112 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
113 |
|
114 |
// create floating DIV above element |
115 |
this.div = document.createElement('div'); |
116 |
var style = this.div.style; |
117 |
style.position = 'absolute'; |
118 |
style.left = '0px'; |
119 |
style.top = '0px'; |
120 |
style.width = (box.width) + 'px'; |
121 |
style.height = box.height + 'px'; |
122 |
style.zIndex = zIndex; |
123 |
|
124 |
if ( typeof title != "undefined" && title != "" ) { |
125 |
this.div.title = title; |
126 |
} |
127 |
if ( box.width != 0 && box.height != 0 ) { |
128 |
this.sized = true; |
129 |
} |
130 |
|
131 |
// style.backgroundColor = '#f00'; // debug |
132 |
if ( this.domElement ) { |
133 |
this.domElement.appendChild(this.div); |
134 |
this.div.innerHTML = this.getHTML( box.width, box.height ); |
135 |
} |
136 |
}, |
137 |
|
138 |
positionElement: function() { |
139 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
140 |
var style = this.div.style; |
141 |
|
142 |
style.position = 'absolute'; |
143 |
//style.left = (this.domElement.offsetLeft)+'px'; |
144 |
//style.top = this.domElement.offsetTop+'px'; |
145 |
style.width = box.width + 'px'; |
146 |
style.height = box.height + 'px'; |
147 |
|
148 |
if ( box.width != 0 && box.height != 0 ) { |
149 |
this.sized = true; |
150 |
} else { |
151 |
return; |
152 |
} |
153 |
|
154 |
var flash = this.div.childNodes[0]; |
155 |
flash.width = box.width; |
156 |
flash.height = box.height; |
157 |
}, |
158 |
|
159 |
getHTML: function(width, height) { |
160 |
// return HTML for movie |
161 |
var html = ''; |
162 |
var flashvars = 'id=' + this.id + |
163 |
'&width=' + width + |
164 |
'&height=' + height; |
165 |
|
166 |
if (navigator.userAgent.match(/MSIE/)) { |
167 |
// IE gets an OBJECT tag |
168 |
var protocol = location.href.match(/^https/i) ? 'https://' : 'http://'; |
169 |
html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,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="'+ZeroClipboard_TableTools.moviePath+'" /><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>'; |
170 |
} |
171 |
else { |
172 |
// all other browsers get an EMBED tag |
173 |
html += '<embed id="'+this.movieId+'" src="'+ZeroClipboard_TableTools.moviePath+'" 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" />'; |
174 |
} |
175 |
return html; |
176 |
}, |
177 |
|
178 |
hide: function() { |
179 |
// temporarily hide floater offscreen |
180 |
if (this.div) { |
181 |
this.div.style.left = '-2000px'; |
182 |
} |
183 |
}, |
184 |
|
185 |
show: function() { |
186 |
// show ourselves after a call to hide() |
187 |
this.reposition(); |
188 |
}, |
189 |
|
190 |
destroy: function() { |
191 |
// destroy control and floater |
192 |
if (this.domElement && this.div) { |
193 |
this.hide(); |
194 |
this.div.innerHTML = ''; |
195 |
|
196 |
var body = document.getElementsByTagName('body')[0]; |
197 |
try { body.removeChild( this.div ); } catch(e) {;} |
198 |
|
199 |
this.domElement = null; |
200 |
this.div = null; |
201 |
} |
202 |
}, |
203 |
|
204 |
reposition: function(elem) { |
205 |
// reposition our floating div, optionally to new container |
206 |
// warning: container CANNOT change size, only position |
207 |
if (elem) { |
208 |
this.domElement = ZeroClipboard_TableTools.$(elem); |
209 |
if (!this.domElement) this.hide(); |
210 |
} |
211 |
|
212 |
if (this.domElement && this.div) { |
213 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
214 |
var style = this.div.style; |
215 |
style.left = '' + box.left + 'px'; |
216 |
style.top = '' + box.top + 'px'; |
217 |
} |
218 |
}, |
219 |
|
220 |
clearText: function() { |
221 |
// clear the text to be copy / saved |
222 |
this.clipText = ''; |
223 |
if (this.ready) this.movie.clearText(); |
224 |
}, |
225 |
|
226 |
appendText: function(newText) { |
227 |
// append text to that which is to be copied / saved |
228 |
this.clipText += newText; |
229 |
if (this.ready) { this.movie.appendText(newText) ;} |
230 |
}, |
231 |
|
232 |
setText: function(newText) { |
233 |
// set text to be copied to be copied / saved |
234 |
this.clipText = newText; |
235 |
if (this.ready) { this.movie.setText(newText) ;} |
236 |
}, |
237 |
|
238 |
setCharSet: function(charSet) { |
239 |
// set the character set (UTF16LE or UTF8) |
240 |
this.charSet = charSet; |
241 |
if (this.ready) { this.movie.setCharSet(charSet) ;} |
242 |
}, |
243 |
|
244 |
setBomInc: function(bomInc) { |
245 |
// set if the BOM should be included or not |
246 |
this.incBom = bomInc; |
247 |
if (this.ready) { this.movie.setBomInc(bomInc) ;} |
248 |
}, |
249 |
|
250 |
setFileName: function(newText) { |
251 |
// set the file name |
252 |
this.fileName = newText; |
253 |
if (this.ready) this.movie.setFileName(newText); |
254 |
}, |
255 |
|
256 |
setAction: function(newText) { |
257 |
// set action (save or copy) |
258 |
this.action = newText; |
259 |
if (this.ready) this.movie.setAction(newText); |
260 |
}, |
261 |
|
262 |
addEventListener: function(eventName, func) { |
263 |
// add user event listener for event |
264 |
// event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel |
265 |
eventName = eventName.toString().toLowerCase().replace(/^on/, ''); |
266 |
if (!this.handlers[eventName]) this.handlers[eventName] = []; |
267 |
this.handlers[eventName].push(func); |
268 |
}, |
269 |
|
270 |
setHandCursor: function(enabled) { |
271 |
// enable hand cursor (true), or default arrow cursor (false) |
272 |
this.handCursorEnabled = enabled; |
273 |
if (this.ready) this.movie.setHandCursor(enabled); |
274 |
}, |
275 |
|
276 |
setCSSEffects: function(enabled) { |
277 |
// enable or disable CSS effects on DOM container |
278 |
this.cssEffects = !!enabled; |
279 |
}, |
280 |
|
281 |
receiveEvent: function(eventName, args) { |
282 |
// receive event from flash |
283 |
eventName = eventName.toString().toLowerCase().replace(/^on/, ''); |
284 |
|
285 |
// special behavior for certain events |
286 |
switch (eventName) { |
287 |
case 'load': |
288 |
// movie claims it is ready, but in IE this isn't always the case... |
289 |
// bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function |
290 |
this.movie = document.getElementById(this.movieId); |
291 |
if (!this.movie) { |
292 |
var self = this; |
293 |
setTimeout( function() { self.receiveEvent('load', null); }, 1 ); |
294 |
return; |
295 |
} |
296 |
|
297 |
// firefox on pc needs a "kick" in order to set these in certain cases |
298 |
if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) { |
299 |
var self = this; |
300 |
setTimeout( function() { self.receiveEvent('load', null); }, 100 ); |
301 |
this.ready = true; |
302 |
return; |
303 |
} |
304 |
|
305 |
this.ready = true; |
306 |
this.movie.clearText(); |
307 |
this.movie.appendText( this.clipText ); |
308 |
this.movie.setFileName( this.fileName ); |
309 |
this.movie.setAction( this.action ); |
310 |
this.movie.setCharSet( this.charSet ); |
311 |
this.movie.setBomInc( this.incBom ); |
312 |
this.movie.setHandCursor( this.handCursorEnabled ); |
313 |
break; |
314 |
|
315 |
case 'mouseover': |
316 |
if (this.domElement && this.cssEffects) { |
317 |
//this.domElement.addClass('hover'); |
318 |
if (this.recoverActive) this.domElement.addClass('active'); |
319 |
} |
320 |
break; |
321 |
|
322 |
case 'mouseout': |
323 |
if (this.domElement && this.cssEffects) { |
324 |
this.recoverActive = false; |
325 |
if (this.domElement.hasClass('active')) { |
326 |
this.domElement.removeClass('active'); |
327 |
this.recoverActive = true; |
328 |
} |
329 |
//this.domElement.removeClass('hover'); |
330 |
} |
331 |
break; |
332 |
|
333 |
case 'mousedown': |
334 |
if (this.domElement && this.cssEffects) { |
335 |
this.domElement.addClass('active'); |
336 |
} |
337 |
break; |
338 |
|
339 |
case 'mouseup': |
340 |
if (this.domElement && this.cssEffects) { |
341 |
this.domElement.removeClass('active'); |
342 |
this.recoverActive = false; |
343 |
} |
344 |
break; |
345 |
} // switch eventName |
346 |
|
347 |
if (this.handlers[eventName]) { |
348 |
for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) { |
349 |
var func = this.handlers[eventName][idx]; |
350 |
|
351 |
if (typeof(func) == 'function') { |
352 |
// actual function reference |
353 |
func(this, args); |
354 |
} |
355 |
else if ((typeof(func) == 'object') && (func.length == 2)) { |
356 |
// PHP style object + method, i.e. [myObject, 'myMethod'] |
357 |
func[0][ func[1] ](this, args); |
358 |
} |
359 |
else if (typeof(func) == 'string') { |
360 |
// name of function |
361 |
window[func](this, args); |
362 |
} |
363 |
} // foreach event handler defined |
364 |
} // user defined handler for event |
365 |
} |
366 |
|
367 |
}; |