1document.addEventListener( 'DOMContentLoaded', () => { 2 if( 'function' !== typeof Choices ) { 3 console.warn( 'ChoicesJS not loaded' ) 4 5 } else { 6 document.querySelectorAll('select').forEach( el => { 7 const config = {} 8 9 // add config conditions here 10 config.shouldSort = false 11 12 new FxChoice( el, config ) 13 }) 14 } 15}) 16 17 18 19class FxChoice { 20 21 // todo - support for additional parameters/settings (for now, add conditions with config) 22 constructor( el, config = {} ) { 23 this.el = el 24 this.config = this.parseConfig( config ) 25 26 this.choices = null 27 28 this.setUpChoices() 29 this.formResetFix() 30 } 31 32 33 // slight adjustment to config to support initialization 34 parseConfig( config ) { 35 const self = this 36 37 let origInitCallback 38 39 if( 'function' === typeof config.callbackOnInit ) { 40 origInitCallback = config.callbackOnInit 41 } 42 43 config.callbackOnInit = () => { 44 if( undefined !== origInitCallback ) { 45 origInitCallback() 46 } 47 48 self.setInitStatus( true ) 49 } 50 51 return config 52 } 53 54 55 setUpChoices() { 56 const self = this 57 58 // prevents duplicate initialization 59 if( true === self.el.fxChoicesIsInitialized ) { 60 return 61 } 62 63 self.choices = new Choices( self.el, self.config ) 64 65 // auto-submit category dropdowns on select 66 if( 'cat' === self.el.id ) { 67 self.el.onchange = null 68 69 // todo â this.choices.passedElement.element is same as self.el (double-check) 70 self.choices.passedElement.element.addEventListener( 'change', e => { 71 const form = e.target.form 72 73 if( null !== form ) { 74 form.submit() 75 } 76 }) 77 } 78 79 } 80 81 82 formResetFix() { 83 const self = this, 84 form = self.el.form 85 86 if( null !== form && null !== self.choices ) { 87 form.addEventListener( 'reset', () => { 88 self.choices.destroy() 89 self.el.fxChoicesIsInitialized = false 90 91 self.setUpChoices() 92 }, { 93 once: true 94 }) 95 } 96 } 97 98 99 setInitStatus( status = false ) { 100 this.el.fxChoicesIsInitialized = status 101 } 102 103}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.