PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/themes/shapely/inc/libraries/epsilon-framework/assets/vendors/epsilon-framework/customizer/controls/section-repeater.ts

https://gitlab.com/chernushov881/charity-fund
TypeScript | 225 lines | 125 code | 28 blank | 72 comment | 31 complexity | 885ba8ce7a2a72ebab3b4340ba2717f9 MD5 | raw file
  1. declare var wp: any;
  2. declare var _: any;
  3. import * as $ from 'jquery';
  4. import { EpsilonFieldRepeater } from './repeater';
  5. import { EpsilonRepeaterSectionRow } from './repeater/repeater-section-row';
  6. import { EpsilonRepeaterSectionUtils } from './repeater/repeater-section-utils';
  7. import { EpsilonRepeaterAddons } from './repeater/repeater-addons';
  8. export class EpsilonSectionRepeater extends EpsilonFieldRepeater {
  9. /**
  10. * Object constructor
  11. * @param control
  12. */
  13. public constructor( control: { container: JQuery, setting: void, params: { rowLabel: any, value: number, id: string, fields: object, choices: { limit: number } } } ) {
  14. super( control );
  15. /**
  16. * We need to move this element to the bottom of the page so it renders properly
  17. */
  18. this._moveElements();
  19. /**
  20. * Events that fires from the main page
  21. * so we can focus our panel and repeatable section
  22. */
  23. this._handleNavigation();
  24. /**
  25. * Initiate Search on sections
  26. */
  27. this._initSearch();
  28. /**
  29. * Handle integration tab functionality
  30. */
  31. this._handleIntegrations();
  32. }
  33. /**
  34. * Repeater container
  35. * @returns {any}
  36. */
  37. public getRepeaterContainer() {
  38. return this.context.find( '.repeater-sections' );
  39. }
  40. /**
  41. * Load utilities
  42. * @public
  43. */
  44. public loadUtils(): any {
  45. return new EpsilonRepeaterSectionUtils( this );
  46. }
  47. /**
  48. * Create existing rows
  49. * @public
  50. */
  51. public createExistingRows(): void {
  52. const control = this;
  53. if ( this.control.params.value.length ) {
  54. for ( let i = 0; i < this.control.params.value.length; i ++ ) {
  55. let row: EpsilonRepeaterSectionRow | boolean,
  56. addons: EpsilonRepeaterAddons;
  57. row = control.utils.add( control.control.params.value[ i ] );
  58. if ( false !== row ) {
  59. addons = new EpsilonRepeaterAddons( control, row );
  60. addons.initPlugins();
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Handle events
  67. * @public
  68. */
  69. public handleEvents(): void {
  70. const self = this;
  71. self.utils.addButton();
  72. /**
  73. * Addition of sections
  74. */
  75. jQuery( '#sections-left-' + this.control.params.id ).on( 'click', '.epsilon-section [data-action="add"]', function( this: any, e ) {
  76. e.preventDefault();
  77. if ( ! self.limit || self.currentIndex < self.limit ) {
  78. let row: EpsilonRepeaterSectionRow,
  79. addons: EpsilonRepeaterAddons;
  80. row = self.utils.add( { type: jQuery( this ).parent().attr( 'data-id' ) } );
  81. addons = new EpsilonRepeaterAddons( self, row );
  82. addons.initPlugins();
  83. jQuery( 'body' ).removeClass( 'adding-section' );
  84. jQuery( '#sections-left-' + self.control.params.id ).find( '.available-sections' ).removeClass( 'opened' );
  85. if ( self.control.params[ 'selective_refresh' ] ) {
  86. wp.customize.previewer.refresh();
  87. }
  88. } else {
  89. jQuery( self.control.selector + ' .limit' ).addClass( 'highlight' );
  90. }
  91. } );
  92. /**
  93. * Section description
  94. */
  95. jQuery( '#sections-left-' + this.control.params.id ).on( 'click', '.epsilon-section [data-action="info"]', function( this: any, e ) {
  96. e.preventDefault();
  97. jQuery( this ).parent().find( '.epsilon-section-image-description' ).toggleClass( 'active' );
  98. jQuery( this ).parent().find( '.epsilon-section-description' ).toggleClass( 'active' );
  99. } );
  100. /**
  101. * Section remove
  102. */
  103. this.context.on( 'click', '.repeater-row-remove', function( this: any, e: Event ) {
  104. self.handleRowDecrementor();
  105. if ( ! self.limit || self.currentIndex < self.limit ) {
  106. jQuery( self.control.selector + ' .limit' ).removeClass( 'highlight' );
  107. }
  108. } );
  109. }
  110. /**
  111. *
  112. * @private
  113. */
  114. private _handleNavigation(): void {
  115. const self = this;
  116. /**
  117. * Event that fires from the main page
  118. * so we can focus our panel and repeatable section
  119. */
  120. wp.customize.previewer.bind( 'epsilon-section-edit', function( data: any ): void {
  121. /**
  122. * In case the section does not exist, we can terminate
  123. */
  124. if ( 'undefined' === typeof( wp.customize.section( data.customizerSection ) ) ) {
  125. return;
  126. }
  127. /**
  128. * Iterate over the controls, minimize everything
  129. */
  130. _.each( self.rows, function( sect: EpsilonRepeaterSectionRow, index: number ) {
  131. if ( ! sect.container.hasClass( 'minimized' ) && index !== data.section ) {
  132. self.utils.toggleMinimize( sect );
  133. }
  134. } );
  135. wp.customize.section( data.customizerSection ).focus();
  136. /**
  137. * Focus repeatable section
  138. */
  139. if ( ! _.isUndefined( self.rows[ data.section ] ) && self.rows[ data.section ].container.hasClass( 'minimized' ) ) {
  140. self.utils.toggleMinimize( self.rows[ data.section ] );
  141. }
  142. } );
  143. }
  144. /**
  145. * Search functionality in the sections library
  146. *
  147. * @param selector
  148. */
  149. private _initSearch() {
  150. const self = this;
  151. let selector = jQuery( '#sections-left-' + self.control.params.id ),
  152. input = selector.find( '.sections-search-input' ),
  153. val: string | any,
  154. collection: JQuery,
  155. id: string | any;
  156. input.on( 'keyup change', _.debounce( function( this: any, e: Event ) {
  157. val = input.val();
  158. if ( 'undefined' !== typeof val ) {
  159. val = val.toLowerCase();
  160. }
  161. collection = selector.find( '.epsilon-section' );
  162. jQuery.each( collection, function() {
  163. id = jQuery( this ).attr( 'data-id' );
  164. if ( 'undefined' !== typeof id ) {
  165. id = id.toLowerCase();
  166. }
  167. jQuery( this )[ id.indexOf( val ) !== - 1 ? 'show' : 'hide' ]();
  168. } );
  169. }, 1000 ) );
  170. }
  171. /**
  172. * Handles the "tab" navigation in the available section list
  173. * @private
  174. */
  175. private _handleIntegrations() {
  176. if ( ! this.control.params.integrations ) {
  177. return;
  178. }
  179. jQuery( '#sections-left-' + this.control.params.id ).on( 'click', '.available-sections-tab-toggler', function( this: any, e: JQueryEventConstructor ) {
  180. let tab: any | string = jQuery( this ).attr( 'data-tab' );
  181. jQuery( this ).siblings().removeClass( 'active' );
  182. jQuery( this ).addClass( 'active' );
  183. e.preventDefault();
  184. if ( 'undefined' !== typeof tab ) {
  185. jQuery( '[data-tab-id="' + tab + '"]' ).siblings( 'div' ).removeClass( 'active' ).slideUp();
  186. jQuery( '[data-tab-id="' + tab + '"]' ).slideDown().addClass( 'active' );
  187. }
  188. } );
  189. }
  190. /**
  191. * Move items "phisically"
  192. * @private
  193. */
  194. private _moveElements() {
  195. jQuery( '#sections-left-' + this.control.params.id ).appendTo( jQuery( '.wp-full-overlay' ) );
  196. }
  197. }