ZeroClipboard.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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)
  175. this.movie.setText(newText);
  176. },
  177. addEventListener: function(eventName, func) {
  178. // add user event listener for event
  179. // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
  180. eventName = eventName.toString().toLowerCase().replace(/^on/, '');
  181. if (!this.handlers[eventName]) this.handlers[eventName] = [];
  182. this.handlers[eventName].push(func);
  183. },
  184. setHandCursor: function(enabled) {
  185. // enable hand cursor (true), or default arrow cursor (false)
  186. this.handCursorEnabled = enabled;
  187. if (this.ready) this.movie.setHandCursor(enabled);
  188. },
  189. setCSSEffects: function(enabled) {
  190. // enable or disable CSS effects on DOM container
  191. this.cssEffects = !!enabled;
  192. },
  193. receiveEvent: function(eventName, args) {
  194. // receive event from flash
  195. eventName = eventName.toString().toLowerCase().replace(/^on/, '');
  196. // special behavior for certain events
  197. switch (eventName) {
  198. case 'load':
  199. // movie claims it is ready, but in IE this isn't always the case...
  200. // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
  201. this.movie = document.getElementById(this.movieId);
  202. if (!this.movie) {
  203. var self = this;
  204. setTimeout( function() { self.receiveEvent('load', null); }, 1 );
  205. return;
  206. }
  207. // firefox on pc needs a "kick" in order to set these in certain cases
  208. if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
  209. var self = this;
  210. setTimeout( function() { self.receiveEvent('load', null); }, 100 );
  211. this.ready = true;
  212. return;
  213. }
  214. this.ready = true;
  215. this.movie.setText( this.clipText );
  216. this.movie.setHandCursor( this.handCursorEnabled );
  217. break;
  218. case 'mouseover':
  219. if (this.domElement && this.cssEffects) {
  220. this.domElement.addClass('hover');
  221. if (this.recoverActive) this.domElement.addClass('active');
  222. }
  223. break;
  224. case 'mouseout':
  225. if (this.domElement && this.cssEffects) {
  226. this.recoverActive = false;
  227. if (this.domElement.hasClass('active')) {
  228. this.domElement.removeClass('active');
  229. this.recoverActive = true;
  230. }
  231. this.domElement.removeClass('hover');
  232. }
  233. break;
  234. case 'mousedown':
  235. if (this.domElement && this.cssEffects) {
  236. this.domElement.addClass('active');
  237. }
  238. break;
  239. case 'mouseup':
  240. if (this.domElement && this.cssEffects) {
  241. this.domElement.removeClass('active');
  242. this.recoverActive = false;
  243. }
  244. break;
  245. } // switch eventName
  246. if (this.handlers[eventName]) {
  247. for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
  248. var func = this.handlers[eventName][idx];
  249. if (typeof(func) == 'function') {
  250. // actual function reference
  251. func(this, args);
  252. }
  253. else if ((typeof(func) == 'object') && (func.length == 2)) {
  254. // PHP style object + method, i.e. [myObject, 'myMethod']
  255. func[0][ func[1] ](this, args);
  256. }
  257. else if (typeof(func) == 'string') {
  258. // name of function
  259. window[func](this, args);
  260. }
  261. } // foreach event handler defined
  262. } // user defined handler for event
  263. }
  264. };