cd2694dbd200af62fbd381b4e789e3b916c3cf02
[motion.git] / public / bower_components / angular-clipboard / angular-clipboard.js
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'));
7     } else {
8         root.angularClipboard = factory(root.angular);
9   }
10 }(this, function (angular) {
11
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;
19             return node;
20         }
21
22         function copyNode(node) {
23             try {
24                 // Set inline style to override css styles
25                 $document[0].body.style.webkitUserSelect = 'initial';
26
27                 var selection = $document[0].getSelection();
28                 selection.removeAllRanges();
29                 node.select();
30
31                 if(!$document[0].execCommand('copy')) {
32                     throw('failure copy');
33                 }
34                 selection.removeAllRanges();
35             } finally {
36                 // Reset inline style
37                 $document[0].body.style.webkitUserSelect = '';
38             }
39         }
40
41         function copyText(text) {
42             var node = createNode(text);
43             $document[0].body.appendChild(node);
44             copyNode(node);
45             $document[0].body.removeChild(node);
46         }
47
48         return {
49             copyText: copyText,
50             supported: 'queryCommandSupported' in document && document.queryCommandSupported('copy')
51         };
52     }])
53     .directive('clipboard', ['clipboard', function (clipboard) {
54         return {
55             restrict: 'A',
56             scope: {
57                 onCopied: '&',
58                 onError: '&',
59                 text: '=',
60                 supported: '=?'
61             },
62             link: function (scope, element) {
63                 scope.supported = clipboard.supported;
64
65                 element.on('click', function (event) {
66                     try {
67                         clipboard.copyText(scope.text);
68                         if (angular.isFunction(scope.onCopied)) {
69                             scope.$evalAsync(scope.onCopied());
70                         }
71                     } catch (err) {
72                         if (angular.isFunction(scope.onError)) {
73                             scope.$evalAsync(scope.onError({err: err}));
74                         }
75                     }
76                 });
77             }
78         };
79     }]);
80
81 }));