3b45881d7cd823ef42b0040d89f0a8d853ea346c
[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         };
51     }])
52     .directive('clipboard', ['clipboard', function (clipboard) {
53         return {
54             restrict: 'A',
55             scope: {
56                 onCopied: '&',
57                 onError: '&',
58                 text: '='
59             },
60             link: function (scope, element) {
61                 element.on('click', function (event) {
62                     try {
63                         clipboard.copyText(scope.text);
64                         if (angular.isFunction(scope.onCopied)) {
65                             scope.$evalAsync(scope.onCopied());
66                         }
67                     } catch (err) {
68                         if (angular.isFunction(scope.onError)) {
69                             scope.$evalAsync(scope.onError({err: err}));
70                         }
71                     }
72                 });
73             }
74         };
75     }]);
76
77 }));