tadv.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * This file is part of the TinyMCE Advanced WordPress plugin and is released under the same license.
  3. * For more information please see tinymce-advanced.php.
  4. *
  5. * Copyright (c) 2007-2019 Andrew Ozz. All rights reserved.
  6. */
  7. jQuery( document ).ready( function( $ ) {
  8. var $importElement = $('#tadv-import');
  9. var $importError = $('#tadv-import-error');
  10. function sortClassic() {
  11. var container = $('.container');
  12. if ( container.sortable( 'instance' ) ) {
  13. container.sortable( 'destroy' );
  14. }
  15. container.sortable({
  16. connectWith: '.container',
  17. items: '> li',
  18. cursor: 'move',
  19. stop: function( event, ui ) {
  20. var toolbar_id;
  21. if ( ui && ( toolbar_id = ui.item.parent().attr('id') ) ) {
  22. ui.item.find('input.tadv-button').attr('name', toolbar_id + '[]');
  23. }
  24. },
  25. activate: function( event, ui ) {
  26. $(this).parent().addClass( 'highlighted' );
  27. },
  28. deactivate: function( event, ui ) {
  29. $(this).parent().removeClass( 'highlighted' );
  30. },
  31. revert: 300,
  32. opacity: 0.7,
  33. placeholder: 'tadv-placeholder',
  34. forcePlaceholderSize: true
  35. });
  36. }
  37. function sortBlock() {
  38. var classicBlock = $( '.container-classic-block' );
  39. var block = $( '.container-block' );
  40. var blockToolbar = $( '#toolbar_block' );
  41. if ( classicBlock.sortable( 'instance' ) ) {
  42. classicBlock.sortable( 'destroy' );
  43. }
  44. if ( block.sortable( 'instance' ) ) {
  45. block.sortable( 'destroy' );
  46. }
  47. if ( blockToolbar.sortable( 'instance' ) ) {
  48. blockToolbar.sortable( 'destroy' );
  49. }
  50. classicBlock.sortable({
  51. connectWith: '.container-classic-block',
  52. items: '> li',
  53. cursor: 'move',
  54. stop: function( event, ui ) {
  55. var toolbar_id = ui.item.parent().attr( 'id' );
  56. resetItemName( toolbar_id, ui.item );
  57. },
  58. activate: function( event, ui ) {
  59. $(this).parent().addClass( 'highlighted' );
  60. },
  61. deactivate: function( event, ui ) {
  62. $(this).parent().removeClass( 'highlighted' );
  63. },
  64. revert: 300,
  65. opacity: 0.7,
  66. placeholder: 'tadv-placeholder',
  67. forcePlaceholderSize: true
  68. });
  69. blockToolbar.sortable({
  70. connectWith: '.container-block',
  71. items: '> li',
  72. cursor: 'move',
  73. stop: function( event, ui ) {
  74. var toolbar_id = ui.item.parent().attr( 'id' );
  75. resetItemName( toolbar_id, ui.item );
  76. sortBlockToolbar();
  77. },
  78. activate: function( event, ui ) {
  79. $(this).parent().addClass( 'highlighted' );
  80. },
  81. deactivate: function( event, ui ) {
  82. $(this).parent().removeClass( 'highlighted' );
  83. },
  84. revert: 300,
  85. opacity: 0.7,
  86. placeholder: 'tadv-placeholder',
  87. forcePlaceholderSize: true
  88. });
  89. block.sortable({
  90. connectWith: '.container-block, #toolbar_block',
  91. items: '> li',
  92. cursor: 'move',
  93. stop: function( event, ui ) {
  94. var toolbar_id = ui.item.parent().attr( 'id' );
  95. resetItemName( toolbar_id, ui.item );
  96. sortBlockToolbar();
  97. },
  98. activate: function( event, ui ) {
  99. $(this).parent().addClass( 'highlighted' );
  100. },
  101. deactivate: function( event, ui ) {
  102. $(this).parent().removeClass( 'highlighted' );
  103. },
  104. receive: function( event, ui ) {
  105. if ( $( event.target ).is( '#toolbar_block_side' ) && ui.item.is( 'li.core-image' ) ) {
  106. block.sortable( 'cancel' );
  107. }
  108. },
  109. revert: 300,
  110. opacity: 0.7,
  111. placeholder: 'tadv-block-placeholder',
  112. forcePlaceholderSize: true
  113. });
  114. }
  115. function resetItemName( name, item ) {
  116. if ( name ) {
  117. item.find( 'input[type="hidden"]' ).attr( 'name', name + '[]' );
  118. }
  119. }
  120. function sortBlockToolbar() {
  121. var container = $( '#toolbar_block' );
  122. var items = container.find( 'li' );
  123. items.sort( function ( a, b ) {
  124. var aa = $( a ).find( 'div' ).attr( 'title' );
  125. var bb = $( b ).find( 'div' ).attr( 'title' );
  126. return ( aa > bb ) ? 1 : -1;
  127. });
  128. container.append( items );
  129. }
  130. // Make block editor tab sortable on load
  131. sortBlock();
  132. $( '.settings-toggle.block' ).on( 'focus', function( event ) {
  133. $( '.wrap' ).removeClass( 'classic-active' ).addClass( 'block-active' );
  134. sortBlock();
  135. });
  136. $( '.settings-toggle.classic' ).on( 'focus', function( event ) {
  137. $( '.wrap' ).removeClass( 'block-active' ).addClass( 'classic-active' );
  138. sortClassic();
  139. });
  140. $( '#menubar' ).on( 'change', function() {
  141. $( '.tadv-mce-menu.tadv-classic-editor' ).toggleClass( 'enabled', $(this).prop('checked') );
  142. });
  143. $( '#menubar_block' ).on( 'change', function() {
  144. $( '.tadv-mce-menu.tadv-block-editor' ).toggleClass( 'enabled', $(this).prop('checked') );
  145. });
  146. $( '#tadvadmins' ).on( 'submit', function() {
  147. $( 'ul.container' ).each( function( i, node ) {
  148. $( node ).find( '.tadv-button' ).attr( 'name', node.id ? node.id + '[]' : '' );
  149. });
  150. });
  151. $( 'input[name="selected_text_color"]' ).on( 'change', function() {
  152. if ( this.id === 'selected_text_color_yes' ) {
  153. $( '.panel-block-text-color' ).removeClass( 'disabled' );
  154. } else {
  155. $( '.panel-block-text-color' ).addClass( 'disabled' );
  156. }
  157. } );
  158. $( 'input[name="selected_text_background_color"]' ).on( 'change', function() {
  159. if ( this.id === 'selected_text_background_color_yes' ) {
  160. $( '.panel-block-background-color' ).removeClass( 'disabled' );
  161. } else {
  162. $( '.panel-block-background-color' ).addClass( 'disabled' );
  163. }
  164. } );
  165. $( '.tadv-popout-help-toggle, .tadv-popout-help-close' ).on( 'click', function( event ) {
  166. $( '.tadv-popout-help' ).toggleClass( 'hidden' );
  167. } );
  168. $('#tadv-export-select').click( function() {
  169. $('#tadv-export').focus().select();
  170. });
  171. $importElement.change( function() {
  172. $importError.empty();
  173. });
  174. $('#tadv-import-verify').click( function() {
  175. var string;
  176. string = ( $importElement.val() || '' ).replace( /^[^{]*/, '' ).replace( /[^}]*$/, '' );
  177. $importElement.val( string );
  178. try {
  179. JSON.parse( string );
  180. $importError.text( 'No errors.' );
  181. } catch( error ) {
  182. $importError.text( error );
  183. }
  184. });
  185. function translate( str ) {
  186. if ( window.tadvTranslation.hasOwnProperty( str ) ) {
  187. return window.tadvTranslation[str];
  188. }
  189. return str;
  190. }
  191. if ( typeof window.tadvTranslation === 'object' ) {
  192. $( '.tadvitem' ).each( function( i, element ) {
  193. var $element = $( element ),
  194. $descr = $element.find( '.descr' ),
  195. text = $descr.text();
  196. if ( text ) {
  197. text = translate( text );
  198. $descr.text( text );
  199. $element.find( '.mce-ico' ).attr( 'title', text );
  200. }
  201. });
  202. $( '.tadv-mce-menu .tadv-translate' ).each( function( i, element ) {
  203. var $element = $( element ),
  204. text = $element.text();
  205. if ( text ) {
  206. $element.text( translate( text ) );
  207. }
  208. });
  209. }
  210. });