ccj_admin.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. jQuery(document).ready( function($) {
  2. $('.page-title-action').hide();
  3. // Initialize the CodeMirror editor
  4. if ( $('#ccj_content').length > 0 ) {
  5. var content_mode = $("#ccj_content").attr('mode');
  6. if ( content_mode == 'html' ) {
  7. var content_mode = {
  8. name: "htmlmixed",
  9. scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
  10. mode: null}]
  11. };
  12. }
  13. var options = {
  14. lineNumbers: true,
  15. mode: content_mode,
  16. matchBrackets: true,
  17. extraKeys: {
  18. "Ctrl-F": "findPersistent",
  19. },
  20. };
  21. var cm_width = $('#title').width() + 16;
  22. var cm_height = 500;
  23. var editor = CodeMirror.fromTextArea(document.getElementById("ccj_content"), options);
  24. editor.setSize(cm_width, cm_height);
  25. $('.CodeMirror').resizable({
  26. resize: function() {
  27. editor.setSize($(this).width(), $(this).height());
  28. } ,
  29. maxWidth: cm_width,
  30. minWidth: cm_width,
  31. minHeight: 200
  32. });
  33. $(window).resize(function () {
  34. var cm_width = $('#title').width() + 16;
  35. var cm_height = $('.CodeMirror').height();
  36. editor.setSize(cm_width, cm_height);
  37. });
  38. var postID = document.getElementById('post_ID') != null ? document.getElementById('post_ID').value : 0;
  39. var getCookie = function (name) {
  40. var value = '; ' + document.cookie;
  41. var parts = value.split('; ' + name + '=');
  42. if (parts.length === 2) return parts.pop().split(';').shift();
  43. };
  44. // Saving cursor state
  45. editor.on('cursorActivity', function () {
  46. var curPos = editor.getCursor();
  47. document.cookie = 'hesh_plugin_pos=' + postID + ',' + curPos.line + ',' + curPos.ch;
  48. });
  49. // Restoring cursor state
  50. var curPos = (getCookie('hesh_plugin_pos') || '0,0,0').split(',');
  51. if (postID === curPos[0]) {
  52. editor.setCursor(parseFloat(curPos[1]), parseFloat(curPos[2]));
  53. }
  54. }
  55. // For post.php or post-new.php pages show the code's title in the page title
  56. if ( $('#titlediv #title').length > 0 ) {
  57. var new_title = $("input[name=custom_code_language]").val().toUpperCase() + ' - ' + $('#titlediv #title').val();
  58. if( $('#titlediv #title').val().length > 0 ) {
  59. $(document).prop('title', new_title );
  60. }
  61. $('#titlediv #title').change(function() {
  62. if ( $(this).val().length > 0 ) {
  63. $(document).prop('title', new_title);
  64. }
  65. });
  66. }
  67. // Make the inactive rows opaque
  68. if ( $('.dashicons-star-empty.ccj_row').length > 0 ) {
  69. $('.dashicons-star-empty.ccj_row').each(function(){
  70. $(this).parent().parent().parent().css('opacity', '0.4');
  71. });
  72. }
  73. // Activate/deactivate codes with AJAX
  74. $(".ccj_activate_deactivate").click( function(e) {
  75. var url = $(this).attr('href');
  76. var code_id = $(this).attr('data-code-id');
  77. e.preventDefault();
  78. $.ajax({
  79. url: url,
  80. success: function(data){
  81. if (data === 'yes') {
  82. ccj_activate_deactivate(code_id, false);
  83. }
  84. if (data === 'no') {
  85. ccj_activate_deactivate(code_id, true);
  86. }
  87. }
  88. });
  89. });
  90. // Toggle the signs for activating/deactivating codes
  91. function ccj_activate_deactivate(code_id, action) {
  92. var row = $('tr#post-'+code_id);
  93. if ( action === true ) {
  94. row.css('opacity', '1');
  95. row.find('.row-actions .ccj_activate_deactivate')
  96. .text(CCJ.deactivate)
  97. .attr('title', CCJ.active_title);
  98. row.find('td.active .dashicons')
  99. .removeClass('dashicons-star-empty')
  100. .addClass('dashicons-star-filled');
  101. row.find('td.active .ccj_activate_deactivate')
  102. .attr('title', CCJ.active_title);
  103. $('#activate-action span').text(CCJ.active);
  104. $('#activate-action .ccj_activate_deactivate').text(CCJ.deactivate);
  105. } else {
  106. row.css('opacity', '0.4');
  107. row.find('.row-actions .ccj_activate_deactivate')
  108. .text(CCJ.activate)
  109. .attr('title', CCJ.deactive_title);
  110. row.find('td.active .dashicons')
  111. .removeClass('dashicons-star-filled')
  112. .addClass('dashicons-star-empty');
  113. row.find('td.active .ccj_activate_deactivate')
  114. .attr('title', CCJ.deactive_title);
  115. $('#activate-action span').text(CCJ.inactive);
  116. $('#activate-action .ccj_activate_deactivate').text(CCJ.activate);
  117. }
  118. }
  119. // Permalink slug
  120. $( '#titlediv' ).on( 'click', '.ccj-edit-slug', function() {
  121. var i,
  122. $el, revert_e,
  123. c = 0,
  124. slug_value = $('#editable-post-name').html(),
  125. real_slug = $('#post_name'),
  126. revert_slug = real_slug.val(),
  127. permalink = $( '#sample-permalink' ),
  128. permalinkOrig = permalink.html(),
  129. permalinkInner = $( '#sample-permalink a' ).html(),
  130. permalinkHref = $('#sample-permalink a').attr('href'),
  131. buttons = $('#ccj-edit-slug-buttons'),
  132. buttonsOrig = buttons.html(),
  133. full = $('#editable-post-name-full');
  134. // Deal with Twemoji in the post-name.
  135. full.find( 'img' ).replaceWith( function() { return this.alt; } );
  136. full = full.html();
  137. permalink.html( permalinkInner );
  138. // Save current content to revert to when cancelling.
  139. $el = $( '#editable-post-name' );
  140. revert_e = $el.html();
  141. buttons.html( '<button type="button" class="save button button-small">' + postL10n.ok + '</button> <button type="button" class="cancel button- link">' + postL10n.cancel + '</button>' );
  142. // Save permalink changes.
  143. buttons.children( '.save' ).click( function() {
  144. var new_slug = $el.children( 'input' ).val();
  145. if ( new_slug == $('#editable-post-name-full').text() ) {
  146. buttons.children('.cancel').click();
  147. return;
  148. }
  149. $.post(
  150. ajaxurl,
  151. {
  152. action: 'ccj_permalink',
  153. code_id: $('#post_ID').val(),
  154. new_slug: new_slug,
  155. permalink: permalinkHref,
  156. ccj_permalink_nonce: $('#ccj-permalink-nonce').val()
  157. },
  158. function(data) {
  159. var box = $('#edit-slug-box');
  160. box.html(data);
  161. if (box.hasClass('hidden')) {
  162. box.fadeIn('fast', function () {
  163. box.removeClass('hidden');
  164. });
  165. }
  166. }
  167. );
  168. });
  169. // Cancel editing of permalink.
  170. buttons.children( '.cancel' ).click( function() {
  171. $('#view-post-btn').show();
  172. $el.html(revert_e);
  173. buttons.html(buttonsOrig);
  174. permalink.html(permalinkOrig);
  175. real_slug.val(revert_slug);
  176. $( '.ccj-edit-slug' ).focus();
  177. });
  178. $el.html( '<input type="text" name="new_slug" id="new-post-slug" value="' + slug_value + '" autocomplete="off" />' ).children( 'input' ).keydown( function( e ) {
  179. var key = e.which;
  180. // On [enter], just save the new slug, don't save the post.
  181. if ( 13 === key ) {
  182. e.preventDefault();
  183. buttons.children( '.save' ).click();
  184. }
  185. // On [esc] cancel the editing.
  186. if ( 27 === key ) {
  187. buttons.children( '.cancel' ).click();
  188. }
  189. } ).keyup( function() {
  190. real_slug.val( this.value );
  191. }).focus();
  192. });
  193. });