// Cross-Browser copy var clip = null; function glue(element, fromId) { // move flash on element, set the copy content if (typeof(element) == 'string') { element = $("#" + element); } else if (typeof(element) == 'object') { element = $(element); } else { return; } if ($.browser.msie) { element.click(function () { window.clipboardData.setData("Text",$("#"+fromId).val()); $("#"+fromId).select(); }); element.mouseover(function() { element.addClass('hover'); }); element.mousedown(function() { element.addClass('active'); }); element.mouseout(function() { if (element.hasClass('active')) element.removeClass('active'); element.removeClass('hover'); }); element.mouseup(function() { element.removeClass('active'); }); } else { if (!clip) { clip = new ZeroClipboard.Client(); } clip.setText($("#"+fromId)); clip.glueToElement(element); } } var ZeroClipboard = { clients: {}, // registered upload clients on page, indexed by id moviePath: 'https://secure.successfulmatch.com/images/ZeroClipboard.swf', // URL to movie nextId: 1, // ID of next movie setMoviePath: function(path) { // set path to ZeroClipboard.swf this.moviePath = path; }, dispatch: function(id, eventName, args) { // receive event from flash movie, send to client var client = this.clients[id]; if (client) { client.receiveEvent(eventName, args); } }, register: function(id, client) { // register new client to receive events this.clients[id] = client; }, getDOMObjectPosition: function(obj, stopObj) { // get absolute coordinates for dom element var info = { left: 0, top: 0, width: obj.width ? obj.width : obj.offsetWidth, height: obj.height ? obj.height : obj.offsetHeight }; while (obj && (obj != stopObj)) { info.left += obj.offsetLeft; info.top += obj.offsetTop; obj = obj.offsetParent; } return info; }, Client: function(elem) { // constructor for new simple upload client this.handlers = {}; // unique ID this.id = ZeroClipboard.nextId++; this.movieId = 'ZeroClipboardMovie_' + this.id; // register client with singleton to receive flash events ZeroClipboard.register(this.id, this); // create movie this.init(); } }; ZeroClipboard.Client.prototype = { id: 0, // unique ID for us ready: false, // whether movie is ready to receive events or not movie: null, // reference to movie object clipText: '', // text to copy to clipboard handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor isSelected: true, // after copy, the text will be selected cssEffects: true, // enable CSS mouse effects on dom container handlers: null, // user event handlers init: function(appendElemId) { this.appendElement = appendElemId ? $("#" + appendElemId) : $("body").eq(0); // create floating DIV out of container var newDiv = $('
'); newDiv.css({ 'position': 'absolute', 'left': '-5000px', 'top': '-5000px', 'width': '1px', 'height': '1px' }); newDiv.append(this.getHTML(500, 500)); this.appendElement.append(newDiv); this.div = newDiv; }, glueToElement: function(element, cssObj) { // glue to DOM element if (!element) return; this.element = element; // float just above object, or zIndex 99 if dom element isn't set var zIndex = 99; if (this.element.css("zIndex")) { zIndex = parseInt(this.element.css("zIndex")) + 1; } // find X/Y position of domElement var box = ZeroClipboard.getDOMObjectPosition(this.element.get(0), this.appendElement.get(0)); // move floating DIV above element if (this.div) { var css = { 'position': 'absolute', 'left': box.left + 'px', 'top': box.top + 'px', 'width': box.width + 'px', 'height': box.height + 'px', 'zIndex': zIndex }; this.div.css(css); if (typeof(cssObj) == 'object') { this.div.css(cssObj); } // change flash's size to same as glued element $("#" + this.movieId).css({ 'width': box.width + 'px', 'height': box.height + 'px' }); } }, getHTML: function(width, height) { // return HTML for movie var html = ''; var flashvars = 'id=' + this.id + '&width=' + width + '&height=' + height; if ($.browser.msie) { // IE gets an OBJECT tag var protocol = location.href.match(/^https/i) ? 'https://' : 'http://'; html += ''; } else { // all other browsers get an EMBED tag html += ''; } return html; }, hide: function() { // temporarily hide floater offscreen if (this.div) { this.div.css('left', '-5000px'); } }, show: function() { // show ourselves after a call to hide() this.reposition(); }, destroy: function() { // destroy control and floater if (this.element && this.div) { this.hide(); this.div.empty(); this.div.remove(); this.element = null; this.div = null; } }, reposition: function() { // reposition our floating div if (this.element && this.div) { var box = ZeroClipboard.getDOMObjectPosition(this.element.get(0), this.appendElement.get(0)); this.div.css({ 'left': box.left + 'px', 'top': box.top + 'px' }); } }, setText: function(from) { // set text to be copied to clipboard if (typeof(from) == "object") { this.from = from; this.clipText = from.val(); } else{ this.clipText = from; } if (this.ready) this.movie.setText(this.clipText); }, addEventListener: function(eventName, func) { // add user event listener for event // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel eventName = eventName.toString().toLowerCase().replace(/^on/, ''); if (!this.handlers[eventName]) this.handlers[eventName] = []; this.handlers[eventName].push(func); }, setHandCursor: function(enabled) { // enable hand cursor (true), or default arrow cursor (false) this.handCursorEnabled = enabled; if (this.ready) this.movie.setHandCursor(enabled); }, setCSSEffects: function(enabled) { // enable or disable CSS effects on DOM container this.cssEffects = !!enabled; }, setIsSelected: function(enabled) { // enable or disable text be selected this.isSelected = enabled; }, receiveEvent: function(eventName, args) { // receive event from flash eventName = eventName.toString().toLowerCase().replace(/^on/, ''); // special behavior for certain events switch (eventName) { case 'load': // movie claims it is ready, but in IE this isn't always the case... // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function this.movie = document.getElementById(this.movieId); if (!this.movie) { var self = this; setTimeout( function() { self.receiveEvent('load', null); }, 1 ); return; } // firefox on pc needs a "kick" in order to set these in certain cases if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) { var self = this; setTimeout( function() { self.receiveEvent('load', null); }, 100 ); this.ready = true; return; } this.ready = true; this.movie.setText( this.clipText ); this.movie.setHandCursor( this.handCursorEnabled ); break; case 'mouseover': if (this.element && this.cssEffects) { this.element.addClass('hover'); if (this.recoverActive) this.element.addClass('active'); } break; case 'mouseout': if (this.element && this.cssEffects) { this.recoverActive = false; if (this.element.hasClass('active')) { this.element.removeClass('active'); this.recoverActive = true; } this.element.removeClass('hover'); } if (this.div) { this.div.css('left', '-5000px'); } break; case 'mousedown': if (this.element && this.cssEffects) { this.element.addClass('active'); } break; case 'mouseup': if (this.element && this.cssEffects) { this.element.removeClass('active'); this.recoverActive = false; } if (this.isSelected && this.from) { this.from.select(); } break; } // switch eventName if (this.handlers[eventName]) { for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) { var func = this.handlers[eventName][idx]; if (typeof(func) == 'function') { // actual function reference func(this, args); } else if ((typeof(func) == 'object') && (func.length == 2)) { // PHP style object + method, i.e. [myObject, 'myMethod'] func[0][ func[1] ](this, args); } else if (typeof(func) == 'string') { // name of function window[func](this, args); } } // foreach event handler defined } // user defined handler for event } }; $(function() { if (clip || $.browser.msie) return; clip = new ZeroClipboard.Client(); });