OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
ZeroClipboard.js
1 /* ZeroClipboard 1.0.4
2  * Author: Joseph Huckaby
3  */
4 
5 var ZeroClipboard_TableTools = {
6 
7  version: "1.0.4-TableTools2",
8  clients: {}, // registered upload clients on page, indexed by id
9  moviePath: '', // URL to movie
10  nextId: 1, // ID of next movie
11 
12  $: function(thingy) {
13  // simple DOM lookup utility function
14  if (typeof(thingy) == 'string') {
15  thingy = document.getElementById(thingy);
16  }
17  if (!thingy.addClass) {
18  // extend element with a few useful methods
19  thingy.hide = function() { this.style.display = 'none'; };
20  thingy.show = function() { this.style.display = ''; };
21  thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
22  thingy.removeClass = function(name) {
23  this.className = this.className.replace( new RegExp("\\s*" + name + "\\s*"), " ").replace(/^\s+/, '').replace(/\s+$/, '');
24  };
25  thingy.hasClass = function(name) {
26  return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
27  };
28  }
29  return thingy;
30  },
31 
32  setMoviePath: function(path) {
33  // set path to ZeroClipboard.swf
34  this.moviePath = path;
35  },
36 
37  dispatch: function(id, eventName, args) {
38  // receive event from flash movie, send to client
39  var client = this.clients[id];
40  if (client) {
41  client.receiveEvent(eventName, args);
42  }
43  },
44 
45  register: function(id, client) {
46  // register new client to receive events
47  this.clients[id] = client;
48  },
49 
50  getDOMObjectPosition: function(obj) {
51  // get absolute coordinates for dom element
52  var info = {
53  left: 0,
54  top: 0,
55  width: obj.width ? obj.width : obj.offsetWidth,
56  height: obj.height ? obj.height : obj.offsetHeight
57  };
58 
59  if ( obj.style.width !== "" ) {
60  info.width = obj.style.width.replace("px","");
61  }
62 
63  if ( obj.style.height !== "" ) {
64  info.height = obj.style.height.replace("px","");
65  }
66 
67  while (obj) {
68  info.left += obj.offsetLeft;
69  info.top += obj.offsetTop;
70  obj = obj.offsetParent;
71  }
72 
73  return info;
74  },
75 
76  Client: function(elem) {
77  // constructor for new simple upload client
78  this.handlers = {};
79 
80  // unique ID
81  this.id = ZeroClipboard_TableTools.nextId++;
82  this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id;
83 
84  // register client with singleton to receive flash events
85  ZeroClipboard_TableTools.register(this.id, this);
86 
87  // create movie
88  if (elem) {
89  this.glue(elem);
90  }
91  }
92 };
93 
94 ZeroClipboard_TableTools.Client.prototype = {
95 
96  id: 0, // unique ID for us
97  ready: false, // whether movie is ready to receive events or not
98  movie: null, // reference to movie object
99  clipText: '', // text to copy to clipboard
100  fileName: '', // default file save name
101  action: 'copy', // action to perform
102  handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
103  cssEffects: true, // enable CSS mouse effects on dom container
104  handlers: null, // user event handlers
105  sized: false,
106 
107  glue: function(elem, title) {
108  // glue to DOM element
109  // elem can be ID or actual DOM element object
110  this.domElement = ZeroClipboard_TableTools.$(elem);
111 
112  // float just above object, or zIndex 99 if dom element isn't set
113  var zIndex = 99;
114  if (this.domElement.style.zIndex) {
115  zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
116  }
117 
118  // find X/Y position of domElement
119  var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
120 
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 = '0px';
126  style.top = '0px';
127  style.width = (box.width) + 'px';
128  style.height = box.height + 'px';
129  style.zIndex = zIndex;
130 
131  if ( typeof title != "undefined" && title !== "" ) {
132  this.div.title = title;
133  }
134  if ( box.width !== 0 && box.height !== 0 ) {
135  this.sized = true;
136  }
137 
138  // style.backgroundColor = '#f00'; // debug
139  if ( this.domElement ) {
140  this.domElement.appendChild(this.div);
141  this.div.innerHTML = this.getHTML( box.width, box.height ).replace(/&/g, '&');
142  }
143  },
144 
145  positionElement: function() {
146  var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
147  var style = this.div.style;
148 
149  style.position = 'absolute';
150  //style.left = (this.domElement.offsetLeft)+'px';
151  //style.top = this.domElement.offsetTop+'px';
152  style.width = box.width + 'px';
153  style.height = box.height + 'px';
154 
155  if ( box.width !== 0 && box.height !== 0 ) {
156  this.sized = true;
157  } else {
158  return;
159  }
160 
161  var flash = this.div.childNodes[0];
162  flash.width = box.width;
163  flash.height = box.height;
164  },
165 
166  getHTML: function(width, height) {
167  // return HTML for movie
168  var html = '';
169  var flashvars = 'id=' + this.id +
170  '&width=' + width +
171  '&height=' + height;
172 
173  if (navigator.userAgent.match(/MSIE/)) {
174  // IE gets an OBJECT tag
175  var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
176  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>';
177  }
178  else {
179  // all other browsers get an EMBED tag
180  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" />';
181  }
182  return html;
183  },
184 
185  hide: function() {
186  // temporarily hide floater offscreen
187  if (this.div) {
188  this.div.style.left = '-2000px';
189  }
190  },
191 
192  show: function() {
193  // show ourselves after a call to hide()
194  this.reposition();
195  },
196 
197  destroy: function() {
198  // destroy control and floater
199  if (this.domElement && this.div) {
200  this.hide();
201  this.div.innerHTML = '';
202 
203  var body = document.getElementsByTagName('body')[0];
204  try { body.removeChild( this.div ); } catch(e) {}
205 
206  this.domElement = null;
207  this.div = null;
208  }
209  },
210 
211  reposition: function(elem) {
212  // reposition our floating div, optionally to new container
213  // warning: container CANNOT change size, only position
214  if (elem) {
215  this.domElement = ZeroClipboard_TableTools.$(elem);
216  if (!this.domElement) {
217  this.hide();
218  }
219  }
220 
221  if (this.domElement && this.div) {
222  var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
223  var style = this.div.style;
224  style.left = '' + box.left + 'px';
225  style.top = '' + box.top + 'px';
226  }
227  },
228 
229  clearText: function() {
230  // clear the text to be copy / saved
231  this.clipText = '';
232  if (this.ready) {
233  this.movie.clearText();
234  }
235  },
236 
237  appendText: function(newText) {
238  // append text to that which is to be copied / saved
239  this.clipText += newText;
240  if (this.ready) { this.movie.appendText(newText) ;}
241  },
242 
243  setText: function(newText) {
244  // set text to be copied to be copied / saved
245  this.clipText = newText;
246  if (this.ready) { this.movie.setText(newText) ;}
247  },
248 
249  setCharSet: function(charSet) {
250  // set the character set (UTF16LE or UTF8)
251  this.charSet = charSet;
252  if (this.ready) { this.movie.setCharSet(charSet) ;}
253  },
254 
255  setBomInc: function(bomInc) {
256  // set if the BOM should be included or not
257  this.incBom = bomInc;
258  if (this.ready) { this.movie.setBomInc(bomInc) ;}
259  },
260 
261  setFileName: function(newText) {
262  // set the file name
263  this.fileName = newText;
264  if (this.ready) {
265  this.movie.setFileName(newText);
266  }
267  },
268 
269  setAction: function(newText) {
270  // set action (save or copy)
271  this.action = newText;
272  if (this.ready) {
273  this.movie.setAction(newText);
274  }
275  },
276 
277  addEventListener: function(eventName, func) {
278  // add user event listener for event
279  // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
280  eventName = eventName.toString().toLowerCase().replace(/^on/, '');
281  if (!this.handlers[eventName]) {
282  this.handlers[eventName] = [];
283  }
284  this.handlers[eventName].push(func);
285  },
286 
287  setHandCursor: function(enabled) {
288  // enable hand cursor (true), or default arrow cursor (false)
289  this.handCursorEnabled = enabled;
290  if (this.ready) {
291  this.movie.setHandCursor(enabled);
292  }
293  },
294 
295  setCSSEffects: function(enabled) {
296  // enable or disable CSS effects on DOM container
297  this.cssEffects = !!enabled;
298  },
299 
300  receiveEvent: function(eventName, args) {
301  var self;
302 
303  // receive event from flash
304  eventName = eventName.toString().toLowerCase().replace(/^on/, '');
305 
306  // special behavior for certain events
307  switch (eventName) {
308  case 'load':
309  // movie claims it is ready, but in IE this isn't always the case...
310  // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
311  this.movie = document.getElementById(this.movieId);
312  if (!this.movie) {
313  self = this;
314  setTimeout( function() { self.receiveEvent('load', null); }, 1 );
315  return;
316  }
317 
318  // firefox on pc needs a "kick" in order to set these in certain cases
319  if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
320  self = this;
321  setTimeout( function() { self.receiveEvent('load', null); }, 100 );
322  this.ready = true;
323  return;
324  }
325 
326  this.ready = true;
327  this.movie.clearText();
328  this.movie.appendText( this.clipText );
329  this.movie.setFileName( this.fileName );
330  this.movie.setAction( this.action );
331  this.movie.setCharSet( this.charSet );
332  this.movie.setBomInc( this.incBom );
333  this.movie.setHandCursor( this.handCursorEnabled );
334  break;
335 
336  case 'mouseover':
337  if (this.domElement && this.cssEffects) {
338  //this.domElement.addClass('hover');
339  if (this.recoverActive) {
340  this.domElement.addClass('active');
341  }
342  }
343  break;
344 
345  case 'mouseout':
346  if (this.domElement && this.cssEffects) {
347  this.recoverActive = false;
348  if (this.domElement.hasClass('active')) {
349  this.domElement.removeClass('active');
350  this.recoverActive = true;
351  }
352  //this.domElement.removeClass('hover');
353  }
354  break;
355 
356  case 'mousedown':
357  if (this.domElement && this.cssEffects) {
358  this.domElement.addClass('active');
359  }
360  break;
361 
362  case 'mouseup':
363  if (this.domElement && this.cssEffects) {
364  this.domElement.removeClass('active');
365  this.recoverActive = false;
366  }
367  break;
368  } // switch eventName
369 
370  if (this.handlers[eventName]) {
371  for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
372  var func = this.handlers[eventName][idx];
373 
374  if (typeof(func) == 'function') {
375  // actual function reference
376  func(this, args);
377  }
378  else if ((typeof(func) == 'object') && (func.length == 2)) {
379  // PHP style object + method, i.e. [myObject, 'myMethod']
380  func[0][ func[1] ](this, args);
381  }
382  else if (typeof(func) == 'string') {
383  // name of function
384  window[func](this, args);
385  }
386  } // foreach event handler defined
387  } // user defined handler for event
388  }
389 
390 };
391 
392 // For the Flash binding to work, ZeroClipboard_TableTools must be on the global
393 // object list
394 window.ZeroClipboard_TableTools = ZeroClipboard_TableTools;