growlyflash 0.7.0 → 0.8.0

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: 0b15ba027b5bb449f0ab3a2d18a0a8273d73867e
4
- data.tar.gz: 5acfd64725e4a5faa2614800696d0b2d818f841f
3
+ metadata.gz: c4fe2e425f9270e864559a019546d124c48240bb
4
+ data.tar.gz: d5933ef2d1f5f593f6c064607725ece3904f787a
5
5
  SHA512:
6
- metadata.gz: f0554c4b4fdca37baa13ec0fa115884c39ceaef8dd72fce7631070c7d1aab35fb8cc3bc71fdb6ad5c3db2500841748684152a8b60cc9d04d1be4bbcf0880eb41
7
- data.tar.gz: a647b4016db94cbeff98a44ec18b91af0b5800b8bfe430fdd0459726403c992e47bd9e612ad36d0c34e0d332b924a0d7b34c24e80e3e6c41a8ddab37c82defc4
6
+ metadata.gz: c875f93ca757ddbc7be54ec7e30f7ac0a56807739ff0b23f1527be98ad950a0fe949487320f67ef4da1c7b768b9a34013f825f89fa63adc3df0818f448528fd9
7
+ data.tar.gz: eeffa29ebc62987c1c2cd9450e4da8971f68971347cb73fc7bb1b5a7e5c373707e1245d5f8513962c43ea4d5d1897fc160a852c6e9ae916d0b48b93c2c692930
@@ -1,77 +1,82 @@
1
1
  class Growlyflash
2
- window.Growlyflash ?= Growlyflash
3
-
4
- Growlyflash.defaults =
5
- align: 'right' # horizontal aligning (left, right or center)
6
- delay: 4000 # auto-dismiss timeout (false to disable auto-dismiss)
7
- dismiss: yes # allow to show close button
8
- spacing: 10 # spacing between alerts
9
- target: 'body' # selector to target element where to place alerts
10
- title: no # switch for adding a title
11
- type: null # bootstrap alert class by default
12
- class: ['alert', 'growlyflash', 'fade']
13
-
14
- # customizable callback to set notification position before it shows
15
- before_show: (options) ->
16
- @el.css @calc_css_position()
17
-
18
- Growlyflash.KEY_MAPPING =
19
- alert: 'warning'
20
- error: 'danger'
21
- notice: 'info'
22
- success: 'success'
23
-
24
- class Growlyflash.FlashStruct
25
- shown: no
26
- constructor: (@msg, @key) -> @type = Growlyflash.KEY_MAPPING[@key]
27
- growl: -> $.growlyflash this
28
- is_equal: (other) -> (@key is other.key) and (@msg is other.msg)
29
- isnt_equal: (other) -> not @is_equal other
30
-
31
- class Growlyflash.Alert
32
- constructor: (@flash, options) ->
33
- {@title, @align, @dismiss, @msg, @spacing, @type, @class} = options
34
-
35
- @el = ($ '<div>',
36
- class: @_classes().join(' ')
37
- html: "#{@_dismiss()}#{@_title()}#{@msg}"
38
- ).appendTo(options.target)
39
-
40
- options.before_show.call(this, options)
41
- @show()
42
-
43
- return unless options.delay
44
- setTimeout =>
45
- @hide -> ($ @).remove()
46
- , options.delay
47
-
48
- show: -> @el.toggleClass 'in'
49
- hide: (fn) -> @el.fadeOut(fn)
50
-
51
- _classes: ->
52
- @class.concat ("alert-#{type}" for type in [@type] when type?), ["growlyflash-#{@align}"]
53
-
54
- _dismiss: ->
55
- return "" unless @dismiss?
56
- """<a class="close" data-dismiss="alert" href="#">&times;</a>"""
57
-
58
- _title: ->
59
- return "" if @title is no
60
- """<strong>#{@type.charAt(0).toUpperCase()}#{@type.substring(1)}!</strong>"""
61
-
62
- calc_top_offset: ->
63
- amount = parseInt(@el.css 'top')
64
- (@el.siblings '.growlyflash').each (_, el) =>
65
- amount = Math.max(amount, parseInt(($ el).css 'top') + ($ el).outerHeight() + @spacing)
66
- amount
67
-
68
- calc_css_position: ->
69
- css = {}
70
- css.top = "#{@calc_top_offset()}px"
71
- css.marginLeft = "-#{@el.width() / 2}px" if @align is 'center'
72
- css
2
+ @defaults =
3
+ align: 'right' # horizontal aligning (left, right or center)
4
+ delay: 4000 # auto-dismiss timeout (false to disable auto-dismiss)
5
+ dismiss: yes # allow to show close button
6
+ spacing: 10 # spacing between alerts
7
+ target: 'body' # selector to target element where to place alerts
8
+ title: no # switch for adding a title
9
+ type: null # bootstrap alert class by default
10
+ class: ['alert', 'growlyflash', 'fade']
11
+
12
+ # customizable callback to set notification position before it shows
13
+ before_show: ->
14
+ @el.css @calc_css_position()
15
+
16
+ @KEY_MAPPING =
17
+ alert: 'warning'
18
+ error: 'danger'
19
+ notice: 'info'
20
+ success: 'success'
21
+
22
+ @DISMISS = """<button type="close" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>"""
23
+
24
+ _titleize = (s) -> s.replace /^./, (m) -> do m.toUpperCase
25
+
26
+ h = @helpers =
27
+ dismiss: -> Growlyflash.DISMISS
28
+ title: (s) -> "<strong>#{_titleize(s)}!</strong> "
29
+
30
+ # Flash message struct
31
+ class @FlashStruct
32
+ toString: -> JSON.stringify "#{@msg}": @key
33
+ constructor: (@msg, @key) ->
34
+ @type = Growlyflash.KEY_MAPPING[@key]
35
+
36
+ class @Alert
37
+ _add = (orig, add) -> @splice -~@indexOf(orig), 0, "#{orig}-#{add}"
38
+ _top = (e) -> parseInt ($ e).css('top')
39
+
40
+ constructor: (@flash, @opts) ->
41
+ {title, target, dismiss, delay, before_show} = @opts
42
+
43
+ html = ""
44
+ html += h.dismiss() if dismiss
45
+ html += h.title(@opts) if title
46
+ html += @flash.msg
47
+
48
+ @el = ($ '<div>', html: html, class: @class_list().join(' '), role: "alert")
49
+ @el.appendTo(target)
50
+
51
+ before_show?.call(this)
52
+ @show()
53
+ setTimeout(@close, @opts.delay) if delay
54
+
55
+ class_list: ->
56
+ list = [].concat(@opts.class)
57
+ add = _add.bind(list)
58
+ add 'alert', "dismissable" if @opts.dismiss
59
+ add 'alert', @opts.type if @opts.type?
60
+ add 'growlyflash', @opts.align if @opts.align?
61
+ list
62
+
63
+ show: => @el.toggleClass('in', on)
64
+ close: => @el.alert('close')
65
+
66
+ calc_top_offset: ({spacing}) ->
67
+ amount = _top(@el)
68
+ (@el.siblings '.growlyflash').each ->
69
+ amount = Math.max(amount, _top(@) + ($ @).outerHeight() + spacing)
70
+ return amount
71
+
72
+ calc_css_position: (css = {}) ->
73
+ css.top = "#{@calc_top_offset(@opts)}px"
74
+ css.marginLeft = "-#{@el.outerWidth() / 2}px" if @opts.align is 'center'
75
+ css
76
+
77
+ window.Growlyflash = Growlyflash
73
78
 
74
79
  $.growlyflash = (flash, options = {}) ->
75
- settings = $.extend on, {}, Growlyflash.defaults, msg: flash.msg, type: flash.type, options
76
- alert = new Growlyflash.Alert(flash, settings)
80
+ options = $.extend on, {}, Growlyflash.defaults, type: flash.type, options
81
+ alert = new Growlyflash.Alert(flash, options)
77
82
  if flash instanceof Growlyflash.FlashStruct then flash else alert
@@ -1,40 +1,40 @@
1
1
  class Growlyflash.Listener
2
- HEADER = 'X-Message'
3
- EVENTS = 'ajax:complete ajaxComplete'
4
-
5
2
  # Alerts stack
6
- class Stack extends Array
7
- constructor: (items...) ->
8
- @splice 0, 0, items...
9
- has_uniq_in: (alerts, counter = 0) ->
10
- return true unless @length > 0
11
- recent = @slice -alerts.length
12
- counter++ for id, item of alerts when recent[id].isnt_equal? item
13
- counter > 0
14
- push_all: (alerts) ->
15
- @push alert.growl() for alert in alerts
16
- this
17
- push_once: (alerts) ->
18
- @push_all alerts if @has_uniq_in alerts
19
- @purge()
3
+ class Stack
4
+ constructor: (@items...) ->
5
+
20
6
  purge: ->
21
- setTimeout (=> @splice 0), 100
22
-
7
+ setTimeout (=> @items.splice(0)), 100
8
+
9
+ push: (alert, dumped) ->
10
+ $.growlyflash(alert)
11
+ @items.push(dumped ? alert.toString())
12
+
13
+ push_only_fresh: (alerts) ->
14
+ recent = @items[-alerts.length..]
15
+ for alert in alerts
16
+ dumped = alert.toString()
17
+ @push(alert, dumped) if dumped not in recent
18
+ do @purge
19
+
20
+ HEADER = 'X-Message'
21
+ EVENTS = 'ajax:complete ajaxComplete'
22
+
23
23
  process = (alerts = {}) ->
24
24
  new Growlyflash.FlashStruct(msg, type) for type, msg of alerts when msg?
25
-
25
+
26
26
  process_from_header = (source) ->
27
27
  return [] unless source?
28
- process $.parseJSON(decodeURIComponent source)
29
-
28
+ process $.parseJSON(decodeURIComponent(source))
29
+
30
30
  constructor: (context) ->
31
31
  @stack ?= new Stack()
32
32
  @process_static() if window.flashes?
33
33
  ($ context).on EVENTS, (_, xhr) =>
34
- @stack.push_once process_from_header(xhr.getResponseHeader HEADER)
35
- return
34
+ @stack.push_only_fresh process_from_header(xhr.getResponseHeader(HEADER))
35
+
36
36
  process_static: ->
37
- @stack.push_all process(window.flashes)
37
+ @stack.push alert for alert in process(window.flashes)
38
38
  delete window.flashes
39
39
 
40
40
  Growlyflash.listen_on = (context) ->
@@ -4,4 +4,6 @@ Growlyflash.KEY_MAPPING =
4
4
  warning: null
5
5
  error: 'error'
6
6
  notice: 'info'
7
- success: 'success'
7
+ success: 'success'
8
+
9
+ Growlyflash.DISMISS = """<a class="close" data-dismiss="alert" href="#">&times;</a>"""
data/growlyflash.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.version = Growlyflash::VERSION
10
10
  spec.authors = ['Tõnis Simo']
11
11
  spec.email = ['anton.estum@gmail.com']
12
- spec.summary = %q{Growl-styled flash messages for Ruby on Rails with Bootstrap.}
13
- spec.description = %q{Growl-styled flash messages for Ruby on Rails with Bootstrap. For XHR requests flash messages are transfering in 'X-Messages' headers, otherwise they are storing in js variables.}
12
+ spec.summary = %q{Popup ActionDispatch::Flash within Bootstrap alert in Rails app like a growl notification.}
13
+ spec.description = %q{This gem popups Rails' ActionDispatch::Flash within Bootstrap alert like a growl notification. It serves messages with both of AJAX (XHR) and regular requests inside HTTP headers.}
14
14
  spec.homepage = 'https://github.com/estum/growlyflash'
15
15
  spec.license = "MIT"
16
16
 
@@ -1,3 +1,3 @@
1
1
  module Growlyflash
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growlyflash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tõnis Simo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-29 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -72,9 +72,9 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
- description: Growl-styled flash messages for Ruby on Rails with Bootstrap. For XHR
76
- requests flash messages are transfering in 'X-Messages' headers, otherwise they
77
- are storing in js variables.
75
+ description: This gem popups Rails' ActionDispatch::Flash within Bootstrap alert like
76
+ a growl notification. It serves messages with both of AJAX (XHR) and regular requests
77
+ inside HTTP headers.
78
78
  email:
79
79
  - anton.estum@gmail.com
80
80
  executables: []
@@ -120,5 +120,6 @@ rubyforge_project:
120
120
  rubygems_version: 2.4.7
121
121
  signing_key:
122
122
  specification_version: 4
123
- summary: Growl-styled flash messages for Ruby on Rails with Bootstrap.
123
+ summary: Popup ActionDispatch::Flash within Bootstrap alert in Rails app like a growl
124
+ notification.
124
125
  test_files: []