multiple_select_rails 0.1.0

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: 93a7dd66f6d68c392b6c562516397f7684dcc3b1
4
+ data.tar.gz: 551d6af5479e88441c0332c729c8f19dfffd095f
5
+ SHA512:
6
+ metadata.gz: 4eb897f645f6b79ca370904d510991950487969e04365e9e418eaff78fb53309a8ea474543de459e83cddf2fc20c5398d26572adbf5a2a6ccdb5c5f549f882ed
7
+ data.tar.gz: 4e6a06e17a453f9e291875971ddd46b7e7a2f852ac66a1f7824b2d42ad0ad0add582d4ef242482a9630c9b9f310d0a24d2e191caab8d053fcdfc5facaf3b6c43
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multiple_select_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Renan Oronfle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Multiple Select Rails
2
+
3
+ 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/multiple_select_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'multiple_select_rails'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install multiple_select_rails
22
+
23
+ ## Usage
24
+
25
+ Include multiple select javascript
26
+
27
+ Add to your app/assets/javascripts/application.js
28
+
29
+ //= require multiple-select
30
+
31
+ Include multiple select stylesheet
32
+
33
+ Add to your app/assets/stylesheets/application.css
34
+
35
+ *= require multiple-select
36
+
37
+ Add to your js
38
+
39
+ $('select').multipleSelect();
40
+
41
+ ## Updating the gem
42
+
43
+ rake task designed to ease the maintenance of this gem.
44
+
45
+ The update task pulls the latest multiple-select code from github and places images, stylesheets and javascripts in the correct gem paths. It also changes background-image properties in the stylesheet to asset pipeline equivalents.
46
+
47
+ rake update
48
+
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/[my-github-username]/multiple_select_rails/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Update gem dependencies"
4
+ task :update do
5
+ system("rm -rf multi-select-src")
6
+
7
+ repository_url = 'git@github.com:wenzhixin/multiple-select.git'
8
+ project_name = 'multiple-select'
9
+ system("git clone #{repository_url}")
10
+ system("cp #{project_name}/multiple-select.png app/assets/images/multiple-select.png")
11
+ system("cp #{project_name}/multiple-select.css app/assets/stylesheets/multiple-select.scss")
12
+ system("cp #{project_name}/jquery.multiple.select.js app/assets/javascripts/multiple-select.js")
13
+
14
+ fixes
15
+
16
+ system("rm -rf multiple-select")
17
+ end
18
+
19
+ def fixes
20
+ replace_string_in_file("app/assets/stylesheets/multiple-select.scss", "url('multiple-select.png')", "image-url('multiple-select.png')")
21
+ end
22
+
23
+ def replace_string_in_file(file, find, replace)
24
+ file_content = File.read(file)
25
+
26
+ File.open(file, "w") do |f|
27
+ f.puts file_content.gsub!(find, replace)
28
+ end
29
+ end
@@ -0,0 +1,590 @@
1
+ /**
2
+ * @author zhixin wen <wenzhixin2010@gmail.com>
3
+ * @version 1.1.0
4
+ *
5
+ * http://wenzhixin.net.cn/p/multiple-select/
6
+ */
7
+
8
+ (function ($) {
9
+
10
+ 'use strict';
11
+
12
+ function MultipleSelect($el, options) {
13
+ var that = this,
14
+ name = $el.attr('name') || options.name || ''
15
+
16
+ $el.parent().hide();
17
+ var elWidth = $el.css("width");
18
+ $el.parent().show();
19
+ if (elWidth=="0px") {elWidth = $el.outerWidth()+20}
20
+
21
+ this.$el = $el.hide();
22
+ this.options = options;
23
+ this.$parent = $('<div' + $.map(['class', 'title'],function (att) {
24
+ var attValue = that.$el.attr(att) || '';
25
+ attValue = (att === 'class' ? ('ms-parent' + (attValue ? ' ' : '')) : '') + attValue;
26
+ return attValue ? (' ' + att + '="' + attValue + '"') : '';
27
+ }).join('') + ' />');
28
+ this.$choice = $('<button type="button" class="ms-choice"><span class="placeholder">' +
29
+ options.placeholder + '</span><div></div></button>');
30
+ this.$drop = $('<div class="ms-drop ' + options.position + '"></div>');
31
+ this.$el.after(this.$parent);
32
+ this.$parent.append(this.$choice);
33
+ this.$parent.append(this.$drop);
34
+
35
+ if (this.$el.prop('disabled')) {
36
+ this.$choice.addClass('disabled');
37
+ }
38
+ this.$parent.css('width', options.width || elWidth);
39
+
40
+ if (!this.options.keepOpen) {
41
+ $('body').click(function (e) {
42
+ if ($(e.target)[0] === that.$choice[0] ||
43
+ $(e.target).parents('.ms-choice')[0] === that.$choice[0]) {
44
+ return;
45
+ }
46
+ if (e.target.tagName.toUpperCase() === "INPUT" &&
47
+ ($(e.target)[0] === that.$drop[0] ||
48
+ $(e.target).parents('.ms-drop')[0] !== that.$drop[0]) &&
49
+ that.options.isOpen) {
50
+ that.close();
51
+ }
52
+ });
53
+ }
54
+
55
+ this.selectAllName = 'name="selectAll' + name + '"';
56
+ this.selectGroupName = 'name="selectGroup' + name + '"';
57
+ this.selectItemName = 'name="selectItem' + name + '"';
58
+ }
59
+
60
+ MultipleSelect.prototype = {
61
+ constructor: MultipleSelect,
62
+
63
+ init: function () {
64
+ var that = this,
65
+ html = [];
66
+ if (this.options.filter) {
67
+ html.push(
68
+ '<div class="ms-search">',
69
+ '<input type="text" autocomplete="off" autocorrect="off" autocapitilize="off" spellcheck="false">',
70
+ '</div>'
71
+ );
72
+ }
73
+ html.push('<ul>');
74
+ if (this.options.selectAll && !this.options.single) {
75
+ html.push(
76
+ '<li class="ms-select-all">',
77
+ '<label>',
78
+ '<input type="checkbox" ' + this.selectAllName + ' /> ',
79
+ this.options.selectAllDelimiter[0] + this.options.selectAllText + this.options.selectAllDelimiter[1],
80
+ '</label>',
81
+ '</li>'
82
+ );
83
+ }
84
+ $.each(this.$el.children(), function (i, elm) {
85
+ html.push(that.optionToHtml(i, elm));
86
+ });
87
+ html.push('<li class="ms-no-results">' + this.options.noMatchesFound + '</li>');
88
+ html.push('</ul>');
89
+ this.$drop.html(html.join(''));
90
+
91
+ this.$drop.find('ul').css('max-height', this.options.maxHeight + 'px');
92
+ this.$drop.find('.multiple').css('width', this.options.multipleWidth + 'px');
93
+
94
+ this.$searchInput = this.$drop.find('.ms-search input');
95
+ this.$selectAll = this.$drop.find('input[' + this.selectAllName + ']');
96
+ this.$selectGroups = this.$drop.find('input[' + this.selectGroupName + ']');
97
+ this.$selectItems = this.$drop.find('input[' + this.selectItemName + ']:enabled');
98
+ this.$disableItems = this.$drop.find('input[' + this.selectItemName + ']:disabled');
99
+ this.$noResults = this.$drop.find('.ms-no-results');
100
+ this.events();
101
+ this.updateSelectAll(true);
102
+ this.update(true);
103
+
104
+ if (this.options.isOpen) {
105
+ this.open();
106
+ }
107
+ },
108
+
109
+ optionToHtml: function (i, elm, group, groupDisabled) {
110
+ var that = this,
111
+ $elm = $(elm),
112
+ html = [],
113
+ multiple = this.options.multiple,
114
+ optAttributesToCopy = ['class', 'title'],
115
+ clss = $.map(optAttributesToCopy, function (att, i) {
116
+ var isMultiple = att === 'class' && multiple;
117
+ var attValue = $elm.attr(att) || '';
118
+ return (isMultiple || attValue) ?
119
+ (' ' + att + '="' + (isMultiple ? ('multiple' + (attValue ? ' ' : '')) : '') + attValue + '"') :
120
+ '';
121
+ }).join(''),
122
+ disabled,
123
+ type = this.options.single ? 'radio' : 'checkbox';
124
+
125
+ if ($elm.is('option')) {
126
+ var value = $elm.val(),
127
+ text = that.options.textTemplate($elm),
128
+ selected = (that.$el.attr('multiple') != undefined) ? $elm.prop('selected') : ($elm.attr('selected') == 'selected'),
129
+ style = this.options.styler(value) ? ' style="' + this.options.styler(value) + '"' : '';
130
+
131
+ disabled = groupDisabled || $elm.prop('disabled');
132
+ if ((this.options.blockSeparator > "") && (this.options.blockSeparator == $elm.val())) {
133
+ html.push(
134
+ '<li' + clss + style + '>',
135
+ '<label class="' + this.options.blockSeparator + (disabled ? 'disabled' : '') + '">',
136
+ text,
137
+ '</label>',
138
+ '</li>'
139
+ );
140
+ } else {
141
+ html.push(
142
+ '<li' + clss + style + '>',
143
+ '<label' + (disabled ? ' class="disabled"' : '') + '>',
144
+ '<input type="' + type + '" ' + this.selectItemName + ' value="' + value + '"' +
145
+ (selected ? ' checked="checked"' : '') +
146
+ (disabled ? ' disabled="disabled"' : '') +
147
+ (group ? ' data-group="' + group + '"' : '') +
148
+ '/> ',
149
+ text,
150
+ '</label>',
151
+ '</li>'
152
+ );
153
+ }
154
+ } else if (!group && $elm.is('optgroup')) {
155
+ var _group = 'group_' + i,
156
+ label = $elm.attr('label');
157
+
158
+ disabled = $elm.prop('disabled');
159
+ html.push(
160
+ '<li class="group">',
161
+ '<label class="optgroup' + (disabled ? ' disabled' : '') + '" data-group="' + _group + '">',
162
+ (this.options.hideOptgroupCheckboxes ? '' : '<input type="checkbox" ' + this.selectGroupName +
163
+ (disabled ? ' disabled="disabled"' : '') + ' /> '),
164
+ label,
165
+ '</label>',
166
+ '</li>');
167
+ $.each($elm.children(), function (i, elm) {
168
+ html.push(that.optionToHtml(i, elm, _group, disabled));
169
+ });
170
+ }
171
+ return html.join('');
172
+ },
173
+
174
+ events: function () {
175
+ var that = this;
176
+
177
+ function toggleOpen(e) {
178
+ e.preventDefault();
179
+ that[that.options.isOpen ? 'close' : 'open']();
180
+ }
181
+
182
+ var label = this.$el.parent().closest('label')[0] || $('label[for=' + this.$el.attr('id') + ']')[0];
183
+ if (label) {
184
+ $(label).off('click').on('click', function (e) {
185
+ if (e.target.nodeName.toLowerCase() !== 'label' || e.target !== this) {
186
+ return;
187
+ }
188
+ toggleOpen(e);
189
+ if (!that.options.filter || !that.options.isOpen) {
190
+ that.focus();
191
+ }
192
+ e.stopPropagation(); // Causes lost focus otherwise
193
+ });
194
+ }
195
+ this.$choice.off('click').on('click', toggleOpen)
196
+ .off('focus').on('focus', this.options.onFocus)
197
+ .off('blur').on('blur', this.options.onBlur);
198
+
199
+ this.$parent.off('keydown').on('keydown', function (e) {
200
+ switch (e.which) {
201
+ case 27: // esc key
202
+ that.close();
203
+ that.$choice.focus();
204
+ break;
205
+ }
206
+ });
207
+ this.$searchInput.off('keydown').on('keydown',function (e) {
208
+ if (e.keyCode === 9 && e.shiftKey) { // Ensure shift-tab causes lost focus from filter as with clicking away
209
+ that.close();
210
+ }
211
+ }).off('keyup').on('keyup', function (e) {
212
+ if (that.options.filterAcceptOnEnter &&
213
+ (e.which === 13 || e.which == 32) && // enter or space
214
+ that.$searchInput.val() // Avoid selecting/deselecting if no choices made
215
+ ) {
216
+ that.$selectAll.click();
217
+ that.close();
218
+ that.focus();
219
+ return;
220
+ }
221
+ that.filter();
222
+ });
223
+ this.$selectAll.off('click').on('click', function () {
224
+ var checked = $(this).prop('checked'),
225
+ $items = that.$selectItems.filter(':visible');
226
+ if ($items.length === that.$selectItems.length) {
227
+ that[checked ? 'checkAll' : 'uncheckAll']();
228
+ } else { // when the filter option is true
229
+ that.$selectGroups.prop('checked', checked);
230
+ $items.prop('checked', checked);
231
+ that.options[checked ? 'onCheckAll' : 'onUncheckAll']();
232
+ that.update();
233
+ }
234
+ });
235
+ this.$selectGroups.off('click').on('click', function () {
236
+ var group = $(this).parent().attr('data-group'),
237
+ $items = that.$selectItems.filter(':visible'),
238
+ $children = $items.filter('[data-group="' + group + '"]'),
239
+ checked = $children.length !== $children.filter(':checked').length;
240
+ $children.prop('checked', checked);
241
+ that.updateSelectAll();
242
+ that.update();
243
+ that.options.onOptgroupClick({
244
+ label: $(this).parent().text(),
245
+ checked: checked,
246
+ children: $children.get()
247
+ });
248
+ });
249
+ this.$selectItems.off('click').on('click', function () {
250
+ that.updateSelectAll();
251
+ that.update();
252
+ that.updateOptGroupSelect();
253
+ that.options.onClick({
254
+ label: $(this).parent().text(),
255
+ value: $(this).val(),
256
+ checked: $(this).prop('checked')
257
+ });
258
+
259
+ if (that.options.single && that.options.isOpen && !that.options.keepOpen) {
260
+ that.close();
261
+ }
262
+ });
263
+ },
264
+
265
+ open: function () {
266
+ if (this.$choice.hasClass('disabled')) {
267
+ return;
268
+ }
269
+ this.options.isOpen = true;
270
+ this.$choice.find('>div').addClass('open');
271
+ this.$drop.show();
272
+
273
+ // fix filter bug: no results show
274
+ this.$selectAll.parent().show();
275
+ this.$noResults.hide();
276
+
277
+ // Fix #77: 'All selected' when no options
278
+ if (this.$el.children().length === 0) {
279
+ this.$selectAll.parent().hide();
280
+ this.$noResults.show();
281
+ }
282
+
283
+ if (this.options.container) {
284
+ var offset = this.$drop.offset();
285
+ this.$drop.appendTo($(this.options.container));
286
+ this.$drop.offset({ top: offset.top, left: offset.left });
287
+ }
288
+ if (this.options.filter) {
289
+ this.$searchInput.val('');
290
+ this.$searchInput.focus();
291
+ this.filter();
292
+ }
293
+ this.options.onOpen();
294
+ },
295
+
296
+ close: function () {
297
+ this.options.isOpen = false;
298
+ this.$choice.find('>div').removeClass('open');
299
+ this.$drop.hide();
300
+ if (this.options.container) {
301
+ this.$parent.append(this.$drop);
302
+ this.$drop.css({
303
+ 'top': 'auto',
304
+ 'left': 'auto'
305
+ });
306
+ }
307
+ this.options.onClose();
308
+ },
309
+
310
+ update: function (isInit) {
311
+ var selects = this.getSelects(),
312
+ $span = this.$choice.find('>span');
313
+
314
+ if (selects.length === 0) {
315
+ $span.addClass('placeholder').html(this.options.placeholder);
316
+ } else if (this.options.countSelected && selects.length < this.options.minimumCountSelected) {
317
+ $span.removeClass('placeholder').html(
318
+ (this.options.displayValues ? selects : this.getSelects('text'))
319
+ .join(this.options.delimiter));
320
+ } else if (this.options.allSelected &&
321
+ selects.length === this.$selectItems.length + this.$disableItems.length) {
322
+ $span.removeClass('placeholder').html(this.options.allSelected);
323
+ } else if ((this.options.countSelected || this.options.etcaetera) && selects.length > this.options.minimumCountSelected) {
324
+ if (this.options.etcaetera) {
325
+ $span.removeClass('placeholder').html((this.options.displayValues ? selects : this.getSelects('text').slice(0, this.options.minimumCountSelected)).join(this.options.delimiter) + '...');
326
+ }
327
+ else {
328
+ $span.removeClass('placeholder').html(this.options.countSelected
329
+ .replace('#', selects.length)
330
+ .replace('%', this.$selectItems.length + this.$disableItems.length));
331
+ }
332
+ } else {
333
+ $span.removeClass('placeholder').html(
334
+ (this.options.displayValues ? selects : this.getSelects('text'))
335
+ .join(this.options.delimiter));
336
+ }
337
+ // set selects to select
338
+ this.$el.val(this.getSelects());
339
+
340
+ // add selected class to selected li
341
+ this.$drop.find('li').removeClass('selected');
342
+ this.$drop.find('input[' + this.selectItemName + ']:checked').each(function () {
343
+ $(this).parents('li').first().addClass('selected');
344
+ });
345
+
346
+ // trigger <select> change event
347
+ if (!isInit) {
348
+ this.$el.trigger('change');
349
+ }
350
+ },
351
+
352
+ updateSelectAll: function (Init) {
353
+ var $items = this.$selectItems;
354
+ if (!Init) { $items = $items.filter(':visible'); }
355
+ this.$selectAll.prop('checked', $items.length &&
356
+ $items.length === $items.filter(':checked').length);
357
+ if (this.$selectAll.prop('checked')) {
358
+ this.options.onCheckAll();
359
+ }
360
+ },
361
+
362
+ updateOptGroupSelect: function () {
363
+ var $items = this.$selectItems.filter(':visible');
364
+ $.each(this.$selectGroups, function (i, val) {
365
+ var group = $(val).parent().attr('data-group'),
366
+ $children = $items.filter('[data-group="' + group + '"]');
367
+ $(val).prop('checked', $children.length &&
368
+ $children.length === $children.filter(':checked').length);
369
+ });
370
+ },
371
+
372
+ //value or text, default: 'value'
373
+ getSelects: function (type) {
374
+ var that = this,
375
+ texts = [],
376
+ values = [];
377
+ this.$drop.find('input[' + this.selectItemName + ']:checked').each(function () {
378
+ texts.push($(this).parents('li').first().text());
379
+ values.push($(this).val());
380
+ });
381
+
382
+ if (type === 'text' && this.$selectGroups.length) {
383
+ texts = [];
384
+ this.$selectGroups.each(function () {
385
+ var html = [],
386
+ text = $.trim($(this).parent().text()),
387
+ group = $(this).parent().data('group'),
388
+ $children = that.$drop.find('[' + that.selectItemName + '][data-group="' + group + '"]'),
389
+ $selected = $children.filter(':checked');
390
+
391
+ if ($selected.length === 0) {
392
+ return;
393
+ }
394
+
395
+ html.push('[');
396
+ html.push(text);
397
+ if ($children.length > $selected.length) {
398
+ var list = [];
399
+ $selected.each(function () {
400
+ list.push($(this).parent().text());
401
+ });
402
+ html.push(': ' + list.join(', '));
403
+ }
404
+ html.push(']');
405
+ texts.push(html.join(''));
406
+ });
407
+ }
408
+ return type === 'text' ? texts : values;
409
+ },
410
+
411
+ setSelects: function (values) {
412
+ var that = this;
413
+ this.$selectItems.prop('checked', false);
414
+ $.each(values, function (i, value) {
415
+ that.$selectItems.filter('[value="' + value + '"]').prop('checked', true);
416
+ });
417
+ this.$selectAll.prop('checked', this.$selectItems.length ===
418
+ this.$selectItems.filter(':checked').length);
419
+ this.update();
420
+ },
421
+
422
+ enable: function () {
423
+ this.$choice.removeClass('disabled');
424
+ },
425
+
426
+ disable: function () {
427
+ this.$choice.addClass('disabled');
428
+ },
429
+
430
+ checkAll: function () {
431
+ this.$selectItems.prop('checked', true);
432
+ this.$selectGroups.prop('checked', true);
433
+ this.$selectAll.prop('checked', true);
434
+ this.update();
435
+ this.options.onCheckAll();
436
+ },
437
+
438
+ uncheckAll: function () {
439
+ this.$selectItems.prop('checked', false);
440
+ this.$selectGroups.prop('checked', false);
441
+ this.$selectAll.prop('checked', false);
442
+ this.update();
443
+ this.options.onUncheckAll();
444
+ },
445
+
446
+ focus: function () {
447
+ this.$choice.focus();
448
+ this.options.onFocus();
449
+ },
450
+
451
+ blur: function () {
452
+ this.$choice.blur();
453
+ this.options.onBlur();
454
+ },
455
+
456
+ refresh: function () {
457
+ this.init();
458
+ },
459
+
460
+ filter: function () {
461
+ var that = this,
462
+ text = $.trim(this.$searchInput.val()).toLowerCase();
463
+ if (text.length === 0) {
464
+ this.$selectItems.parent().show();
465
+ this.$disableItems.parent().show();
466
+ this.$selectGroups.parent().show();
467
+ } else {
468
+ this.$selectItems.each(function () {
469
+ var $parent = $(this).parent();
470
+ $parent[$parent.text().toLowerCase().indexOf(text) < 0 ? 'hide' : 'show']();
471
+ });
472
+ this.$disableItems.parent().hide();
473
+ this.$selectGroups.each(function () {
474
+ var $parent = $(this).parent();
475
+ var group = $parent.attr('data-group'),
476
+ $items = that.$selectItems.filter(':visible');
477
+ $parent[$items.filter('[data-group="' + group + '"]').length === 0 ? 'hide' : 'show']();
478
+ });
479
+
480
+ //Check if no matches found
481
+ if (this.$selectItems.filter(':visible').length) {
482
+ this.$selectAll.parent().show();
483
+ this.$noResults.hide();
484
+ } else {
485
+ this.$selectAll.parent().hide();
486
+ this.$noResults.show();
487
+ }
488
+ }
489
+ this.updateOptGroupSelect();
490
+ this.updateSelectAll();
491
+ }
492
+ };
493
+
494
+ $.fn.multipleSelect = function () {
495
+ var option = arguments[0],
496
+ args = arguments,
497
+
498
+ value,
499
+ allowedMethods = [
500
+ 'getSelects', 'setSelects',
501
+ 'enable', 'disable',
502
+ 'checkAll', 'uncheckAll',
503
+ 'focus', 'blur',
504
+ 'refresh', 'close'
505
+ ];
506
+
507
+ this.each(function () {
508
+ var $this = $(this),
509
+ data = $this.data('multipleSelect'),
510
+ options = $.extend({}, $.fn.multipleSelect.defaults,
511
+ $this.data(), typeof option === 'object' && option);
512
+
513
+ if (!data) {
514
+ data = new MultipleSelect($this, options);
515
+ $this.data('multipleSelect', data);
516
+ }
517
+
518
+ if (typeof option === 'string') {
519
+ if ($.inArray(option, allowedMethods) < 0) {
520
+ throw "Unknown method: " + option;
521
+ }
522
+ value = data[option](args[1]);
523
+ } else {
524
+ data.init();
525
+ if (args[1]) {
526
+ value = data[args[1]].apply(data, [].slice.call(args, 2));
527
+ }
528
+ }
529
+ });
530
+
531
+ return value ? value : this;
532
+ };
533
+
534
+ $.fn.multipleSelect.defaults = {
535
+ name: '',
536
+ isOpen: false,
537
+ placeholder: '',
538
+ selectAll: true,
539
+ selectAllText: 'Select all',
540
+ selectAllDelimiter: ['[', ']'],
541
+ allSelected: 'All selected',
542
+ minimumCountSelected: 3,
543
+ countSelected: '# of % selected',
544
+ noMatchesFound: 'No matches found',
545
+ multiple: false,
546
+ multipleWidth: 80,
547
+ single: false,
548
+ filter: false,
549
+ width: undefined,
550
+ maxHeight: 250,
551
+ container: null,
552
+ position: 'bottom',
553
+ keepOpen: false,
554
+ blockSeparator: '',
555
+ displayValues: false,
556
+ delimiter: ', ',
557
+
558
+ styler: function () {
559
+ return false;
560
+ },
561
+ textTemplate: function ($elm) {
562
+ return $elm.text();
563
+ },
564
+
565
+ onOpen: function () {
566
+ return false;
567
+ },
568
+ onClose: function () {
569
+ return false;
570
+ },
571
+ onCheckAll: function () {
572
+ return false;
573
+ },
574
+ onUncheckAll: function () {
575
+ return false;
576
+ },
577
+ onFocus: function () {
578
+ return false;
579
+ },
580
+ onBlur: function () {
581
+ return false;
582
+ },
583
+ onOptgroupClick: function () {
584
+ return false;
585
+ },
586
+ onClick: function () {
587
+ return false;
588
+ }
589
+ };
590
+ })(jQuery);
@@ -0,0 +1,191 @@
1
+ /**
2
+ * @author zhixin wen <wenzhixin2010@gmail.com>
3
+ */
4
+
5
+ .ms-parent {
6
+ display: inline-block;
7
+ position: relative;
8
+ vertical-align: middle;
9
+ }
10
+
11
+ .ms-choice {
12
+ display: block;
13
+ width: 100%;
14
+ height: 26px;
15
+ padding: 0;
16
+ overflow: hidden;
17
+ cursor: pointer;
18
+ border: 1px solid #aaa;
19
+ text-align: left;
20
+ white-space: nowrap;
21
+ line-height: 26px;
22
+ color: #444;
23
+ text-decoration: none;
24
+ -webkit-border-radius: 4px;
25
+ -moz-border-radius: 4px;
26
+ border-radius: 4px;
27
+ background-color: #fff;
28
+ }
29
+
30
+ .ms-choice.disabled {
31
+ background-color: #f4f4f4;
32
+ background-image: none;
33
+ border: 1px solid #ddd;
34
+ cursor: default;
35
+ }
36
+
37
+ .ms-choice > span {
38
+ position: absolute;
39
+ top: 0;
40
+ left: 0;
41
+ right: 20px;
42
+ white-space: nowrap;
43
+ overflow: hidden;
44
+ text-overflow: ellipsis;
45
+ display: block;
46
+ padding-left: 8px;
47
+ }
48
+
49
+ .ms-choice > span.placeholder {
50
+ color: #999;
51
+ }
52
+
53
+ .ms-choice > div {
54
+ position: absolute;
55
+ top: 0;
56
+ right: 0;
57
+ width: 20px;
58
+ height: 25px;
59
+ background: image-url('multiple-select.png') left top no-repeat;
60
+ }
61
+
62
+ .ms-choice > div.open {
63
+ background: image-url('multiple-select.png') right top no-repeat;
64
+ }
65
+
66
+ .ms-drop {
67
+ width: 100%;
68
+ overflow: hidden;
69
+ display: none;
70
+ margin-top: -1px;
71
+ padding: 0;
72
+ position: absolute;
73
+ z-index: 1000;
74
+ background: #fff;
75
+ color: #000;
76
+ border: 1px solid #aaa;
77
+ -webkit-border-radius: 4px;
78
+ -moz-border-radius: 4px;
79
+ border-radius: 4px;
80
+ }
81
+
82
+ .ms-drop.bottom {
83
+ top: 100%;
84
+ -webkit-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
85
+ -moz-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
86
+ box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
87
+ }
88
+
89
+ .ms-drop.top {
90
+ bottom: 100%;
91
+ -webkit-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
92
+ -moz-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
93
+ box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
94
+ }
95
+
96
+ .ms-search {
97
+ display: inline-block;
98
+ margin: 0;
99
+ min-height: 26px;
100
+ padding: 4px;
101
+ position: relative;
102
+ white-space: nowrap;
103
+ width: 100%;
104
+ z-index: 10000;
105
+ }
106
+
107
+ .ms-search input {
108
+ width: 100%;
109
+ height: auto !important;
110
+ min-height: 24px;
111
+ padding: 0 20px 0 5px;
112
+ margin: 0;
113
+ outline: 0;
114
+ font-family: sans-serif;
115
+ font-size: 1em;
116
+ border: 1px solid #aaa;
117
+ -webkit-border-radius: 0;
118
+ -moz-border-radius: 0;
119
+ border-radius: 0;
120
+ -webkit-box-shadow: none;
121
+ -moz-box-shadow: none;
122
+ box-shadow: none;
123
+ background: #fff image-url('multiple-select.png') no-repeat 100% -22px;
124
+ background: image-url('multiple-select.png') no-repeat 100% -22px, -webkit-gradient(linear, left bottom, left top, color-stop(0.85, white), color-stop(0.99, #eeeeee));
125
+ background: image-url('multiple-select.png') no-repeat 100% -22px, -webkit-linear-gradient(center bottom, white 85%, #eeeeee 99%);
126
+ background: image-url('multiple-select.png') no-repeat 100% -22px, -moz-linear-gradient(center bottom, white 85%, #eeeeee 99%);
127
+ background: image-url('multiple-select.png') no-repeat 100% -22px, -o-linear-gradient(bottom, white 85%, #eeeeee 99%);
128
+ background: image-url('multiple-select.png') no-repeat 100% -22px, -ms-linear-gradient(top, #ffffff 85%, #eeeeee 99%);
129
+ background: image-url('multiple-select.png') no-repeat 100% -22px, linear-gradient(top, #ffffff 85%, #eeeeee 99%);
130
+ }
131
+
132
+ .ms-search, .ms-search input {
133
+ -webkit-box-sizing: border-box;
134
+ -khtml-box-sizing: border-box;
135
+ -moz-box-sizing: border-box;
136
+ -ms-box-sizing: border-box;
137
+ box-sizing: border-box;
138
+ }
139
+
140
+ .ms-drop ul {
141
+ overflow: auto;
142
+ margin: 0;
143
+ padding: 5px 8px;
144
+ }
145
+
146
+ .ms-drop ul > li {
147
+ list-style: none;
148
+ display: list-item;
149
+ background-image: none;
150
+ position: static;
151
+ }
152
+
153
+ .ms-drop ul > li .disabled {
154
+ opacity: .35;
155
+ filter: Alpha(Opacity=35);
156
+ }
157
+
158
+ .ms-drop ul > li.multiple {
159
+ display: block;
160
+ float: left;
161
+ }
162
+
163
+ .ms-drop ul > li.group {
164
+ clear: both;
165
+ }
166
+
167
+ .ms-drop ul > li.multiple label {
168
+ width: 100%;
169
+ display: block;
170
+ white-space: nowrap;
171
+ overflow: hidden;
172
+ text-overflow: ellipsis;
173
+ }
174
+
175
+ .ms-drop ul > li label {
176
+ font-weight: normal;
177
+ display: block;
178
+ white-space: nowrap;
179
+ }
180
+
181
+ .ms-drop ul > li label.optgroup {
182
+ font-weight: bold;
183
+ }
184
+
185
+ .ms-drop input[type="checkbox"] {
186
+ vertical-align: middle;
187
+ }
188
+
189
+ .ms-drop .ms-no-results {
190
+ display: none;
191
+ }
@@ -0,0 +1,6 @@
1
+ module MultipleSelectRails
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module MultipleSelectRails
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie; end
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module MultipleSelectRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ require "multiple_select_rails/version"
3
+
4
+ module MultipleSelectRails
5
+ module Rails
6
+ if ::Rails.version < "3.1"
7
+ require "multiple_select_rails/railtie"
8
+ else
9
+ require "multiple_select_rails/engine"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multiple_select_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multiple_select_rails"
8
+ spec.version = MultipleSelectRails::VERSION
9
+ spec.authors = ["Renan Oronfle"]
10
+ spec.email = ["renanoronfle@gmail.com"]
11
+
12
+ spec.summary = %q{Multiple select jQuery plugin for Rails asset pipeline}
13
+ spec.description = %q{Multiple select jQuery plugin for Rails asset pipeline}
14
+ spec.homepage = "https://github.com/renanoronfle/multiple_select_rails"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "railties", ">= 3.0"
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiple_select_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Renan Oronfle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-25 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
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.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Multiple select jQuery plugin for Rails asset pipeline
56
+ email:
57
+ - renanoronfle@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - app/assets/images/multiple-select.png
69
+ - app/assets/javascripts/multiple-select.js
70
+ - app/assets/stylesheets/multiple-select.scss
71
+ - lib/multiple_select_rails.rb
72
+ - lib/multiple_select_rails/engine.rb
73
+ - lib/multiple_select_rails/railtie.rb
74
+ - lib/multiple_select_rails/version.rb
75
+ - multiple_select_rails.gemspec
76
+ homepage: https://github.com/renanoronfle/multiple_select_rails
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.0
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Multiple select jQuery plugin for Rails asset pipeline
100
+ test_files: []