Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2629 vikas 1
// Simple Set Clipboard System
2
// Author: Joseph Huckaby
3
 
4
var ZeroClipboard = {
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.nextId++;
77
		this.movieId = 'ZeroClipboardMovie_' + this.id;
78
 
79
		// register client with singleton to receive flash events
80
		ZeroClipboard.register(this.id, this);
81
 
82
		// create movie
83
		if (elem) this.glue(elem);
84
	}
85
};
86
 
87
ZeroClipboard.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.$(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.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 = (this.domElement.offsetLeft)+'px';
119
		//style.left = (this.domElement.offsetLeft+2)+'px';
120
		style.top = this.domElement.offsetTop+'px';
121
		style.width = (box.width) + 'px';
122
		//style.width = (box.width-4) + 'px';
123
		style.height = box.height + 'px';
124
		style.zIndex = zIndex;
125
		if ( typeof title != "undefined" && title != "" ) {
126
			this.div.title = title;
127
		}
128
		if ( box.width != 0 && box.height != 0 ) {
129
			this.sized = true;
130
		}
131
 
132
		// style.backgroundColor = '#f00'; // debug
133
		this.domElement.parentNode.appendChild(this.div);
134
 
135
		this.div.innerHTML = this.getHTML( box.width, box.height );
136
	},
137
 
138
	positionElement: function() {
139
		var box = ZeroClipboard.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
		}
151
 
152
		var flash = this.div.childNodes[0];
153
		flash.width = box.width;
154
		flash.height = box.height;
155
	},
156
 
157
	getHTML: function(width, height) {
158
		// return HTML for movie
159
		var html = '';
160
		var flashvars = 'id=' + this.id + 
161
			'&width=' + width + 
162
			'&height=' + height;
163
 
164
		if (navigator.userAgent.match(/MSIE/)) {
165
			// IE gets an OBJECT tag
166
			var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
167
			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.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>';
168
		}
169
		else {
170
			// all other browsers get an EMBED tag
171
			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" />';
172
		}
173
		return html;
174
	},
175
 
176
	hide: function() {
177
		// temporarily hide floater offscreen
178
		if (this.div) {
179
			this.div.style.left = '-2000px';
180
		}
181
	},
182
 
183
	show: function() {
184
		// show ourselves after a call to hide()
185
		this.reposition();
186
	},
187
 
188
	destroy: function() {
189
		// destroy control and floater
190
		if (this.domElement && this.div) {
191
			this.hide();
192
			this.div.innerHTML = '';
193
 
194
			var body = document.getElementsByTagName('body')[0];
195
			try { body.removeChild( this.div ); } catch(e) {;}
196
 
197
			this.domElement = null;
198
			this.div = null;
199
		}
200
	},
201
 
202
	reposition: function(elem) {
203
		// reposition our floating div, optionally to new container
204
		// warning: container CANNOT change size, only position
205
		if (elem) {
206
			this.domElement = ZeroClipboard.$(elem);
207
			if (!this.domElement) this.hide();
208
		}
209
 
210
		if (this.domElement && this.div) {
211
			var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
212
			var style = this.div.style;
213
			style.left = '' + box.left + 'px';
214
			style.top = '' + box.top + 'px';
215
		}
216
	},
217
 
218
	clearText: function() {
219
		// clear the text to be copy / saved
220
		this.clipText = '';
221
		if (this.ready) this.movie.clearText();
222
	},
223
 
224
	appendText: function(newText) {
225
		// append text to that which is to be copied / saved
226
		this.clipText += newText;
227
		if (this.ready) { this.movie.appendText(newText) ;}
228
	},
229
 
230
	setText: function(newText) {
231
		// set text to be copied to be copied / saved
232
		this.clipText = newText;
233
		if (this.ready) { this.movie.setText(newText) ;}
234
	},
235
 
236
	setCharSet: function(charSet) {
237
		// set the character set (UTF16LE or UTF8)
238
		this.charSet = charSet;
239
		if (this.ready) { this.movie.setCharSet(charSet) ;}
240
	},
241
 
242
	setBomInc: function(bomInc) {
243
		// set if the BOM should be included or not
244
		this.incBom = bomInc;
245
		if (this.ready) { this.movie.setBomInc(bomInc) ;}
246
	},
247
 
248
	setFileName: function(newText) {
249
		// set the file name
250
		this.fileName = newText;
251
		if (this.ready) this.movie.setFileName(newText);
252
	},
253
 
254
	setAction: function(newText) {
255
		// set action (save or copy)
256
		this.action = newText;
257
		if (this.ready) this.movie.setAction(newText);
258
	},
259
 
260
	addEventListener: function(eventName, func) {
261
		// add user event listener for event
262
		// event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
263
		eventName = eventName.toString().toLowerCase().replace(/^on/, '');
264
		if (!this.handlers[eventName]) this.handlers[eventName] = [];
265
		this.handlers[eventName].push(func);
266
	},
267
 
268
	setHandCursor: function(enabled) {
269
		// enable hand cursor (true), or default arrow cursor (false)
270
		this.handCursorEnabled = enabled;
271
		if (this.ready) this.movie.setHandCursor(enabled);
272
	},
273
 
274
	setCSSEffects: function(enabled) {
275
		// enable or disable CSS effects on DOM container
276
		this.cssEffects = !!enabled;
277
	},
278
 
279
	receiveEvent: function(eventName, args) {
280
		// receive event from flash
281
		eventName = eventName.toString().toLowerCase().replace(/^on/, '');
282
 
283
		// special behavior for certain events
284
		switch (eventName) {
285
			case 'load':
286
				// movie claims it is ready, but in IE this isn't always the case...
287
				// bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
288
				this.movie = document.getElementById(this.movieId);
289
				if (!this.movie) {
290
					var self = this;
291
					setTimeout( function() { self.receiveEvent('load', null); }, 1 );
292
					return;
293
				}
294
 
295
				// firefox on pc needs a "kick" in order to set these in certain cases
296
				if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
297
					var self = this;
298
					setTimeout( function() { self.receiveEvent('load', null); }, 100 );
299
					this.ready = true;
300
					return;
301
				}
302
 
303
				this.ready = true;
304
				this.movie.clearText();
305
				this.movie.appendText( this.clipText );
306
				this.movie.setFileName( this.fileName );
307
				this.movie.setAction( this.action );
308
				this.movie.setCharSet( this.charSet );
309
				this.movie.setBomInc( this.incBom );
310
				this.movie.setHandCursor( this.handCursorEnabled );
311
				break;
312
 
313
			case 'mouseover':
314
				if (this.domElement && this.cssEffects) {
315
					//this.domElement.addClass('hover');
316
					if (this.recoverActive) this.domElement.addClass('active');
317
				}
318
				break;
319
 
320
			case 'mouseout':
321
				if (this.domElement && this.cssEffects) {
322
					this.recoverActive = false;
323
					if (this.domElement.hasClass('active')) {
324
						this.domElement.removeClass('active');
325
						this.recoverActive = true;
326
					}
327
					//this.domElement.removeClass('hover');
328
				}
329
				break;
330
 
331
			case 'mousedown':
332
				if (this.domElement && this.cssEffects) {
333
					this.domElement.addClass('active');
334
				}
335
				break;
336
 
337
			case 'mouseup':
338
				if (this.domElement && this.cssEffects) {
339
					this.domElement.removeClass('active');
340
					this.recoverActive = false;
341
				}
342
				break;
343
		} // switch eventName
344
 
345
		if (this.handlers[eventName]) {
346
			for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
347
				var func = this.handlers[eventName][idx];
348
 
349
				if (typeof(func) == 'function') {
350
					// actual function reference
351
					func(this, args);
352
				}
353
				else if ((typeof(func) == 'object') && (func.length == 2)) {
354
					// PHP style object + method, i.e. [myObject, 'myMethod']
355
					func[0][ func[1] ](this, args);
356
				}
357
				else if (typeof(func) == 'string') {
358
					// name of function
359
					window[func](this, args);
360
				}
361
			} // foreach event handler defined
362
		} // user defined handler for event
363
	}
364
 
365
};