Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created January 21, 2010 21:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehale/283199 to your computer and use it in GitHub Desktop.
Save mikehale/283199 to your computer and use it in GitHub Desktop.
A jquery API for ZeroClipBoard
/*
Requirements:
This script requires the jquery.includeMany plugin and ZeroClipboard.
Example:
$("#elementToClickOn").copyable(function(e, clip) {
clip.setText($("#elementWithText").html());
});
*/
var baseUri = $("script[src$='jquery.copyable.js']").attr('src').replace("jquery.copyable.js", "");
$.include([baseUri + 'ZeroClipBoard.js'], function() {
ZeroClipboard.setMoviePath(baseUri + 'ZeroClipboard.swf');
});
jQuery.fn.copyable = function(getTextFunction) {
var self = this;
self.each(function() {
var copyable = $(this);
if ($.browser.msie) {
var clip = copyable.data('clip');
if (clip === undefined) {
clip = new Object();
copyable.data('clip', clip);
}
clip.setText = function(data) {
window.clipboardData.setData('Text', data);
};
copyable.mousedown(function(e) {
getTextFunction(e, clip);
});
} else {
var copyableId = copyable.attr("id");
var containerId = copyable.attr("id") + "_container";
var clip = copyable.data('clip');
if (clip === undefined) {
clip = new ZeroClipboard.Client();
copyable.data('clip', clip);
}
//wrapper trigger with a div container
copyable.wrap("<div id='" + containerId + "' style='position:relative;display:inline'></div>");
copyable.mousedown(getTextFunction);
clip.glue(copyableId, containerId);
clip.setHandCursor(false);
//connect flash events to underlying element
clip.addEventListener('mouseDown', function(client) {
copyable.trigger("mousedown", client);
});
clip.addEventListener('mouseOver', function(client) {
copyable.trigger("mouseover", client);
});
clip.addEventListener('mouseOut', function(client) {
copyable.trigger("mouseout", client);
});
clip.addEventListener('mouseUp', function(client) {
copyable.trigger("mouseup", client);
});
}
});
return self;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment