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 var classes = this.className.split(/\s+/);
22 for (var k = 0; k < classes.length; k++) {
23 if (classes[k] == name) { idx = k; k = classes.length; }
26 classes.splice( idx, 1 );
27 this.className = classes.join(' ');
31 thingy.hasClass = function(name) {
32 return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
38 setMoviePath: function(path) {
39 // set path to ZeroClipboard.swf
40 this.moviePath = path;
43 dispatch: function(id, eventName, args) {
44 // receive event from flash movie, send to client
45 var client = this.clients[id];
47 client.receiveEvent(eventName, args);
51 register: function(id, client) {
52 // register new client to receive events
53 this.clients[id] = client;
56 getDOMObjectPosition: function(obj, stopObj) {
57 // get absolute coordinates for dom element
61 width: obj.width ? obj.width : obj.offsetWidth,
62 height: obj.height ? obj.height : obj.offsetHeight
65 while (obj && (obj != stopObj)) {
66 info.left += obj.offsetLeft;
67 info.top += obj.offsetTop;
68 obj = obj.offsetParent;
74 Client: function(elem) {
75 // constructor for new simple upload client
79 this.id = ZeroClipboard.nextId++;
80 this.movieId = 'ZeroClipboardMovie_' + this.id;
82 // register client with singleton to receive flash events
83 ZeroClipboard.register(this.id, this);
86 if (elem) this.glue(elem);
90 ZeroClipboard.Client.prototype = {
92 id: 0, // unique ID for us
93 ready: false, // whether movie is ready to receive events or not
94 movie: null, // reference to movie object
95 clipText: '', // text to copy to clipboard
96 handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
97 cssEffects: true, // enable CSS mouse effects on dom container
98 handlers: null, // user event handlers
100 glue: function(elem, appendElem, stylesToAdd) {
101 // glue to DOM element
102 // elem can be ID or actual DOM element object
103 this.domElement = ZeroClipboard.$(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, 10) + 1;
111 if (typeof(appendElem) == 'string') {
112 appendElem = ZeroClipboard.$(appendElem);
114 else if (typeof(appendElem) == 'undefined') {
115 appendElem = document.getElementsByTagName('body')[0];
118 // find X/Y position of domElement
119 var box = ZeroClipboard.getDOMObjectPosition(this.domElement, appendElem);
121 // create floating DIV above element
122 this.div = document.createElement('div');
123 var style = this.div.style;
124 style.position = 'absolute';
125 style.left = '' + box.left + 'px';
126 style.top = '' + box.top + 'px';
127 style.width = '' + box.width + 'px';
128 style.height = '' + box.height + 'px';
129 style.zIndex = zIndex;
131 if (typeof(stylesToAdd) == 'object') {
132 for (addedStyle in stylesToAdd) {
133 style[addedStyle] = stylesToAdd[addedStyle];
137 // style.backgroundColor = '#f00'; // debug
139 appendElem.appendChild(this.div);
141 this.div.innerHTML = this.getHTML( box.width, box.height );
144 getHTML: function(width, height) {
145 // return HTML for movie
147 var flashvars = 'id=' + this.id +
151 if (navigator.userAgent.match(/MSIE/)) {
152 // IE gets an OBJECT tag
153 var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
154 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>';
157 // all other browsers get an EMBED tag
158 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" />';
164 // temporarily hide floater offscreen
166 this.div.style.left = '-2000px';
171 // show ourselves after a call to hide()
175 destroy: function() {
176 // destroy control and floater
177 if (this.domElement && this.div) {
179 this.div.innerHTML = '';
181 var body = document.getElementsByTagName('body')[0];
182 try { body.removeChild( this.div ); } catch(e) {;}
184 this.domElement = null;
189 reposition: function(elem) {
190 // reposition our floating div, optionally to new container
191 // warning: container CANNOT change size, only position
193 this.domElement = ZeroClipboard.$(elem);
194 if (!this.domElement) this.hide();
197 if (this.domElement && this.div) {
198 var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
199 var style = this.div.style;
200 style.left = '' + box.left + 'px';
201 style.top = '' + box.top + 'px';
205 setText: function(newText) {
206 // set text to be copied to clipboard
207 this.clipText = newText;
208 if (this.ready) this.movie.setText(newText);
211 addEventListener: function(eventName, func) {
212 // add user event listener for event
213 // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
214 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
215 if (!this.handlers[eventName]) this.handlers[eventName] = [];
216 this.handlers[eventName].push(func);
219 setHandCursor: function(enabled) {
220 // enable hand cursor (true), or default arrow cursor (false)
221 this.handCursorEnabled = enabled;
222 if (this.ready) this.movie.setHandCursor(enabled);
225 setCSSEffects: function(enabled) {
226 // enable or disable CSS effects on DOM container
227 this.cssEffects = !!enabled;
230 receiveEvent: function(eventName, args) {
231 // receive event from flash
232 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
234 // special behavior for certain events
237 // movie claims it is ready, but in IE this isn't always the case...
238 // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
239 this.movie = document.getElementById(this.movieId);
242 setTimeout( function() { self.receiveEvent('load', null); }, 1 );
246 // firefox on pc needs a "kick" in order to set these in certain cases
247 if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
249 setTimeout( function() { self.receiveEvent('load', null); }, 100 );
255 this.movie.setText( this.clipText );
256 this.movie.setHandCursor( this.handCursorEnabled );
260 if (this.domElement && this.cssEffects) {
261 this.domElement.addClass('hover');
262 if (this.recoverActive) this.domElement.addClass('active');
267 if (this.domElement && this.cssEffects) {
268 this.recoverActive = false;
269 if (this.domElement.hasClass('active')) {
270 this.domElement.removeClass('active');
271 this.recoverActive = true;
273 this.domElement.removeClass('hover');
278 if (this.domElement && this.cssEffects) {
279 this.domElement.addClass('active');
284 if (this.domElement && this.cssEffects) {
285 this.domElement.removeClass('active');
286 this.recoverActive = false;
289 } // switch eventName
291 if (this.handlers[eventName]) {
292 for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
293 var func = this.handlers[eventName][idx];
295 if (typeof(func) == 'function') {
296 // actual function reference
299 else if ((typeof(func) == 'object') && (func.length == 2)) {
300 // PHP style object + method, i.e. [myObject, 'myMethod']
301 func[0][ func[1] ](this, args);
303 else if (typeof(func) == 'string') {
305 window[func](this, args);
307 } // foreach event handler defined
308 } // user defined handler for event