Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Last active August 29, 2015 14:17
Show Gist options
  • Save pinalbhatt/6c5bfa72273a4f64fc6c to your computer and use it in GitHub Desktop.
Save pinalbhatt/6c5bfa72273a4f64fc6c to your computer and use it in GitHub Desktop.
AngularJS Toastr App [PBDesk.Toastr]
/*
Depends on Toastr JS Library
https://github.com/CodeSeven/toastr
<link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
*/
(function () {
'use strict';
angular.module('PBDesk.Toastr', [])
.constant('Toastr', window.toastr)
.factory('ToastrFactory', ToastrFactory);
ToastrFactory.$inject = ['Toastr'];
function ToastrFactory(Toastr) {
var service = {
toastr: Toastr,
setOptions: setOptions,
popSuccess: popSuccess,
popError: popError,
popWarning: popWarning,
popInfo: popInfo
};
return service;
function setOptions(toastrOptions) {
if (toastrOptions !== 'undefined') {
Toastr.options = toastrOptions;
}
else {
Toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": true,
"progressBar": true,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
}
}
function popSuccess(msg, title, options) {
pop('success', msg, title, options);
}
function popError(msg, title, options) {
pop('error', msg, title, options);
}
function popWarning(msg, title, options) {
pop('warning', msg, title, options);
}
function popInfo(msg, title, options) {
pop('info', msg, title, options);
}
function pop(popType, msg, title, options) {
setOptions(options);
Toastr[popType](msg, title);
}
}
})();
(function () {
'use strict';
angular
.module('PBDesk.Toastr')
.controller('ToastrTestController', ToastrTestController);
ToastrTestController.$inject = ['$scope', 'Toastr', 'ToastrFactory'];
function ToastrTestController($scope, Toastr, ToastrFactory) {
$scope.title = 'Toastr Test';
$scope.pop = function () {
Toastr.info('msg', 'ttl');
ToastrFactory.toastr.warning('warning');
ToastrFactory.popSuccess('smag', 'stitle');
ToastrFactory.popError('smag', 'stitle');
ToastrFactory.popWarning('smag', 'stitle');
ToastrFactory.popInfo('smag', 'stitle');
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment