bootstrap-findahead-rails 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjBmYmI0YTM2MjE3NGYzMjcyMTNmYTVhOTljOGRkMDM1ZDhiOWRhYQ==
5
+ data.tar.gz: !binary |-
6
+ ZTJhYTMzMzIwMDg4MjFjNGRhNGE4YjIyNjM3MWYzZTU3ZWY3MDE1Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NzFiZDYxZmMyZjgzNGYzYTIyNzEwMDgzNzc4MWU3MzZjMjllMTUzOWYzNTc0
10
+ MjNmMWNmYmQxN2Y2MGM3YWYwM2U1NzhlYzkwNDVmNzlmOGRiYjk1NTEwNmJj
11
+ YTU2OWMwOTI2YzQ1YzY0M2ViNzNkZjRiNGUxZmYzZTU5NTY2Yjc=
12
+ data.tar.gz: !binary |-
13
+ N2JiZDFhNmMxYTUzMTc2ZTk0NWExNjFmMWM1OTAyNmE4NmJhMzI1NDE2NTk2
14
+ MDllYTM0ZTFhYjVlYTk5ZjFmNDY2MTg0MDhmNWQ0ZWQzMWIxZjE5ZmNiNDVh
15
+ ODcxYzA0MmY2YjgzYjkwZmM3MjI5ZjQ5N2UwYTQ3OWRkMWVmMTU=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bootstrap-findahead-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pierrick Rouxel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # bootstrap-findahead-rails
2
+
3
+ A search component for Twitter Bootstrap
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ group :assets do
10
+ gem 'bootstrap-findahead-rails'
11
+ end
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Stylesheets
18
+ Add necessary stylesheet file to app/assets/stylesheets/application.css
19
+
20
+ *= require bootstrap-findahead
21
+
22
+ Javascripts
23
+ Add necessary javascript(s) files to app/assets/javascripts/application.js
24
+
25
+ //= require bootstrap-findahead
26
+
27
+ ## Usage
28
+
29
+ ### Via JavaScript
30
+
31
+ ```javascript
32
+ $('.findahead').typeahead()
33
+ ```
34
+
35
+ ### Options
36
+
37
+ | Name | Type | Default | Description |
38
+ | ------------- | ------------- | --------------------- | ------------------------------------------------------------ |
39
+ | source | function | [] | The data source to query against. Should be a function is passed two arguments, the **query** value in the input field and the **process** callback. The function may be used synchronously by returning the data source directly or asynchronously via the process callback's single argument. |
40
+ | items | number | 8 | The max number of items to display in the dropdown. |
41
+ | minLength | number | 1 | The minimum character length needed before triggering autocomplete suggestions |
42
+ | matcher | function | case insensitive | The method used to determine if a query matches an **item**. Accepts a single argument, the item against which to test the query. Access the current query with **this.query**. Return a boolean **true** if query is a match. |
43
+ | sorter | function | exact match, case sensitive, case insensitive | Method used to sort autocomplete results. Accepts a single argument **items** and has the scope of the findahead instance. Reference the current query with **this.query**. |
44
+ | updater | function | returns selected item | The method used to return selected item. Accepts a single argument, the **item** and has the scope of the findahead instance. |
45
+ | highlighter | function | highlights all default matches | Method used to highlight autocomplete results. Accepts a single argument **item** and has the scope of the findahead instance. Should return html. |
46
+ | timer | number | 0 | Time to wait before last type to execute query. In milliseconds. |
47
+ | visualLoading | boolean | true | Show or hide visual loading component |
48
+
49
+ ### Methods
50
+
51
+ **.findahead(options)**
52
+ Initializes an input with a findahead.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
61
+
62
+ ## Thanks
63
+
64
+ Thanks to twitter bootstrap for typeahead.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bootstrap/findahead/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bootstrap-findahead-rails"
8
+ spec.version = Bootstrap::Findahead::Rails::VERSION
9
+ spec.authors = ["Pierrick Rouxel"]
10
+ spec.email = ["pierrick.rouxel@me.com"]
11
+ spec.description = %q{A search component for Twitter Bootstrap}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/pierrickrouxel/bootstrap-findahead-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "railties", ">= 3.1"
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,10 @@
1
+ require "bootstrap/findahead/rails/version"
2
+
3
+ module Bootstrap
4
+ module Findahead
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Bootstrap
2
+ module Findahead
3
+ module Rails
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,342 @@
1
+ !function($){
2
+
3
+ "use strict"; // jshint ;_;
4
+
5
+ /* FINDAHEAD PUBLIC CLASS DEFINITION
6
+ * ================================= */
7
+
8
+ var Findahead = function (element, options) {
9
+ this.$element = $(element)
10
+ this.options = $.extend({}, $.fn.findahead.defaults, options)
11
+ this.matcher = this.options.matcher || this.matcher
12
+ this.sorter = this.options.sorter || this.sorter
13
+ this.highlighter = this.options.highlighter || this.highlighter
14
+ this.updater = this.options.updater || this.updater
15
+ this.$zone = $("<div></div>").addClass("findahead control-group")
16
+ this.$loading = $("<div></div>")
17
+ this.$menu = $(this.options.menu)
18
+ this.shown = false
19
+ this.source = this.options.source
20
+
21
+ this.drawAddon()
22
+ this.listen()
23
+ }
24
+
25
+ Findahead.prototype = {
26
+
27
+ constructor: Findahead
28
+
29
+ , drawAddon: function() {
30
+ this.$element.after(this.$zone)
31
+ this.$zone.append(this.$element)
32
+ this.$zone.append(this.$loading)
33
+ }
34
+
35
+ , setLoadingEnabled: function(enable) {
36
+ if (this.options.visualLoading && enable) {
37
+ this.$loading.addClass("loading")
38
+ } else {
39
+ this.$loading.removeClass("loading")
40
+ }
41
+ }
42
+
43
+ , setNoItemEnabled: function(enable) {
44
+ if (enable) {
45
+ this.$zone.addClass("error")
46
+ } else {
47
+ this.$zone.removeClass("error")
48
+ }
49
+ }
50
+
51
+ , resetTimeout: function() {
52
+ // To avoid type spacing with less than 1 second
53
+ if ( this.timeout != null ) {
54
+ clearTimeout(this.timeout)
55
+ }
56
+
57
+ this.setNoItemEnabled(false)
58
+ this.setLoadingEnabled(false)
59
+
60
+ // Doesn't do search if keyword contains less than minLength letters
61
+ var value = this.$element.val()
62
+ if ( value.length > this.options.minLength ) {
63
+ this.setLoadingEnabled(true)
64
+ this.timeout = setTimeout($.proxy(this.lookup, this), this.options.timer)
65
+ }
66
+
67
+ this.shown ? this.hide() : this
68
+ }
69
+
70
+ , matcher: function(item) {
71
+ return ~item.toLowerCase().indexOf(this.query.toLowerCase())
72
+ }
73
+
74
+ , sorter: function(items) {
75
+ return items
76
+ }
77
+
78
+ , updater: function(item) {
79
+ return item
80
+ }
81
+
82
+ , select: function() {
83
+ var val = this.$menu.find('.active').data('value')
84
+ if (val != null) {
85
+ this.$element
86
+ .val(this.updater(val).toString())
87
+ .change()
88
+ return this.hide()
89
+ }
90
+ }
91
+
92
+ , show: function () {
93
+ var pos = $.extend({}, this.$element.position(), {
94
+ height: this.$element[0].offsetHeight
95
+ })
96
+
97
+ this.$menu
98
+ .insertAfter(this.$element)
99
+ .css({
100
+ top: pos.top + pos.height
101
+ , left: pos.left
102
+ })
103
+ .show()
104
+
105
+ this.shown = true
106
+ return this
107
+ }
108
+
109
+ , hide: function () {
110
+ this.$menu.hide()
111
+ this.shown = false
112
+ return this
113
+ }
114
+
115
+ , lookup: function (event) {
116
+ var items
117
+
118
+ this.query = this.$element.val()
119
+
120
+ items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source
121
+
122
+ return items ? this.process(items) : this
123
+ }
124
+
125
+ , process: function (items) {
126
+ var that = this
127
+
128
+ this.setLoadingEnabled(false)
129
+
130
+ items = $.grep(items, function (item) {
131
+ return that.matcher(item.toString())
132
+ })
133
+
134
+ if (items.length < 1) {
135
+ this.setNoItemEnabled(true)
136
+ return this.shown ? this.hide() : this
137
+ }
138
+
139
+ items = this.sorter(items)
140
+
141
+ return this.render(items.slice(0, this.options.items)).show()
142
+ }
143
+
144
+ , highlighter: function (item) {
145
+ var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
146
+ return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
147
+ return '<strong>' + match + '</strong>'
148
+ })
149
+ }
150
+
151
+ , render: function (items) {
152
+ var that = this
153
+
154
+ if (items.length > 0) {
155
+ items = $(items).map(function (i, item) {
156
+ i = $(that.options.item).data('value', item)
157
+ i.find('a').html(that.highlighter(item.toString()))
158
+ return i[0]
159
+ })
160
+
161
+ items.first().addClass('active')
162
+ }
163
+ this.$menu.html(items)
164
+ return this
165
+ }
166
+
167
+ , next: function (event) {
168
+ var active = this.$menu.find('.active').removeClass('active')
169
+ , next = active.next()
170
+
171
+ if (!next.length) {
172
+ next = $(this.$menu.find('li')[0])
173
+ }
174
+
175
+ next.addClass('active')
176
+ }
177
+
178
+ , prev: function (event) {
179
+ var active = this.$menu.find('.active').removeClass('active')
180
+ , prev = active.prev()
181
+
182
+ if (!prev.length) {
183
+ prev = this.$menu.find('li').last()
184
+ }
185
+
186
+ prev.addClass('active')
187
+ }
188
+
189
+ , listen: function () {
190
+ this.$element
191
+ .on('blur', $.proxy(this.blur, this))
192
+ .on('keypress', $.proxy(this.keypress, this))
193
+ .on('keyup', $.proxy(this.keyup, this))
194
+
195
+ if (this.eventSupported('keydown')) {
196
+ this.$element.on('keydown', $.proxy(this.keydown, this))
197
+ }
198
+
199
+ this.$menu
200
+ .on('click', $.proxy(this.click, this))
201
+ .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
202
+
203
+ }
204
+
205
+ , eventSupported: function(eventName) {
206
+ var isSupported = eventName in this.$element
207
+ if (!isSupported) {
208
+ this.$element.setAttribute(eventName, 'return;')
209
+ isSupported = typeof this.$element[eventName] === 'function'
210
+ }
211
+ return isSupported
212
+ }
213
+
214
+ , move: function (e) {
215
+ if (!this.shown) return
216
+
217
+ switch(e.keyCode) {
218
+ case 9: // tab
219
+ case 13: // enter
220
+ case 27: // escape
221
+ e.preventDefault()
222
+ break
223
+
224
+ case 38: // up arrow
225
+ e.preventDefault()
226
+ this.prev()
227
+ break
228
+
229
+ case 40: // down arrow
230
+ e.preventDefault()
231
+ this.next()
232
+ break
233
+ }
234
+
235
+ e.stopPropagation()
236
+ }
237
+
238
+ , keydown: function (e) {
239
+ this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27])
240
+ this.move(e)
241
+ }
242
+
243
+ , keypress: function (e) {
244
+ if (this.suppressKeyPressRepeat) return
245
+ this.move(e)
246
+ }
247
+
248
+ , keyup: function (e) {
249
+ switch(e.keyCode) {
250
+ case 40: // down arrow
251
+ case 38: // up arrow
252
+ case 37: // left arrow
253
+ case 39: // right arrow
254
+ case 16: // shift
255
+ case 17: // ctrl
256
+ case 18: // alt
257
+ break
258
+
259
+ case 9: // tab
260
+ case 13: // enter
261
+ if (!this.shown) return
262
+ this.select()
263
+ break
264
+
265
+ case 27: // escape
266
+ if (!this.shown) return
267
+ this.hide()
268
+ break
269
+
270
+ default:
271
+ this.resetTimeout()
272
+ }
273
+
274
+ e.stopPropagation()
275
+ e.preventDefault()
276
+ }
277
+
278
+ , blur: function (e) {
279
+ var that = this
280
+ setTimeout(function () { that.hide() }, 150)
281
+ }
282
+
283
+ , click: function (e) {
284
+ e.stopPropagation()
285
+ e.preventDefault()
286
+ this.select()
287
+ }
288
+
289
+ , mouseenter: function (e) {
290
+ this.$menu.find('.active').removeClass('active')
291
+ $(e.currentTarget).addClass('active')
292
+ }
293
+
294
+ }
295
+
296
+ /* FINDAHEAD PLUGIN DEFINITION
297
+ * =========================== */
298
+
299
+ var old = $.fn.findahead
300
+
301
+ $.fn.findahead = function (option) {
302
+ return this.each(function () {
303
+ var $this = $(this)
304
+ , data = $this.data('findahead')
305
+ , options = typeof option == 'object' && option
306
+ if (!data) $this.data('findahead', (data = new Findahead(this, options)))
307
+ if (typeof option == 'string') data[option]()
308
+ })
309
+ }
310
+
311
+ $.fn.findahead.defaults = {
312
+ source: []
313
+ , items: 8
314
+ , menu: '<ul class="dropdown-menu"></ul>'
315
+ , item: '<li><a href="#"></a></li>'
316
+ , minLength: 1
317
+ , timer: 0
318
+ , visualLoading: true
319
+ }
320
+
321
+ $.fn.findahead.Constructor = Findahead
322
+
323
+
324
+ /* FINDAHEAD NO CONFLICT
325
+ * =================== */
326
+
327
+ $.fn.findahead.noConflict = function () {
328
+ $.fn.findahead = old
329
+ return this
330
+ }
331
+
332
+ /* FINDAHEAD DATA-API
333
+ * ================== */
334
+
335
+ $(document).on('focus.findahead.data-api', '[data-provide="findahead"]', function (e) {
336
+ var $this = $(this)
337
+ if ($this.data('findahead')) return
338
+ e.preventDefault()
339
+ $this.findahead($this.data())
340
+ })
341
+
342
+ }(window.jQuery);
@@ -0,0 +1,13 @@
1
+ .findahead {
2
+ position: relative;
3
+ height: 30px;
4
+ }
5
+
6
+ .findahead .loading {
7
+ position: absolute;
8
+ top: 4px;
9
+ right: 15px;
10
+ width: 22px;
11
+ height: 22px;
12
+ background: url(loading.gif);
13
+ }
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-findahead-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierrick Rouxel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A search component for Twitter Bootstrap
56
+ email:
57
+ - pierrick.rouxel@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bootstrap-findahead-rails.gemspec
68
+ - lib/bootstrap/findahead/rails.rb
69
+ - lib/bootstrap/findahead/rails/version.rb
70
+ - vendor/assets/images/loading.gif
71
+ - vendor/assets/javascripts/bootstrap-findahead.js
72
+ - vendor/assets/stylesheets/bootstrap-findahead.css
73
+ homepage: https://github.com/pierrickrouxel/bootstrap-findahead-rails
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A search component for Twitter Bootstrap
97
+ test_files: []