ZeroClipboard.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Simple Set Clipboard System
  2. // Author: Joseph Huckaby
  3. // Download by http://www.codefans.net
  4. var ZeroClipboard = {
  5. version: "1.0.7",
  6. clients: {}, // registered upload clients on page, indexed by id
  7. moviePath: './static/flash/ZeroClipboard.swf', // URL to movie
  8. nextId: 1, // ID of next movie
  9. $a: function(thingy) {
  10. // simple DOM lookup utility function
  11. if (typeof(thingy) == 'string') thingy = document.getElementById(thingy);
  12. if (!thingy.addClass) {
  13. // extend element with a few useful methods
  14. thingy.hide = function() { this.style.display = 'none'; };
  15. thingy.show = function() { this.style.display = ''; };
  16. thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
  17. thingy.removeClass = function(name) {
  18. var classes = this.className.split(/\s+/);
  19. var idx = -1;
  20. for (var k = 0; k < classes.length; k++) {
  21. if (classes[k] == name) { idx = k; k = classes.length; }
  22. }
  23. if (idx > -1) {
  24. classes.splice( idx, 1 );
  25. this.className = classes.join(' ');
  26. }
  27. return this;
  28. };
  29. thingy.hasClass = function(name) {
  30. return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
  31. };
  32. }
  33. return thingy;
  34. },
  35. setMoviePath: function(path) {
  36. // set path to ZeroClipboard.swf
  37. this.moviePath = path;
  38. },
  39. dispatch: function(id, eventName, args) {
  40. // receive event from flash movie, send to client
  41. var client = this.clients[id];
  42. if (client) {
  43. client.receiveEvent(eventName, args);
  44. }
  45. },
  46. register: function(id, client) {
  47. // register new client to receive events
  48. this.clients[id] = client;
  49. },
  50. getDOMObjectPosition: function(obj, stopObj) {
  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. while (obj && (obj != stopObj)) {
  59. info.left += obj.offsetLeft;
  60. info.top += obj.offsetTop;
  61. obj = obj.offsetParent;
  62. }
  63. return info;
  64. },
  65. Client: function(elem) {
  66. // constructor for new simple upload client
  67. this.handlers = {};
  68. // unique ID
  69. this.id = ZeroClipboard.nextId++;
  70. this.movieId = 'ZeroClipboardMovie_' + this.id;
  71. // register client with singleton to receive flash events
  72. ZeroClipboard.register(this.id, this);
  73. // create movie
  74. if (elem) this.glue(elem);
  75. }
  76. };
  77. ZeroClipboard.Client.prototype = {
  78. id: 0, // unique ID for us
  79. ready: false, // whether movie is ready to receive events or not
  80. movie: null, // reference to movie object
  81. clipText: '', // text to copy to clipboard
  82. handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
  83. cssEffects: true, // enable CSS mouse effects on dom container
  84. handlers: null, // user event handlers
  85. glue: function(elem, appendElem, stylesToAdd) {
  86. // glue to DOM element
  87. // elem can be ID or actual DOM element object
  88. this.domElement = ZeroClipboard.$a(elem);
  89. // float just above object, or zIndex 99 if dom element isn't set
  90. var zIndex = 99;
  91. if (this.domElement.style.zIndex) {
  92. zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
  93. }
  94. if (typeof(appendElem) == 'string') {
  95. appendElem = ZeroClipboard.$a(appendElem);
  96. }
  97. else if (typeof(appendElem) == 'undefined') {
  98. appendElem = document.getElementsByTagName('body')[0];
  99. }
  100. // find X/Y position of domElement
  101. var box = ZeroClipboard.getDOMObjectPosition(this.domElement, appendElem);
  102. // create floating DIV above element
  103. this.div = document.createElement('div');
  104. var style = this.div.style;
  105. style.position = 'absolute';
  106. style.left = '' + box.left + 'px';
  107. style.top = '' + box.top + 'px';
  108. style.width = '' + box.width + 'px';
  109. style.height = '' + box.height + 'px';
  110. style.zIndex = zIndex;
  111. if (typeof(stylesToAdd) == 'object') {
  112. for (addedStyle in stylesToAdd) {
  113. style[addedStyle] = stylesToAdd[addedStyle];
  114. }
  115. }
  116. // style.backgroundColor = '#f00'; // debug
  117. appendElem.appendChild(this.div);
  118. this.div.innerHTML = this.getHTML( box.width, box.height );
  119. },
  120. getHTML: function(width, height) {
  121. // return HTML for movie
  122. var html = '';
  123. var flashvars = 'id=' + this.id +
  124. '&width=' + width +
  125. '&height=' + height;
  126. if (navigator.userAgent.match(/MSIE/)) {
  127. // IE gets an OBJECT tag
  128. var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
  129. 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>';
  130. }
  131. else {
  132. // all other browsers get an EMBED tag
  133. 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" />';
  134. }
  135. return html;
  136. },
  137. hide: function() {
  138. // temporarily hide floater offscreen
  139. if (this.div) {
  140. this.div.style.left = '-2000px';
  141. }
  142. },
  143. show: function() {
  144. // show ourselves after a call to hide()
  145. this.reposition();
  146. },
  147. destroy: function() {
  148. // destroy control and floater
  149. if (this.domElement && this.div) {
  150. this.hide();
  151. this.div.innerHTML = '';
  152. var body = document.getElementsByTagName('body')[0];
  153. try { body.removeChild( this.div ); } catch(e) {;}
  154. this.domElement = null;
  155. this.div = null;
  156. }
  157. },
  158. reposition: function(elem) {
  159. // reposition our floating div, optionally to new container
  160. // warning: container CANNOT change size, only position
  161. if (elem) {
  162. this.domElement = ZeroClipboard.$(elem);
  163. if (!this.domElement) this.hide();
  164. }
  165. if (this.domElement && this.div) {
  166. var box = ZeroClipboard.getDOMObjectPosition(this.domElement);
  167. var style = this.div.style;
  168. style.left = '' + box.left + 'px';
  169. style.top = '' + box.top + 'px';
  170. }
  171. },
  172. setText: function(newText) {
  173. // set text to be copied to clipboard
  174. this.clipText = newText;
  175. if (this.ready) 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. };