mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Adds initial clipboard implementation
This commit is contained in:
parent
e9d9fa6157
commit
231d62db0e
57
dist/clipboard.js
vendored
Normal file
57
dist/clipboard.js
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var Clipboard = (function () {
|
||||
|
||||
// Constructor
|
||||
|
||||
function Clipboard(triggers) {
|
||||
_classCallCheck(this, Clipboard);
|
||||
|
||||
this._triggers = triggers;
|
||||
this.init();
|
||||
}
|
||||
|
||||
// Getters & Setters
|
||||
|
||||
_createClass(Clipboard, [{
|
||||
key: 'init',
|
||||
|
||||
// Methods
|
||||
|
||||
value: function init() {
|
||||
[].forEach.call(this.triggers, this.bind);
|
||||
}
|
||||
}, {
|
||||
key: 'bind',
|
||||
value: function bind(trigger) {
|
||||
trigger.addEventListener('click', function (e) {
|
||||
var targetSelector = e.currentTarget.getAttribute('for');
|
||||
var target = document.getElementById(targetSelector);
|
||||
|
||||
target.select();
|
||||
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'triggers',
|
||||
get: function get() {
|
||||
return document.querySelectorAll(this._triggers);
|
||||
},
|
||||
set: function set(val) {
|
||||
return this._triggers = val;
|
||||
}
|
||||
}]);
|
||||
|
||||
return Clipboard;
|
||||
})();
|
43
src/clipboard.js
Normal file
43
src/clipboard.js
Normal file
@ -0,0 +1,43 @@
|
||||
class Clipboard {
|
||||
|
||||
// Constructor
|
||||
|
||||
constructor(triggers) {
|
||||
this._triggers = triggers;
|
||||
this.init();
|
||||
}
|
||||
|
||||
// Getters & Setters
|
||||
|
||||
get triggers() {
|
||||
return document.querySelectorAll(this._triggers);
|
||||
}
|
||||
|
||||
set triggers(val) {
|
||||
return this._triggers = val;
|
||||
}
|
||||
|
||||
// Methods
|
||||
|
||||
init() {
|
||||
[].forEach.call(this.triggers, this.bind);
|
||||
}
|
||||
|
||||
bind(trigger) {
|
||||
trigger.addEventListener('click', e => {
|
||||
var targetSelector = e.currentTarget.getAttribute('for');
|
||||
var target = document.getElementById(targetSelector);
|
||||
|
||||
target.select();
|
||||
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user