clipboard-rails 1.3.1.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4c164f67d3b02062ff36276e5ac0d5885ec745f
4
- data.tar.gz: 7d6720a4a790b98163283670792f98f114fea518
3
+ metadata.gz: 7adecfec979b0797c27dc425c3de4195849e77e4
4
+ data.tar.gz: 1d89fbbb306cafb7628f6507c91a8c6dedfc7839
5
5
  SHA512:
6
- metadata.gz: 28262a8002e5f7c506ab13df15904a7063c2748c752a9a7039c3ccb0b178423f3fd839eca548d2b81317e66f7989e760bdd9cf93f4d06dee083cff8b1ebd88e4
7
- data.tar.gz: 601422e8a78fdac820ecc331f74b030a0b817e888de1d6850ad04dc60d3b788d379abf34bdabe4a241ed959260a2f600a28971a76516621d7783c003efd299cf
6
+ metadata.gz: 1cd6b859d27a015fd064e882b56d2e41eae9dbedf1ba393af25b9ddc60c871d6bc447c1ece687b18d84e1614f5c0d2885a8404629e88134afd3d3a87de7eaa71
7
+ data.tar.gz: 45a8f9a8b016983bc7544dc0bb9bba13250f3b5379b5fa0071e623bd70e9eecc0e46148dc03645adb0199d9510cd57806d10a8dca5989bab4ed855f9ddc2ab6b
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # clipboard-rails
2
+ [![Gem Version](https://badge.fury.io/rb/clipboard-rails.svg)](http://badge.fury.io/rb/clipboard-rails)
2
3
 
3
4
  clipboard-rails gem is the integration of clipboard.js javascript library for your Rails 4 application.
4
5
 
@@ -28,6 +29,35 @@ Now you need to edit your `app/assets/javascripts/application.js` file and add t
28
29
 
29
30
  ## Usage
30
31
 
32
+ Here is the example working code to test with your Rails application.
33
+
34
+ Add this sample code to your `app/assets/javascripts/application.js` file
35
+
36
+ ``` javascript
37
+ $(document).ready(function(){
38
+
39
+ var clip = new Clipboard('.btn');
40
+ console.log(clip);
41
+
42
+ });
43
+ ```
44
+
45
+ *Note:* Here i am using `gem 'jquery-turbolinks'` for using the jquery $(document).ready function
46
+
47
+ Add this sample code to your template file like `index.html.erb`
48
+
49
+ ``` html
50
+ <!-- Target -->
51
+ <textarea id="bar">Mussum ipsum cacilds...</textarea>
52
+
53
+ <!-- Trigger -->
54
+ <button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">
55
+ Cut to clipboard
56
+ </button>
57
+ ```
58
+
59
+ ## Full documentation
60
+
31
61
  Read the clipboard.js documentation here http://zenorocha.github.io/clipboard.js/
32
62
 
33
63
  ## Development
@@ -1,5 +1,5 @@
1
1
  module Clipboard
2
2
  module Rails
3
- VERSION = "1.3.1.1"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -51,57 +51,57 @@ exports.unbind = function(el, type, fn, capture){
51
51
  };
52
52
 
53
53
  },{"closest":2,"component-event":4}],2:[function(require,module,exports){
54
- var matches = require('matches-selector')
55
-
56
- module.exports = function (element, selector, checkYoSelf) {
57
- var parent = checkYoSelf ? element : element.parentNode
58
-
59
- while (parent && parent !== document) {
60
- if (matches(parent, selector)) return parent;
61
- parent = parent.parentNode
62
- }
63
- }
54
+ var matches = require('matches-selector')
55
+
56
+ module.exports = function (element, selector, checkYoSelf) {
57
+ var parent = checkYoSelf ? element : element.parentNode
58
+
59
+ while (parent && parent !== document) {
60
+ if (matches(parent, selector)) return parent;
61
+ parent = parent.parentNode
62
+ }
63
+ }
64
64
 
65
65
  },{"matches-selector":3}],3:[function(require,module,exports){
66
-
67
- /**
68
- * Element prototype.
69
- */
70
-
71
- var proto = Element.prototype;
72
-
73
- /**
74
- * Vendor function.
75
- */
76
-
77
- var vendor = proto.matchesSelector
78
- || proto.webkitMatchesSelector
79
- || proto.mozMatchesSelector
80
- || proto.msMatchesSelector
81
- || proto.oMatchesSelector;
82
-
83
- /**
84
- * Expose `match()`.
85
- */
86
-
87
- module.exports = match;
88
-
89
- /**
90
- * Match `el` to `selector`.
91
- *
92
- * @param {Element} el
93
- * @param {String} selector
94
- * @return {Boolean}
95
- * @api public
96
- */
97
-
98
- function match(el, selector) {
99
- if (vendor) return vendor.call(el, selector);
100
- var nodes = el.parentNode.querySelectorAll(selector);
101
- for (var i = 0; i < nodes.length; ++i) {
102
- if (nodes[i] == el) return true;
103
- }
104
- return false;
66
+
67
+ /**
68
+ * Element prototype.
69
+ */
70
+
71
+ var proto = Element.prototype;
72
+
73
+ /**
74
+ * Vendor function.
75
+ */
76
+
77
+ var vendor = proto.matchesSelector
78
+ || proto.webkitMatchesSelector
79
+ || proto.mozMatchesSelector
80
+ || proto.msMatchesSelector
81
+ || proto.oMatchesSelector;
82
+
83
+ /**
84
+ * Expose `match()`.
85
+ */
86
+
87
+ module.exports = match;
88
+
89
+ /**
90
+ * Match `el` to `selector`.
91
+ *
92
+ * @param {Element} el
93
+ * @param {String} selector
94
+ * @return {Boolean}
95
+ * @api public
96
+ */
97
+
98
+ function match(el, selector) {
99
+ if (vendor) return vendor.call(el, selector);
100
+ var nodes = el.parentNode.querySelectorAll(selector);
101
+ for (var i = 0; i < nodes.length; ++i) {
102
+ if (nodes[i] == el) return true;
103
+ }
104
+ return false;
105
105
  }
106
106
  },{}],4:[function(require,module,exports){
107
107
  var bind = window.addEventListener ? 'addEventListener' : 'attachEvent',
@@ -141,12 +141,12 @@ exports.unbind = function(el, type, fn, capture){
141
141
  };
142
142
  },{}],5:[function(require,module,exports){
143
143
  function E () {
144
- // Keep this empty so it's easier to inherit from
144
+ // Keep this empty so it's easier to inherit from
145
145
  // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
146
146
  }
147
147
 
148
148
  E.prototype = {
149
- on: function (name, callback, ctx) {
149
+ on: function (name, callback, ctx) {
150
150
  var e = this.e || (this.e = {});
151
151
 
152
152
  (e[name] || (e[name] = [])).push({
@@ -265,7 +265,7 @@ var ClipboardAction = (function () {
265
265
  };
266
266
 
267
267
  /**
268
- * Creates a fake input element, sets its value from `text` property,
268
+ * Creates a fake textarea element, sets its value from `text` property,
269
269
  * and makes a selection on it.
270
270
  */
271
271
 
@@ -278,9 +278,10 @@ var ClipboardAction = (function () {
278
278
  return _this.removeFake();
279
279
  });
280
280
 
281
- this.fakeElem = document.createElement('input');
281
+ this.fakeElem = document.createElement('textarea');
282
282
  this.fakeElem.style.position = 'absolute';
283
283
  this.fakeElem.style.left = '-9999px';
284
+ this.fakeElem.style.top = document.body.scrollTop + 'px';
284
285
  this.fakeElem.setAttribute('readonly', '');
285
286
  this.fakeElem.value = this.text;
286
287
  this.selectedText = this.text;
@@ -383,6 +384,14 @@ var ClipboardAction = (function () {
383
384
  * @param {String} action
384
385
  */
385
386
 
387
+ /**
388
+ * Destroy lifecycle.
389
+ */
390
+
391
+ ClipboardAction.prototype.destroy = function destroy() {
392
+ this.removeFake();
393
+ };
394
+
386
395
  _createClass(ClipboardAction, [{
387
396
  key: 'action',
388
397
  set: function set() {
@@ -458,8 +467,6 @@ var _tinyEmitter = require('tiny-emitter');
458
467
 
459
468
  var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
460
469
 
461
- var prefix = 'data-clipboard-';
462
-
463
470
  /**
464
471
  * Base class which takes a selector, delegates a click event to it,
465
472
  * and instantiates a new `ClipboardAction` on each click.
@@ -482,6 +489,12 @@ var Clipboard = (function (_Emitter) {
482
489
  this.delegateClick(selector);
483
490
  }
484
491
 
492
+ /**
493
+ * Helper function to retrieve attribute value.
494
+ * @param {String} suffix
495
+ * @param {Element} element
496
+ */
497
+
485
498
  /**
486
499
  * Defines if attributes would be resolved using internal setter functions
487
500
  * or custom functions that were passed in the constructor.
@@ -491,9 +504,9 @@ var Clipboard = (function (_Emitter) {
491
504
  Clipboard.prototype.resolveOptions = function resolveOptions() {
492
505
  var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
493
506
 
494
- this.action = typeof options.action === 'function' ? options.action : this.setAction;
495
- this.target = typeof options.target === 'function' ? options.target : this.setTarget;
496
- this.text = typeof options.text === 'function' ? options.text : this.setText;
507
+ this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
508
+ this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
509
+ this.text = typeof options.text === 'function' ? options.text : this.defaultText;
497
510
  };
498
511
 
499
512
  /**
@@ -504,17 +517,26 @@ var Clipboard = (function (_Emitter) {
504
517
  Clipboard.prototype.delegateClick = function delegateClick(selector) {
505
518
  var _this = this;
506
519
 
507
- _delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
508
- return _this.initialize(e);
520
+ this.binding = _delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
521
+ return _this.onClick(e);
509
522
  });
510
523
  };
511
524
 
525
+ /**
526
+ * Undelegates a click event on body.
527
+ * @param {String} selector
528
+ */
529
+
530
+ Clipboard.prototype.undelegateClick = function undelegateClick() {
531
+ _delegateEvents2['default'].unbind(document.body, 'click', this.binding);
532
+ };
533
+
512
534
  /**
513
535
  * Defines a new `ClipboardAction` on each click event.
514
536
  * @param {Event} e
515
537
  */
516
538
 
517
- Clipboard.prototype.initialize = function initialize(e) {
539
+ Clipboard.prototype.onClick = function onClick(e) {
518
540
  if (this.clipboardAction) {
519
541
  this.clipboardAction = null;
520
542
  }
@@ -529,48 +551,62 @@ var Clipboard = (function (_Emitter) {
529
551
  };
530
552
 
531
553
  /**
532
- * Sets the `action` lookup function.
554
+ * Default `action` lookup function.
533
555
  * @param {Element} trigger
534
556
  */
535
557
 
536
- Clipboard.prototype.setAction = function setAction(trigger) {
537
- if (!trigger.hasAttribute(prefix + 'action')) {
538
- return;
539
- }
540
-
541
- return trigger.getAttribute(prefix + 'action');
558
+ Clipboard.prototype.defaultAction = function defaultAction(trigger) {
559
+ return getAttributeValue('action', trigger);
542
560
  };
543
561
 
544
562
  /**
545
- * Sets the `target` lookup function.
563
+ * Default `target` lookup function.
546
564
  * @param {Element} trigger
547
565
  */
548
566
 
549
- Clipboard.prototype.setTarget = function setTarget(trigger) {
550
- if (!trigger.hasAttribute(prefix + 'target')) {
551
- return;
552
- }
567
+ Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
568
+ var selector = getAttributeValue('target', trigger);
553
569
 
554
- var target = trigger.getAttribute(prefix + 'target');
555
- return document.querySelector(target);
570
+ if (selector) {
571
+ return document.querySelector(selector);
572
+ }
556
573
  };
557
574
 
558
575
  /**
559
- * Sets the `text` lookup function.
576
+ * Default `text` lookup function.
560
577
  * @param {Element} trigger
561
578
  */
562
579
 
563
- Clipboard.prototype.setText = function setText(trigger) {
564
- if (!trigger.hasAttribute(prefix + 'text')) {
565
- return;
566
- }
580
+ Clipboard.prototype.defaultText = function defaultText(trigger) {
581
+ return getAttributeValue('text', trigger);
582
+ };
567
583
 
568
- return trigger.getAttribute(prefix + 'text');
584
+ /**
585
+ * Destroy lifecycle.
586
+ */
587
+
588
+ Clipboard.prototype.destroy = function destroy() {
589
+ this.undelegateClick();
590
+
591
+ if (this.clipboardAction) {
592
+ this.clipboardAction.destroy();
593
+ this.clipboardAction = null;
594
+ }
569
595
  };
570
596
 
571
597
  return Clipboard;
572
598
  })(_tinyEmitter2['default']);
573
599
 
600
+ function getAttributeValue(suffix, element) {
601
+ var attribute = 'data-clipboard-' + suffix;
602
+
603
+ if (!element.hasAttribute(attribute)) {
604
+ return;
605
+ }
606
+
607
+ return element.getAttribute(attribute);
608
+ }
609
+
574
610
  exports['default'] = Clipboard;
575
611
  module.exports = exports['default'];
576
612
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipboard-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohammed Sadiq
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-30 00:00:00.000000000 Z
11
+ date: 2015-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler