fl-builder-admin-posts.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. (function($){
  2. /**
  3. * Helper class for dealing with the post edit screen.
  4. *
  5. * @class FLBuilderAdminPosts
  6. * @since 1.0
  7. * @static
  8. */
  9. FLBuilderAdminPosts = {
  10. /**
  11. * Initializes the builder for the post edit screen.
  12. *
  13. * @since 1.0
  14. * @method init
  15. */
  16. init: function()
  17. {
  18. $('.fl-enable-editor').on('click', this._enableEditorClicked);
  19. $('.fl-enable-builder').on('click', this._enableBuilderClicked);
  20. $('.fl-launch-builder').on('click', this._launchBuilderClicked);
  21. /* WPML Support */
  22. $('#icl_cfo').on('click', this._wpmlCopyClicked);
  23. this._hideFLBuilderAdminButtons();
  24. this._hideBlockEditorInserter();
  25. },
  26. /**
  27. * Fires when the text editor button is clicked
  28. * and switches the current post to use that
  29. * instead of the builder.
  30. *
  31. * @since 1.0
  32. * @access private
  33. * @method _enableEditorClicked
  34. */
  35. _enableEditorClicked: function()
  36. {
  37. if ( ! $( 'body' ).hasClass( 'fl-builder-enabled' ) ) {
  38. return;
  39. }
  40. if ( confirm( FLBuilderAdminPostsStrings.switchToEditor ) ) {
  41. $('.fl-builder-admin-tabs a').removeClass('fl-active');
  42. $(this).addClass('fl-active');
  43. _wpnonce = $('.fl-builder-admin-tabs').find('#_fl_enable_editor').val();
  44. FLBuilderAdminPosts.ajax({
  45. action: 'fl_builder_disable',
  46. _wpnonce: _wpnonce
  47. }, FLBuilderAdminPosts._enableEditorComplete);
  48. }
  49. },
  50. /**
  51. * Callback for enabling the editor.
  52. *
  53. * @since 1.0
  54. * @access private
  55. * @method _enableEditorComplete
  56. */
  57. _enableEditorComplete: function()
  58. {
  59. $('body').removeClass('fl-builder-enabled');
  60. $(window).resize();
  61. },
  62. /**
  63. * Callback for enabling the editor.
  64. *
  65. * @since 1.0
  66. * @access private
  67. * @method _enableBuilderClicked
  68. */
  69. _enableBuilderClicked: function()
  70. {
  71. if($('body').hasClass('fl-builder-enabled')) {
  72. return;
  73. }
  74. else {
  75. $('.fl-builder-admin-tabs a').removeClass('fl-active');
  76. $(this).addClass('fl-active');
  77. FLBuilderAdminPosts._launchBuilder();
  78. }
  79. },
  80. /**
  81. * Fires when the page builder button is clicked
  82. * and switches the current post to use that
  83. * instead of the text editor.
  84. *
  85. * @since 1.0
  86. * @access private
  87. * @method _launchBuilderClicked
  88. * @param {Object} e An event object.
  89. */
  90. _launchBuilderClicked: function(e)
  91. {
  92. e.preventDefault();
  93. FLBuilderAdminPosts._launchBuilder();
  94. },
  95. /**
  96. * Callback for enabling the builder.
  97. *
  98. * @since 1.0
  99. * @access private
  100. * @method _launchBuilder
  101. */
  102. _launchBuilder: function()
  103. {
  104. var postId = $('#post_ID').val(),
  105. title = $('#title');
  106. if(typeof title !== 'undefined' && title.val() === '') {
  107. title.val('Post #' + postId);
  108. }
  109. $(window).off('beforeunload');
  110. $('body').addClass('fl-builder-enabled');
  111. $('.fl-builder-loading').show();
  112. $('form#post').append('<input type="hidden" name="fl-builder-redirect" value="' + postId + '" />');
  113. $('form#post').submit();
  114. },
  115. /**
  116. * Fires when the WPML copy button is clicked.
  117. *
  118. * @since 1.1.7
  119. * @access private
  120. * @method _wpmlCopyClicked
  121. * @param {Object} e An event object.
  122. */
  123. _wpmlCopyClicked: function(e)
  124. {
  125. var originalPostId = $('#icl_translation_of').val();
  126. if(typeof originalPostId !== 'undefined') {
  127. $('.fl-builder-loading').show();
  128. FLBuilderAdminPosts.ajax({
  129. action: 'fl_builder_duplicate_wpml_layout',
  130. original_post_id: originalPostId
  131. }, FLBuilderAdminPosts._wpmlCopyComplete);
  132. }
  133. },
  134. /**
  135. * Callback for when the WPML copy button is clicked.
  136. *
  137. * @since 1.1.7
  138. * @access private
  139. * @method _wpmlCopyComplete
  140. * @param {String} response The JSON encoded response.
  141. */
  142. _wpmlCopyComplete: function(response)
  143. {
  144. response = JSON.parse(response);
  145. $('.fl-builder-loading').hide();
  146. if(response.has_layout && response.enabled) {
  147. $('body').addClass('fl-builder-enabled');
  148. }
  149. },
  150. /**
  151. * Hide the Page Builder Admin Buttons if Content Editor is hidden in the ACF Field Settings.
  152. *
  153. * @since 2.1.7
  154. * @access private
  155. * @method _hideFLBuilderAdminButtons
  156. */
  157. _hideFLBuilderAdminButtons: function()
  158. {
  159. if ( $( '.acf-postbox' ).is( ':visible' ) && $( '#postdivrich' ).is( ':hidden' ) && ! $( '.fl-enable-builder' ).hasClass('fl-active') ){
  160. $( '.fl-builder-admin' ).hide();
  161. }
  162. },
  163. /**
  164. * Hide the Gutenberg Block Editor Inserter button.
  165. *
  166. * @since 2.4
  167. * @access private
  168. * @method _hideBlockEditorInserter
  169. */
  170. _hideBlockEditorInserter: function()
  171. {
  172. setTimeout( function(){
  173. if ( $( 'body' ).hasClass( 'fl-builder-enabled' ) ) {
  174. $( '.block-editor-inserter' ).hide();
  175. $( '.wp-block-paragraph' ).parent().remove();
  176. $( '.wp-block[data-type="core/paragraph"]' ).hide();
  177. }
  178. }, 100 );
  179. },
  180. /**
  181. * Makes an AJAX request.
  182. *
  183. * @since 1.0
  184. * @method ajax
  185. * @param {Object} data An object with data to send in the request.
  186. * @param {Function} callback A function to call when the request is complete.
  187. */
  188. ajax: function(data, callback)
  189. {
  190. // Add the post ID to the data.
  191. data.post_id = $('#post_ID').val();
  192. // Show the loader.
  193. $('.fl-builder-loading').show();
  194. // Send the request.
  195. $.post(ajaxurl, data, function(response) {
  196. FLBuilderAdminPosts._ajaxComplete();
  197. if(typeof callback !== 'undefined') {
  198. callback.call(this, response);
  199. }
  200. });
  201. },
  202. /**
  203. * Generic callback for when an AJAX request is complete.
  204. *
  205. * @since 1.0
  206. * @access private
  207. * @method _ajaxComplete
  208. */
  209. _ajaxComplete: function()
  210. {
  211. $('.fl-builder-loading').hide();
  212. }
  213. };
  214. /* Initializes the post edit screen. */
  215. $(function(){
  216. FLBuilderAdminPosts.init();
  217. });
  218. })(jQuery);