Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set global default for success/error callbacks #56

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
coverage
bower_components
node_modules
.DS_Store
.idea/
52 changes: 44 additions & 8 deletions angular-clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
}(this, function (angular) {

return angular.module('angular-clipboard', [])
.provider('angularClipboard', function () {
this.options = {
onCopiedDefaultCallback: false,
onErrorDefaultCallback: false
};
this.configure = function (options) {
angular.extend(this.options, options);
};
this.$get = function() {
return {
defaultOnCopied: this.options.onCopiedDefaultCallback,
defaultOnError: this.options.onErrorDefaultCallback,
};
};
return this;
})
.factory('clipboard', ['$document', '$window', function ($document, $window) {
function createNode(text, context) {
var node = $document[0].createElement('textarea');
Expand Down Expand Up @@ -51,27 +67,47 @@ return angular.module('angular-clipboard', [])
supported: 'queryCommandSupported' in $document[0] && $document[0].queryCommandSupported('copy')
};
}])
.directive('clipboard', ['clipboard', function (clipboard) {
.directive('clipboard', ['clipboard', 'angularClipboard', function (clipboard, angularClipboardProvider) {
return {
restrict: 'A',
scope: {
onCopied: '&',
onError: '&',
onCopied: '&?',
onError: '&?',
text: '=',
supported: '=?'
},
link: function (scope, element) {
link: function (scope, element, attrs) {
scope.supported = clipboard.supported;

var onCopiedCallback = false;
if (angular.isFunction(scope.onCopied)) {
onCopiedCallback = scope.onCopied;
} else if (angular.isFunction(angularClipboardProvider.defaultOnCopied)) {
onCopiedCallback = function(params){
angularClipboardProvider.defaultOnCopied(params.text);
};
}
scope.onCopiedCallback = onCopiedCallback;

var onErrorCallback = false;
if (angular.isFunction(scope.onError)) {
onErrorCallback = scope.onError;
} else if (angular.isFunction(angularClipboardProvider.defaultOnError)) {
onErrorCallback = function(params){
angularClipboardProvider.defaultOnError(params.err);
};
}
scope.onErrorCallback = onErrorCallback;

element.on('click', function (event) {
try {
clipboard.copyText(scope.text, element[0]);
if (angular.isFunction(scope.onCopied)) {
scope.$evalAsync(scope.onCopied());
if (scope.onCopiedCallback) {
scope.onCopiedCallback({text:scope.text});
}
} catch (err) {
if (angular.isFunction(scope.onError)) {
scope.$evalAsync(scope.onError({err: err}));
if (scope.onErrorCallback) {
scope.onErrorCallback({err: err});
}
}
});
Expand Down
21 changes: 20 additions & 1 deletion demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>Click button to copy text to clipboard</h1>

<p>
<textarea ng-model="textToCopy" rows="5" cols="30"></textarea><br />
<button clipboard supported="supported" text="textToCopy" on-copied="success()" on-error="fail(err)">Copy</button>
<button clipboard supported="supported" text="textToCopy" on-error="fail(err)">Copy</button>
</p>

<p>
Expand All @@ -22,6 +22,25 @@ <h1>Click button to copy text to clipboard</h1>
<script>
var demoApp = angular.module('demoApp', ['angular-clipboard']);

demoApp.config([ "angularClipboardProvider", function(angularClipboardProvider) {
angularClipboardProvider.configure({
onCopiedDefaultCallback: callbackWithOneParam
});

function callbackWithBothParams(copiedText, $injector){
var $log = $injector.get("$log");
$log.info("callbackWithBothParams(): Text copied, and callback was injected with the $log service");
}

function callbackWithoutParams(){
console.log("callbackWithoutParams");
}

function callbackWithOneParam(text){
console.log("callbackWithOneParam(): " + text);
}
}]);

demoApp.controller('DemoCtrl', ['$scope', function ($scope) {
$scope.supported = false;

Expand Down
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function(config) {
reporters: ['progress', 'coverage'],

preprocessors: {
'angular-clipboard.js': ['coverage']
//'angular-clipboard.js': ['coverage']
},

coverageReporter: {
Expand Down Expand Up @@ -56,14 +56,14 @@ module.exports = function(config) {
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Firefox'],
browsers: ['Chrome'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
singleRun: false

});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"devDependencies": {
"jasmine-core": "^2.3.4",
"karma": "~1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "~1.1.0",
"karma-firefox-launcher": "~1.0.0",
"karma-jasmine": "~1.0.2"
"karma-jasmine": "~1.1.0"
}
}
114 changes: 111 additions & 3 deletions test/angular-clipboard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ describe('angular-clipboard', function () {

beforeEach(angular.mock.inject(function ($rootScope, $compile) {
scope = $rootScope;
elm = $compile('<button clipboard supported="supported" text="textToCopy" on-copied="success()" on-error="fail(err)">Copy</button>')(scope);
elm = $compile('<button clipboard supported="supported" text="textToCopy" on-copied="success(text)" on-error="fail(err)">Copy</button>')(scope);

scope.supported = undefined;
scope.textToCopy = 'Copy me!';
scope.copied = false;
scope.success = function () {scope.copied = true;};
scope.fail = function (err) {};
scope.success = function (text) {
//console.log("scope.success called: ", text);
scope.copied = true;
};
scope.fail = function (err) {
//console.log(err);
};
scope.$digest();

spyOn(scope, 'success').and.callThrough();
Expand Down Expand Up @@ -50,4 +55,107 @@ describe('angular-clipboard', function () {
it('should feature detect and set supported', function () {
expect(scope.supported).toEqual(true);
});

});

describe('with provider', function() {

var $compile, $rootScope, element, flag;

/*
this.options = {
onCopiedDefaultCallback: false,
onErrorDefaultCallback: false
};
this.configure = function (options) {
angular.extend(this.options, options);
};
*/


describe('set with default callbacks', function() {

beforeEach(module('angular-clipboard', function (angularClipboardProvider) {

flag = false;

function onCopied(text) {
flag = text;
//console.log("default onCopied called: ", text);
}

function onError(err) {
flag = err;
}

angularClipboardProvider.configure({
onCopiedDefaultCallback: onCopied,
onErrorDefaultCallback: onError
});

}));

beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;

$rootScope.textToCopy = 'Copy me!';

$rootScope.success = function() {
flag = 'override copied';
}

$rootScope.fail = function(err) {
flag = 'override fail';
}

}));

describe('and no callbacks in attributes', function() {

beforeEach(function() {
element = $compile('<button clipboard text="textToCopy">Copy</button>')($rootScope);
$rootScope.$digest();
});

it('should call the default on-copied', function () {
spyOn(document, 'execCommand').and.returnValue(true);
element.triggerHandler('click');
expect(flag).toBe($rootScope.textToCopy);
});

it('should call the default on-error', function () {
spyOn(document, 'execCommand').and.returnValue(false);
element.triggerHandler('click');
expect(flag).toBe('failure copy');
});
});

describe('but with callbacks in attributes', function() {

beforeEach(function() {
element = $compile('<button clipboard text="textToCopy" on-copied="success()" on-error="fail(err)">Copy</button>')($rootScope);
$rootScope.$digest();
});

it('should override the default success callback', function() {
spyOn(document, 'execCommand').and.returnValue(true);
element.triggerHandler('click');
expect(flag).toBe('override copied');

})
it('should override the default error callback', function() {
spyOn(document, 'execCommand').and.returnValue(false);
element.triggerHandler('click');
expect(flag).toBe('override fail');

})
});

});





});