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