search_selector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec6d006cd1cc884fe45e29de8bb34487c4803271
4
+ data.tar.gz: 61039c2b023b4d022f5ed8fa6792da51de95bda9
5
+ SHA512:
6
+ metadata.gz: 89bec611590ccf419f9888cb70c022d4ff0e6efcd6b9a82be980896140af877d2ca55fec3237f27fe8cbc07b2221686d416b7d7da444179691b875379a900c41
7
+ data.tar.gz: 730d2569f136a7bac223d8b874723d3ecaabbf59d6d9091f11c3816c433b2a394e5cd4d4880ff3a4532b814615edf4d675ec78e5688482ab89a55483163841a7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Alexander Gorbunov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = SearchSelector
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SearchSelector'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,167 @@
1
+ (
2
+ ($) ->
3
+ class SearchSelector
4
+ templates: {
5
+ ui: '''
6
+ <div class="ss-dropdown ss-hide">
7
+ <div>
8
+ <input type="text" placeholder="начните вводить..." class="ss-filter-field">
9
+ </div>
10
+ <ul class="ss-items">
11
+ </ul>
12
+ </div>
13
+ '''
14
+
15
+ item: '''
16
+ <li>
17
+ <a href="#" class="ss-item-handler" data-id="{id}">{name}</a>
18
+ </li>
19
+ '''
20
+ }
21
+
22
+ triggers:
23
+ opened: 'ss-opened'
24
+ closed: 'ss-closed'
25
+ change: 'ss-change'
26
+ keyup: 'ss-keyup'
27
+
28
+ css:
29
+ hide: 'ss-hide'
30
+
31
+ selectors:
32
+ handler: '.ss-handler'
33
+ itemHandler: '.ss-item-handler'
34
+ dropDown: '.ss-dropdown'
35
+ items: '.ss-dropdown .ss-items'
36
+ item: '.ss-dropdown .ss-items .ss-item-handler'
37
+ filterField: '.ss-filter-field'
38
+
39
+ bindings: {
40
+ 'click handler': ->
41
+ if @isOpen
42
+ @close()
43
+ else
44
+ @open()
45
+
46
+ 'click item': (e) ->
47
+ e.preventDefault()
48
+ cityId = $(e.currentTarget).data('id')
49
+ @select(cityId)
50
+ @close()
51
+
52
+ 'keyup filterField': (e) ->
53
+ @trigger('keyup', e.currentTarget)
54
+ }
55
+
56
+ find: (selector) -> @el.find(selector)
57
+ trigger: (triggerName, options = null) ->
58
+ @handler.trigger(@triggers[triggerName], options)
59
+
60
+ constructor: (@options = {}) ->
61
+ @el = $(options['el'])
62
+ throw 'Anchor element not found' unless @el.length
63
+ @selected = null
64
+ @selected_caption = null
65
+
66
+ @el.append($(@template('ui')))
67
+ @handler = @find(@selectors.handler).eq(0)
68
+ for event_selector, callback of @bindings
69
+ do (event_selector, callback) =>
70
+ [event, selector] = event_selector.split(' ')
71
+ # Заменяем на css селектор
72
+ selector = @selectors[selector]
73
+ @el.on event, selector, =>
74
+ callback.apply(@, arguments) if typeof callback == 'function' and event and selector
75
+ @init()
76
+
77
+ init: ->
78
+ @$dropDown = @find(@selectors.dropDown)
79
+ @$items = @find(@selectors.items)
80
+ @isOpen = false
81
+ @items = []
82
+ @setItems(@options['items']) if @options['items']?
83
+ @select(@options['value']) if @options['value']?
84
+
85
+ open: ->
86
+ offset = @handler.offset()
87
+ @$dropDown.css(top: offset.top + parseInt(@handler.outerHeight(true)))
88
+ @$dropDown.css(left: offset.left)
89
+ @$dropDown.removeClass(@css.hide)
90
+ @isOpen = true
91
+ @trigger('opened')
92
+
93
+ close: ->
94
+ @$dropDown.addClass(@css.hide)
95
+ @isOpen = false
96
+ @trigger('closed')
97
+
98
+ drawDropDown: ->
99
+ @$items.empty()
100
+ for item of @items
101
+ [key, val] =
102
+ if @items instanceof Array
103
+ [item, item]
104
+ else
105
+ [item, @items[item]]
106
+ $item = $(@template('item', id: key, name: val))
107
+ @$items.append($item)
108
+
109
+ template: (name, params = {}) ->
110
+ template = @templates[name]
111
+ for key, val of params
112
+ template = template.replace(new RegExp("\\{#{key}\\}", 'gi'), val)
113
+ template
114
+
115
+ select: (item) ->
116
+ if @items instanceof Array
117
+ if item in @items
118
+ @selected = item
119
+ @selected_caption = item
120
+ @handler.html(item)
121
+ @trigger('change', [@val(), @text()])
122
+ else
123
+ if item.toString() in Object.keys(@items)
124
+ @selected = item
125
+ @selected_caption = @items[item]
126
+ @handler.html(@text())
127
+ @trigger('change', [@val(), @text()])
128
+
129
+ val: -> @selected
130
+
131
+ text: -> @selected_caption
132
+
133
+ setItems: (items) ->
134
+ @items = items
135
+ @drawDropDown()
136
+
137
+ jQuery.fn.search_selector = (options) ->
138
+ $el = $(this)
139
+ if typeof options == 'string'
140
+ searchSelector = $el.data('search-selector')
141
+ switch options
142
+ when 'val'
143
+ searchSelector.selected
144
+ when 'text'
145
+ searchSelector.text()
146
+ when 'search_selector'
147
+ searchSelector
148
+ else
149
+ $ss = $('<span class="ss-element"></span>')
150
+ $el.before($ss).detach()
151
+ $ss.append($el)
152
+ $el.addClass('ss-handler')
153
+ searchSelector = new SearchSelector($.extend(options, el: $ss))
154
+ $ss.data('search-selector', searchSelector)
155
+ $el.data('search-selector', searchSelector)
156
+
157
+ $(document).on 'click', (e) ->
158
+ searchSelector.close() if $(e.target).closest('.ss-element').data('search-selector') != searchSelector
159
+
160
+ )(jQuery)
161
+
162
+
163
+
164
+
165
+
166
+
167
+
@@ -0,0 +1,51 @@
1
+ .ss-handler {
2
+ color: #ff821e;
3
+ cursor: pointer;
4
+ font-size: 14px;
5
+ line-height: 14px;
6
+ // margin-left: 20px;
7
+ // margin-right: 10px;
8
+ // margin-top: 15px;
9
+ border-bottom: 1px dashed #ff821e;
10
+ display: inline-block;
11
+ white-space: nowrap;
12
+ overflow: hidden;
13
+ text-overflow: ellipsis;
14
+ }
15
+
16
+ .ss-hide {
17
+ display: none !important;
18
+ }
19
+
20
+ .ss-dropdown {
21
+ margin: 0;
22
+ padding: 0;
23
+ position: absolute;
24
+ // width: 200px;
25
+ z-index: 1;
26
+ background: rgba(#323232, 1);
27
+ ul {
28
+ list-style: none;
29
+ li {
30
+ padding: 0px;
31
+ a {
32
+ color: white;
33
+ text-decoration: none;
34
+ display: block;
35
+ outline: none;
36
+ padding: 3px 10px 3px 10px;
37
+ &:hover {
38
+ background: #ff821e;
39
+ }
40
+ }
41
+ }
42
+ }
43
+ .ss-filter-field {
44
+ margin: 0 10px;
45
+ width: calc(100% - 20px);
46
+ height: 24px;
47
+ border: none;
48
+ padding: 5px;
49
+ outline: none;
50
+ }
51
+ }
@@ -0,0 +1,3 @@
1
+ module SearchSelector
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ module SearchSelector
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :search_selector do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_selector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Gorbunov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: DropDown selector with search field.
28
+ email:
29
+ - lexgorbunov@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - app/assets/javascripts/search_selector.js.coffee
38
+ - app/assets/stylesheets/search_selector.css.scss
39
+ - lib/search_selector.rb
40
+ - lib/search_selector/version.rb
41
+ - lib/tasks/search_selector_tasks.rake
42
+ homepage: https://github.com/lexgorbunov/search_selector
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: DropDown selector with search field.
66
+ test_files: []