Skip to content

Commit 9d8b1b3

Browse files
committed
Merge pull request #2 from fixate/master
Made directive bindable to models and interpolated text, removed markup generating code.
2 parents 91294eb + 152cbc2 commit 9d8b1b3

File tree

4 files changed

+97
-103
lines changed

4 files changed

+97
-103
lines changed

README.md

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,37 @@ angular.module('demo', ['angular.zeroclipboard']).
2323
swfPath: '../bower_components/zeroclipboard/ZeroClipboard.swf'
2424
});
2525

26-
// set directive options
27-
uiZeroclipConfigProvider.setOptions({
28-
buttonText: 'Copy Me!'
29-
});
30-
3126
}]);
3227
```
3328

3429

3530
## Config
3631

37-
* `uiZeroclipConfigProvider.setZcConf({})` Config the ZeroClipboard
32+
Configuration passed into `ZeroClipboard.config`
33+
34+
* `uiZeroclipConfigProvider.setZcConf({
35+
moviePath: '../path/to/ZeroClipboard.swf'
36+
* })`
3837

3938
The params is an object. and just same as [ZeroClipboard official config](https://github.com/zeroclipboard/zeroclipboard/blob/1.x-master/docs/instructions.md)
4039

41-
* `uiZeroclipConfigProvider.setOptions()` Config this directive Config
40+
## Usage
4241

43-
```js
44-
{
45-
// set the button class
46-
buttonClass: '',
47-
48-
// set button's Text
49-
buttonText: 'Copy',
50-
51-
// set `true`, then when trigger an event by clipboard, it will emit an angular event by .$emit()
52-
// then you can listen the angular type event in your controller.
53-
// the event which be emited will bt like: 'ZeroClipboard.[eventType]', for example: 'ZeroClipboard.complete'
54-
// ATTENTION: if you set this to true, the callback functions you set below will be ignored.
55-
emitEvent: true,
56-
57-
// set the callback function of the events which ZeroClipboard dispataches
58-
load: null,
59-
mouseover: null,
60-
mouseout: null,
61-
mousedown: null,
62-
mouseup: null,
63-
complete: null,
64-
noflash: null,
65-
wrongflash: null,
66-
dataRequested: null
67-
}
42+
Example using a two-way model binding
43+
44+
```html
45+
<input type="text" ng-model="myText" />
46+
<button ui-zeroclip zeroclip-copied="copied=true" zeroclip-model="myText">Copy</button>
47+
<span ng-show="copied">Text Copied!</span>
48+
```
49+
50+
Example using interpolated text:
51+
52+
```html
53+
<input type="text" ng-model="myText" />
54+
<button ui-zeroclip zeroclip-copied="copied=true"
55+
zeroclip-text="This was your text: {{ myText }}">Copy</button>
56+
<span ng-show="copied">The sentence "This was your text: {{ myText }}" was copied!</span>
6857
```
6958

7059
## LICENSE

bower.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "angular-zeroclipboard",
3-
"version": "0.2.0",
3+
"version": "0.3.2",
44
"description": "Angular Wrapper for ZeroClipboard",
55
"main": "src/angular-zeroclipboard.js",
66
"keywords": [
77
"angular",
88
"zeroclipboard"
99
],
1010
"authors": [
11-
"Leigh Zhu"
11+
"Leigh Zhu",
12+
"Stan Bondi"
1213
],
1314
"ignore": [
1415
"**/.*",
@@ -24,6 +25,6 @@
2425
"homepage": "http://github.com/lisposter/angular-zeroclipboard",
2526
"dependencies": {
2627
"angular": "~1.2.16",
27-
"zeroclipboard": "~1.3.5"
28+
"zeroclipboard": ">=2"
2829
}
2930
}

demo/demo.html

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
<meta charset="UTF-8">
55
<title>Angular ZeroClipboard</title>
66
<script src="../bower_components/angular/angular.js"></script>
7-
<script src="../bower_components/zeroclipboard/ZeroClipboard.min.js"></script>
7+
<script src="../bower_components/zeroclipboard/dist/ZeroClipboard.js"></script>
88
<script src="../src/angular-zeroclipboard.js"></script>
99
<script>
10-
angular.module('demo', ['angular.zeroclipboard']).
10+
angular.module('demo', ['zeroclipboard']).
1111
config(['uiZeroclipConfigProvider', function(uiZeroclipConfigProvider) {
1212

1313
// config ZeroClipboard
1414
uiZeroclipConfigProvider.setZcConf({
15-
moviePath: '../bower_components/zeroclipboard/ZeroClipboard.swf'
16-
});
17-
18-
// set directive options
19-
uiZeroclipConfigProvider.setOptions({
20-
buttonText: 'Copy Me!',
21-
emitEvent: true
15+
swfPath: '../bower_components/zeroclipboard/dist/ZeroClipboard.swf'
2216
});
2317

2418
}]).
2519
controller('demoCtrl', ['$scope', function($scope) {
26-
$scope.$on('ZeroClipboard.complete', function() {
27-
console.log('copy complete');
28-
})
20+
$scope.complete = function(e) {
21+
console.log('copy complete', e);
22+
$scope.copied = true
23+
};
24+
25+
$scope.$watch('input', function(v) {
26+
$scope.copied = false
27+
});
2928
}]);
3029
</script>
3130
</head>
@@ -34,10 +33,20 @@
3433
<h1>Angular ZeroClipboard Demo</h1>
3534

3635
<form action="">
37-
<label for="">input: <input type="text" ui-zeroclip ng-model="input"></label>
36+
<div>
37+
<h1>Copy from input</h1>
38+
<label for="">input: <input type="text" ng-model="input" id="input1"></label>
39+
<button ui-zeroclip zeroclip-copied="complete($event)" zeroclip-model="input" >Copy</button>
40+
<span ng-show="copied">Copied!</span>
41+
</div>
3842

39-
<label for="">textarea: <textarea cols="30" rows="10" ui-zeroclip ng-model="textarea"></textarea></label>
43+
<div>
44+
<h1>Copy interpolated text</h1>
45+
<span>This will copy: "This input text: {{ input }}"</span>
46+
<button ui-zeroclip zeroclip-copied="copiedText=true" zeroclip-text="This input text: {{ input }}" >Copy</button>
47+
<span ng-show="copiedText">Copied!</span>
48+
</div>
4049
</form>
41-
50+
4251
</body>
43-
</html>
52+
</html>

src/angular-zeroclipboard.js

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('angular.zeroclipboard', []).
2-
provider('uiZeroclipConfig', function() {
1+
angular.module('zeroclipboard', [])
2+
.provider('uiZeroclipConfig', function() {
33
// default configs
44
var _zeroclipConfig = {
55
buttonClass: '',
@@ -15,62 +15,57 @@ provider('uiZeroclipConfig', function() {
1515
hoverClass: "zeroclipboard-is-hover",
1616
activeClass: "zeroclipboard-is-active"
1717
};
18-
var _options = {
19-
buttonClass: '',
20-
buttonText: 'Copy',
21-
emitEvent: false
22-
};
2318
this.setZcConf = function(zcConf) {
24-
angular.extend(_zeroclipConfig, zcConf);
25-
};
26-
this.setOptions = function(options) {
27-
angular.extend(_options, options);
19+
angular.extend(_zeroclipConfig, zcConf);
2820
};
2921
this.$get = function() {
30-
return {
31-
zeroclipConfig: _zeroclipConfig,
32-
options: _options
33-
}
22+
return {
23+
zeroclipConfig: _zeroclipConfig
24+
}
3425
};
35-
}).
36-
directive('uiZeroclip', ['$document', '$window', 'uiZeroclipConfig',
37-
function($document, $window, uiZeroclipConfig) {
26+
})
27+
28+
.directive('uiZeroclip', ['$document', '$window', 'uiZeroclipConfig',
29+
function($document, $window, uiZeroclipConfig) {
30+
3831
var zeroclipConfig = uiZeroclipConfig.zeroclipConfig || {};
39-
var options = uiZeroclipConfig.options;
40-
var _id = 0;
32+
var ZeroClipboard = $window.ZeroClipboard
4133

42-
function insertAfter(newNode, referenceNode) {
43-
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
44-
}
4534
return {
46-
priority: 10,
47-
link: function(scope, elm, attrs) {
48-
// config
49-
ZeroClipboard.config(zeroclipConfig);
50-
var btn = elm[0];
51-
if (!attrs.id) {
52-
attrs.$set('id', 'uiZeroclip' + _id);
53-
btn = document.createElement('button');
54-
btn.appendChild(document.createTextNode(options.buttonText));
55-
btn.setAttribute('data-clipboard-target', 'uiZeroclip' + _id);
56-
btn.setAttribute('class', options.buttonClass);
57-
_id++;
58-
insertAfter(btn, elm[0]);
59-
}
60-
if (angular.isFunction(ZeroClipboard)) {
61-
scope.client = new ZeroClipboard(btn);
62-
}
63-
var _events = ['load', 'mouseover', 'mouseout', 'mouseup', 'mousedown', 'complete', 'dataRequested', 'noflash', 'wrongflash'];
64-
_events.forEach(function(evt) {
65-
if (options.emitEvent) {
66-
scope.client.on(evt, function() {
67-
scope.$emit('ZeroClipboard.' + evt);
68-
});
69-
} else {
70-
scope.client.on(evt, options[evt]);
71-
}
72-
})
35+
scope: {
36+
onCopied: '&zeroclipCopied',
37+
client: '=?uiZeroclip',
38+
value: '=zeroclipModel',
39+
text: '@zeroclipText'
40+
},
41+
link: function(scope, element, attrs) {
42+
// config
43+
ZeroClipboard.config(zeroclipConfig);
44+
var btn = element[0];
45+
46+
if (angular.isFunction(ZeroClipboard)) {
47+
client = scope.client = new ZeroClipboard(btn);
7348
}
49+
50+
scope.$watch('value', function(v) {
51+
if (v == undefined) { return; }
52+
element.attr('data-clipboard-text', v);
53+
});
54+
55+
scope.$watch('text', function(v) {
56+
element.attr('data-clipboard-text', v);
57+
});
58+
59+
client.on('aftercopy', _completeHnd = function(e) {
60+
scope.$apply(function() {
61+
scope.onCopied({$event: e});
62+
});
63+
});
64+
65+
scope.$on('$destroy', function() {
66+
client.off('complete', _completeHnd)
67+
});
68+
}
7469
}
75-
}
76-
]);
70+
}
71+
]);

0 commit comments

Comments
 (0)