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

Adding SweetAlert as a service #10

Merged
merged 1 commit into from
Jun 3, 2016
Merged
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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ Have a look at the [DEMO](http://tushariscoolster.github.io/ng-sweet-alert) or t
| sweet-on-confirm| `checkConfirm()`


----------

## Use SweetAlert as service

swal() gets two arguments;
first argument is parameters Objects (with default values).
second argument is Callback function when clicking on "OK"/"Cancel", which is a promise.
register to the promise (using 'then') and handle the resolve / reject according to your business logic.

Add 'SweetAlert' to your directive / controller / ect)
Use SweetAlert.confirm(msg, options) / SweetAlert.alert(msg, options) / SweetAlert.info(msg, options) / SweetAlert.success(msg, options)
pass arguments:

msg; String - The message to be displayed in the alert / confirm box (mandatory).
options; Object (optinal):
title: String - the title of the box.
type: String - "warning" / "info" / "error" / "success" / "" (empty string will not display a graphic icon).
showCancelButton: Boolean - shows the "cancel" button (true will behave like confirm dialog, false will behave like alert dialog).

Use returned promise;

```javascript
SweetAlert.confirm("Are you sure?", {title : "Careful now!"})
.then(function(p) { do something on success },
function(p) { do something on fail }
);

SweetAlert.success("You have successfully completed our poll!", {title: "Good job!"});
```


## Documentation: SweetAlert as a service


| | Option|
----------------- | ---------------------------- | ------------------
| msg (mandatory)| `"Are you sure you want to do that.."`
| options| `"{title: 'Delete this?', type: 'warning' showCancelButton: true}"`


----------

## Contributing
Expand Down
187 changes: 130 additions & 57 deletions ng-sweet-alert.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,135 @@
/**
* * @description directive for sweet alert
* @author Tushar Borole
* @createDate 18/04/2015
* @version 1.0.3
* @lastmodifiedDate 06/18/2015
*/



(function () {
'use strict';
// Check we have sweet alert js included
if (angular.isUndefined(window.swal)) {
throw "Please inlcude sweet alert js and css from http://t4t5.github.io/sweetalert/";
}

angular
.module('ng-sweet-alert',[])
.directive('sweetalert', sweetalert);

sweetalert.$inject = ['$parse'];

/* @ngInject */
function sweetalert($parse) {
// Usage:
//
// Creates:
//
var directive = {
link: link
};
return directive;

function link(scope, element, attrs, controller) {
var sweetElement = angular.element(element);
sweetElement.click(function () {
var sweetOptions = scope.$eval(attrs.sweetOptions);
var sweetConfirmOption = scope.$eval(attrs.sweetConfirmOption);
var sweetCancelOption = scope.$eval(attrs.sweetCancelOption);


swal(sweetOptions,
function (isConfirm) {
if (isConfirm) {
if (sweetConfirmOption) swal(sweetConfirmOption);
if (attrs.sweetOnConfirm) scope.$evalAsync(attrs.sweetOnConfirm);
sweetElement.trigger("click");
} else {
if (sweetCancelOption) swal(sweetCancelOption);
if (attrs.sweetOnCancel) scope.$evalAsync(attrs.sweetOnCancel);
}
});

})

}
* @author Tushar Borole
* @createDate 18/04/2015
* @version 1.0.3
* @lastmodifiedDate 06/18/2015
*/



(function() {
'use strict';


// Check we have sweet alert js included
if (angular.isUndefined(window.swal)) {
throw "Please inlcude sweet alert js and css from http://t4t5.github.io/sweetalert/";
}

angular
.module('ng-sweet-alert', [])
.directive('sweetalert', sweetalert)
.factory('SweetAlert', sweetalert_service);

sweetalert.$inject = ['$parse'];

/* @ngInject */
function sweetalert($parse) {
// Usage:
//
// Creates:
//
var directive = {
link: link
};
return directive;

function link(scope, element, attrs, controller) {
var sweetElement = angular.element(element);
sweetElement.click(function() {
var sweetOptions = scope.$eval(attrs.sweetOptions);
var sweetConfirmOption = scope.$eval(attrs.sweetConfirmOption);
var sweetCancelOption = scope.$eval(attrs.sweetCancelOption);


swal(sweetOptions,
function(isConfirm) {
if (isConfirm) {
if (sweetConfirmOption) swal(sweetConfirmOption);
if (attrs.sweetOnConfirm) scope.$evalAsync(attrs.sweetOnConfirm);

} else {
if (sweetCancelOption) swal(sweetCancelOption);
if (attrs.sweetOnCancel) scope.$evalAsync(attrs.sweetOnCancel);
}
});

})

}
}

// Use SweetAlert as service
//
// swal() gets two arguments;
// first argument is parameters Objects (with default values).
// second argument is Callback function when clicking on "OK"/"Cancel", which is a promise.
// register to the promise (using 'then') and handle the resolve / reject according to your business logic.
//
// Add 'SweetAlert' to your directive / controller / ect)
// Use SweetAlert.confirm(msg, options) / SweetAlert.alert(msg, options) / SweetAlert.info(msg, options) / SweetAlert.success(msg, options)
// pass arguments:
// msg; String - The message to be displayed in the alert / confirm box (mandatory).
// options; Object (optinal):
// title: String - the title of the box.
// type: String - "warning" / "info" / "error" / "success" / "" (empty string will not display a graphic icon).
// showCancelButton: Boolean - shows the "cancel" button (true will behave like confirm dialog, false will behave like alert dialog).
// Use returned promise;
//
// SweetAlert.confirm("Are you sure?", {title : "Careful now!"})
// .then(function(p) { do something on success },
// function(p) { do something on fail }
// );
//
// SweetAlert.success("You have successfully completed our poll!", {title: "Good job!"});

sweetalert_service.$inject = ['$q'];

function sweetalert_service($q) {
function swal_alert(message, options) {
return swal_confirm(message, angular.extend({
title: "Alert",
text: message,
type: "warning",
showCancelButton: false
}, options));
}

function swal_info(message, options) {
return swal_alert(message, angular.extend({
type: "info"
}, options));
}

function swal_success(message, options) {
return swal_alert(message, angular.extend({
type: "success"
}, options));
}

function swal_confirm(message, options) {
var defered = $q.defer();
var options = angular.extend({
title: "Alert",
text: message,
type: "warning",
showCancelButton: true
}, options);
swal(options, function() {
defered.resolve();
}, function() {
defered.reject();
});
return defered.promise;
}
return {
alert: swal_alert,
confirm: swal_confirm,
info: swal_info,
success: swal_success
};
}

})();