comment.js 2.7 KB

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