balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

select-tree.js (1663B)


      1 ( function ( $, rwmb ) {
      2 	'use strict';
      3 
      4 	function setInitialRequiredProp() {
      5 		var $this = $( this ),
      6 			required = $this.prop( 'required' );
      7 
      8 		if ( required ) {
      9 			$this.data( 'initial-required', required );
     10 		}
     11 	}
     12 
     13 	function unsetRequiredProp() {
     14 		$( this ).prop( 'required', false );
     15 	}
     16 
     17 	function setRequiredProp() {
     18 		var $this = $( this );
     19 
     20 		if ( $this.data( 'initial-required' ) ) {
     21 			$this.prop( 'required', true );
     22 		}
     23 	}
     24 
     25 	function toggleTree() {
     26 		var $this = $( this ),
     27 			val = $this.val(),
     28 			$tree = $this.siblings( '.rwmb-select-tree' ),
     29 			$selected = $tree.filter( "[data-parent-id='" + val + "']" ),
     30 			$notSelected = $tree.not( $selected );
     31 
     32 		$selected.removeClass( 'hidden' ).find( 'select' ).each( setRequiredProp );
     33 		$notSelected.addClass( 'hidden' ).find( 'select' ).each( unsetRequiredProp ).prop( 'selectedIndex', 0 );
     34 	}
     35 
     36 	function instantiateSelect2() {
     37 		var $this = $( this ),
     38 			options = $this.data( 'options' );
     39 
     40 		$this
     41 			.removeClass( 'select2-hidden-accessible' ).removeAttr( 'data-select2-id' )
     42 			.children().removeAttr( 'data-select2-id' ).end()
     43 			.siblings( '.select2-container' ).remove().end()
     44 			.select2( options );
     45 
     46 		toggleTree.call( this );
     47 	}
     48 
     49 	function init( e ) {
     50 		var $select = $( e.target ).find( '.rwmb-select-tree > select' );
     51 
     52 		$select.each ( setInitialRequiredProp );
     53 		$select.each( function() {
     54 			const $this = $( this ),
     55 				options = $this.data( 'options' );
     56 
     57 			$this.select2( options );
     58 		} );
     59 	}
     60 
     61 	rwmb.$document
     62 		.on( 'mb_ready', init )
     63 		.on( 'change', '.rwmb-select-tree > select', toggleTree )
     64 		.on( 'clone', '.rwmb-select-tree > select', instantiateSelect2 );
     65 } )( jQuery, rwmb );