Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /**
  2. * Interface Builder
  3. */
  4. ;( function( $, underscore ) {
  5.  
  6. 'use strict';
  7.  
  8. var cxInterfaceBuilder = {
  9. component: {
  10. showClass: 'show',
  11. localStorage: {},
  12. controlConditions: window.cxInterfaceBuilder.conditions || {},
  13. controlValues: window.cxInterfaceBuilder.fields || {},
  14. conditionState: {},
  15. init: function() {
  16. this.conditionsHandleInit();
  17. },
  18.  
  19. addEvent: function() {
  20. $( 'body' )
  21. .off( 'click.cxInterfaceBuilder' )
  22. .on( 'click.cxInterfaceBuilder',
  23. this.tabClass + ' ' + this.buttonClass + ', ' +
  24. this.toggleClass + ' ' + this.buttonClass + ', ' +
  25. this.accordionClass + ' ' + this.buttonClass,
  26.  
  27. this.componentClick.bind( this )
  28. );
  29. },
  30.  
  31. conditionsHandleInit: function() {
  32. var self = this;
  33.  
  34. $( window ).on( 'cx-switcher-change', function( event ) {
  35. var controlName = event.controlName,
  36. controlStatus = event.controlStatus;
  37.  
  38. self.updateConditionRules( controlName, controlStatus );
  39. self.renderConditionRules();
  40. } );
  41.  
  42. $( window ).on( 'cx-select2-change', function( event ) {
  43. var controlName = event.controlName,
  44. controlStatus = event.controlStatus;
  45.  
  46. self.updateConditionRules( controlName, controlStatus );
  47. self.renderConditionRules();
  48. } );
  49.  
  50. $( window ).on( 'cx-radio-change', function( event ) {
  51. var controlName = event.controlName,
  52. controlStatus = event.controlStatus;
  53.  
  54. self.updateConditionRules( controlName, controlStatus );
  55. self.renderConditionRules();
  56. } );
  57.  
  58. $( window ).on( 'cx-checkbox-change', function( event ) {
  59. var controlName = event.controlName,
  60. controlStatus = event.controlStatus,
  61. updatedStatus = {};
  62.  
  63. $.each( controlStatus[ controlName ], function( checkbox, value ) {
  64. updatedStatus[ checkbox ] = cxInterfaceBuilder.utils.filterBoolValue( value );
  65. } );
  66.  
  67. self.updateConditionRules( controlName, updatedStatus );
  68. self.renderConditionRules();
  69. } );
  70.  
  71. this.generateConditionRules();
  72. self.renderConditionRules();
  73.  
  74. },
  75.  
  76. generateConditionRules: function() {
  77. var self = this;
  78.  
  79. $.each( this.controlConditions, function( control, conditions ) {
  80. $.each( conditions, function( control, value ) {
  81. if( self.controlValues.hasOwnProperty( control ) ) {
  82. self.conditionState[ control ] = self.controlValues[ control ];
  83. }
  84. } );
  85. } );
  86. },
  87.  
  88. updateConditionRules: function( name, status ) {
  89. this.conditionState[ name ] = status;
  90. },
  91.  
  92. renderConditionRules: function() {
  93. var self = this;
  94.  
  95. $.each( this.controlConditions, function( control, conditions ) {
  96. var $selector = $( '.cx-control[data-control-name="' + control + '"]' ),
  97. hidden = true;
  98.  
  99. $selector.addClass( 'cx-control-hidden' );
  100.  
  101. $.each( conditions, function( control, value ) {
  102. hidden = true;
  103.  
  104. if( self.conditionState.hasOwnProperty( control ) ) {
  105.  
  106. if( self.conditionState[ control ] === value ) {
  107. hidden = false;
  108. }
  109.  
  110. if( 'object' === typeof self.conditionState[ control ] ) {
  111. hidden = false;
  112.  
  113. $.each( value, function( prop, val ) {
  114. if( val !== self.conditionState[ control ][ prop ] ) {
  115. hidden = true;
  116.  
  117. return false;
  118. }
  119. } );
  120. }
  121. }
  122.  
  123. if( hidden ) {
  124. return false;
  125. }
  126. } );
  127.  
  128. if( hidden ) {
  129. $selector.addClass( 'cx-control-hidden' );
  130. } else {
  131. $selector.removeClass( 'cx-control-hidden' );
  132. }
  133. } );
  134. },
  135.  
  136. },
  137. };
  138.  
  139. cxInterfaceBuilder.init();
  140.  
  141. }( jQuery, window._ ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement