atd-autoproofread.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* jshint devel: true, onevar: false */
  2. /* global tinyMCE, AtD_restore_if_proofreading, AtD_check, AtD_unbind_proofreader_listeners,
  3. AtD, AtD_bind_proofreader_listeners, AtD_check_when
  4. */
  5. /* the AtD/jQuery and AtD/TinyMCE plugins check if this variable exists and increment it when a proofread check happens */
  6. var AtD_proofread_click_count = 0;
  7. /* This is function called when the publish/update button is pressed */
  8. function AtD_submit_check( e ) {
  9. /* User has already checked their document... no need to hold up their submit */
  10. if (AtD_proofread_click_count > 0) {
  11. return;
  12. }
  13. /* Let's not submit the form, shall we? */
  14. e.stopImmediatePropagation();
  15. e.preventDefault();
  16. /* We'll call the AtD function based on which editor is currently active */
  17. if ( typeof(tinyMCE) !== 'undefined' && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden() ) {
  18. /* Woo! We're running tinyMCE! */
  19. tinyMCE.activeEditor.execCommand('mceWritingImprovementTool', AtD_submit_check_callback);
  20. } else {
  21. /* Go Go HTML editor! */
  22. AtD_restore_if_proofreading();
  23. AtD_check( AtD_submit_check_callback );
  24. }
  25. }
  26. /* This is the callback function that runs after the publish/update button is pressed */
  27. function AtD_submit_check_callback(count) {
  28. count = Number( count || 0 );
  29. AtD_unbind_proofreader_listeners();
  30. if ( 0 === count || 1 < AtD_proofread_click_count ) {
  31. /* if no errors were found, submit form */
  32. AtD_update_post();
  33. } else if ( -1 === count ) {
  34. /* If there was an error, alert the user and submit form */
  35. alert( AtD.getLang('message_server_error', 'There was a problem communicating with the Proofreading service. Try again in one minute.') );
  36. AtD_update_post();
  37. } else {
  38. var original_post_status = jQuery('#original_post_status').val();
  39. /* Okay, the user has tried to publish/update already but there are still errors. Ask them what to do */
  40. var message;
  41. if ( original_post_status === 'publish' ) {
  42. message = AtD.getLang('dialog_confirm_post_publish', 'The proofreader has suggestions for this post. Are you sure you want to publish it?\n\nPress OK to publish your post, or Cancel to view the suggestions and edit your post.');
  43. } else {
  44. message = AtD.getLang('dialog_confirm_post_update', 'The proofreader has suggestions for this post. Are you sure you want to update it?\n\nPress OK to update your post, or Cancel to view the suggestions and edit your post.');
  45. }
  46. if ( confirm( message ) ) {
  47. AtD_update_post();
  48. } else {
  49. AtD_bind_proofreader_listeners();
  50. AtD_kill_autoproofread();
  51. }
  52. /* Let's do some interface clean-up */
  53. jQuery('#publish').removeClass('button-primary-disabled');
  54. jQuery('#ajax-loading').hide();
  55. }
  56. }
  57. /* Stop the proofreader from doing its auto proofread thing (activated when the proofread button is clicked) */
  58. function AtD_kill_autoproofread() {
  59. jQuery('#publish').unbind('click.AtD_submit_check');
  60. }
  61. /* a function to force the post to be submitted */
  62. function AtD_update_post() {
  63. if ( typeof(tinyMCE) === 'undefined' || !tinyMCE.activeEditor || tinyMCE.activeEditor.isHidden() ) {
  64. AtD_restore_if_proofreading();
  65. }
  66. jQuery('#publish').unbind('click.AtD_submit_check').click();
  67. }
  68. /* init the autoproofread options */
  69. jQuery( document ).ready( function($){
  70. var orig_status = $('#original_post_status').val();
  71. /* check if auto-check is enabled && if #content exists */
  72. if ( typeof AtD_check_when !== 'undefined' && $('#content').length &&
  73. ( ( orig_status !== 'publish' && AtD_check_when.onpublish ) ||
  74. ( ( orig_status === 'publish' || orig_status === 'schedule' ) && AtD_check_when.onupdate ) ) ) {
  75. $('#publish').bind( 'click.AtD_submit_check', AtD_submit_check );
  76. }
  77. });