ZeroClipboard.js 9.6 KB

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