fl-builder-admin-posts.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. },
  24. /**
  25. * Fires when the text editor button is clicked
  26. * and switches the current post to use that
  27. * instead of the builder.
  28. *
  29. * @since 1.0
  30. * @access private
  31. * @method _enableEditorClicked
  32. */
  33. _enableEditorClicked: function()
  34. {
  35. if ( ! $( 'body' ).hasClass( 'fl-builder-enabled' ) ) {
  36. return;
  37. }
  38. if ( confirm( FLBuilderAdminPostsStrings.switchToEditor ) ) {
  39. $('.fl-builder-admin-tabs a').removeClass('fl-active');
  40. $(this).addClass('fl-active');
  41. FLBuilderAdminPosts.ajax({
  42. action: 'fl_builder_disable',
  43. }, FLBuilderAdminPosts._enableEditorComplete);
  44. }
  45. },
  46. /**
  47. * Callback for enabling the editor.
  48. *
  49. * @since 1.0
  50. * @access private
  51. * @method _enableEditorComplete
  52. */
  53. _enableEditorComplete: function()
  54. {
  55. $('body').removeClass('fl-builder-enabled');
  56. $(window).resize();
  57. },
  58. /**
  59. * Callback for enabling the editor.
  60. *
  61. * @since 1.0
  62. * @access private
  63. * @method _enableBuilderClicked
  64. */
  65. _enableBuilderClicked: function()
  66. {
  67. if($('body').hasClass('fl-builder-enabled')) {
  68. return;
  69. }
  70. else {
  71. $('.fl-builder-admin-tabs a').removeClass('fl-active');
  72. $(this).addClass('fl-active');
  73. FLBuilderAdminPosts._launchBuilder();
  74. }
  75. },
  76. /**
  77. * Fires when the page builder button is clicked
  78. * and switches the current post to use that
  79. * instead of the text editor.
  80. *
  81. * @since 1.0
  82. * @access private
  83. * @method _launchBuilderClicked
  84. * @param {Object} e An event object.
  85. */
  86. _launchBuilderClicked: function(e)
  87. {
  88. e.preventDefault();
  89. FLBuilderAdminPosts._launchBuilder();
  90. },
  91. /**
  92. * Callback for enabling the builder.
  93. *
  94. * @since 1.0
  95. * @access private
  96. * @method _launchBuilder
  97. */
  98. _launchBuilder: function()
  99. {
  100. var postId = $('#post_ID').val(),
  101. title = $('#title');
  102. if(typeof title !== 'undefined' && title.val() === '') {
  103. title.val('Post #' + postId);
  104. }
  105. $(window).off('beforeunload');
  106. $('body').addClass('fl-builder-enabled');
  107. $('.fl-builder-loading').show();
  108. $('form#post').append('<input type="hidden" name="fl-builder-redirect" value="' + postId + '" />');
  109. $('form#post').submit();
  110. },
  111. /**
  112. * Fires when the WPML copy button is clicked.
  113. *
  114. * @since 1.1.7
  115. * @access private
  116. * @method _wpmlCopyClicked
  117. * @param {Object} e An event object.
  118. */
  119. _wpmlCopyClicked: function(e)
  120. {
  121. var originalPostId = $('#icl_translation_of').val();
  122. if(typeof originalPostId !== 'undefined') {
  123. $('.fl-builder-loading').show();
  124. FLBuilderAdminPosts.ajax({
  125. action: 'fl_builder_duplicate_wpml_layout',
  126. original_post_id: originalPostId
  127. }, FLBuilderAdminPosts._wpmlCopyComplete);
  128. }
  129. },
  130. /**
  131. * Callback for when the WPML copy button is clicked.
  132. *
  133. * @since 1.1.7
  134. * @access private
  135. * @method _wpmlCopyComplete
  136. * @param {String} response The JSON encoded response.
  137. */
  138. _wpmlCopyComplete: function(response)
  139. {
  140. response = JSON.parse(response);
  141. $('.fl-builder-loading').hide();
  142. if(response.has_layout && response.enabled) {
  143. $('body').addClass('fl-builder-enabled');
  144. }
  145. },
  146. /**
  147. * Makes an AJAX request.
  148. *
  149. * @since 1.0
  150. * @method ajax
  151. * @param {Object} data An object with data to send in the request.
  152. * @param {Function} callback A function to call when the request is complete.
  153. */
  154. ajax: function(data, callback)
  155. {
  156. // Add the post ID to the data.
  157. data.post_id = $('#post_ID').val();
  158. // Show the loader.
  159. $('.fl-builder-loading').show();
  160. // Send the request.
  161. $.post(ajaxurl, data, function(response) {
  162. FLBuilderAdminPosts._ajaxComplete();
  163. if(typeof callback !== 'undefined') {
  164. callback.call(this, response);
  165. }
  166. });
  167. },
  168. /**
  169. * Generic callback for when an AJAX request is complete.
  170. *
  171. * @since 1.0
  172. * @access private
  173. * @method _ajaxComplete
  174. */
  175. _ajaxComplete: function()
  176. {
  177. $('.fl-builder-loading').hide();
  178. }
  179. };
  180. /* Initializes the post edit screen. */
  181. $(function(){
  182. FLBuilderAdminPosts.init();
  183. });
  184. })(jQuery);