Skip to content

Commit

Permalink
fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Oct 29, 2013
1 parent 81b2668 commit 44b37f5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 82 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,34 @@ jQuery Clipboard plugin: copy any text to the user's clipboard. Implements ZeroC
<script type="text/javascript" src="/path/to/javascripts/jquery.clipboard.js"></script>
```

**Important: please check that you are using at least jQuery 1.7 - jQuery Clipboard won't work with versions below!**


### 2. Apply On An Element

```javascript
$(document).ready(function(){
$('.code-block a.code-copy').clipboard({
$(document).ready(function() {
var copy_sel = $('.code-block a.code-copy');

// Disables other default handlers on click (avoid issues)
copy_sel.on('click', function(e) {
e.preventDefault();
});

// Apply clipboard click event
copy_sel.clipboard({
path: '/path/to/flashes/jquery.clipboard.swf',
copy: $(this).parents('.code-block').find('.code-lines').text()

copy: function() {
var this_sel = $(this);

// Hide "Copy" and show "Copied, copy again?" message in link
this_sel.find('.code-copy-first').hide();
this_sel.find('.code-copy-done').show();

// Return text in closest element (useful when you have multiple boxes that can be copied)
return this_sel.closest('.code-block').text();
}
});
});
```
Expand Down
146 changes: 67 additions & 79 deletions jquery.clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,89 @@
* Released under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Version: v1.1
* Date: Tue Oct 29, 2013
*/

/* Component: jQuery Clipboard */
(function ($) {
$.fn.clipboard = function (params) {
if (typeof params == 'object' && !params.length) {
var settings = $.extend({
path: 'jquery.clipboard.swf',
copy: null,
beforeCopy: null,
afterCopy: null,
clickAfter: true
}, params);

return this.each(function () {
var o = $(this);
var $clip = null;
var $is_loaded = false;

if (o.is(':visible') && (typeof settings.copy == 'string' || $.isFunction(settings.copy))) {
var clip = new ZeroClipboard($(this), {
moviePath: settings.path,
trustedDomains: '*',
hoverClass: 'hover',
activeClass: 'active'
});

if ($.isFunction(settings.copy)) {
o.bind('Clipboard_copy',settings.copy);
}
if ($.isFunction(settings.beforeCopy)) {
o.bind('Clipboard_beforeCopy',settings.beforeCopy);
}
if ($.isFunction(settings.afterCopy)) {
o.bind('Clipboard_afterCopy',settings.afterCopy);
}
$.fn.clipboard = function (params) {
if ((typeof params == 'object' && !params.length) || (typeof params == 'undefined')) {
var settings = $.extend({
path: 'jquery.clipboard.swf',
copy: null,
beforeCopy: null,
afterCopy: null,
clickAfter: true
}, (params || {}));

return this.each(function () {
var o = $(this);

clip.addEventListener('mouseOver', function (client) {
o.trigger('mouseenter');
});
clip.addEventListener('mouseOut', function (client) {
o.trigger('mouseleave');
});
clip.addEventListener('mouseDown', function (client) {
o.trigger('mousedown');

if (!$.isFunction(settings.copy)) {
clip.setText(settings.copy);
} else {
clip.setText(o.triggerHandler('Clipboard_copy'));
}

if ($.isFunction(settings.beforeCopy)) {
o.trigger('Clipboard_beforeCopy');
}
});
if (o.is(':visible') && (typeof settings.copy == 'string' || $.isFunction(settings.copy))) {
if ($.isFunction(settings.copy)) {
o.bind('Clipboard_copy',settings.copy);
}
if ($.isFunction(settings.beforeCopy)) {
o.bind('Clipboard_beforeCopy',settings.beforeCopy);
}
if ($.isFunction(settings.afterCopy)) {
o.bind('Clipboard_afterCopy',settings.afterCopy);
}

clip.addEventListener('complete', function (client, text) {
if ($.isFunction(settings.afterCopy)) {
o.trigger('Clipboard_afterCopy');
} else {
if (text.length > 500) {
text = text.substr(0, 500) + '...\n\n(' + (text.length - 500) + ' characters not shown)';
}

o.removeClass('hover');
}
if($clip === null) {
$clip = new ZeroClipboard(null, {
moviePath: settings.path,
trustedDomains: '*',
hoverClass: 'hover',
activeClass: 'active'
});

if (settings.clickAfter) {
o.trigger('click');
}
});
$clip.on('load', function(client) {
client.on('mouseover', function (client) {
$(this).trigger('mouseenter');
});

client.on('mouseout', function (client) {
$(this).trigger('mouseleave');
});

clip.glue(o[0], o.parent()[0]);
client.on('mousedown', function (client) {
$(this).trigger('mousedown');

if (!$.isFunction(settings.copy)) {
client.setText(settings.copy);
} else {
client.setText($(this).triggerHandler('Clipboard_copy'));
}

if ($.isFunction(settings.beforeCopy)) {
$(this).trigger('Clipboard_beforeCopy');
}
});
} else if (typeof params == 'string') {
return this.each(function () {
var o = $(this);
});

params = params.toLowerCase();
var clipboardId = o.data('clipboardId');
var clipElm = $('#' + clipboardId + '.clipboard');
client.on('complete', function (client, args) {
if ($.isFunction(settings.afterCopy)) {
$(this).trigger('Clipboard_afterCopy');
} else {
$(this).removeClass('hover');
}

if (params == 'remove') {
clipElm.remove();
o.removeClass('active hover');
} else if (params == 'hide') {
clipElm.hide();
o.removeClass('active hover');
} else if (params == 'show') {
clipElm.show();
if (settings.clickAfter) {
$(this).trigger('click');
}
});
});
}

$clip.glue(o[0]);
}
}
});
}
}
})(jQuery);

/* Component: ZeroClipboard */
Expand Down

0 comments on commit 44b37f5

Please sign in to comment.