voltron-flash 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17d92f27c04a2dc4ce3090358c460b8d231235e9
4
- data.tar.gz: 3bc8bae02f7f43312f94f5741d6aa680daa994b7
3
+ metadata.gz: d4db74de2e9ac309a8ce39bedbffe0b41c0d7f0d
4
+ data.tar.gz: 568162b3c4b4ee45271331f0b656ef8f5ae906cb
5
5
  SHA512:
6
- metadata.gz: 82aaaca6352277bdd6cc88a6a398d99d1100330a087f671be6165442b8d93bc66f392654d40c58c4efce33f93258c38ec3e9bac685f65b802caa473cc27940ac
7
- data.tar.gz: d27833d1d040666499cf590df8a9e1a3a61fdd3b1b6b6ee6f74a86bff18409d2efc5e8b20b773f379a7223448b8ed6d745b22b400f3a54be7e6d1e55cdb3bf77
6
+ metadata.gz: 49c3a886e3a7f5bc90066fce398dc0de5df022591cb04c23252d65edb57048d585024d55ba0a47d7772075144d12f2254af40e7e1bb863c0eb84639a2bf0e2a6
7
+ data.tar.gz: 6452ee01e01e8e753032367a06eb6d0e2eed5984e1de37f2faf93f5618c3711c39211f4205dd4dcd7307cb8f6645849768f22e3a4d0251f56a2f888fc1c533db
data/.gitignore CHANGED
@@ -10,4 +10,4 @@ tmp/
10
10
  log/
11
11
  voltron-*.gem
12
12
  *.bak
13
- *.sqlite3
13
+ public/
data/.travis.yml CHANGED
@@ -3,4 +3,6 @@ language: ruby
3
3
  rvm:
4
4
  - 2.2.3
5
5
  - 2.3.1
6
+ - 2.3.3
7
+ - 2.4.1
6
8
  before_install: gem install bundler -v 1.12.5
data/Gemfile CHANGED
@@ -1,5 +1,9 @@
1
1
  source 'https://rubygems.org'
2
- source 'http://gem.minow.io'
2
+ source 'https://gem.minow.io'
3
3
 
4
4
  # Specify your gem's dependencies in voltron-flash.gemspec
5
5
  gemspec
6
+
7
+ group :test do
8
+ gem 'coveralls', require: false
9
+ end
data/README.md CHANGED
@@ -1,43 +1,194 @@
1
+ [![Coverage Status](https://coveralls.io/repos/github/ehainer/voltron-flash/badge.svg?branch=master)](https://coveralls.io/github/ehainer/voltron-flash?branch=master)
1
2
  [![Build Status](https://travis-ci.org/ehainer/voltron-flash.svg?branch=master)](https://travis-ci.org/ehainer/voltron-flash)
3
+ [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)
2
4
 
3
5
  # Voltron::Flash
4
6
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/voltron/flash`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- TODO: Delete this and the text above, and describe your gem
7
+ Unifying `flash` and `flash.now` logic into one single method, while also being able to pass flash messages easily through AJAX requests.
8
8
 
9
9
  ## Installation
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'voltron-flash'
14
+ gem 'voltron-flash', '~> 0.1.6'
15
15
  ```
16
16
 
17
17
  And then execute:
18
18
 
19
- $ bundle
19
+ $ bundle install
20
20
 
21
21
  Or install it yourself as:
22
22
 
23
23
  $ gem install voltron-flash
24
24
 
25
+ Then run the following to create the voltron.rb initializer (if not exists already) and add the upload config:
26
+
27
+ $ rails g voltron:flash:install
28
+
29
+ Also, include the necessary js and css by adding the following to your application.js and application.css respectively
30
+
31
+ ```javascript
32
+ //= require voltron-flash
33
+ ```
34
+
35
+ ```css
36
+ /*
37
+ *= require voltron-flash
38
+ */
39
+ ```
40
+
41
+ If you want to customize the out-of-the-box functionality or styles, you can copy the assets (javascript/css) to your app assets directory by running:
42
+
43
+ $ rails g voltron:flash:install:assets
44
+
45
+ Optionlly, you may copy the flash markup template by running:
46
+
47
+ $ rails g voltron:flash:install:views
48
+
49
+ The template will be placed in the `<rails_root>/app/views/voltron/flash/` directory.
50
+
25
51
  ## Usage
26
52
 
27
- TODO: Write usage instructions here
53
+ Voltron Flash is designed to handle flash messages appropriately depending on whether a controller renders or redirects, and whether or not the request is an AJAX request. All the developer needs to do is use the new `flash!` message.
54
+
55
+ ```ruby
56
+ class UserController < ActionController::Base
57
+
58
+ def create
59
+ @user = User.new(user_params)
60
+
61
+ if @user.save
62
+ flash! notice: 'User created successfully'
63
+ redirect_to user_path(@user)
64
+
65
+ # Or, you can still add flashes like so if you want. The old ways still work fine
66
+ redirect_to user_path(@user), notice: 'User created successfully'
67
+ else
68
+ flash! alert: @user.errors.full_messages
69
+ render :new
70
+ end
71
+
72
+ # ... define other things, like our +user_params+ method
73
+ end
74
+
75
+ end
76
+ ```
77
+
78
+ I hear you: "What's the point of that? You just made a single method that determines whether to use flash vs. flash.now. That's pretty boring."
79
+
80
+ Meh, depends on your perspective. But part of what makes this useful is it's ability to pass flash messages back as a part of the AJAX response headers.
81
+
82
+ Using that same example controller from above, the following code (assuming jQuery) is all that's required to handle the flash messages given an AJAX request to the same `create` action.
83
+
84
+ ```js
85
+ $.ajax({
86
+ url: '/users/create',
87
+ method: 'POST',
88
+ data: {
89
+ authenticity_token: '...'
90
+ first_name: 'Test',
91
+ last_name: 'Example',
92
+ email: '',
93
+ // ...
94
+ }
95
+ })
96
+ ```
97
+
98
+ Note that there is nothing in that code that relates specifically to the handling flash messages. That's because that is entirely handled by the Voltron Flash js module (see: Installation, specifically for assets).
99
+
100
+ ## Module Usage
101
+
102
+ Included with the gem is a Voltron module that handles the creation/addition/removal of flash messages. Out of the box there should be little to nothing you need to do with it, as it's fairly flexible in terms of how it can be used already.
103
+
104
+ The most commonly utilized methods will likely be `new` and `clear`, which are used like so:
105
+
106
+ ```js
107
+ // Adds several messages at once with optionally defined options
108
+ Voltron('Flash/new', {
109
+ notice: 'This is a notice message', // Specify as single message,
110
+ alert: ['This is an alert', 'This too is an alert'] // or an array of messages
111
+ }, {
112
+ bind: '#container',
113
+ group: true
114
+ });
115
+
116
+ // To clear all flash messages
117
+ Voltron('Flash/clear');
118
+ // To clear a specific flash message by fading it out over 1 second
119
+ Voltron('Flash/clear', {
120
+ element: '.selector-matching-flash-or-child-element' // Can be css selector, raw DOM element, or jQuery object
121
+ }, {
122
+ concealMethod: 'fadeOut',
123
+ concealTime: 1000
124
+ });
125
+
126
+ // The above is functionally equivalent to:
127
+
128
+ Voltron.getModule('Flash').new({
129
+ notice: 'This is a notice message',
130
+ alert: ['This is an alert', 'This too is an alert']
131
+ }, {
132
+ bind: '#container',
133
+ group: true
134
+ });
135
+
136
+ // To clear all flash messages
137
+ Voltron.getModule('Flash').clear();
138
+ // To clear a specific flash message by fading it out over 1 second
139
+ Voltron.getModule('Flash').clear({
140
+ element: '.selector-matching-flash-or-child-element' // Can be css selector, raw DOM element, or jQuery object
141
+ }, {
142
+ concealMethod: 'fadeOut',
143
+ concealTime: 1000
144
+ });
145
+ ```
146
+
147
+ ## Configuration
148
+
149
+ #### Rails Configuration
150
+
151
+ | Option | Default | Comment |
152
+ |--------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
153
+ | header | X-Flash | The AJAX response header that will contain the flash messages JSON. Should be no reason to change this unless it conflicts with an already defined response header. |
154
+ | group | true | Whether or not to "group" flash messages of the same type together in a single container with one "close" button. If `false`, each flash message regardless of type will be in it's own containing div with it's own close (X) button. This option can be overridden when adding flash messages via the js module's `new` method. |
155
+
156
+ #### Module Configuration
157
+
158
+ The following are options that can optionally be defined as the second argument to the module's `new` or `clear` methods. Each can also be set as a "default" by calling `Voltron('Flash/setConfig', { option: value, ... })` in a module initializer. Useful for reducing the amount of code needed to be written, since you can define things like `bind` once instead of every time you add a flash message.
159
+
160
+ | Option | Default | Comment |
161
+ |----------------|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
162
+ | class | (blank) | A css class (or class names) to be added to the newly created flash message div. Note that this is different than the `containerClass` option, which adds a class to the wrapping div. |
163
+ | bind | body | A css selector, jQuery object, or raw DOM element that will be the target for added flash messages. You can control where the markup is injected relative to this element with the `addMethod` option. |
164
+ | id | flashes | A unique id of the containing flash div (the wrapper element). If this is changed and flash messages can possibly be shown upon page render, it's important that you change the id of the element in the flash template as well (see: Installation, specifically the part about views) |
165
+ | containerClass | (blank) | A css class (or class names) to be added to the wrapper div when a flash message is added. If the wrapper div is already present on the page when a new flash message is added, this class will be added then. |
166
+ | addMethod | prepend | One of [prepend](http://api.jquery.com/prepend/), [append](http://api.jquery.com/append/), [before](http://api.jquery.com/before/), or [after](http://api.jquery.com/after/), determines where the wrapper div will appear relative to the `bind` element. |
167
+ | revealMethod | slideDown | Any jQuery method that could be used to reveal the flash element. Go-to options are among slideDown/slideUp, fadeIn, or show |
168
+ | revealTime | 200 | The time (is milliseconds) to reveal the element using `revealMethod`. Will always be passed as the first argument to `revealMethod`, regardless of what it is. |
169
+ | concealMethod | slideUp | Any jQuery method that could be used to hide the flash element when closed. Go-to options are among slideDown/slideUp, fadeOut, or hide |
170
+ | concealTime | 200 | The time (is milliseconds) to hide the element using `concealMethod`. Will always be passed as the first argument to `concealMethod`, regardless of what it is. |
171
+ | autoClose | false | If true, will automatically hide the flash message after `autoCloseAfter` milliseconds |
172
+ | autoCloseAfter | 5000 | The number of milliseconds to allow the flash message to be visible after it's added. |
173
+ | autoAdd | true | This controls whether or not to automatically insert flash messages picked up by responding AJAX requests using the options defined. If false, it's assumed that one will observe the `onFlashReceived` event and handle things on their own. |
174
+
175
+ ## Events
176
+
177
+ Keeping it simple, the Flash module only dispatches one event out of the box. More can be added as needed by modifying `voltron-flash.js`, but for now:
178
+
179
+ | Event Name | Callback Function | Data | Comment |
180
+ |----------------|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
181
+ | flash:received | onFlashReceived | <ul><li>**flash:** The Voltron Flash JS instance</li><li>**flashes:** An object of received flash messages. The key is the flash message type (notice, alert, etc.), the value is always an array of messages.</li><li>**request:** The XHR object of the AJAX request</li><li>**event:** The AJAX completion event from jQuery</li></ul> | Dispatched as soon as an [AJAX request completes](http://api.jquery.com/ajaxComplete/) |
28
182
 
29
183
  ## Development
30
184
 
31
185
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
186
 
33
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
-
35
187
  ## Contributing
36
188
 
37
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/voltron-flash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
-
189
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ehainer/voltron-flash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
39
190
 
40
191
  ## License
41
192
 
42
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
193
+ The gem is available as open source under the terms of the [GNU General Public License](https://www.gnu.org/licenses/gpl-3.0.en.html).
43
194
 
@@ -38,13 +38,13 @@ Voltron.addModule('Flash', function(){
38
38
  },
39
39
 
40
40
  addListener: function(){
41
+ var self = this;
41
42
  $(document).ajaxComplete(function(event, request){
42
- if(_defaults.autoAdd){
43
- var flashes = request.getResponseHeader(Voltron.getConfig('flash/header', 'X-Flash'));
44
- var flash = $.parseJSON(flashes);
45
- if(flash){
46
- Voltron('Flash/new', flash);
47
- }
43
+ var flashes = request.getResponseHeader(Voltron.getConfig('flash/header', 'X-Flash'));
44
+ var flash = $.parseJSON(flashes);
45
+ Voltron.dispatch('flash:received', { event: event, request: request, flash: self, flashes: flash });
46
+ if(_defaults.autoAdd && flash){
47
+ Voltron('Flash/new', flash);
48
48
  }
49
49
  });
50
50
  },
@@ -53,11 +53,13 @@ Voltron.addModule('Flash', function(){
53
53
  options = $.extend(_defaults, options);
54
54
  var flash;
55
55
 
56
- this.getContainer(options).addClass(options.class);
56
+ this.getContainer(options).addClass(options.containerClass);
57
57
 
58
58
  $.each(flashes, $.proxy(function(type, messages){
59
59
  flash = this.addFlash(type, messages, options);
60
60
 
61
+ flash.addClass(options.class);
62
+
61
63
  if(flash.find('.flash-message').length == 1){
62
64
  // If this is the first flash message, reveal the whole container
63
65
  flash.find('.flash-message').show();
@@ -78,7 +80,7 @@ Voltron.addModule('Flash', function(){
78
80
  },
79
81
 
80
82
  addFlash: function(type, messages, options){
81
- var flash = this.getFlash(type);
83
+ var flash = this.getFlash(type, options);
82
84
 
83
85
  flash.append($.map($.makeArray(messages), function(message){
84
86
  return $('<p />', { class: 'flash-message' }).html(message).hide();
@@ -96,7 +98,7 @@ Voltron.addModule('Flash', function(){
96
98
  return flash;
97
99
  },
98
100
 
99
- getFlash: function(type){
101
+ getFlash: function(type, options){
100
102
  if(Voltron.getConfig('flash/group') && $('.flash.' + type).length){
101
103
  return $('.flash.' + type).first();
102
104
  }
@@ -135,7 +137,7 @@ Voltron.addModule('Flash', function(){
135
137
  }
136
138
  });
137
139
  }else{
138
- // .clear() was called preumably with no arguments,
140
+ // .clear() was called presumably with no arguments,
139
141
  // in which case hide and remove the entire flashes container element
140
142
  options = $.extend(_defaults, options);
141
143
  $('#' + options.id)[options.concealMethod](options.concealTime, function(){
@@ -2,7 +2,7 @@ module Voltron
2
2
  module FlashHelper
3
3
 
4
4
  def voltron_flashes(*classes)
5
- render template: "voltron/flash", locals: { container_class: classes.flatten.compact.join(" ") }
5
+ render template: 'voltron/flash/flashes', locals: { container_class: classes.flatten.compact.join(' ') }
6
6
  end
7
7
 
8
8
  alias_method :flashes, :voltron_flashes
@@ -4,20 +4,16 @@ module Voltron
4
4
  module Install
5
5
  class AssetsGenerator < Rails::Generators::Base
6
6
 
7
- source_root File.expand_path("../../../../templates", __FILE__)
7
+ source_root File.expand_path('../../../../../../../', __FILE__)
8
8
 
9
- desc "Install Voltron Flash assets"
9
+ desc 'Install Voltron Flash assets'
10
10
 
11
11
  def copy_javascripts_assets
12
- copy_file "app/assets/javascripts/voltron-flash.js", Rails.root.join("app", "assets", "javascripts", "voltron-flash.js")
12
+ copy_file 'app/assets/javascripts/voltron-flash.js', Rails.root.join('app', 'assets', 'javascripts', 'voltron-flash.js')
13
13
  end
14
14
 
15
15
  def copy_stylesheets_assets
16
- copy_file "app/assets/stylesheets/voltron-flash.scss", Rails.root.join("app", "assets", "stylesheets", "voltron-flash.scss")
17
- end
18
-
19
- def copy_views
20
- copy_file "app/views/voltron/flash.html.erb", Rails.root.join("app", "views", "voltron", "flash.html.erb")
16
+ copy_file 'app/assets/stylesheets/voltron-flash.scss', Rails.root.join('app', 'assets', 'stylesheets', 'voltron-flash.scss')
21
17
  end
22
18
 
23
19
  end
@@ -0,0 +1,19 @@
1
+ module Voltron
2
+ module Flash
3
+ module Generators
4
+ module Install
5
+ class ViewsGenerator < Rails::Generators::Base
6
+
7
+ source_root File.expand_path('../../../../../../../', __FILE__)
8
+
9
+ desc 'Install Voltron Flash views'
10
+
11
+ def copy_views
12
+ copy_file 'app/views/voltron/flash/flashes.html.erb', Rails.root.join('app', 'views', 'voltron', 'flash', 'flashes.html.erb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,17 +3,17 @@ module Voltron
3
3
  module Generators
4
4
  class InstallGenerator < Rails::Generators::Base
5
5
 
6
- source_root File.expand_path("../../../templates", __FILE__)
6
+ source_root File.expand_path('../../../../../../', __FILE__)
7
7
 
8
- desc "Add Voltron Flash initializer"
8
+ desc 'Add Voltron Flash initializer'
9
9
 
10
10
  def inject_initializer
11
11
 
12
- voltron_initialzer_path = Rails.root.join("config", "initializers", "voltron.rb")
12
+ voltron_initialzer_path = Rails.root.join('config', 'initializers', 'voltron.rb')
13
13
 
14
14
  unless File.exist? voltron_initialzer_path
15
15
  unless system("cd #{Rails.root.to_s} && rails generate voltron:install")
16
- puts "Voltron initializer does not exist. Please ensure you have the 'voltron' gem installed and run `rails g voltron:install` to create it"
16
+ puts 'Voltron initializer does not exist. Please ensure you have the \'voltron\' gem installed and run `rails g voltron:install` to create it'
17
17
  return false
18
18
  end
19
19
  end
@@ -20,8 +20,8 @@ module Voltron
20
20
  attr_accessor :header, :group
21
21
 
22
22
  def initialize
23
- @header ||= "X-Flash"
24
- @group ||= true
23
+ @header ||= 'X-Flash'
24
+ @group = true unless @group === false
25
25
  end
26
26
 
27
27
  def to_h
@@ -4,9 +4,7 @@ module Voltron
4
4
 
5
5
  isolate_namespace Voltron
6
6
 
7
- config.autoload_paths += Dir["#{config.root}/lib/**/"]
8
-
9
- initializer "voltron.flash.initialize" do
7
+ initializer 'voltron.flash.initialize' do
10
8
  ::ActionController::Base.send :prepend, ::Voltron::Flash
11
9
  ::ActionController::Base.send :helper, ::Voltron::FlashHelper
12
10
  end
@@ -1,5 +1,5 @@
1
1
  module Voltron
2
2
  module Flash
3
- VERSION = '0.1.5'.freeze
3
+ VERSION = '0.1.6'.freeze
4
4
  end
5
5
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Voltron library to more easily deal with flash messages}
13
13
  spec.homepage = 'https://github.com/ehainer/voltron-flash'
14
- spec.license = 'MIT'
14
+ spec.license = 'GNU GPL v3'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
17
  spec.bindir = 'exe'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voltron-flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Hainer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -182,13 +182,11 @@ files:
182
182
  - app/assets/javascripts/voltron-flash.js
183
183
  - app/assets/stylesheets/voltron-flash.scss
184
184
  - app/helpers/voltron/flash_helper.rb
185
- - app/views/voltron/flash.html.erb
185
+ - app/views/voltron/flash/flashes.html.erb
186
186
  - bin/console
187
187
  - bin/setup
188
- - lib/generators/templates/app/assets/javascripts/voltron-flash.js
189
- - lib/generators/templates/app/assets/stylesheets/voltron-flash.scss
190
- - lib/generators/templates/app/views/voltron/flash.html.erb
191
188
  - lib/generators/voltron/flash/install/assets_generator.rb
189
+ - lib/generators/voltron/flash/install/views_generator.rb
192
190
  - lib/generators/voltron/flash/install_generator.rb
193
191
  - lib/voltron/config/flash.rb
194
192
  - lib/voltron/flash.rb
@@ -197,7 +195,7 @@ files:
197
195
  - voltron-flash.gemspec
198
196
  homepage: https://github.com/ehainer/voltron-flash
199
197
  licenses:
200
- - MIT
198
+ - GNU GPL v3
201
199
  metadata: {}
202
200
  post_install_message:
203
201
  rdoc_options: []
@@ -1,148 +0,0 @@
1
- //= require voltron
2
-
3
- Voltron.addModule('Flash', function(){
4
- var _defaults = {
5
- class: '',
6
- bind: 'body',
7
- id: 'flashes',
8
- containerClass: '',
9
- addMethod: 'prepend',
10
- revealMethod: 'slideDown',
11
- revealTime: 200,
12
- concealMethod: 'slideUp',
13
- concealTime: 200,
14
- autoClose: false,
15
- autoCloseAfter: 5000,
16
- autoAdd: true
17
- };
18
-
19
- return {
20
- initialize: function(){
21
- Voltron('Dispatch/addEventWatcher', 'click');
22
- this.on('click:close-alert', 'click:close-notice', 'click:close-warning', this.clear);
23
- this.addListener();
24
- },
25
-
26
- setConfig: function(options){
27
- _defaults = $.extend(_defaults, options);
28
- this.afterConfigChange();
29
- return this;
30
- },
31
-
32
- addListener: function(){
33
- $(document).ajaxComplete(function(event, request){
34
- if(_defaults.autoAdd){
35
- var flashes = request.getResponseHeader(Voltron.getConfig('flash/header', 'X-Flash'));
36
- var flash = $.parseJSON(flashes);
37
- if(flash){
38
- Voltron('Flash/new', flash);
39
- }
40
- }
41
- });
42
- },
43
-
44
- new: function(flashes, options){
45
- options = $.extend(_defaults, options);
46
- var flash;
47
-
48
- this.getContainer(options).addClass(options.class);
49
-
50
- $.each(flashes, $.proxy(function(type, messages){
51
- flash = this.addFlash(type, messages, options);
52
-
53
- if(flash.find('.flash-message').length == 1){
54
- // If this is the first flash message, reveal the whole container
55
- flash.find('.flash-message').show();
56
- flash[options.revealMethod](options.revealTime);
57
- }else{
58
- // Otherwise reveal just the newest message(s)
59
- flash.find('.flash-message')[options.revealMethod](options.revealTime);
60
- }
61
-
62
- if(options.autoClose){
63
- setTimeout(function(){
64
- V('Flash/clear', { element: flash });
65
- }, options.autoCloseAfter);
66
- }
67
- }, this));
68
-
69
- return this;
70
- },
71
-
72
- addFlash: function(type, messages, options){
73
- var flash = this.getFlash(type);
74
-
75
- flash.append($.map($.makeArray(messages), function(message){
76
- return $('<p />', { class: 'flash-message' }).html(message).hide();
77
- }));
78
-
79
- if(!Voltron.getConfig('flash/group') || !flash.find('.flash-close').length){
80
- flash.append($('<button />', { class: 'flash-close', type: 'button', id: 'close-' + type, 'data-dispatch': 'click' }));
81
- }
82
-
83
- if(!this.getContainer(options).length){
84
- Voltron.debug('warn', 'Element with which to bind flash messages to could not be found. The bind selector given was %o. Please ensure an element matching the given selector exists.', options.bind);
85
- }
86
-
87
- this.getContainer(options).append(flash);
88
- return flash;
89
- },
90
-
91
- getFlash: function(type){
92
- if(Voltron.getConfig('flash/group') && $('.flash.' + type).length){
93
- return $('.flash.' + type).first();
94
- }
95
- return $('<div />', { class: ['flash', type].join(' ') }).hide();
96
- },
97
-
98
- getContainer: function(options){
99
- if(options.addMethod == 'after'){
100
- if(!$(options.bind).next('#' + options.id).length){
101
- $(options.bind)[options.addMethod]($('<div />', { id: options.id, class: options.containerClass }));
102
- }
103
- return $(options.bind).next('#' + options.id);
104
- }else if(options.addMethod == 'before'){
105
- if(!$(options.bind).prev('#' + options.id).length){
106
- $(options.bind)[options.addMethod]($('<div />', { id: options.id, class: options.containerClass }));
107
- }
108
- return $(options.bind).prev('#' + options.id);
109
- }else{
110
- if(!$(options.bind).find('#' + options.id).length){
111
- $(options.bind)[options.addMethod]($('<div />', { id: options.id, class: options.containerClass }));
112
- }
113
- return $(options.bind).find('#' + options.id);
114
- }
115
- },
116
-
117
- clear: function(o, options){
118
- if(o && o.element){
119
- // If o.element exists, it's the button that was clicked,
120
- // in which case, remove the closest flash message
121
- o.options = $.extend(_defaults, options);
122
- $(o.element).closest('.flash')[o.options.concealMethod](o.options.concealTime, function(){
123
- $(this).remove();
124
- // If we removed the last flash message, also remove the container
125
- if($('#' + o.options.id).find('.flash').length == 0){
126
- $('#' + o.options.id).remove();
127
- }
128
- });
129
- }else{
130
- // .clear() was called preumably with no arguments,
131
- // in which case hide and remove the entire flashes container element
132
- options = $.extend(_defaults, options);
133
- $('#' + options.id)[options.concealMethod](options.concealTime, function(){
134
- $(this).remove();
135
- });
136
- }
137
- },
138
-
139
- afterConfigChange: function(){
140
- if(_defaults.autoClose && $('#' + _defaults.id + ' .flash').length){
141
- var elements = $('#' + _defaults.id + ' .flash');
142
- setTimeout(function(){
143
- Voltron('Flash/clear', { element: elements });
144
- }, _defaults.autoCloseAfter);
145
- }
146
- }
147
- };
148
- }, true);
@@ -1,86 +0,0 @@
1
- $flash-red: #FCD7CF;
2
- $flash-green: #B1CEC4;
3
- $flash-yellow: #FFECC3;
4
- $flash-padding: 10px;
5
-
6
- .flash {
7
- position: relative;
8
- width: 100%;
9
- padding: $flash-padding;
10
- padding-right: $flash-padding*4;
11
- -webkit-box-sizing: border-box;
12
- -moz-box-sizing: border-box;
13
- box-sizing: border-box;
14
- overflow: hidden;
15
-
16
- &.alert {
17
- background-color: $flash-red;
18
- color: darken($flash-red, 70%);
19
- .flash-close:before, .flash-close:after {
20
- background-color: darken($flash-red, 70%);
21
- }
22
- }
23
-
24
- &.warning {
25
- background-color: $flash-yellow;
26
- color: darken($flash-yellow, 70%);
27
- .flash-close:before, .flash-close:after {
28
- background-color: darken($flash-yellow, 70%);
29
- }
30
- }
31
-
32
- &.notice {
33
- background-color: $flash-green;
34
- color: darken($flash-green, 70%);
35
- .flash-close:before, .flash-close:after {
36
- background-color: darken($flash-green, 70%);
37
- }
38
- }
39
- }
40
-
41
- .flash-message {
42
- font-family: sans-serif;
43
- margin: 0;
44
- line-height: 22px;
45
- -webkit-box-sizing: border-box;
46
- -moz-box-sizing: border-box;
47
- box-sizing: border-box;
48
- }
49
-
50
- .flash-close {
51
- position: absolute;
52
- display: inline-block;
53
- width: $flash-padding*2;
54
- height: $flash-padding*2;
55
- overflow: hidden;
56
- background: none;
57
- border: 0;
58
- cursor: pointer;
59
- right: $flash-padding;
60
- top: $flash-padding;
61
- outline: none;
62
- &:before, &:after {
63
- content: '';
64
- position: absolute;
65
- width: 100%;
66
- height: 4px;
67
- top: 50%;
68
- left: 0;
69
- background: #000;
70
- margin-top: -1px;
71
- }
72
- &:before {
73
- -o-transform: rotate(45deg);
74
- -ms-transform: rotate(45deg);
75
- -moz-transform: rotate(45deg);
76
- -webkit-transform: rotate(45deg);
77
- transform: rotate(45deg);
78
- }
79
- &:after {
80
- -o-transform: rotate(-45deg);
81
- -ms-transform: rotate(-45deg);
82
- -moz-transform: rotate(-45deg);
83
- -webkit-transform: rotate(-45deg);
84
- transform: rotate(-45deg);
85
- }
86
- }
@@ -1,22 +0,0 @@
1
- <% if flash.count > 0 %>
2
- <div id="flashes" class="<%= container_class %>">
3
- <% if Voltron.config.flash.group %>
4
- <% flash.each do |type,messages| %>
5
- <div class="flash <%= type %>">
6
- <p class="flash-message"><%= Array.wrap(messages).join("<br />").html_safe %></p>
7
- <button class="flash-close" type="button" id="close-<%= type %>" data-dispatch="click"></button>
8
- </div>
9
- <% end %>
10
- <% else %>
11
- <% flash.each do |type,messages| %>
12
- <% Array.wrap(messages).each do |message| %>
13
- <div class="flash <%= type %>">
14
- <p class="flash-message"><%= message.html_safe %></p>
15
- <button class="flash-close" type="button" id="close-<%= type %>" data-dispatch="click"></button>
16
- </div>
17
- <% end %>
18
- <% end %>
19
- <% end %>
20
- </div>
21
- <% end %>
22
- <% flash.clear %>