Last active
April 7, 2016 06:44
-
-
Save BideoWego/2bafc45bdb774983a5adb30ea6d1fd01 to your computer and use it in GitHub Desktop.
Angular flash message alert service and directive with Rails style alert type names and a Bootstrap template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="flash" ng-if="flash.length"> | |
<div class="alerts" ng-repeat="(key, messages) in flash.alerts"> | |
<div class="alert alert-dismissible" role="alert" ng-repeat="message in messages" ng-class="'alert-' + key"> | |
<button type="button" class="close" data-dismiss="alert" aria-label="Close" ng-click="flash.destroy(key, $index)"> | |
<span aria-hidden="true">×</span> | |
</button> | |
{{ message }} | |
</div> | |
</div> | |
</div> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------- | |
// flashDirective | |
// ---------------------------------------- | |
Othello.directive('flash', function() { | |
return { | |
templateUrl: '/js/templates/shared/flash.html', | |
restrict: 'AE', | |
scope: { | |
flash: '=' | |
} | |
}; | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------- | |
// FlashService | |
// ---------------------------------------- | |
// Usage: | |
// 1. Inject FlashService into your controller | |
// 2. Bind to $scope | |
// $scope.flash = FlashService; | |
// or | |
// Use internally as FlashService | |
// 3. Use in your view or controller | |
// $scope.create('Hello!'); | |
// <a href="" ng-click="flash.create('Hello!')">Greet</a> | |
// | |
// May be created or destroyed via | |
// $scope.flash.create(typeOrMessage, [message if type specified]) | |
// $scope.flash.destroy(type, index) | |
// | |
// Messages are available via | |
// $scope.flash.alerts[alertType] | |
Othello.factory('FlashService', | |
['_', | |
function(_) { | |
// ---------------------------------------- | |
// Private | |
// ---------------------------------------- | |
var _reservedWords = [ | |
'success', | |
'notice', | |
'info', | |
'warn', | |
'warning', | |
'danger', | |
'error' | |
]; | |
var _resolveType = function(type) { | |
return { | |
notice: 'info', | |
error: 'danger', | |
warn: 'warning' | |
}[type] || type; | |
}; | |
var _createTypeIfNotExists = function(type) { | |
if (_alerts[type] === undefined) { | |
_alerts[type] = []; | |
} | |
}; | |
var _resolveCreateOptions = function(a, b) { | |
if (_.isObject(a)) { | |
a.type = a.type || 'info'; | |
return a; | |
} | |
var type = a, | |
message = b; | |
if (!_.contains(_reservedWords, a)) { | |
type = 'info'; | |
message = a; | |
} | |
return { | |
type: type, | |
message: message | |
}; | |
}; | |
var _alerts = {}; | |
// ---------------------------------------- | |
// Public | |
// ---------------------------------------- | |
var FlashService = { | |
length: 0, | |
alerts: _alerts, | |
create: function(a, b) { | |
var options = _resolveCreateOptions(a, b), | |
type = options.type, | |
message = options.message; | |
type = _resolveType(type); | |
_createTypeIfNotExists(type); | |
if (!_.find(_alerts[type], function(alert) { | |
return alert === message; | |
})) { | |
_alerts[type].push(message); | |
this.length++; | |
} | |
return this; | |
}, | |
destroy: function(type, id) { | |
type = _resolveType(type); | |
_alerts[type].splice(id, 1); | |
this.length--; | |
return this; | |
}, | |
clear: function() { | |
_.each(_alerts, function(value, key) { | |
_alerts[key] = []; | |
}); | |
} | |
}; | |
return FlashService; | |
}]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment