Skip to content

Commit

Permalink
Added support to send flashes only to named subscribers.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmluke committed Oct 3, 2013
1 parent 2f4645d commit 615ad89
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ module.exports = function (grunt) {
browsers: ['PhantomJS']
},
debug: {
singleRun: false
singleRun: false,
reporters: ['progress', 'junit']
}
},
concat: {
Expand Down
14 changes: 14 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,23 @@
</div>

<div ng-view></div>

<div class="row row-alert">
<div class="span12">
<div id="alert-1" flash-alert active-class="in" class="alert fade" duration="0">
<button type="button" class="close" ng-click="hide()">&times;</button>
<i class="icon-ok-sign"></i>
<strong class="alert-heading">{{flash.type}}</strong>
<span class="alert-message">{{flash.message}}</span>
<h4 class="pull-right">Alert #1</h4>
</div>

</div>
</div>
</div>

<script src="components/jquery/jquery.js"></script>
<script src="components/bootstrap/docs/assets/js/bootstrap.min.js"></script>
<script src="components/angular/angular.js"></script>
<script src="components/angular-route/angular-route.js"></script>
<script src="angular-flash.js"></script>
Expand Down
23 changes: 20 additions & 3 deletions app/scripts/controllers/main-ctrl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(function () {
'use strict';

var MainCtrl = function ($scope, flash, $timeout) {
var MainCtrl = function ($scope, flash) {


$scope.all = function () {
$scope.info();
Expand All @@ -14,21 +15,37 @@
flash.info = 'info message';
};

$scope.info1 = function () {
flash.to('alert-1').info = 'info message';
};

$scope.warn = function () {
flash.warn = 'warn message';
};

$scope.warn1 = function () {
flash.to('alert-1').warn = 'warn message';
};

$scope.success = function () {
flash.success = 'success message';
};

$scope.success1 = function () {
flash.to('alert-1').success = 'success message';
};

$scope.error = function () {
flash.error = 'error message';
};

$scope.error1 = function () {
flash.to('alert-1').error = 'error message';
};

$scope.all();
};

angular.module('App.main-ctrl', [])
.controller('MainCtrl', ['$scope', 'flash', '$timeout', MainCtrl]);
}());
.controller('MainCtrl', ['$scope', 'flash', MainCtrl]);
}());
25 changes: 21 additions & 4 deletions app/views/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">angular-flash <i class="icon-bolt"></i></a>
<button class="btn btn-info" ng-click="info()"><i class="icon-info-sign"></i> Info</button>
<button class="btn btn-warning" ng-click="warn()"><i class="icon-warning-sign"></i> Warning</button>
<button class="btn btn-success" ng-click="success()"><i class="icon-ok-sign"></i> Success</button>
<button class="btn btn-danger" ng-click="error()"><i class="icon-exclamation-sign"></i> Error</button>

<div class="btn-group">
<button class="btn btn-info" ng-click="info()"><i class="icon-info-sign"></i> Info</button>
<button class="btn btn-info" ng-click="info1()">Alert #1</button>
</div>

<div class="btn-group">
<button class="btn btn-warning" ng-click="warn()"><i class="icon-warning-sign"></i> Warning</button>
<button class="btn btn-warning" ng-click="warn1()">Alert #1</button>
</div>

<div class="btn-group">
<button class="btn btn-success" ng-click="success()"><i class="icon-ok-sign"></i> Success</button>
<button class="btn btn-success" ng-click="success1()">Alert #1</button>
</div>

<div class="btn-group">
<button class="btn btn-danger" ng-click="error()"><i class="icon-exclamation-sign"></i> Error</button>
<button class="btn btn-danger" ng-click="error1()">Alert #1</button>
</div>

<button class="btn btn-primary" ng-click="all()"><i class="icon-flag"></i> All</button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-flash",
"description": "Flash messages for Angular Js",
"version": "0.1.8",
"version": "0.1.9",
"main": [
"dist/angular-flash.min.js",
"dist/angular-flash.js"
Expand Down
34 changes: 25 additions & 9 deletions dist/angular-flash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**!
* @license angular-flash v0.1.8
* @license angular-flash v0.1.9
* Copyright (c) 2013 William L. Bunselmeyer. https://github.com/wmluke/angular-flash
* License: MIT
*/
Expand All @@ -10,6 +10,8 @@

var Flash = function (options) {
var _options = angular.extend({
id: null,
subscribers: [],
classnames: {
error: [],
warn: [],
Expand All @@ -19,37 +21,45 @@
}, options);

var _self = this;
var _subscribers = [];
var _success;
var _info;
var _warn;
var _error;
var _type;

function _notify(type, message) {
angular.forEach(_subscribers, function (subscriber) {
if (!subscriber.type || subscriber.type === type) {
angular.forEach(_options.subscribers, function (subscriber) {
var matchesType = !subscriber.type || subscriber.type === type;
var matchesId = (!_options.id && !subscriber.id) || subscriber.id === _options.id;
if (matchesType && matchesId) {
subscriber.cb(message, type);
}
});
}

this.clean = function () {
_subscribers = [];
_options.subscribers = [];
_success = null;
_info = null;
_warn = null;
_error = null;
_type = null;
};

this.subscribe = function (subscriber, type) {
_subscribers.push({
this.subscribe = function (subscriber, type, id) {
_options.subscribers.push({
cb: subscriber,
type: type
type: type,
id: id
});
};

this.to = function (id) {
var options = angular.copy(_options);
options.id = id;
return new Flash(options);
};

Object.defineProperty(this, 'success', {
get: function () {
return _success;
Expand Down Expand Up @@ -111,6 +121,12 @@
return _options.classnames;
}
});

Object.defineProperty(this, 'id', {
get: function () {
return _options.id;
}
});
};

angular.module('angular-flash.service', [])
Expand Down Expand Up @@ -196,7 +212,7 @@
}
}

flash.subscribe(show, attr.flashAlert);
flash.subscribe(show, attr.flashAlert, attr.id);

/**
* Fixes timing issues: display the last flash message sent before this directive subscribed.
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-flash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "angular-flash",
"description": "Flash messages for Angular JS",
"private": false,
"version": "0.1.8",
"version": "0.1.9",
"homepage": "https://github.com/wmluke/angular-flash",
"author": {
"name": "William L. Bunselmeyer",
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var FooController = function(flash){

// Publish a error flash
flash.error = 'Fail!';

// Publish an info flash to the `alert-1` subscriber
flash.to('alert-1').info = 'Only for alert 1';
};

FooController.$inject = ['flash'];
Expand Down Expand Up @@ -78,6 +81,12 @@ Use the `flash-alert` directive to subscribe to flash messages...
<span class="alert-message">{{flash.message}}</span>
</div>

<!-- Subscribe to all flash messages sent to `alert-1`. -->
<div id="alert-1" flash-alert active-class="in" class="alert fade">
<strong class="alert-heading">Boo!</strong>
<span class="alert-message">{{flash.message}}</span>
</div>

<!-- Set the display duration in milli-secs. Default is 5000, 0 disables the fade-away. -->
<div flash-alert active-class="in" class="alert fade" duration="0">
<!-- Manually hide the alert with `hide()` -->
Expand Down
2 changes: 1 addition & 1 deletion src/directives/flash-alert-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
}
}

flash.subscribe(show, attr.flashAlert);
flash.subscribe(show, attr.flashAlert, attr.id);

/**
* Fixes timing issues: display the last flash message sent before this directive subscribed.
Expand Down
30 changes: 23 additions & 7 deletions src/services/flash-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

var Flash = function (options) {
var _options = angular.extend({
id: null,
subscribers: [],
classnames: {
error: [],
warn: [],
Expand All @@ -14,37 +16,45 @@
}, options);

var _self = this;
var _subscribers = [];
var _success;
var _info;
var _warn;
var _error;
var _type;

function _notify(type, message) {
angular.forEach(_subscribers, function (subscriber) {
if (!subscriber.type || subscriber.type === type) {
angular.forEach(_options.subscribers, function (subscriber) {
var matchesType = !subscriber.type || subscriber.type === type;
var matchesId = (!_options.id && !subscriber.id) || subscriber.id === _options.id;
if (matchesType && matchesId) {
subscriber.cb(message, type);
}
});
}

this.clean = function () {
_subscribers = [];
_options.subscribers = [];
_success = null;
_info = null;
_warn = null;
_error = null;
_type = null;
};

this.subscribe = function (subscriber, type) {
_subscribers.push({
this.subscribe = function (subscriber, type, id) {
_options.subscribers.push({
cb: subscriber,
type: type
type: type,
id: id
});
};

this.to = function (id) {
var options = angular.copy(_options);
options.id = id;
return new Flash(options);
};

Object.defineProperty(this, 'success', {
get: function () {
return _success;
Expand Down Expand Up @@ -106,6 +116,12 @@
return _options.classnames;
}
});

Object.defineProperty(this, 'id', {
get: function () {
return _options.id;
}
});
};

angular.module('angular-flash.service', [])
Expand Down
Loading

0 comments on commit 615ad89

Please sign in to comment.