1 (function (root, factory) {
2 /* istanbul ignore next */
3 if (typeof define === 'function' && define.amd) {
4 define(['angular'], factory);
5 } else if (typeof module === 'object' && module.exports) {
6 module.exports = factory(require('angular'));
8 root.angularClipboard = factory(root.angular);
10 }(this, function (angular) {
12 return angular.module('angular-clipboard', [])
13 .factory('clipboard', ['$document', function ($document) {
14 function createNode(text) {
15 var node = $document[0].createElement('textarea');
16 node.style.position = 'absolute';
17 node.style.left = '-10000px';
18 node.textContent = text;
22 function copyNode(node) {
24 // Set inline style to override css styles
25 $document[0].body.style.webkitUserSelect = 'initial';
27 var selection = $document[0].getSelection();
28 selection.removeAllRanges();
31 if(!$document[0].execCommand('copy')) {
32 throw('failure copy');
34 selection.removeAllRanges();
37 $document[0].body.style.webkitUserSelect = '';
41 function copyText(text) {
42 var node = createNode(text);
43 $document[0].body.appendChild(node);
45 $document[0].body.removeChild(node);
50 supported: 'queryCommandSupported' in document && document.queryCommandSupported('copy')
53 .directive('clipboard', ['clipboard', function (clipboard) {
62 link: function (scope, element) {
63 scope.supported = clipboard.supported;
65 element.on('click', function (event) {
67 clipboard.copyText(scope.text);
68 if (angular.isFunction(scope.onCopied)) {
69 scope.$evalAsync(scope.onCopied());
72 if (angular.isFunction(scope.onError)) {
73 scope.$evalAsync(scope.onError({err: err}));