PageRenderTime 28ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/dashboard.js

https://gitlab.com/webkod3r/tripolis
JavaScript | 189 lines | 138 code | 34 blank | 17 comment | 15 complexity | e6aee15ae7cbc39c41e0caa2e93ef635 MD5 | raw file
  1. /* global pagenow, ajaxurl, postboxes, wpActiveEditor:true */
  2. var ajaxWidgets, ajaxPopulateWidgets, quickPressLoad;
  3. jQuery(document).ready( function($) {
  4. var welcomePanel = $( '#welcome-panel' ),
  5. welcomePanelHide = $('#wp_welcome_panel-hide'),
  6. updateWelcomePanel;
  7. updateWelcomePanel = function( visible ) {
  8. $.post( ajaxurl, {
  9. action: 'update-welcome-panel',
  10. visible: visible,
  11. welcomepanelnonce: $( '#welcomepanelnonce' ).val()
  12. });
  13. };
  14. if ( welcomePanel.hasClass('hidden') && welcomePanelHide.prop('checked') ) {
  15. welcomePanel.removeClass('hidden');
  16. }
  17. $('.welcome-panel-close, .welcome-panel-dismiss a', welcomePanel).click( function(e) {
  18. e.preventDefault();
  19. welcomePanel.addClass('hidden');
  20. updateWelcomePanel( 0 );
  21. $('#wp_welcome_panel-hide').prop('checked', false);
  22. });
  23. welcomePanelHide.click( function() {
  24. welcomePanel.toggleClass('hidden', ! this.checked );
  25. updateWelcomePanel( this.checked ? 1 : 0 );
  26. });
  27. // These widgets are sometimes populated via ajax
  28. ajaxWidgets = ['dashboard_primary'];
  29. ajaxPopulateWidgets = function(el) {
  30. function show(i, id) {
  31. var p, e = $('#' + id + ' div.inside:visible').find('.widget-loading');
  32. if ( e.length ) {
  33. p = e.parent();
  34. setTimeout( function(){
  35. p.load( ajaxurl + '?action=dashboard-widgets&widget=' + id + '&pagenow=' + pagenow, '', function() {
  36. p.hide().slideDown('normal', function(){
  37. $(this).css('display', '');
  38. });
  39. });
  40. }, i * 500 );
  41. }
  42. }
  43. if ( el ) {
  44. el = el.toString();
  45. if ( $.inArray(el, ajaxWidgets) !== -1 ) {
  46. show(0, el);
  47. }
  48. } else {
  49. $.each( ajaxWidgets, show );
  50. }
  51. };
  52. ajaxPopulateWidgets();
  53. postboxes.add_postbox_toggles(pagenow, { pbshow: ajaxPopulateWidgets } );
  54. /* QuickPress */
  55. quickPressLoad = function() {
  56. var act = $('#quickpost-action'), t;
  57. $( '#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]' ).prop( 'disabled' , false );
  58. t = $('#quick-press').submit( function( e ) {
  59. e.preventDefault();
  60. $('#dashboard_quick_press #publishing-action .spinner').show();
  61. $('#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]').prop('disabled', true);
  62. $.post( t.attr( 'action' ), t.serializeArray(), function( data ) {
  63. // Replace the form, and prepend the published post.
  64. $('#dashboard_quick_press .inside').html( data );
  65. $('#quick-press').removeClass('initial-form');
  66. quickPressLoad();
  67. highlightLatestPost();
  68. $('#title').focus();
  69. });
  70. function highlightLatestPost () {
  71. var latestPost = $('.drafts ul li').first();
  72. latestPost.css('background', '#fffbe5');
  73. setTimeout(function () {
  74. latestPost.css('background', 'none');
  75. }, 1000);
  76. }
  77. } );
  78. $('#publish').click( function() { act.val( 'post-quickpress-publish' ); } );
  79. $('#title, #tags-input, #content').each( function() {
  80. var input = $(this), prompt = $('#' + this.id + '-prompt-text');
  81. if ( '' === this.value ) {
  82. prompt.removeClass('screen-reader-text');
  83. }
  84. prompt.click( function() {
  85. $(this).addClass('screen-reader-text');
  86. input.focus();
  87. });
  88. input.blur( function() {
  89. if ( '' === this.value ) {
  90. prompt.removeClass('screen-reader-text');
  91. }
  92. });
  93. input.focus( function() {
  94. prompt.addClass('screen-reader-text');
  95. });
  96. });
  97. $('#quick-press').on( 'click focusin', function() {
  98. wpActiveEditor = 'content';
  99. });
  100. autoResizeTextarea();
  101. };
  102. quickPressLoad();
  103. $( '.meta-box-sortables' ).sortable( 'option', 'containment', '#wpwrap' );
  104. function autoResizeTextarea() {
  105. if ( document.documentMode && document.documentMode < 9 ) {
  106. return;
  107. }
  108. // Add a hidden div. We'll copy over the text from the textarea to measure its height.
  109. $('body').append( '<div class="quick-draft-textarea-clone" style="display: none;"></div>' );
  110. var clone = $('.quick-draft-textarea-clone'),
  111. editor = $('#content'),
  112. editorHeight = editor.height(),
  113. // 100px roughly accounts for browser chrome and allows the
  114. // save draft button to show on-screen at the same time.
  115. editorMaxHeight = $(window).height() - 100;
  116. // Match up textarea and clone div as much as possible.
  117. // Padding cannot be reliably retrieved using shorthand in all browsers.
  118. clone.css({
  119. 'font-family': editor.css('font-family'),
  120. 'font-size': editor.css('font-size'),
  121. 'line-height': editor.css('line-height'),
  122. 'padding-bottom': editor.css('paddingBottom'),
  123. 'padding-left': editor.css('paddingLeft'),
  124. 'padding-right': editor.css('paddingRight'),
  125. 'padding-top': editor.css('paddingTop'),
  126. 'white-space': 'pre-wrap',
  127. 'word-wrap': 'break-word',
  128. 'display': 'none'
  129. });
  130. // propertychange is for IE < 9
  131. editor.on('focus input propertychange', function() {
  132. var $this = $(this),
  133. // &nbsp; is to ensure that the height of a final trailing newline is included.
  134. textareaContent = $this.val() + '&nbsp;',
  135. // 2px is for border-top & border-bottom
  136. cloneHeight = clone.css('width', $this.css('width')).text(textareaContent).outerHeight() + 2;
  137. // Default to having scrollbars
  138. editor.css('overflow-y', 'auto');
  139. // Only change the height if it has indeed changed and both heights are below the max.
  140. if ( cloneHeight === editorHeight || ( cloneHeight >= editorMaxHeight && editorHeight >= editorMaxHeight ) ) {
  141. return;
  142. }
  143. // Don't allow editor to exceed height of window.
  144. // This is also bound in CSS to a max-height of 1300px to be extra safe.
  145. if ( cloneHeight > editorMaxHeight ) {
  146. editorHeight = editorMaxHeight;
  147. } else {
  148. editorHeight = cloneHeight;
  149. }
  150. // No scrollbars as we change height, not for IE < 9
  151. editor.css('overflow', 'hidden');
  152. $this.css('height', editorHeight + 'px');
  153. });
  154. }
  155. } );