MrViSiOn

JS Giulian

Sep 8th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($){
  2.  
  3.     var textoTemplate = '<div class="texto" style="display: none;"><input type="text" class="" /></div>',
  4.         optionsTemplate = '<option value="&&&&" >$$$$$</option>',
  5.         selectTemplate = '<select name="$$$$" id="%%%%%">%%options%%</select>';
  6.  
  7.     var convertirEnSelector = function( elementos ) {
  8.         $( elementos ).each(function( index, item ) {
  9.             var vcgroup = $( item ).find( '.vpc-group' ), component_id = $( item ).data('component_id');
  10.             $( vpc.config.components ).each( function( index2, item2 ){
  11.                 if ( item2.component_id == component_id ) {
  12.                     var options = '', name = item2.cname;
  13.  
  14.                     // var optionsArray = item2.options;
  15.                     //
  16.                     // optionsArray = $.map(optionsArray, function(value, index) {
  17.                     //  return [value];
  18.                     // });
  19.                     var optionsVisible = vcgroup.find( '.vpc-single-option-wrap' ) ;
  20.                     for ( i = 0; optionsVisible.length > i; i++) {
  21.                         if ( ! $(optionsVisible[i]).attr('style') || $(optionsVisible[i]).css('display') != 'none' ) {
  22.                             var item = $(optionsVisible[i]).find('input');
  23.                             item.hide();
  24.                             options += optionsTemplate.replace('&&&&', item.attr('id')).replace('$$$$$', item.attr('value'));
  25.                         }
  26.                     }
  27.  
  28.                     vcgroup.find ('> div').hide();
  29.                     var id = 'select' + Math.random().toString(36).substr(2);
  30.                     vcgroup.find('select').remove();
  31.                     vcgroup.append ( selectTemplate.replace( '$$$$', name ).replace( '%%options%%', options ).replace( '%%%%%', id ) );
  32.  
  33.                     $( '#' + id ).unbind().change( function() {
  34.                         $('[data-oid="'+ $(this).val() +'"]').click();
  35.                         vcgroup.find ( '> div' ).hide();
  36.                     });
  37.                 }
  38.             });
  39.         });
  40.     };
  41.  
  42.     var introducirTexto = function ( elementos ) {
  43.         $( elementos ).each(function( index, item ){
  44.             var vcgroup = $( item ).parent().parent();
  45.             if ( ! vcgroup.find('.texto').length ) {
  46.                 vcgroup.append( textoTemplate );
  47.             }
  48.         });
  49.     };
  50.  
  51.     $(document).ready(function(){
  52.         var elementosASelectores = $('[data-campo1="si"]:visible');
  53.         convertirEnSelector ( elementosASelectores );
  54.  
  55.         var elementosConTexto = $('[data-campo1="no"]').find('input[data-campo2="si"]');
  56.         introducirTexto ( elementosConTexto );
  57.  
  58.         $('[data-campo1="no"]').find('input[data-campo2]').click(function(){
  59.             if ( $(this).data('campo2') === 'si' ) {
  60.                 $(this).parent().parent().find('.texto').show();
  61.             }else{
  62.                 $(this).parent().parent().find('.texto').hide();
  63.             }
  64.         });
  65.  
  66.         $('[data-campo1="si"]').click(function(ev) {
  67.             var that = $(this);
  68.             var updateSelectors = function( el ) {
  69.                 if ($(el).find( '.vpc-single-option-wrap:visible' ).length > 0) {
  70.                     convertirEnSelector ( $(el) );
  71.                 }
  72.             };
  73.             setTimeout(function () {
  74.                 // updateSelectors(that);
  75.             }, 50);
  76.             // if ( ! ($(that).find('.vpc-options').is(':visible') ) ){
  77.             //
  78.             // }else{
  79.             //  updateSelectors(that);
  80.             // }
  81.         });
  82.     });
  83. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment