Giter Club home page Giter Club logo

message-center's Introduction

Build Status

Message center service

This will allow you to add flash messages to your AngularJS app.

This service will expose a little API to allow you to display user messages on your site that can be closed and configured.

Service

The service is in charge to expose to the developer an API to create and delete messages from the Message Center.

Message types

There are four types of messages, these types are used for severity and presentation purposes:

  • info: no severity meaning inferred from this message type.
  • warning: warn the user about something.
  • danger: an error has happened or a dangerous situation has been detected.
  • success: everything went as expected.

If you use an invalid message type the Message Center will throw an error.

Message status

There are four status a message can be in. Depending on the status of the message it will be displayed or not to the screen, or even deleted from the message center. It's the developer's task to set the correct initial status and understand the implications of that.

  • unseen: this means that the message has not yet been seen by the user. This will be changed to shown automatically in the next route change. This is the default status if anything else is specified.
  • shown: this means that the message has been already shown to the user. This message will be deleted from the Message Center in the next route change.
  • next: this one is a bit special. Odds are that you will show a message and right after that change your route. If that happens your message will only be seen for a fraction of a second. To avoid that use the next status, that will make the message available to the next page.
  • permanent: use this status to leave the message there until the $rootScope is reset or the user intentionally closes the message.

Creating new messages

To create a new message you need to follow 3 simple steps:

  1. Inject the MessageCenterModule module in your current module. For instance in your app.js file you may add:
```js
angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers',
  'MessageCenterModule', // <---
]);
```
  1. Inject the messageCenterService service in your controller where you want to insert the message. For instance:
```js
angular.module('myApp.controllers', []). // Since you injected globally in app.js you don't need to do it here.
controller('fooController', ['messageCenterService', function (messageCenterService) { … }]);
```
  1. Use the service methods. Feel free to explore the code for more available methods. For instance to add a message:
```js
messageCenterService.add('success', 'Your action has been completed!', { status: messageCenterService.status.permanent });
```

HTML

If you need to add HTML to a message you can do so by passing in the html option. HTML is disabled by default.

messageCenterService.add('success', '<strong>HTML</strong> <em>is</em> <span>allowed</span>.', { html: true });

Timer

You can add a timer to the alert so it auto dismisses. To do so, just add a timeout option in miliseconds.

messageCenterService.add('success', 'Bye bye in 3s!', { timeout: 3000 });

Directive

The directive mcMessages will allow you to place the messages wherever you want in your layout. Just drop <mc-messages></mc-messages> or <div mc-messages></div> somewhere in your partials and if there are any messages to be shown they will be shown there. Since it's a regular directive you can perform the common alterations and modifications to it to suit your needs.

Bootstrap

This messages integrate seamlessly with the alerts from Twitter bootstrap.

Installation

bower install message-center --save

message-center's People

Contributors

bitdeli-chef avatar dotob avatar e0ipso avatar eduards avatar egertv avatar kitak avatar ma-zal avatar retbrown avatar simison avatar tdjones avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

message-center's Issues

Update to Angular 1.4.X

Are you planning on checking the compatibility and making it compatible?

Please let us know your plans!

Thank you

Timeout working?

Hi,

It seems like the timeout option isn't working for me? Is that a new feature - not sure if I have the latest as its via bower, so sorry if thats the case.

Thanks

improvement

Please can you put how state is set for default?

A good improvement would be add a extended message.

Good module, thanks

Directive not shown inside ui-view

Given the current setup:

<body ng-app="app">
    <div ui-view=""></div>
</div>

And including the <mc-messages></mc-messages> directive inside the view, it will only show if the directive is also included outside the view, as such:

<body ng-app="app">
    <div style="display: none" mc-messages></div>
    <div ui-view=""></div>
</div>

Why is this happening?

HTML option not working

The following example from the README doesn't work as expected:

messageCenterService.add('success', '<strong>HTML</strong> <em>is</em> <span>allowed</span>.', { html: true });

Results in a plain-text message (using the included mc-messages directive):
screen shot 2014-08-13 at 2 36 32

Default timeout?

Hi

Is it possible to have a default timeout for messages?

Thanks

Status transition from "next" after route change.

Currently when a next message is added and route is changed, the new status becomes unseen.
Is there any design-driven reason for that? Could the state be changed to shown instead? We are showing the message in the new state anyway, aren't we?

My use-case is this. State A adds next message "ab" and triggers transition to state B. While at state B, some new messages are added with status shown. Now at some point (on user action) I want to clear all showed messages, including those coming from previous state (such as "abc"). But since "abc" is "unseen", removeShown() cannot be used for that.

I know I could call markShown() just before removeShown() for this case, but that looks more like a workaround to me.

Next status and route change

After creating a message with the "next" status the message is displayed on the succeeding page. When I then navigate to another page the message continues to be displayed. The message.processed is true and the message.status is unseen. As a result, when changeReaction (removeShown()) is called on route change the message is not being removed.

I am not putting the mc-messages in the partial pages. It is in index.html just preceding the ui-view.

Demo?

It would help to choose if you have a demo which shows your modules strength. Thanks.

Icon ?

Hello,

this would be cool if we could set an icon to a message (fontawesome for exemple) :

Alert with icon

Leaky listener causes performance issues and undesirable behaviour with multiple directive use.

When using this library with ui-router I noticed that I was getting undesirable behaviour, 'next' wasn't working.
I tracked the issue down to the way message-center attaches its $locationChangeStart listener to the root scope.
If you have multiple directives used, or in the case of ui-router how ever it swaps the ui-views the rootScope's listener is continually added too.
This results in multiple calls to 'changeReaction' on every state transition as it builds up each time a view with the directive is loaded.

I'm able to reproduce this with the test file by embedding the <div mc-messages></div> in the views:

<script type="text/ng-template" id="edit">
      <article data-ng-controller="EditController">
        <div mc-messages></div>
        <h1>Edit</h1>
        <button id="saveSuccess" data-ng-click="saveSuccess()">Save</button>
        <button id="saveTempSuccess" data-ng-click="saveTempSuccess()">Save (temp)</button>
        <button id="saveMultipleSuccess" data-ng-click="saveMultipleSuccess()">Save (multiple success)</button>
        <button id="saveMultipleTypes" data-ng-click="saveMultipleTypes()">Save (multiple types)</button>
        <button id="saveFailure" data-ng-click="saveFailure()">Save (failure)</button>
        <button id="saveSuccessGoHome" data-ng-click="saveSuccessGoHome()">Save and Go Home</button>

      </article>
    </script>

    <script type="text/ng-template" id="html">
      <article data-ng-controller="AllowHTMLController">
        <div mc-messages></div>
        <h1>HTML</h1>
        <button id="allowedHTML" data-ng-click="allowedHTML()">Allowed HTML</button>
        <button id="plainText" data-ng-click="plainText()">Plain Text</button>
      </article>
    </script>

    <script type="text/ng-template" id="permanent">
      <article data-ng-controller="PermanentController">
          <div mc-messages></div>
          <h1>Permanent</h1>
          <button id="goIndex" data-ng-click="goIndex()">Index page</button>
      </article>
    </script>


    <article data-ng-view />

if you add logging to the directive:

$rootScope.$on('$locationChangeStart', changeReaction);
--> console.log("Watchers: " + $rootScope.$$listeners.$locationChangeStart.length);

You'll see the count increase as you transition states and the 'next' style messages break.

$locationChangeSuccess called after controllers are initialized

Coming from $state.go(...) (https://github.com/angular-ui/ui-router), $locationChangeSuccess is called AFTER controller are initialized and views are loaded. This causes a small problem displaying messages.

Scenario:
I call $state.go('newState') and controller A gets initialized.
controller A has a method init() which will call messageCenterService.add('info', 'Message', options);
AFTER this, angular will now broadcast $locationChangeSuccess and the following happens:

// Update 'unseen' messages to be marked as 'shown'.
messageCenterService.markShown();
// Remove the messages that have been shown.
messageCenterService.removeShown();

(https://github.com/e0ipso/message-center/blob/master/message-center.js lines 125-132)

Because the newly created info-message is in an 'unseen' state, it will be marked as 'shown' and removed. Due to this, after everything is loaded, user can't see the message.

Workaround for this is to also listen to $locationChangeSuccess in controller A and recall the message.

$rootScope.$on('$locationChangeSuccess', function locationChanged(event, to) {
    showMessage();
});

But this needs a better solution, not a hack.

Need ability to remove messages without the controller or view changing

Here's my case scenario: I'm building an upload feature. If the user selects a file with bad data, we show an error message and give them a chance to re-upload the file once they've addressed the changes. Once they start to re-upload the file, I need to be able to remove the error messages and start fresh.

The only way I can do that with this directive is to force the $rootScope.mcMessages message to change.

// function that gets triggered when user initiates upload
$scope.upload = function() { 
  // First, remove any errors that might have occurred from a previous upload attempt
   messageCenterService.reset();
   // Shouldn't have to explicitly do this next part but unfortunately I have to
   $rootScope.mcMessages = messageCenterService.mcMessages;

   // Parse the file

   if ( errors occurred ) {
     messageCenterService.add("danger", "Error occurred, please try again!");
     // Shouldn't have to explicitly do this next part but unfortunately I have to
     $rootScope.mcMessages = messageCenterService.mcMessages;
  }
}

I feel like the directive itself should be what's refreshing the $rootScope.mcMessages array. Why do I have to do this explicitly myself? Am I missing something here?

Allowing HTML tags in message?

Trying to embed <strong>test</strong> tags in the message treats them as already escaped. I'd prefer it to interpret it as HTML, not as text. Use case is - what if I want to put images or symbology in here?

You should be able to use something like ng-bind-html to perform this.

Possible problem, markShown() is called before removeShown()

link: function(scope, element, attrs) {
        var changeReaction = function (event, to, from) {
          // Update 'unseen' messages to be marked as 'shown'.
          messageCenterService.markShown();
          // Remove the messages that have been shown.
          messageCenterService.removeShown();
          $rootScope.mcMessages = messageCenterService.mcMessages;
        };
        $rootScope.$on('$locationChangeStart', changeReaction);

        scope.animation = attrs.animation || 'fade in';
      }

Not sure I understand but maybe there is a problem that markShown() is called before removeShown() aren't unseen messages are market as shown and then removed before they have a chance to be seen?

Add doesn't work after reset

The following, self-explanatory code results in an empty mcMessages array (no message displayed).

messageCenterService.reset();
messageCenterService.add('danger', 'This is dangerous');

A workaround for this is to use:

messageCenterService.markShown();
messageCenterService.removeShown();
messageCenterService.add('danger', 'This is dangerous');

Does not show message

Minimal code, does not seem to work
On a more complex application with routing will show the message after 1st route change and then will continue to work

See the Pen qmgDy by Muly (@mulyoved) on CodePen.

<script async src="//codepen.io/assets/embed/ei.js"></script>

Please make a new release

Please release a new version (tag) and if possible publish to NPM so help browserify installations.

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.