ultimate-flash 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ultimate-flash.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ultimate-flash (0.6.0)
5
+ ultimate-base (~> 0.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ ultimate-base (0.2.4)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ ultimate-flash!
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011-2012 Dmitry Karpunin (aka KODer) koderfunk@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ Ruby on Rails oriented jQuery plugin for smart notifications
2
+
3
+ **Binding**:
4
+
5
+ ```javascript
6
+ //= require ultimate/flash
7
+ window.flash = $(".l-page__flashes").ultimateFlash({locale: "ru"}, true);
8
+ ```
9
+
10
+ ## Licence ##
11
+
12
+ The MIT License
13
+
14
+ Copyright (c) 2011-2012 Dmitry Karpunin (aka KODer) koderfunk@gmail.com
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in
24
+ all copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,235 @@
1
+ ###*
2
+ * Ultimate Flash 0.6.0 - Ruby on Rails oriented jQuery plugin based on Ultimate Backbone
3
+ *
4
+ * Copyright 2011-2012 Karpunin Dmitry (KODer) / Evrone.com
5
+ *
6
+ * Dual licensed under the MIT and GPL licenses:
7
+ * http://www.opensource.org/licenses/mit-license.php
8
+ * http://www.gnu.org/licenses/gpl.html
9
+ *
10
+ ###
11
+
12
+ ###*
13
+ * $.fn.ultimateFlash() invoke Ultimate Flash functionality at first call on jQuery object
14
+ * for the first element in the set of matched elements.
15
+ * Subsequent calls forwarding on view methods or return view property.
16
+ * If last argument {Boolean} true, then returns {Flash}.
17
+ * @usage
18
+ * standart actions:
19
+ * construction .ultimateFlash([Object options = {}]) : {jQuery} jContainer
20
+ * updateOptions .pluginName({Object} options) : {Object} view options
21
+ * get options .pluginName("options") : {Object} view options
22
+ * show .ultimateFlash("show", String type, String text) : {jQuery} jFlash | {Boolean} false
23
+ * notice .ultimateFlash("notice", String text) : {jQuery} jFlash | {Boolean} false
24
+ * alert .ultimateFlash("alert", String text) : {jQuery} jFlash | {Boolean} false
25
+ * extended actions:
26
+ * auto .ultimateFlash("auto", {ArrayOrObject} obj) : {Array} ajFlashes | {Boolean} false
27
+ * ajaxSuccess .ultimateFlash("ajaxSuccess"[, Arguments successArgs = []])
28
+ * ajaxError .ultimateFlash("ajaxError"[, String text = translations.defaultErrorText][, Arguments errorArgs = []])
29
+ ###
30
+
31
+ # TODO customizable show() and hide()
32
+ # TODO improve English
33
+ # TODO jGrowl features
34
+
35
+ #= require ultimate/backbone/view
36
+ #= require ultimate/backbone/extra/jquery-plugin-adapter
37
+
38
+ Ultimate.Backbone.Plugins ||= {}
39
+
40
+ class Ultimate.Backbone.Plugins.Flash extends Ultimate.Backbone.View
41
+
42
+ el: ".l-page__flashes"
43
+
44
+ @defaultLocales:
45
+ en:
46
+ defaultErrorText: "Error"
47
+ defaultThrownError: "server connection error"
48
+ formFieldsError: "Form filled with errors"
49
+ ru:
50
+ defaultErrorText: "Ошибка"
51
+ defaultThrownError: "ошибка соединения с сервером"
52
+ formFieldsError: "Форма заполнена с ошибками"
53
+
54
+ slideTime: 200
55
+ showTime: 3600
56
+ showTimePerChar: 30
57
+ showAjaxErrors: true # catch global jQuery.ajaxErrors(), try detect message and show it
58
+ showAjaxSuccesses: true # catch global jQuery.ajaxSuccessess(), try detect message and show it
59
+ detectFormErrors: true # can be function (parsedJSON)
60
+ detectPlainTextMaxLength: 200 # if response has plain text and its length fits, show it
61
+ hideOnClick: true # click on notice fire hide()
62
+ removeOnHide: true # remove notice DOM-element on hide
63
+ forceAddDotsAfterLastWord: false
64
+ forceRemoveDotsAfterLastWord: false
65
+ regExpLastWordWithoutDot: /[\wа-яёА-ЯЁ]{3,}$/
66
+ regExpLastWordWithDot: /([\wа-яёА-ЯЁ]{3,})\.$/
67
+
68
+ events:
69
+ "click .flash:not(:animated)" : "closeFlashClick"
70
+
71
+ initialize: (options) ->
72
+ super
73
+ # init flashes come from server in page
74
+ @jFlashes().each (index, flash) =>
75
+ jFlash = $(flash)
76
+ jFlash.html @_prepareText(jFlash.html())
77
+ @_setTimeout jFlash
78
+ # binding hook ajaxError handler
79
+ @$el.ajaxError =>
80
+ if @showAjaxErrors
81
+ a = @_ajaxSuccessParseArguments(arguments)
82
+ Ultimate.Backbone.debug ".Plugins.Flash.ajaxError", a
83
+ @ajaxError a.data, a.jqXHR
84
+ # binding hook ajaxSuccess handler
85
+ @$el.ajaxSuccess =>
86
+ if @showAjaxSuccesses
87
+ a = @_ajaxSuccessParseArguments(arguments)
88
+ Ultimate.Backbone.debug ".Plugins.Flash.ajaxSuccess", a
89
+ @ajaxSuccess a.data, a.jqXHR
90
+
91
+ # delegate event for hide on click
92
+ closeFlashClick: (event) ->
93
+ if @hideOnClick
94
+ @_hide $(event.currentTarget)
95
+ false
96
+
97
+ jFlashes: (filterSelector) ->
98
+ _jFlashes = @$(".flash")
99
+ if filterSelector then _jFlashes.filter(filterSelector) else _jFlashes
100
+
101
+ _prepareText: (text) ->
102
+ text = _.clean(text)
103
+ # Add dot after last word (if word has minimum 3 characters)
104
+ text += "." if @forceAddDotsAfterLastWord and @regExpLastWordWithoutDot.test(text)
105
+ text = text.replace(@regExpLastWordWithDot, "$1") if @forceRemoveDotsAfterLastWord
106
+ text
107
+
108
+ _hide: (jFlash) ->
109
+ clearTimeout jFlash.data("timeoutId")
110
+ jFlash.slideUp @slideTime, =>
111
+ jFlash.remove() if @removeOnHide
112
+
113
+ _setTimeout: (jFlash, timeout = @showTime + jFlash.text().length * @showTimePerChar) ->
114
+ if timeout
115
+ jFlash.data "timeoutId", setTimeout =>
116
+ jFlash.removeData("timeoutId")
117
+ @_hide jFlash
118
+ , timeout
119
+
120
+ show: (type, text, timeout = null) ->
121
+ return false if not _.isString(text) or _.isBlank(text)
122
+ jFlash = $("<div class=\"flash #{type}\" style=\"display: none;\">#{@_prepareText(text)}</div>")
123
+ jFlash.appendTo(@$el).slideDown @slideTime
124
+ @_setTimeout jFlash, timeout
125
+ jFlash
126
+
127
+ notice: (text, timeout = null) -> @show "notice", text, timeout
128
+
129
+ alert: (text, timeout = null) -> @show "alert", text, timeout
130
+
131
+ auto: (obj) ->
132
+ if _.isArray(obj)
133
+ @show(pair[0], pair[1]) for pair in obj
134
+ else if _.isObject(obj)
135
+ @show(key, text) for key, text of obj
136
+ else false
137
+
138
+ ###*
139
+ * @param {Arguments} successArgs=[] аргументы колбэка jQuery.success()
140
+ * @return {data: data, jqXHR: jqXHR}
141
+ ###
142
+ _ajaxSuccessParseArguments: (successArgs) ->
143
+ # detect event as first element
144
+ if successArgs[0] instanceof jQuery.Event
145
+ # convert arguments to Array
146
+ successArgs = args(successArgs)
147
+ # remove event
148
+ successArgs.shift()
149
+ # arrange arguments
150
+ if _.isString(successArgs[0])
151
+ # from jQuery.ajax().success()
152
+ [data, _textStatus, jqXHR] = successArgs
153
+ else
154
+ # from jQuery.ajaxSuccess() or jQuery.ajaxError()
155
+ [jqXHR, _ajaxSettings, data] = successArgs
156
+ data: data
157
+ jqXHR: jqXHR
158
+
159
+ ###*
160
+ * @param {String|Object} data some data from ajax response
161
+ * @param {jqXHR} jqXHR jQuery XHR
162
+ * @return {Boolean} статус выполнения показа сообщения
163
+ ###
164
+ # MAYBE jqXHR set default as jQuery.hxr or similar
165
+ ajaxSuccess: (data, jqXHR) ->
166
+ # prevent recall
167
+ return false if jqXHR.breakFlash
168
+ jqXHR.breakFlash = true
169
+ # detect notice
170
+ if _.isString(data)
171
+ # catch plain text message
172
+ data = _.trim(data)
173
+ return @notice(data) if data.length <= @detectPlainTextMaxLength and not $.isHTML(data)
174
+ else if _.isObject(data)
175
+ # catch json data with flash-notice
176
+ return @auto(data["flash"]) if data["flash"]
177
+ false
178
+
179
+ ###*
180
+ * @param {String} [text="Ошибка"] вступительный (либо полный) текст формируемой ошибки
181
+ * @param {String} thrownError some error from ajax response
182
+ * @param {jqXHR} jqXHR jQuery XHR
183
+ * @return {Boolean} статус выполнения показа сообщения
184
+ ###
185
+ ajaxError: (text, thrownError, jqXHR) ->
186
+ unless _.isObject(jqXHR)
187
+ jqXHR = thrownError
188
+ thrownError = text
189
+ text = @translations.defaultErrorText
190
+ # prevent undefined responses
191
+ return false if jqXHR.status < 100
192
+ # prevent recall
193
+ return false if jqXHR.breakFlash
194
+ jqXHR.breakFlash = true
195
+ if jqXHR.responseText
196
+ try
197
+ # try parse respose as json
198
+ if parsedJSON = $.parseJSON(jqXHR.responseText)
199
+ # catch "flash" object and call auto() method with autodetecting flash-notice type
200
+ return @auto(parsedJSON["flash"]) if parsedJSON["flash"]
201
+ # catch "error" object and call alert() method
202
+ return @alert(parsedJSON["error"]) if parsedJSON["error"]
203
+ # may be parsedJSON is form errors
204
+ if @detectFormErrors is true
205
+ # show message about form with errors
206
+ return @alert(@translations.formFieldsError)
207
+ else if _.isFunction(@detectFormErrors)
208
+ # using showFormError as callback
209
+ return @detectFormErrors.apply(@, [parsedJSON])
210
+ else
211
+ # nothing
212
+ return false
213
+ catch e
214
+ # nop
215
+ if jqXHR.status >= 400 and jqXHR.responseText
216
+ # try detect Rails raise message
217
+ if raiseMatches = jqXHR.responseText.match(/<\/h1>\n<pre>(.+?)<\/pre>/)
218
+ Ultimate.Backbone.debug "replace thrownError = \"#{thrownError}\" with raiseMatches[1] = \"#{raiseMatches[1]}\""
219
+ thrownError = raiseMatches[1]
220
+ else
221
+ # try detect short text message as error
222
+ if jqXHR.responseText.length <= @detectPlainTextMaxLength
223
+ Ultimate.Backbone.debug "replace thrownError = \"#{thrownError}\" with jqXHR.responseText = \"#{jqXHR.responseText}\""
224
+ thrownError = jqXHR.responseText
225
+ else
226
+ if _.isString(thrownError) and not _.isBlank(thrownError)
227
+ Ultimate.Backbone.debug "replace thrownError = \"#{thrownError}\" with @translations.defaultThrownError = \"#{@translations.defaultThrownError}\""
228
+ thrownError = @translations.defaultThrownError
229
+ text += ": " if text
230
+ text += "#{thrownError} [#{jqXHR.status}]"
231
+ return @alert(text)
232
+
233
+
234
+
235
+ Ultimate.Backbone.createJQueryPlugin "ultimateFlash", Ultimate.Backbone.Plugins.Flash
@@ -0,0 +1,54 @@
1
+ @mixin ultimate-flash(
2
+ $notice-background: #393,
3
+ $notice-border-color: #6c6,
4
+ $alert-background: #c33,
5
+ $alert-border-color: #e66,
6
+ $default-background: #06c,
7
+ $default-border-color: #39e,
8
+ $text-color: #fff,
9
+ $opacity: 1,
10
+ $background-alpha: 1
11
+ ){
12
+ position: fixed;
13
+ width: 100%;
14
+ left: 0;
15
+ z-index: 9999;
16
+ text-align: center;
17
+ // >>> recomendations:
18
+ // @include invert_link_decoration();
19
+ // white-space: nowrap;
20
+
21
+ > .flash {
22
+ @if $background-alpha < 1 {
23
+ $notice-background: rgba($notice-background, $background-alpha);
24
+ $alert-background: rgba($alert-background, $background-alpha);
25
+ $default-background: rgba($default-background, $background-alpha);
26
+ }
27
+ padding: 10px;
28
+ top: 0;
29
+ width: 100%;
30
+ font-size: 24px;
31
+ line-height: 30px;
32
+ color: $text-color;
33
+ @include pie-bg($default-background);
34
+ border-bottom: 2px solid $default-border-color;
35
+ @if $opacity < 1 {
36
+ @include opacity($opacity);
37
+ }
38
+ // >>> recomendations:
39
+ // @include text-shadow_hard(#000 0 1px 0);
40
+ z-index: 9999;
41
+ a {
42
+ color: $text-color;
43
+ }
44
+ &.notice {
45
+ @include pie-bg($notice-background);
46
+ border-color: $notice-border-color;
47
+ }
48
+ &.alert {
49
+ @include pie-bg($alert-background);
50
+ border-color: $alert-border-color;
51
+ }
52
+ }
53
+
54
+ }
@@ -0,0 +1,3 @@
1
+ %section.l-page__flashes
2
+ - flash.each do |key, text|
3
+ .flash{class: key}= text
@@ -0,0 +1,8 @@
1
+ require "ultimate-flash/version"
2
+ require "ultimate-flash/engine"
3
+
4
+ module Ultimate
5
+ module Flash
6
+
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Ultimate
2
+ module Flash
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Ultimate
2
+ module Flash
3
+ VERSION = "0.6.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ultimate-flash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ultimate-flash"
7
+ s.version = Ultimate::Flash::VERSION
8
+ s.authors = ["Dmitry KODer Karpunin"]
9
+ s.email = ["koderfunk@gmail.com"]
10
+ s.homepage = "http://github.com/KODerFunk/ultimate-flash"
11
+ s.summary = %q{Ruby on Rails oriented jQuery plugin for smart notifications}
12
+ s.description = %q{Ruby on Rails oriented jQuery plugin for smart notifications}
13
+
14
+ s.rubyforge_project = "ultimate-flash"
15
+
16
+ s.add_dependency "ultimate-base", "~> 0.2"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ultimate-flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitry KODer Karpunin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ultimate-base
16
+ requirement: &17170900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *17170900
25
+ description: Ruby on Rails oriented jQuery plugin for smart notifications
26
+ email:
27
+ - koderfunk@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/javascripts/ultimate/flash.js.coffee
39
+ - app/assets/stylesheets/ultimate/flash.scss
40
+ - app/views/application/_flashes.html.haml
41
+ - lib/ultimate-flash.rb
42
+ - lib/ultimate-flash/engine.rb
43
+ - lib/ultimate-flash/version.rb
44
+ - ultimate-flash.gemspec
45
+ homepage: http://github.com/KODerFunk/ultimate-flash
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: ultimate-flash
65
+ rubygems_version: 1.8.10
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Ruby on Rails oriented jQuery plugin for smart notifications
69
+ test_files: []