comment.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @output wp-admin/js/comment.js
  3. */
  4. /* global postboxes */
  5. /**
  6. * Binds to the document ready event.
  7. *
  8. * @since 2.5.0
  9. *
  10. * @param {jQuery} $ The jQuery object.
  11. */
  12. jQuery( function($) {
  13. postboxes.add_postbox_toggles('comment');
  14. var $timestampdiv = $('#timestampdiv'),
  15. $timestamp = $( '#timestamp' ),
  16. stamp = $timestamp.html(),
  17. $timestampwrap = $timestampdiv.find( '.timestamp-wrap' ),
  18. $edittimestamp = $timestampdiv.siblings( 'a.edit-timestamp' );
  19. /**
  20. * Adds event that opens the time stamp form if the form is hidden.
  21. *
  22. * @listens $edittimestamp:click
  23. *
  24. * @param {Event} event The event object.
  25. * @return {void}
  26. */
  27. $edittimestamp.on( 'click', function( event ) {
  28. if ( $timestampdiv.is( ':hidden' ) ) {
  29. // Slide down the form and set focus on the first field.
  30. $timestampdiv.slideDown( 'fast', function() {
  31. $( 'input, select', $timestampwrap ).first().trigger( 'focus' );
  32. } );
  33. $(this).hide();
  34. }
  35. event.preventDefault();
  36. });
  37. /**
  38. * Resets the time stamp values when the cancel button is clicked.
  39. *
  40. * @listens .cancel-timestamp:click
  41. *
  42. * @param {Event} event The event object.
  43. * @return {void}
  44. */
  45. $timestampdiv.find('.cancel-timestamp').on( 'click', function( event ) {
  46. // Move focus back to the Edit link.
  47. $edittimestamp.show().trigger( 'focus' );
  48. $timestampdiv.slideUp( 'fast' );
  49. $('#mm').val($('#hidden_mm').val());
  50. $('#jj').val($('#hidden_jj').val());
  51. $('#aa').val($('#hidden_aa').val());
  52. $('#hh').val($('#hidden_hh').val());
  53. $('#mn').val($('#hidden_mn').val());
  54. $timestamp.html( stamp );
  55. event.preventDefault();
  56. });
  57. /**
  58. * Sets the time stamp values when the ok button is clicked.
  59. *
  60. * @listens .save-timestamp:click
  61. *
  62. * @param {Event} event The event object.
  63. * @return {void}
  64. */
  65. $timestampdiv.find('.save-timestamp').on( 'click', function( event ) { // Crazyhorse - multiple OK cancels.
  66. var aa = $('#aa').val(), mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val(),
  67. newD = new Date( aa, mm - 1, jj, hh, mn );
  68. event.preventDefault();
  69. if ( newD.getFullYear() != aa || (1 + newD.getMonth()) != mm || newD.getDate() != jj || newD.getMinutes() != mn ) {
  70. $timestampwrap.addClass( 'form-invalid' );
  71. return;
  72. } else {
  73. $timestampwrap.removeClass( 'form-invalid' );
  74. }
  75. $timestamp.html(
  76. wp.i18n.__( 'Submitted on:' ) + ' <b>' +
  77. /* translators: 1: Month, 2: Day, 3: Year, 4: Hour, 5: Minute. */
  78. wp.i18n.__( '%1$s %2$s, %3$s at %4$s:%5$s' )
  79. .replace( '%1$s', $( 'option[value="' + mm + '"]', '#mm' ).attr( 'data-text' ) )
  80. .replace( '%2$s', parseInt( jj, 10 ) )
  81. .replace( '%3$s', aa )
  82. .replace( '%4$s', ( '00' + hh ).slice( -2 ) )
  83. .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) +
  84. '</b> '
  85. );
  86. // Move focus back to the Edit link.
  87. $edittimestamp.show().trigger( 'focus' );
  88. $timestampdiv.slideUp( 'fast' );
  89. });
  90. });