bootstrap-multiselect-rails4 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +27 -0
  4. data/README.rdoc +3 -0
  5. data/Rakefile +38 -0
  6. data/lib/bootstrap-multiselect-rails4.rb +4 -0
  7. data/lib/bootstrap-multiselect-rails4/version.rb +3 -0
  8. data/lib/tasks/bootstrap-multiselect-rails4_tasks.rake +4 -0
  9. data/test/bootstrap-multiselect-rails4_test.rb +7 -0
  10. data/test/dummy/README.rdoc +261 -0
  11. data/test/dummy/Rakefile +7 -0
  12. data/test/dummy/app/assets/javascripts/application.js +15 -0
  13. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  14. data/test/dummy/app/controllers/application_controller.rb +3 -0
  15. data/test/dummy/app/helpers/application_helper.rb +2 -0
  16. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  17. data/test/dummy/config.ru +4 -0
  18. data/test/dummy/config/application.rb +59 -0
  19. data/test/dummy/config/boot.rb +10 -0
  20. data/test/dummy/config/database.yml +25 -0
  21. data/test/dummy/config/environment.rb +5 -0
  22. data/test/dummy/config/environments/development.rb +37 -0
  23. data/test/dummy/config/environments/production.rb +67 -0
  24. data/test/dummy/config/environments/test.rb +37 -0
  25. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  26. data/test/dummy/config/initializers/inflections.rb +15 -0
  27. data/test/dummy/config/initializers/mime_types.rb +5 -0
  28. data/test/dummy/config/initializers/secret_token.rb +7 -0
  29. data/test/dummy/config/initializers/session_store.rb +8 -0
  30. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  31. data/test/dummy/config/locales/en.yml +5 -0
  32. data/test/dummy/config/routes.rb +58 -0
  33. data/test/dummy/public/404.html +26 -0
  34. data/test/dummy/public/422.html +26 -0
  35. data/test/dummy/public/500.html +25 -0
  36. data/test/dummy/public/favicon.ico +0 -0
  37. data/test/dummy/script/rails +6 -0
  38. data/test/test_helper.rb +15 -0
  39. data/vendor/assets/javascripts/bootstrap-multiselect.js +538 -0
  40. data/vendor/assets/stylesheets/bootstrap-multiselect.css +1 -0
  41. metadata +142 -0
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,15 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
+
12
+ # Load fixtures from the engine
13
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
14
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
15
+ end
@@ -0,0 +1,538 @@
1
+ /**
2
+ * bootstrap-multiselect.js 1.0.0
3
+ * https://github.com/davidstutz/bootstrap-multiselect
4
+ *
5
+ * Copyright 2012 David Stutz
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ !function($) {"use strict";// jshint ;_;
20
+
21
+ if ( typeof ko != 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect) {
22
+ ko.bindingHandlers.multiselect = {
23
+ init : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
24
+ },
25
+ update : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
26
+ var ms = $(element).data('multiselect');
27
+ if (!ms) {
28
+ $(element).multiselect(ko.utils.unwrapObservable(valueAccessor()));
29
+ }
30
+ else
31
+ if (allBindingsAccessor().options && allBindingsAccessor().options().length !== ms.originalOptions.length) {
32
+ ms.updateOriginalOptions();
33
+ $(element).multiselect('rebuild');
34
+ }
35
+ }
36
+ };
37
+ }
38
+
39
+ function Multiselect(select, options) {
40
+
41
+ this.options = this.getOptions(options);
42
+ this.$select = $(select);
43
+ this.originalOptions = this.$select.clone()[0].options;
44
+ //we have to clone to create a new reference
45
+ this.query = '';
46
+ this.searchTimeout = null;
47
+
48
+ this.options.multiple = this.$select.attr('multiple') == "multiple";
49
+
50
+ this.$container = $(this.options.buttonContainer).append('<button type="button" class="multiselect dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText(this.getSelected(), this.$select) + '</button>')
51
+ .append('<ul class="multiselect-container dropdown-menu' + (this.options.dropRight ? ' pull-right' : '') + '"></ul>');
52
+
53
+ if (this.options.buttonWidth) {
54
+ $('button', this.$container).css({
55
+ 'width' : this.options.buttonWidth
56
+ });
57
+ }
58
+
59
+ // Set max height of dropdown menu to activate auto scrollbar.
60
+ if (this.options.maxHeight) {
61
+ // TODO: Add a class for this option to move the css declarations.
62
+ $('.multiselect-container', this.$container).css({
63
+ 'max-height' : this.options.maxHeight + 'px',
64
+ 'overflow-y' : 'auto',
65
+ 'overflow-x' : 'hidden'
66
+ });
67
+ }
68
+
69
+ // Enable filtering.
70
+ if (this.options.enableFiltering || this.options.enableCaseInsensitiveFiltering) {
71
+ this.buildFilter();
72
+ }
73
+
74
+ this.buildDropdown();
75
+ this.updateButtonText();
76
+
77
+ this.$select.hide().after(this.$container);
78
+ };
79
+
80
+ Multiselect.prototype = {
81
+
82
+ defaults : {
83
+ // Default text function will either print 'None selected' in case no
84
+ // option is selected, or a list of the selected options up to a length of 3 selected options.
85
+ // If more than 3 options are selected, the number of selected options is printed.
86
+ buttonText : function(options, select) {
87
+ if (options.length == 0) {
88
+ return this.nonSelectedText + '<b class="caret"></b>';
89
+ }
90
+ else
91
+ if (options.length > 3) {
92
+ return options.length + ' ' + this.nSelectedText + ' <b class="caret"></b>';
93
+ }
94
+ else {
95
+ var selected = '';
96
+ options.each(function() {
97
+ var label = ($(this).attr('label') !== undefined) ? $(this).attr('label') : $(this).html();
98
+
99
+ selected += label + ', ';
100
+ });
101
+ return selected.substr(0, selected.length - 2) + ' <b class="caret"></b>';
102
+ }
103
+ },
104
+ // Is triggered on change of the selected options.
105
+ onChange : function(option, checked) {
106
+
107
+ },
108
+ buttonClass : 'btn',
109
+ dropRight : false,
110
+ selectedClass : 'active',
111
+ buttonWidth : 'auto',
112
+ buttonContainer : '<div class="btn-group" />',
113
+ // Maximum height of the dropdown menu.
114
+ // If maximum height is exceeded a scrollbar will be displayed.
115
+ maxHeight : false,
116
+ includeSelectAllOption : false,
117
+ selectAllText : ' Select all',
118
+ selectAllValue : 'multiselect-all',
119
+ enableFiltering : false,
120
+ enableCaseInsensitiveFiltering : false,
121
+ filterPlaceholder : 'Search',
122
+ // possible options: 'text', 'value', 'both'
123
+ filterBehavior : 'text',
124
+ preventInputChangeEvent: false,
125
+ nonSelectedText: 'None selected',
126
+ nSelectedText: 'selected'
127
+ },
128
+
129
+ constructor : Multiselect,
130
+
131
+ // Will build an dropdown element for the given option.
132
+ createOptionValue : function(element) {
133
+ if ($(element).is(':selected')) {
134
+ $(element).attr('selected', 'selected').prop('selected', true);
135
+ }
136
+
137
+ // Support the label attribute on options.
138
+ var label = $(element).attr('label') || $(element).html();
139
+ var value = $(element).val();
140
+ var inputType = this.options.multiple ? "checkbox" : "radio";
141
+
142
+ var $li = $('<li><a href="javascript:void(0);"><label class="' + inputType + '"><input type="' + inputType + '" /></label></a></li>');
143
+
144
+ var selected = $(element).prop('selected') || false;
145
+ var $checkbox = $('input', $li);
146
+ $checkbox.val(value);
147
+
148
+ if (value == this.options.selectAllValue) {
149
+ $checkbox.parent().parent().addClass('multiselect-all');
150
+ }
151
+
152
+ $('label', $li).append(" " + label);
153
+
154
+ $('.multiselect-container', this.$container).append($li);
155
+
156
+ if ($(element).is(':disabled')) {
157
+ $checkbox.attr('disabled', 'disabled').prop('disabled', true).parents('li').addClass('disabled');
158
+ }
159
+
160
+ $checkbox.prop('checked', selected);
161
+
162
+ if (selected && this.options.selectedClass) {
163
+ $checkbox.parents('li').addClass(this.options.selectedClass);
164
+ }
165
+ },
166
+
167
+ toggleActiveState : function(shouldBeActive) {
168
+ if (this.$select.attr('disabled') == undefined) {
169
+ $('button.multiselect.dropdown-toggle', this.$container).removeClass('disabled');
170
+ }
171
+ else {
172
+ $('button.multiselect.dropdown-toggle', this.$container).addClass('disabled');
173
+ }
174
+ },
175
+
176
+ // Build the dropdown and bind event handling.
177
+ buildDropdown : function() {
178
+ var alreadyHasSelectAll = this.$select[0][0] ? this.$select[0][0].value == this.options.selectAllValue : false;
179
+
180
+ // If options.includeSelectAllOption === true, add the include all
181
+ // checkbox.
182
+ if (this.options.includeSelectAllOption && this.options.multiple && !alreadyHasSelectAll) {
183
+ this.$select.prepend('<option value="' + this.options.selectAllValue + '">' + this.options.selectAllText + '</option>');
184
+ }
185
+
186
+ this.toggleActiveState();
187
+
188
+ this.$select.children().each($.proxy(function(index, element) {
189
+ // Support optgroups and options without a group simultaneously.
190
+ var tag = $(element).prop('tagName').toLowerCase();
191
+ if (tag == 'optgroup') {
192
+ var group = element;
193
+ var groupName = $(group).prop('label');
194
+
195
+ // Add a header for the group.
196
+ var $li = $('<li><label class="multiselect-group"></label></li>');
197
+ $('label', $li).text(groupName);
198
+ $('.multiselect-container', this.$container).append($li);
199
+
200
+ // Add the options of the group.
201
+ $('option', group).each($.proxy(function(index, element) {
202
+ this.createOptionValue(element);
203
+ }, this));
204
+ }
205
+ else
206
+ if (tag == 'option') {
207
+ this.createOptionValue(element);
208
+ }
209
+ else {
210
+ // Ignore illegal tags.
211
+ }
212
+ }, this));
213
+
214
+ // Bind the change event on the dropdown elements.
215
+ $('.multiselect-container li input', this.$container).on('change', $.proxy(function(event) {
216
+ var checked = $(event.target).prop('checked') || false;
217
+ var isSelectAllOption = $(event.target).val() == this.options.selectAllValue;
218
+
219
+ // Apply or unapply the configured selected class.
220
+ if (this.options.selectedClass) {
221
+ if (checked) {
222
+ $(event.target).parents('li').addClass(this.options.selectedClass);
223
+ }
224
+ else {
225
+ $(event.target).parents('li').removeClass(this.options.selectedClass);
226
+ }
227
+ }
228
+
229
+ var $option = $('option', this.$select).filter(function() {
230
+ return $(this).val() == $(event.target).val();
231
+ });
232
+
233
+ var $optionsNotThis = $('option', this.$select).not($option);
234
+ var $checkboxesNotThis = $('input', this.$container).not($(event.target));
235
+
236
+ // Toggle all options if the select all option was changed.
237
+ if (isSelectAllOption) {
238
+ $checkboxesNotThis.filter(function() {
239
+ return $(this).is(':checked') != checked;
240
+ }).trigger('click');
241
+ }
242
+
243
+ if (checked) {
244
+ $option.prop('selected', true);
245
+
246
+ if (this.options.multiple) {
247
+ $option.attr('selected', 'selected');
248
+ }
249
+ else {
250
+ if (this.options.selectedClass) {
251
+ $($checkboxesNotThis).parents('li').removeClass(this.options.selectedClass);
252
+ }
253
+
254
+ $($checkboxesNotThis).prop('checked', false);
255
+
256
+ $optionsNotThis.removeAttr('selected').prop('selected', false);
257
+
258
+ // It's a single selection, so close.
259
+ $(this.$container).find(".multiselect.dropdown-toggle").click();
260
+ }
261
+
262
+ if (this.options.selectedClass == "active") {
263
+ $optionsNotThis.parents("a").css("outline", "");
264
+ }
265
+
266
+ }
267
+ else {
268
+ $option.removeAttr('selected').prop('selected', false);
269
+ }
270
+
271
+ this.updateButtonText();
272
+
273
+ this.options.onChange($option, checked);
274
+
275
+ this.$select.change();
276
+
277
+ if(this.options.preventInputChangeEvent) {
278
+ return false;
279
+ }
280
+ }, this));
281
+
282
+ $('.multiselect-container li a', this.$container).on('touchstart click', function(event) {
283
+ event.stopPropagation();
284
+ $(event.target).blur();
285
+ });
286
+
287
+ // Keyboard support.
288
+ this.$container.on('keydown', $.proxy(function(event) {
289
+ if ($('input[type="text"]', this.$container).is(':focus'))
290
+ return;
291
+ if ((event.keyCode == 9 || event.keyCode == 27) && this.$container.hasClass('open')) {
292
+ // Close on tab or escape.
293
+ $(this.$container).find(".multiselect.dropdown-toggle").click();
294
+ }
295
+ else {
296
+ var $items = $(this.$container).find("li:not(.divider):visible a");
297
+
298
+ if (!$items.length) {
299
+ return;
300
+ }
301
+
302
+ var index = $items.index($items.filter(':focus'));
303
+
304
+ // Navigation up.
305
+ if (event.keyCode == 38 && index > 0) {
306
+ index--;
307
+ }
308
+ // Navigate down.
309
+ else
310
+ if (event.keyCode == 40 && index < $items.length - 1) {
311
+ index++;
312
+ }
313
+ else
314
+ if (!~index) {
315
+ index = 0;
316
+ }
317
+
318
+ var $current = $items.eq(index);
319
+ $current.focus();
320
+
321
+ // Override style for items in li:active.
322
+ if (this.options.selectedClass == "active") {
323
+ $current.css("outline", "thin dotted #333").css("outline", "5px auto -webkit-focus-ring-color");
324
+
325
+ $items.not($current).css("outline", "");
326
+ }
327
+
328
+ if (event.keyCode == 32 || event.keyCode == 13) {
329
+ var $checkbox = $current.find('input');
330
+
331
+ $checkbox.prop("checked", !$checkbox.prop("checked"));
332
+ $checkbox.change();
333
+ }
334
+
335
+ event.stopPropagation();
336
+ event.preventDefault();
337
+ }
338
+ }, this));
339
+ },
340
+
341
+ // Build and bind filter.
342
+ buildFilter: function() {
343
+ $('.multiselect-container', this.$container).prepend('<div class="input-prepend"><span class="add-on"><i class="icon-search"></i></span><input class="multiselect-search" type="text" placeholder="' + this.options.filterPlaceholder + '"></div>');
344
+
345
+ $('.multiselect-search', this.$container).val(this.query).on('click', function(event) {
346
+ event.stopPropagation();
347
+ }).on('keydown', $.proxy(function(event) {
348
+ // This is useful to catch "keydown" events after the browser has
349
+ // updated the control.
350
+ clearTimeout(this.searchTimeout);
351
+
352
+ this.searchTimeout = this.asyncFunction($.proxy(function() {
353
+
354
+ if (this.query != event.target.value) {
355
+ this.query = event.target.value;
356
+
357
+ $.each($('.multiselect-container li', this.$container), $.proxy(function(index, element) {
358
+ var value = $('input', element).val();
359
+ if (value != this.options.selectAllValue) {
360
+ var text = $('label', element).text();
361
+ var value = $('input', element).val();
362
+ if (value && text && value != this.options.selectAllValue) {
363
+ // by default lets assume that element is not
364
+ // interesting for this search
365
+ var showElement = false;
366
+
367
+ var filterCandidate = '';
368
+ if ((this.options.filterBehavior == 'text' || this.options.filterBehavior == 'both')) {
369
+ filterCandidate = text;
370
+ }
371
+ if ((this.options.filterBehavior == 'value' || this.options.filterBehavior == 'both')) {
372
+ filterCandidate = value;
373
+ }
374
+
375
+ if (this.options.enableCaseInsensitiveFiltering && filterCandidate.toLowerCase().indexOf(this.query.toLowerCase()) > -1) {
376
+ showElement = true;
377
+ }
378
+ else if (filterCandidate.indexOf(this.query) > -1) {
379
+ showElement = true;
380
+ }
381
+
382
+ if (showElement) {
383
+ $(element).show();
384
+ }
385
+ else {
386
+ $(element).hide();
387
+ }
388
+ }
389
+ }
390
+ }, this));
391
+ }
392
+ }, this), 300, this);
393
+ }, this));
394
+ },
395
+
396
+ // Destroy - unbind - the plugin.
397
+ destroy : function() {
398
+ this.$container.remove();
399
+ this.$select.show();
400
+ },
401
+
402
+ // Refreshs the checked options based on the current state of the select.
403
+ refresh : function() {
404
+ $('option', this.$select).each($.proxy(function(index, element) {
405
+ var $input = $('.multiselect-container li input', this.$container).filter(function() {
406
+ return $(this).val() == $(element).val();
407
+ });
408
+
409
+ if ($(element).is(':selected')) {
410
+ $input.prop('checked', true);
411
+
412
+ if (this.options.selectedClass) {
413
+ $input.parents('li').addClass(this.options.selectedClass);
414
+ }
415
+ }
416
+ else {
417
+ $input.prop('checked', false);
418
+
419
+ if (this.options.selectedClass) {
420
+ $input.parents('li').removeClass(this.options.selectedClass);
421
+ }
422
+ }
423
+
424
+ if ($(element).is(":disabled")) {
425
+ $input.attr('disabled', 'disabled').prop('disabled', true).parents('li').addClass('disabled');
426
+ }
427
+ else {
428
+ $input.removeAttr('disabled').prop('disabled', false).parents('li').removeClass('disabled');
429
+ }
430
+ }, this));
431
+
432
+ this.updateButtonText();
433
+ },
434
+
435
+ // Select an option by its value.
436
+ select : function(value) {
437
+ var $option = $('option', this.$select).filter(function() {
438
+ return $(this).val() == value;
439
+ });
440
+ var $checkbox = $('.multiselect-container li input', this.$container).filter(function() {
441
+ return $(this).val() == value;
442
+ });
443
+
444
+ if (this.options.selectedClass) {
445
+ $checkbox.parents('li').addClass(this.options.selectedClass);
446
+ }
447
+
448
+ $checkbox.prop('checked', true);
449
+
450
+ $option.attr('selected', 'selected').prop('selected', true);
451
+
452
+ this.updateButtonText();
453
+ this.options.onChange($option, true);
454
+ },
455
+
456
+ // Deselect an option by its value.
457
+ deselect : function(value) {
458
+ var $option = $('option', this.$select).filter(function() {
459
+ return $(this).val() == value;
460
+ });
461
+ var $checkbox = $('.multiselect-container li input', this.$container).filter(function() {
462
+ return $(this).val() == value;
463
+ });
464
+
465
+ if (this.options.selectedClass) {
466
+ $checkbox.parents('li').removeClass(this.options.selectedClass);
467
+ }
468
+
469
+ $checkbox.prop('checked', false);
470
+
471
+ $option.removeAttr('selected').prop('selected', false);
472
+
473
+ this.updateButtonText();
474
+ this.options.onChange($option, false);
475
+ },
476
+
477
+ // Rebuild the whole dropdown menu.
478
+ rebuild : function() {
479
+ $('.multiselect-container', this.$container).html('');
480
+ this.buildDropdown(this.$select, this.options);
481
+ this.updateButtonText();
482
+
483
+ // Enable filtering.
484
+ if (this.options.enableFiltering || this.options.enableCaseInsensitiveFiltering) {
485
+ this.buildFilter();
486
+ }
487
+ },
488
+
489
+ // Get options by merging defaults and given options.
490
+ getOptions : function(options) {
491
+ return $.extend({}, this.defaults, options);
492
+ },
493
+
494
+ updateButtonText : function() {
495
+ var options = this.getSelected();
496
+ $('button', this.$container).html(this.options.buttonText(options, this.$select));
497
+ },
498
+
499
+ // Get all selected options.
500
+ getSelected : function() {
501
+ return $('option:selected[value!="' + this.options.selectAllValue + '"]', this.$select);
502
+ },
503
+
504
+ updateOriginalOptions : function() {
505
+ this.originalOptions = this.$select.clone()[0].options;
506
+ },
507
+
508
+ asyncFunction : function(callback, timeout, self) {
509
+ var args = Array.prototype.slice.call(arguments, 3);
510
+ return setTimeout(function() {
511
+ callback.apply(self || window, args);
512
+ }, timeout);
513
+ }
514
+ };
515
+
516
+ $.fn.multiselect = function(option, parameter) {
517
+ return this.each(function() {
518
+ var data = $(this).data('multiselect'), options = typeof option == 'object' && option;
519
+
520
+ // Initialize the multiselect.
521
+ if (!data) {
522
+ $(this).data('multiselect', ( data = new Multiselect(this, options)));
523
+ }
524
+
525
+ // Call multiselect method.
526
+ if ( typeof option == 'string') {
527
+ data[option](parameter);
528
+ }
529
+ });
530
+ };
531
+
532
+ $.fn.multiselect.Constructor = Multiselect;
533
+
534
+ $(function() {
535
+ $("select[data-role=multiselect]").multiselect();
536
+ });
537
+
538
+ }(window.jQuery);