Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery( function ( $ )
  2. {
  3.     'use strict';
  4.  
  5.     var $wrapper = $( '#wpbody' );
  6.  
  7.     /**
  8.      * Functions to handle input's name.
  9.      */
  10.     var input = {
  11.         updateGroupIndex: function ()
  12.         {
  13.             var that = this,
  14.                 $clones = $( this ).parents( '.rwmb-group-clone' ),
  15.                 totalLevel = $clones.length;
  16.             $clones.each( function ( i, clone )
  17.             {
  18.                 var index = parseInt( $( clone ).parent().data( 'next-index' ) ) - 1,
  19.                     level = totalLevel - i;
  20.                 input.replaceName.call( that, level, index );
  21.  
  22.                 // Stop each() loop immediately when reach the new clone group.
  23.                 if ( $( clone ).data( 'clone-group-new' ) )
  24.                 {
  25.                     return false;
  26.                 }
  27.             } );
  28.         },
  29.         updateIndex     : function ()
  30.         {
  31.             var $this = $( this );
  32.  
  33.             // Update index only for sub fields in a group
  34.             if ( !$this.closest( '.rwmb-group-clone' ).length )
  35.             {
  36.                 return;
  37.             }
  38.  
  39.             // Update [group index]
  40.             // input.updateGroupIndex.call( this );
  41.  
  42.             // Do not update index if field is not cloned
  43.             if ( !$this.closest( '.rwmb-input' ).children( '.rwmb-clone' ).length )
  44.             {
  45.                 return;
  46.             }
  47.  
  48.             var index = parseInt( $this.closest( '.rwmb-input' ).data( 'next-index' ) ) - 1,
  49.                 level = $this.parents( '.rwmb-clone' ).length;
  50.             input.replaceName.call( this, level, index );
  51.  
  52.             // Stop propagation.
  53.             return false;
  54.         },
  55.         // Replace the level-nth [\d] with new index
  56.         replaceName     : function ( level, index )
  57.         {
  58.             var $input = $( this ),
  59.                 name = $input.attr( 'name' );
  60.             if ( !name )
  61.             {
  62.                 return;
  63.             }
  64.  
  65.             var regex = new RegExp( '((?:\\[\\d+\\].*?){' + ( level - 1 ) + '}.*?)(\\[\\d+\\])' ),
  66.                 newValue = '$1' + '[' + index + ']';
  67.  
  68.             name = name.replace( regex, newValue );
  69.             $input.attr( 'name', name );
  70.         }
  71.     };
  72.  
  73.     $wrapper.on( 'clone', ':input[class|="rwmb"]', input.updateIndex );
  74.  
  75.     /**
  76.      * When clone a group:
  77.      * 1) Remove sub fields' clones and keep only their first clone
  78.      * 2) Reset sub fields' [data-next-index] to 1
  79.      * 3) Set [name] for sub fields (which is done when 'clone' event is fired
  80.      * 4) Repeat steps 1)-3) for sub groups
  81.      */
  82.     $wrapper.on( 'clone_instance', '.rwmb-clone', function ()
  83.     {
  84.         if ( !$( this ).hasClass( 'rwmb-group-clone' ) )
  85.         {
  86.             return false;
  87.         }
  88.  
  89.         $( this )
  90.             // Add new [data-clone-group-new] to detect which group is cloned. This data is used to update sub inputs' group index
  91.             .data( 'clone-group-new', true )
  92.             // Remove clones, and keep only their first clone. Reset [data-next-index] to 1
  93.             .find( '.rwmb-input' ).each( function ()
  94.             {
  95.                 $( this ).data( 'next-index', 1 ).children( '.rwmb-clone:gt(0)' ).remove();
  96.             } ).end()
  97.             // Update [group index] for inputs
  98.             .find( ':input[class|="rwmb"]' ).each( function ()
  99.             {
  100.                 input.updateGroupIndex.call( this );
  101.             } );
  102.  
  103.         // Stop propagation to not trigger the same event on parent's clone.
  104.         return false;
  105.     } );
  106. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement