1 // Simple Set Clipboard System
2 // Author: Joseph Huckaby
7 clients: {}, // registered upload clients on page, indexed by id
8 moviePath: 'ZeroClipboard.swf', // 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
57 info.left += obj.offsetLeft;
58 info.top += obj.offsetTop;
59 obj = obj.offsetParent;
65 Client: function(elem) {
66 // constructor for new simple upload client
70 this.id = ZeroClipboard.nextId++;
71 this.movieId = 'ZeroClipboardMovie_' + this.id;
73 // register client with singleton to receive flash events
74 ZeroClipboard.register(this.id, this);
77 if (elem) this.glue(elem);
81 ZeroClipboard.Client.prototype = {
83 id: 0, // unique ID for us
84 ready: false, // whether movie is ready to receive events or not
85 movie: null, // reference to movie object
86 clipText: '', // text to copy to clipboard
87 handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
88 cssEffects: true, // enable CSS mouse effects on dom container
89 handlers: null, // user event handlers
91 glue: function(elem) {
92 // glue to DOM element
93 // elem can be ID or actual DOM element object
94 this.domElement = ZeroClipboard.$(elem);
96 // float just above object, or zIndex 99 if dom element isn't set
98 if (this.domElement.style.zIndex) {
99 zIndex = parseInt(this.domElement.style.zIndex) + 1;
102 // find X/Y position of domElement
103 var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
105 // create floating DIV above element
106 this.div = document.createElement('div');
107 var style = this.div.style;
108 style.position = 'absolute';
109 style.left = '' + box.left + 'px';
110 style.top = '' + box.top + 'px';
111 style.width = '' + box.width + 'px';
112 style.height = '' + box.height + 'px';
113 style.zIndex = zIndex;
115 // style.backgroundColor = '#f00'; // debug
117 var body = document.getElementsByTagName('body')[0];
118 body.appendChild(this.div);
120 this.div.innerHTML = this.getHTML( box.width, box.height );
123 getHTML: function(width, height) {
124 // return HTML for movie
126 var flashvars = 'id=' + this.id +
130 if (navigator.userAgent.match(/MSIE/)) {
131 // IE gets an OBJECT tag
132 var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
133 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="'+ZeroClipboard.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>';
136 // all other browsers get an EMBED tag
137 html += '<embed id="'+this.movieId+'" src="'+ZeroClipboard.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" />';
143 // temporarily hide floater offscreen
145 this.div.style.left = '-2000px';
150 // show ourselves after a call to hide()
154 destroy: function() {
155 // destroy control and floater
156 if (this.domElement && this.div) {
158 this.div.innerHTML = '';
160 var body = document.getElementsByTagName('body')[0];
161 try { body.removeChild( this.div ); } catch(e) {;}
163 this.domElement = null;
168 reposition: function(elem) {
169 // reposition our floating div, optionally to new container
170 // warning: container CANNOT change size, only position
172 this.domElement = ZeroClipboard.$(elem);
173 if (!this.domElement) this.hide();
176 if (this.domElement && this.div) {
177 var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
178 var style = this.div.style;
179 style.left = '' + box.left + 'px';
180 style.top = '' + box.top + 'px';
184 setText: function(newText) {
185 // set text to be copied to clipboard
186 this.clipText = newText;
187 if (this.ready) this.movie.setText(newText);
190 addEventListener: function(eventName, func) {
191 // add user event listener for event
192 // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
193 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
194 if (!this.handlers[eventName]) this.handlers[eventName] = [];
195 this.handlers[eventName].push(func);
198 setHandCursor: function(enabled) {
199 // enable hand cursor (true), or default arrow cursor (false)
200 this.handCursorEnabled = enabled;
201 if (this.ready) this.movie.setHandCursor(enabled);
204 setCSSEffects: function(enabled) {
205 // enable or disable CSS effects on DOM container
206 this.cssEffects = !!enabled;
209 receiveEvent: function(eventName, args) {
210 // receive event from flash
211 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
213 // special behavior for certain events
216 // movie claims it is ready, but in IE this isn't always the case...
217 // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
218 this.movie = document.getElementById(this.movieId);
221 setTimeout( function() { self.receiveEvent('load', null); }, 1 );
225 // firefox on pc needs a "kick" in order to set these in certain cases
226 if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
228 setTimeout( function() { self.receiveEvent('load', null); }, 100 );
234 this.movie.setText( this.clipText );
235 this.movie.setHandCursor( this.handCursorEnabled );
239 if (this.domElement && this.cssEffects) {
240 this.domElement.addClass('hover');
241 if (this.recoverActive) this.domElement.addClass('active');
246 if (this.domElement && this.cssEffects) {
247 this.recoverActive = false;
248 if (this.domElement.hasClass('active')) {
249 this.domElement.removeClass('active');
250 this.recoverActive = true;
252 this.domElement.removeClass('hover');
257 if (this.domElement && this.cssEffects) {
258 this.domElement.addClass('active');
263 if (this.domElement && this.cssEffects) {
264 this.domElement.removeClass('active');
265 this.recoverActive = false;
268 } // switch eventName
270 if (this.handlers[eventName]) {
271 for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
272 var func = this.handlers[eventName][idx];
274 if (typeof(func) == 'function') {
275 // actual function reference
278 else if ((typeof(func) == 'object') && (func.length == 2)) {
279 // PHP style object + method, i.e. [myObject, 'myMethod']
280 func[0][ func[1] ](this, args);
282 else if (typeof(func) == 'string') {
284 window[func](this, args);
286 } // foreach event handler defined
287 } // user defined handler for event