block.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Ninja Forms Form Block
  3. *
  4. * A block for embedding a Ninja Forms form into a post/page.
  5. */
  6. ( function( blocks, i18n, element, components ) {
  7. var el = element.createElement, // function to create elements
  8. TextControl = components.TextControl,// text input control
  9. InspectorControls = wp.editor.InspectorControls, // sidebar controls
  10. Sandbox = components.Sandbox; // needed to register the block
  11. // register our block
  12. blocks.registerBlockType( 'ninja-forms/form', {
  13. title: 'Ninja Forms',
  14. icon: 'feedback',
  15. category: 'common',
  16. attributes: {
  17. formID: {
  18. type: 'integer',
  19. default: 0
  20. },
  21. formName: {
  22. type: 'string',
  23. default: ''
  24. }
  25. },
  26. edit: function( props ) {
  27. var focus = props.focus;
  28. var formID = props.attributes.formID;
  29. var formName = props.attributes.formName;
  30. var children = [];
  31. if( ! formID ) formID = ''; // Default.
  32. if( ! formName ) formName = ''; // Default
  33. // this function is required, but we don't need it to do anything
  34. function nfOnValueChange( formName ) { }
  35. // show the dropdown when we click on the input
  36. function nfFocusClick( event ) {
  37. var elID = event.target.getAttribute( 'id' );
  38. var idArray = elID.split( '-' );
  39. var nfOptions = document.getElementById( 'nf-filter-container-' + idArray[ idArray.length -1 ] );
  40. // get the related input element
  41. var nfInput = document.getElementById( 'nf-formFilter-' + idArray[ idArray.length -1 ] );
  42. // set focus to the element so the onBlur function runs properly
  43. nfInput.focus();
  44. nfOptions.style.display = 'block';
  45. }
  46. // function for select the form on filter drop down item click
  47. function selectForm( event ) {
  48. //set the attributes from the selected for item
  49. props.setAttributes( {
  50. formID: event.target.getAttribute( 'data-formid' ),
  51. formName: event.target.innerText
  52. } );
  53. /**
  54. * Get the main div of the filter to tell if this is being
  55. * selected from the sidebar or block so we can hide the dropdown
  56. */
  57. var elID = event.target.parentNode.parentNode;
  58. var idArray = elID.getAttribute( 'id' ).split( '-' );
  59. var nfOptions = document.getElementById( 'nf-filter-container-' + idArray[ idArray.length -1 ] );
  60. var inputEl = document.getElementById( 'nf-formFilter-sidebar' );
  61. inputEl.value = '';
  62. nfOptions.style.display = 'none';
  63. }
  64. function nfHideOptions( event ) {
  65. /**
  66. * Get the main div of the filter to tell if this is being
  67. * selected from the sidebar or block so we can hide the dropdown
  68. */
  69. var elID = event.target.getAttribute( 'id' );
  70. var idArray = elID.split( '-' );
  71. var nfOptions = document.getElementById( 'nf-filter-container-' + idArray[ idArray.length -1 ] );
  72. nfOptions.style.display = 'none';
  73. }
  74. function nfInputKeyUp( event ) {
  75. var val = event.target.value;
  76. /**
  77. * Get the main div of the filter to tell if this is being
  78. * selected from the sidebar or block so we can SHOW the dropdown
  79. */
  80. var filterInputContainer = event.target.parentNode.parentNode.parentNode;
  81. filterInputContainer.querySelector( '.nf-filter-option-container' ).style.display = 'block';
  82. filterInputContainer.style.display = 'block';
  83. // Let's filter the forms here
  84. _.each( ninjaFormsBlock.forms, function( form, index ) {
  85. var liEl = filterInputContainer.querySelector( "[data-formid='" + form.value + "']" );
  86. if ( 0 <= form.label.toLowerCase().indexOf( val.toLowerCase() ) ) {
  87. // shows options that DO contain the text entered
  88. liEl.style.display = 'block';
  89. } else {
  90. // hides options the do not contain the text entered
  91. liEl.style.display = 'none';
  92. }
  93. });
  94. }
  95. // Set up the form items from the localized php variables
  96. var formItems = [];
  97. _.each( ninjaFormsBlock.forms, function( form, index ) {
  98. formItems.push( el( 'li', { className: 'nf-filter-option',
  99. 'data-formid': form.value, onMouseDown: selectForm},
  100. form.label + " ( ID: " + form.value + " )" ))
  101. });
  102. // Set up form filter for the block
  103. var inputFilterMain = el( 'div', { id: 'nf-filter-input-main',
  104. className: 'nf-filter-input' },
  105. el( TextControl, { id: 'nf-formFilter-main',
  106. placeHolder: 'Select a Form',
  107. className: 'nf-filter-input-el blocks-select-control__input',
  108. onChange: nfOnValueChange,
  109. onClick: nfFocusClick,
  110. onKeyUp: nfInputKeyUp,
  111. onBlur: nfHideOptions
  112. } ),
  113. el( 'span', { id: 'nf-filter-input-icon-main',
  114. className: 'nf-filter-input-icon',
  115. onClick: nfFocusClick,
  116. dangerouslySetInnerHTML: { __html: '&#9662;' } } ),
  117. el( 'div', { id: 'nf-filter-container-main',
  118. className: 'nf-filter-option-container' },
  119. el( 'ul', null, formItems )
  120. )
  121. );
  122. // Create filter input for the sidebar blocks settings
  123. var inputFilterSidebar = el( 'div', { id: 'nf-filter-input-sidebar',
  124. className: 'nf-filter-input' },
  125. el( TextControl, { id: 'nf-formFilter-sidebar',
  126. placeHolder: 'Select a Form',
  127. className: 'nf-filter-input-el blocks-select-control__input',
  128. onChange: nfOnValueChange,
  129. onClick: nfFocusClick,
  130. onKeyUp: nfInputKeyUp,
  131. onBlur: nfHideOptions
  132. } ),
  133. el( 'span', { id: 'nf-filter-input-icon-sidebar',
  134. className: 'nf-filter-input-icon',
  135. onClick: nfFocusClick,
  136. dangerouslySetInnerHTML: { __html: '&#9662;' } } ),
  137. el( 'div', { id: 'nf-filter-container-sidebar',
  138. className: 'nf-filter-option-container' },
  139. el( 'ul', null, formItems )
  140. )
  141. );
  142. // Set up the form filter dropdown in the side bar 'block' settings
  143. var inspectorControls = el( InspectorControls, {},
  144. el( 'span', null, 'Current selected form:' ),
  145. el( 'br', null ),
  146. el( 'span', null, formName ),
  147. el( 'br', null ),
  148. el ('hr', null ),
  149. el ( 'label', { for: 'nf-formFilter-sidebar' }, 'Type to' +
  150. ' filter' +
  151. ' forms' ),
  152. inputFilterSidebar
  153. // el( SelectControl, { label: 'Form ID', value: formID, options: ninjaFormsBlock.forms, onChange: onFormChange } )
  154. );
  155. /**
  156. * Create the div container, add an overlay so the user can interact
  157. * with the form in Gutenberg, then render the iframe with form
  158. */
  159. if( '' === formID ) {
  160. children.push( el( 'div', {style : {width: '100%'}},
  161. el( 'img', { src: ninjaFormsBlock.block_logo}),
  162. // el( SelectControl, { value: formID, options: ninjaFormsBlock.forms, onChange: onFormChange }),
  163. el ( 'div', null, 'Type to Filter'),
  164. inputFilterMain
  165. ) );
  166. } else {
  167. children.push(
  168. el( 'div', { className: 'nf-iframe-container' },
  169. el( 'div', { className: 'nf-iframe-overlay' } ),
  170. el( 'iframe', { src: ninjaFormsBlock.siteUrl + '?nf_preview_form='
  171. + formID + '&nf_iframe', height: '0', width: '500', scrolling: 'no' })
  172. )
  173. )
  174. }
  175. children.push(inspectorControls);
  176. return [
  177. children
  178. ];
  179. },
  180. save: function( props ) {
  181. var formID = props.attributes.formID;
  182. if( ! formID ) return '';
  183. /**
  184. * we're essentially just adding a short code, here is where
  185. * it's save in the editor
  186. *
  187. * return content wrapped in DIV b/c raw HTML is unsupported
  188. * going forward
  189. */
  190. var returnHTML = '[ninja_forms id=' + parseInt( formID ) + ']';
  191. return el( 'div', null, returnHTML);
  192. }
  193. } );
  194. } )(
  195. window.wp.blocks,
  196. window.wp.i18n,
  197. window.wp.element,
  198. window.wp.components
  199. );