gutenberg
2 years ago
admin.js
3 years ago
admin.min.js
3 years ago
deactivation-feedback.js
3 years ago
deactivation-feedback.min.js
3 years ago
editor.js
7 years ago
editor.min.js
5 years ago
evf-admin-email.js
2 years ago
evf-admin-email.min.js
2 years ago
evf-clipboard.js
7 years ago
evf-clipboard.min.js
7 years ago
evf-enhanced-select.js
3 years ago
evf-enhanced-select.min.js
3 years ago
evf-file-uploader.js
3 years ago
evf-file-uploader.min.js
3 years ago
evf-setup.js
2 years ago
evf-setup.min.js
2 years ago
extensions.js
2 years ago
extensions.min.js
2 years ago
form-builder.js
2 years ago
form-builder.min.js
2 years ago
form-template-controller.js
3 years ago
form-template-controller.min.js
3 years ago
settings.js
3 years ago
settings.min.js
3 years ago
tools.js
4 years ago
tools.min.js
4 years ago
upgrade.js
2 years ago
upgrade.min.js
2 years ago
evf-enhanced-select.js
216 lines
| 1 | /*global evf_enhanced_select_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | function getEnhancedSelectFormatString() { |
| 5 | return { |
| 6 | 'language': { |
| 7 | errorLoading: function() { |
| 8 | // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error. |
| 9 | return evf_enhanced_select_params.i18n_searching; |
| 10 | }, |
| 11 | inputTooLong: function( args ) { |
| 12 | var overChars = args.input.length - args.maximum; |
| 13 | |
| 14 | if ( 1 === overChars ) { |
| 15 | return evf_enhanced_select_params.i18n_input_too_long_1; |
| 16 | } |
| 17 | |
| 18 | return evf_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', overChars ); |
| 19 | }, |
| 20 | inputTooShort: function( args ) { |
| 21 | var remainingChars = args.minimum - args.input.length; |
| 22 | |
| 23 | if ( 1 === remainingChars ) { |
| 24 | return evf_enhanced_select_params.i18n_input_too_short_1; |
| 25 | } |
| 26 | |
| 27 | return evf_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars ); |
| 28 | }, |
| 29 | loadingMore: function() { |
| 30 | return evf_enhanced_select_params.i18n_load_more; |
| 31 | }, |
| 32 | maximumSelected: function( args ) { |
| 33 | if ( args.maximum === 1 ) { |
| 34 | return evf_enhanced_select_params.i18n_selection_too_long_1; |
| 35 | } |
| 36 | |
| 37 | return evf_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum ); |
| 38 | }, |
| 39 | noResults: function() { |
| 40 | return evf_enhanced_select_params.i18n_no_matches; |
| 41 | }, |
| 42 | searching: function() { |
| 43 | return evf_enhanced_select_params.i18n_searching; |
| 44 | } |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | try { |
| 50 | $( document.body ).on( 'evf-enhanced-select-init', function() { |
| 51 | |
| 52 | // Regular select boxes |
| 53 | $( ':input.evf-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() { |
| 54 | var select2_args = $.extend({ |
| 55 | minimumResultsForSearch: 10, |
| 56 | allowClear: $( this ).data( 'allow_clear' ) ? true : false, |
| 57 | placeholder: $( this ).data( 'placeholder' ) |
| 58 | }, getEnhancedSelectFormatString() ); |
| 59 | |
| 60 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 61 | }); |
| 62 | |
| 63 | $( ':input.evf-enhanced-select-nostd' ).filter( ':not(.enhanced)' ).each( function() { |
| 64 | var select2_args = $.extend({ |
| 65 | minimumResultsForSearch: 10, |
| 66 | allowClear: true, |
| 67 | placeholder: $( this ).data( 'placeholder' ) |
| 68 | }, getEnhancedSelectFormatString() ); |
| 69 | |
| 70 | $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); |
| 71 | }); |
| 72 | |
| 73 | // Setup multi-select2 with Select/Unselect All buttons. |
| 74 | var SelectionAdapter, DropdownAdapter; |
| 75 | $.fn.select2.amd.require( |
| 76 | [ |
| 77 | "select2/selection/single", |
| 78 | "select2/selection/placeholder", |
| 79 | "select2/dropdown", |
| 80 | "select2/dropdown/search", |
| 81 | "select2/dropdown/attachBody", |
| 82 | "select2/utils", |
| 83 | "select2/selection/eventRelay", |
| 84 | ], |
| 85 | function ( |
| 86 | SingleSelection, |
| 87 | Placeholder, |
| 88 | Dropdown, |
| 89 | DropdownSearch, |
| 90 | AttachBody, |
| 91 | Utils, |
| 92 | EventRelay |
| 93 | ) { |
| 94 | // Add placeholder which shows current number of selections |
| 95 | SelectionAdapter = Utils.Decorate(SingleSelection, Placeholder); |
| 96 | |
| 97 | // Allow to flow/fire events |
| 98 | SelectionAdapter = Utils.Decorate(SelectionAdapter, EventRelay); |
| 99 | |
| 100 | // Add search box in dropdown |
| 101 | DropdownAdapter = Utils.Decorate(Dropdown, DropdownSearch); |
| 102 | |
| 103 | // Add attach-body in dropdown |
| 104 | DropdownAdapter = Utils.Decorate(DropdownAdapter, AttachBody); |
| 105 | function UnselectAll() {} |
| 106 | UnselectAll.prototype.render = function (decorated) { |
| 107 | var self = this; |
| 108 | var $rendered = decorated.call(this); |
| 109 | var $unSelectAllButton = $( |
| 110 | '<button class="btn btn-default evf-select2-unselect-all-btn" type="button">' + evf_enhanced_select_params.i18n_unselect_all + '</button>' |
| 111 | ); |
| 112 | |
| 113 | $unSelectAllButton.on("click", function () { |
| 114 | self.$element.val([]); |
| 115 | self.$element.trigger("change"); |
| 116 | self.trigger("close"); |
| 117 | }); |
| 118 | $rendered |
| 119 | .find(".select2-dropdown") |
| 120 | .prepend($unSelectAllButton); |
| 121 | |
| 122 | return $rendered; |
| 123 | }; |
| 124 | |
| 125 | // Add unselect all button in dropdown |
| 126 | DropdownAdapter = Utils.Decorate(DropdownAdapter, UnselectAll); |
| 127 | |
| 128 | function SelectAll() {} |
| 129 | SelectAll.prototype.render = function (decorated) { |
| 130 | var self = this; |
| 131 | var $rendered = decorated.call(this); |
| 132 | var $selectAllButton = $( |
| 133 | '<button class="btn btn-default evf-select2-select-all-btn" type="button">' + evf_enhanced_select_params.i18n_select_all + '</button>' |
| 134 | ); |
| 135 | |
| 136 | $selectAllButton.on("click", function () { |
| 137 | var $options = self.$element.find("option"); |
| 138 | var values = []; |
| 139 | |
| 140 | $options.each(function () { |
| 141 | values.push($(this).val()); |
| 142 | }); |
| 143 | self.$element.val(values); |
| 144 | self.$element.trigger("change"); |
| 145 | self.trigger("close"); |
| 146 | }); |
| 147 | $rendered |
| 148 | .find(".select2-dropdown") |
| 149 | .prepend($selectAllButton); |
| 150 | |
| 151 | return $rendered; |
| 152 | }; |
| 153 | |
| 154 | // Add select all button in dropdown |
| 155 | DropdownAdapter = Utils.Decorate(DropdownAdapter, SelectAll); |
| 156 | |
| 157 | var allSelect2 = $( "select.evf-select2-multiple" ); |
| 158 | |
| 159 | |
| 160 | if(0 === allSelect2.length){ |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | allSelect2.each( function() { |
| 165 | var $this = $( this ); |
| 166 | |
| 167 | $this.find('option').filter(function() { |
| 168 | return ( ( $(this).val() ).length == 0); |
| 169 | }).remove(); |
| 170 | |
| 171 | function formatResult(state) { |
| 172 | if ( ! state.id ) { |
| 173 | return state.text; |
| 174 | } |
| 175 | return $( '<div></div>' ).text( state.text ).addClass( 'wrap' ); |
| 176 | } |
| 177 | |
| 178 | var select2_args = $.extend({ |
| 179 | templateResult: formatResult, |
| 180 | closeOnSelect: false, |
| 181 | placeholder: $( this ).data( 'placeholder' ) || '', |
| 182 | selectionAdapter: SelectionAdapter, |
| 183 | dropdownAdapter: DropdownAdapter, |
| 184 | width: typeof $( this ).attr( 'style' ) !== 'undefined' && -1 !== $( this ).attr( 'style' ).indexOf( 'width' ) ? $( this ).css( 'width' ) : '100%', |
| 185 | templateSelection: function(data) { |
| 186 | |
| 187 | if ( ! data.id ) { |
| 188 | return data.text; |
| 189 | } |
| 190 | |
| 191 | var selected_len = ($this.val() || []).length; |
| 192 | return $this.data( 'selected_msg' ) ? $this.data( 'selected_msg' ).replace( '%qty%', selected_len ) : ''; |
| 193 | } |
| 194 | }, getEnhancedSelectFormatString() ); |
| 195 | |
| 196 | $this.selectWoo( select2_args ); |
| 197 | }); |
| 198 | } |
| 199 | ); |
| 200 | |
| 201 | }).trigger( 'evf-enhanced-select-init' ); |
| 202 | |
| 203 | $( 'html' ).on( 'click', function( event ) { |
| 204 | if ( this === event.target ) { |
| 205 | $( '.evf-enhanced-select' ).filter( '.select2-hidden-accessible' ).selectWoo( 'close' ); |
| 206 | } |
| 207 | } ); |
| 208 | $('.evf-enhanced-normal-select').each(function () { |
| 209 | $(this).select2(); |
| 210 | }) |
| 211 | } catch( err ) { |
| 212 | // If select2 failed (conflict?) log the error but don't stop other scripts breaking. |
| 213 | window.console.log( err ); |
| 214 | } |
| 215 | }); |
| 216 |