|
1 | | -function initSelectizeInputs() { |
2 | | - $('[data-selectize-input]').each( function() { |
3 | | - var remote = $(this).attr( 'data-opt-remote' ) ? $(this).attr( 'data-opt-remote' ) : ''; |
4 | | - var field_text = $(this).attr( 'data-opt-text' ) ? $(this).attr( 'data-opt-text' ) : 'name'; |
5 | | - var field_value = $(this).attr( 'data-opt-value' ) ? $(this).attr( 'data-opt-value' ) : 'id'; |
6 | | - var opts = { |
7 | | - closeAfterSelect: true, |
8 | | - create: false, |
9 | | - hideSelected: true, |
10 | | - labelField: field_text, |
11 | | - options: [], |
12 | | - plugins: ['remove_button'], |
13 | | - searchField: field_text, |
14 | | - valueField: field_value |
15 | | - }; |
| 1 | +(function () { |
| 2 | + 'use strict' |
16 | 3 |
|
17 | | - if (!$(this).data('opts') ) { |
18 | | - $.each( this.attributes, function( i, attr ) { |
19 | | - if( attr.name.startsWith( 'data-opt-' ) ) { |
20 | | - var name = attr.name.substr( 9 ); |
21 | | - if( name != 'remote' && name != 'text' && name != 'value' ) opts[name] = ( attr.value == 'true' ) ? true : ( ( attr.value == 'false' ) ? false : attr.value ); |
22 | | - } |
23 | | - }); |
24 | | - } |
25 | | - else { |
26 | | - opts = $.extend({}, opts, $(this).data('opts')); |
27 | | - } |
| 4 | + // --- functions ------------------------------------------------------------ |
| 5 | + function initSelectizeInputs() { |
| 6 | + $('[data-selectize-input]').each(function () { |
| 7 | + var remote = $(this).attr('data-opt-remote') ? $(this).attr('data-opt-remote') : ''; |
| 8 | + var field_text = $(this).attr('data-opt-text') ? $(this).attr('data-opt-text') : 'name'; |
| 9 | + var field_value = $(this).attr('data-opt-value') ? $(this).attr('data-opt-value') : 'id'; |
| 10 | + var opts = { |
| 11 | + closeAfterSelect: true, |
| 12 | + create: false, |
| 13 | + hideSelected: true, |
| 14 | + labelField: field_text, |
| 15 | + options: [], |
| 16 | + plugins: ['remove_button'], |
| 17 | + searchField: field_text, |
| 18 | + valueField: field_value |
| 19 | + }; |
28 | 20 |
|
29 | | - opts['load'] = function( query, callback ) { |
30 | | - if( !query.length ) return callback(); |
31 | | - $.ajax({ |
32 | | - url: remote + '?q[' + field_text + '_contains]=' + encodeURIComponent( query ), |
33 | | - type: 'GET', |
34 | | - error: function() { |
35 | | - callback(); |
36 | | - }, |
37 | | - success: function(res) { |
38 | | - callback( res.slice( 0, 10 ) ); |
39 | | - } |
40 | | - }); |
41 | | - }; |
42 | | - $(this).selectize( opts ); |
43 | | - }); |
44 | | -} |
| 21 | + if (!$(this).data('opts')) { |
| 22 | + $.each(this.attributes, function (i, attr) { |
| 23 | + if (attr.name.startsWith('data-opt-')) { |
| 24 | + var name = attr.name.substr(9); |
| 25 | + if (name != 'remote' && name != 'text' && name != 'value') opts[name] = (attr.value == 'true') ? true : ((attr.value == 'false') ? false : attr.value); |
| 26 | + } |
| 27 | + }); |
| 28 | + } else { |
| 29 | + opts = $.extend({}, opts, $(this).data('opts')); |
| 30 | + } |
45 | 31 |
|
46 | | -$(document).on('has_many_add:after', function () { |
47 | | - initSelectizeInputs(); |
48 | | -}); |
| 32 | + opts['load'] = function (query, callback) { |
| 33 | + if (!query.length) return callback(); |
| 34 | + $.ajax({ |
| 35 | + url: remote + '?q[' + field_text + '_contains]=' + encodeURIComponent(query), |
| 36 | + type: 'GET', |
| 37 | + error: function () { |
| 38 | + callback(); |
| 39 | + }, |
| 40 | + success: function (res) { |
| 41 | + callback(res.slice(0, 10)); |
| 42 | + } |
| 43 | + }); |
| 44 | + }; |
| 45 | + $(this).selectize(opts); |
| 46 | + }); |
| 47 | + } |
49 | 48 |
|
50 | | -$(document).ready( function() { |
51 | | - initSelectizeInputs(); |
52 | | -}); |
| 49 | + // --- events --------------------------------------------------------------- |
| 50 | + $(document).ready(initSelectizeInputs); |
| 51 | + $(document).on('has_many_add:after', initSelectizeInputs); |
| 52 | + $(document).on('turbolinks:load', initSelectizeInputs); |
| 53 | +})() |
0 commit comments