-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathangular-clipboard.js
102 lines (91 loc) · 3.63 KB
/
angular-clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('angular'));
} else {
root.angularClipboard = factory(root.angular);
}
}(this, function (angular) {
return angular.module('angular-clipboard', [])
.factory('clipboard', ['$document', '$window', function ($document, $window) {
function createNode(text, context) {
var node = $document[0].createElement('textarea');
node.style.position = 'absolute';
node.style.fontSize = '12pt';
node.style.border = '0';
node.style.padding = '0';
node.style.margin = '0';
node.style.left = '-10000px';
node.style.top = ($window.pageYOffset || $document[0].documentElement.scrollTop) + 'px';
node.textContent = text;
return node;
}
function copyNode(node) {
try {
// Set inline style to override css styles
$document[0].body.style.webkitUserSelect = 'initial';
var selection = $document[0].getSelection();
selection.removeAllRanges();
var range = document.createRange();
range.selectNodeContents(node);
selection.addRange(range);
// This makes it work in all desktop browsers (Chrome)
node.select();
// This makes it work on Mobile Safari
node.setSelectionRange(0, 999999);
try {
if(!$document[0].execCommand('copy')) {
throw('failure copy');
}
} finally {
selection.removeAllRanges();
}
} finally {
// Reset inline style
$document[0].body.style.webkitUserSelect = '';
}
}
function copyText(text, context) {
var left = $window.pageXOffset || $document[0].documentElement.scrollLeft;
var top = $window.pageYOffset || $document[0].documentElement.scrollTop;
var container = (context && context.container) || $document[0].body;
var node = createNode(text, context);
container.appendChild(node);
copyNode(node);
$window.scrollTo(left, top);
container.removeChild(node);
}
return {
copyText: copyText,
supported: 'queryCommandSupported' in $document[0] && $document[0].queryCommandSupported('copy')
};
}])
.directive('clipboard', ['clipboard', function (clipboard) {
return {
restrict: 'A',
scope: {
onCopied: '&',
onError: '&',
text: '=',
supported: '=?'
},
link: function (scope, element) {
scope.supported = clipboard.supported;
element.on('click', function (event) {
try {
clipboard.copyText(scope.text, element[0]);
if (angular.isFunction(scope.onCopied)) {
scope.$evalAsync(scope.onCopied());
}
} catch (err) {
if (angular.isFunction(scope.onError)) {
scope.$evalAsync(scope.onError({err: err}));
}
}
});
}
};
}]);
}));