Skip to content

Commit 6ad1f03

Browse files
committed
feature: config to choose emit angualr event.
bugfix: duplicate create clip client docs: update for 'emitEvent' config
1 parent 6a39d83 commit 6ad1f03

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ The params is an object. and just same as [ZeroClipboard official config](https:
4848
// set button's Text
4949
buttonText: 'Copy',
5050

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+
5157
// set the callback function of the events which ZeroClipboard dispataches
5258
load: null,
5359
mouseover: null,
@@ -63,6 +69,8 @@ The params is an object. and just same as [ZeroClipboard official config](https:
6369

6470
## LICENSE
6571

72+
MIT LICENSE
73+
6674
Copyright (C) 2014 lisposter(Leigh Zhu)
6775

6876
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

demo/demo.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en" ng-app="demo">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Angular ZeroClipBoard</title>
5+
<title>Angular ZeroClipboard</title>
66
<script src="../bower_components/angular/angular.js"></script>
77
<script src="../bower_components/zeroclipboard/ZeroClipboard.min.js"></script>
88
<script src="../src/angular-zeroclipboard.js"></script>
@@ -17,20 +17,26 @@
1717

1818
// set directive options
1919
uiZeroclipConfigProvider.setOptions({
20-
buttonText: 'Copy Me!'
20+
buttonText: 'Copy Me!',
21+
emitEvent: true
2122
});
2223

24+
}]).
25+
controller('demoCtrl', ['$scope', function($scope) {
26+
$scope.$on('ZeroClipboard.complete', function() {
27+
console.log('copy complete');
28+
})
2329
}]);
2430
</script>
2531
</head>
26-
<body>
32+
<body ng-controller="demoCtrl">
2733

2834
<h1>Angular ZeroClipboard Demo</h1>
2935

3036
<form action="">
3137
<label for="">input: <input type="text" ui-zeroclip ng-model="input"></label>
3238

33-
<label for="">textarea: <textarea name="" id="" cols="30" rows="10" ui-zeroclip ng-model="textarea"></textarea></label>
39+
<label for="">textarea: <textarea cols="30" rows="10" ui-zeroclip ng-model="textarea"></textarea></label>
3440
</form>
3541

3642
</body>

src/angular-zeroclipboard.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('angular.zeroclipboard', []).provider('uiZeroclipConfig', function() {
2-
1+
angular.module('angular.zeroclipboard', []).
2+
provider('uiZeroclipConfig', function() {
33
// default configs
44
var _zeroclipConfig = {
55
buttonClass: '',
@@ -18,27 +18,16 @@ angular.module('angular.zeroclipboard', []).provider('uiZeroclipConfig', functio
1818
var _options = {
1919
buttonClass: '',
2020
buttonText: 'Copy',
21-
load: null,
22-
mouseover: null,
23-
mouseout: null,
24-
mousedown: null,
25-
mouseup: null,
26-
complete: null,
27-
noflash: null,
28-
wrongflash: null,
29-
dataRequested: null
21+
emitEvent: false
3022
};
31-
3223
this.setZcConf = function(zcConf) {
3324
angular.extend(_zeroclipConfig, zcConf);
3425
};
35-
3626
this.setOptions = function(options) {
3727
angular.extend(_options, options);
3828
};
39-
40-
this.$get = ['$rootScope',
41-
function($rootScope) {
29+
this.$get = [
30+
function() {
4231
return {
4332
zeroclipConfig: _zeroclipConfig,
4433
options: _options
@@ -50,16 +39,14 @@ directive('uiZeroclip', ['$document', '$window', 'uiZeroclipConfig',
5039
function($document, $window, uiZeroclipConfig) {
5140
var zeroclipConfig = uiZeroclipConfig.zeroclipConfig || {};
5241
var options = uiZeroclipConfig.options;
53-
var copyBtns = [];
5442
var _id = 0;
5543

5644
function insertAfter(newNode, referenceNode) {
5745
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
5846
}
5947
return {
6048
priority: 10,
61-
require: 'ngModel',
62-
link: function(scope, elm, attrs, ngModel) {
49+
link: function(scope, elm, attrs) {
6350
// config
6451
ZeroClipboard.config(zeroclipConfig);
6552
if (!attrs.id) {
@@ -70,20 +57,20 @@ directive('uiZeroclip', ['$document', '$window', 'uiZeroclipConfig',
7057
btn.setAttribute('class', options.buttonClass);
7158
_id++;
7259
insertAfter(btn, elm[0]);
73-
copyBtns.push(btn);
7460
}
7561
if (angular.isFunction(ZeroClipboard)) {
76-
scope.client = new ZeroClipboard(copyBtns);
62+
scope.client = new ZeroClipboard(btn);
7763
}
78-
scope.client.on('load', options.load);
79-
scope.client.on('mouseover', options.mouseover);
80-
scope.client.on('mouseout', options.mouseout);
81-
scope.client.on('mouseup', options.mouseup);
82-
scope.client.on('mousedown', options.mousedown);
83-
scope.client.on('complete', options.complete);
84-
scope.client.on('dataRequested', options.dataRequested);
85-
scope.client.on('noflash', options.noflash);
86-
scope.client.on('wrongflash', options.wrongflash);
64+
var _events = ['load', 'mouseover', 'mouseout', 'mouseup', 'mousedown', 'complete', 'dataRequested', 'noflash', 'wrongflash'];
65+
_events.forEach(function(evt) {
66+
if (options.emitEvent) {
67+
scope.client.on(evt, function() {
68+
scope.$emit('ZeroClipboard.' + evt);
69+
});
70+
} else {
71+
scope.client.on(evt, options[evt]);
72+
}
73+
})
8774
}
8875
}
8976
}

0 commit comments

Comments
 (0)