1 // Simple Set Clipboard System
2 // Author: Joseph Huckaby
4 var ZeroClipboard_TableTools = {
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
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+$/, '');
22 thingy.hasClass = function(name) {
23 return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
29 setMoviePath: function(path) {
30 // set path to ZeroClipboard.swf
31 this.moviePath = path;
34 dispatch: function(id, eventName, args) {
35 // receive event from flash movie, send to client
36 var client = this.clients[id];
38 client.receiveEvent(eventName, args);
42 register: function(id, client) {
43 // register new client to receive events
44 this.clients[id] = client;
47 getDOMObjectPosition: function(obj) {
48 // get absolute coordinates for dom element
52 width: obj.width ? obj.width : obj.offsetWidth,
53 height: obj.height ? obj.height : obj.offsetHeight
56 if ( obj.style.width != "" )
57 info.width = obj.style.width.replace("px","");
59 if ( obj.style.height != "" )
60 info.height = obj.style.height.replace("px","");
63 info.left += obj.offsetLeft;
64 info.top += obj.offsetTop;
65 obj = obj.offsetParent;
71 Client: function(elem) {
72 // constructor for new simple upload client
76 this.id = ZeroClipboard_TableTools.nextId++;
77 this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id;
79 // register client with singleton to receive flash events
80 ZeroClipboard_TableTools.register(this.id, this);
83 if (elem) this.glue(elem);
87 ZeroClipboard_TableTools.Client.prototype = {
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
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);
105 // float just above object, or zIndex 99 if dom element isn't set
107 if (this.domElement.style.zIndex) {
108 zIndex = parseInt(this.domElement.style.zIndex) + 1;
111 // find X/Y position of domElement
112 var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
114 // create floating DIV above element
115 this.div = document.createElement('div');
116 var style = this.div.style;
117 style.position = 'absolute';
120 style.width = (box.width) + 'px';
121 style.height = box.height + 'px';
122 style.zIndex = zIndex;
124 if ( typeof title != "undefined" && title != "" ) {
125 this.div.title = title;
127 if ( box.width != 0 && box.height != 0 ) {
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 );
138 positionElement: function() {
139 var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
140 var style = this.div.style;
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';
148 if ( box.width != 0 && box.height != 0 ) {
154 var flash = this.div.childNodes[0];
155 flash.width = box.width;
156 flash.height = box.height;
159 getHTML: function(width, height) {
160 // return HTML for movie
162 var flashvars = 'id=' + this.id +
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>';
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" />';
179 // temporarily hide floater offscreen
181 this.div.style.left = '-2000px';
186 // show ourselves after a call to hide()
190 destroy: function() {
191 // destroy control and floater
192 if (this.domElement && this.div) {
194 this.div.innerHTML = '';
196 var body = document.getElementsByTagName('body')[0];
197 try { body.removeChild( this.div ); } catch(e) {;}
199 this.domElement = null;
204 reposition: function(elem) {
205 // reposition our floating div, optionally to new container
206 // warning: container CANNOT change size, only position
208 this.domElement = ZeroClipboard_TableTools.$(elem);
209 if (!this.domElement) this.hide();
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';
220 clearText: function() {
221 // clear the text to be copy / saved
223 if (this.ready) this.movie.clearText();
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) ;}
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) ;}
238 setCharSet: function(charSet) {
239 // set the character set (UTF16LE or UTF8)
240 this.charSet = charSet;
241 if (this.ready) { this.movie.setCharSet(charSet) ;}
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) ;}
250 setFileName: function(newText) {
252 this.fileName = newText;
253 if (this.ready) this.movie.setFileName(newText);
256 setAction: function(newText) {
257 // set action (save or copy)
258 this.action = newText;
259 if (this.ready) this.movie.setAction(newText);
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);
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);
276 setCSSEffects: function(enabled) {
277 // enable or disable CSS effects on DOM container
278 this.cssEffects = !!enabled;
281 receiveEvent: function(eventName, args) {
282 // receive event from flash
283 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
285 // special behavior for certain events
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);
293 setTimeout( function() { self.receiveEvent('load', null); }, 1 );
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/)) {
300 setTimeout( function() { self.receiveEvent('load', null); }, 100 );
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 );
316 if (this.domElement && this.cssEffects) {
317 //this.domElement.addClass('hover');
318 if (this.recoverActive) this.domElement.addClass('active');
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;
329 //this.domElement.removeClass('hover');
334 if (this.domElement && this.cssEffects) {
335 this.domElement.addClass('active');
340 if (this.domElement && this.cssEffects) {
341 this.domElement.removeClass('active');
342 this.recoverActive = false;
345 } // switch eventName
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];
351 if (typeof(func) == 'function') {
352 // actual function reference
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);
359 else if (typeof(func) == 'string') {
361 window[func](this, args);
363 } // foreach event handler defined
364 } // user defined handler for event