Changeset 34517
- Timestamp:
- 09/24/2015 08:15:54 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/js/edit-comments.js
r34506 r34517 4 4 (function($) { 5 5 var getCount, updateCount, updateCountText, updatePending, updateApproved, 6 updateHtmlTitle, updateDashboardText, adminTitle = document.title; 6 updateHtmlTitle, updateDashboardText, adminTitle = document.title, 7 isDashboard = $('#dashboard_right_now').length, 8 titleDiv, titleRegEx; 9 10 getCount = function(el) { 11 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 ); 12 if ( isNaN(n) ) { 13 return 0; 14 } 15 return n; 16 }; 17 18 updateCount = function(el, n) { 19 var n1 = ''; 20 if ( isNaN(n) ) { 21 return; 22 } 23 n = n < 1 ? '0' : n.toString(); 24 if ( n.length > 3 ) { 25 while ( n.length > 3 ) { 26 n1 = thousandsSeparator + n.substr(n.length - 3) + n1; 27 n = n.substr(0, n.length - 3); 28 } 29 n = n + n1; 30 } 31 el.html(n); 32 }; 33 34 updateApproved = function( diff, commentPostId ) { 35 var postSelector = '.post-com-count-' + commentPostId, 36 noClass = 'comment-count-no-comments', 37 approvedClass = 'comment-count-approved', 38 approved, 39 noComments; 40 41 updateCountText( 'span.approved-count', diff ); 42 43 if ( ! commentPostId ) { 44 return; 45 } 46 47 // cache selectors to not get dupes 48 approved = $( 'span.' + approvedClass, postSelector ); 49 noComments = $( 'span.' + noClass, postSelector ); 50 51 approved.each(function() { 52 var a = $(this), n = getCount(a) + diff; 53 if ( n < 1 ) 54 n = 0; 55 56 if ( 0 === n ) { 57 a.removeClass( approvedClass ).addClass( noClass ); 58 } else { 59 a.addClass( approvedClass ).removeClass( noClass ); 60 } 61 updateCount( a, n ); 62 }); 63 64 noComments.each(function() { 65 var a = $(this); 66 if ( diff > 0 ) { 67 a.removeClass( noClass ).addClass( approvedClass ); 68 } else { 69 a.addClass( noClass ).removeClass( approvedClass ); 70 } 71 updateCount( a, diff ); 72 }); 73 }; 74 75 updateCountText = function( selector, diff ) { 76 $( selector ).each(function() { 77 var a = $(this), n = getCount(a) + diff; 78 if ( n < 1 ) { 79 n = 0; 80 } 81 updateCount( a, n ); 82 }); 83 }; 84 85 updateDashboardText = function ( response ) { 86 if ( ! isDashboard || ! response || ! response.i18n_comments_text ) { 87 return; 88 } 89 90 var rightNow = $( '#dashboard_right_now' ); 91 92 $( '.comment-count a', rightNow ).text( response.i18n_comments_text ); 93 $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text ) 94 .parent() 95 [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' ); 96 }; 97 98 updateHtmlTitle = function ( diff ) { 99 var newTitle, regExMatch, titleCount, commentFrag; 100 101 titleRegEx = titleRegEx || new RegExp( 'Comments (\\([0-9' + thousandsSeparator + ']+\\))?' ); 102 // count funcs operate on a $'d element 103 titleDiv = titleDiv || $( '<div />' ); 104 newTitle = adminTitle; 105 106 commentFrag = titleRegEx.exec( document.title ); 107 if ( commentFrag ) { 108 commentFrag = commentFrag[0]; 109 titleDiv.html( commentFrag ); 110 titleCount = getCount( titleDiv ) + diff; 111 } else { 112 titleDiv.html( 0 ); 113 titleCount = diff; 114 } 115 116 if ( titleCount >= 1 ) { 117 updateCount( titleDiv, titleCount ); 118 regExMatch = titleRegEx.exec( document.title ); 119 if ( regExMatch ) { 120 newTitle = document.title.replace( regExMatch[0], 'Comments (' + titleDiv.text() + ') ' ); 121 } 122 } else { 123 regExMatch = titleRegEx.exec( newTitle ); 124 if ( regExMatch ) { 125 newTitle = newTitle.replace( regExMatch[0], 'Comments' ); 126 } 127 } 128 document.title = newTitle; 129 }; 130 131 updatePending = function( diff, commentPostId ) { 132 var postSelector = '.post-com-count-' + commentPostId, 133 noClass = 'comment-count-no-pending', 134 noParentClass = 'post-com-count-no-pending', 135 pendingClass = 'comment-count-pending', 136 pending, 137 noPending; 138 139 if ( ! isDashboard ) { 140 updateHtmlTitle( diff ); 141 } 142 143 $( 'span.pending-count' ).each(function() { 144 var a = $(this), n = getCount(a) + diff; 145 if ( n < 1 ) 146 n = 0; 147 a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0'); 148 updateCount( a, n ); 149 }); 150 151 if ( ! commentPostId ) { 152 return; 153 } 154 155 // cache selectors to not get dupes 156 pending = $( 'span.' + pendingClass, postSelector ); 157 noPending = $( 'span.' + noClass, postSelector ); 158 159 pending.each(function() { 160 var a = $(this), n = getCount(a) + diff; 161 if ( n < 1 ) 162 n = 0; 163 164 if ( 0 === n ) { 165 a.parent().addClass( noParentClass ); 166 a.removeClass( pendingClass ).addClass( noClass ); 167 } else { 168 a.parent().removeClass( noParentClass ); 169 a.addClass( pendingClass ).removeClass( noClass ); 170 } 171 updateCount( a, n ); 172 }); 173 174 noPending.each(function() { 175 var a = $(this); 176 if ( diff > 0 ) { 177 a.parent().removeClass( noParentClass ); 178 a.removeClass( noClass ).addClass( pendingClass ); 179 } else { 180 a.parent().addClass( noParentClass ); 181 a.addClass( noClass ).removeClass( pendingClass ); 182 } 183 updateCount( a, diff ); 184 }); 185 }; 7 186 8 187 setCommentsList = function() { 9 188 var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff, 10 lastConfidentTime = 0, titleDiv, titleRegEx, 11 isDashboard = $('#dashboard_right_now').length; 189 lastConfidentTime = 0; 12 190 13 191 totalInput = $('input[name="_total"]', '#comments-form'); 14 192 perPageInput = $('input[name="_per_page"]', '#comments-form'); 15 193 pageInput = $('input[name="_page"]', '#comments-form'); 194 195 // Updates the current total (stored in the _total input) 196 updateTotalCount = function( total, time, setConfidentTime ) { 197 if ( time < lastConfidentTime ) 198 return; 199 200 if ( setConfidentTime ) 201 lastConfidentTime = time; 202 203 totalInput.val( total.toString() ); 204 }; 16 205 17 206 // this fires when viewing "All" … … 107 296 108 297 return settings; 109 };110 111 // Updates the current total (stored in the _total input)112 updateTotalCount = function( total, time, setConfidentTime ) {113 if ( time < lastConfidentTime )114 return;115 116 if ( setConfidentTime )117 lastConfidentTime = time;118 119 totalInput.val( total.toString() );120 };121 122 getCount = function(el) {123 var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );124 if ( isNaN(n) )125 return 0;126 return n;127 };128 129 updateCount = function(el, n) {130 var n1 = '';131 if ( isNaN(n) )132 return;133 n = n < 1 ? '0' : n.toString();134 if ( n.length > 3 ) {135 while ( n.length > 3 ) {136 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;137 n = n.substr(0, n.length - 3);138 }139 n = n + n1;140 }141 el.html(n);142 };143 144 updateHtmlTitle = function ( diff ) {145 var newTitle, regExMatch, titleCount, commentFrag;146 147 titleRegEx = titleRegEx || new RegExp( 'Comments (\\([0-9' + thousandsSeparator + ']+\\))?' );148 // count funcs operate on a $'d element149 titleDiv = titleDiv || $( '<div />' );150 newTitle = adminTitle;151 152 commentFrag = titleRegEx.exec( document.title );153 if ( commentFrag ) {154 commentFrag = commentFrag[0];155 titleDiv.html( commentFrag );156 titleCount = getCount( titleDiv ) + diff;157 } else {158 titleDiv.html( 0 );159 titleCount = diff;160 }161 162 if ( titleCount >= 1 ) {163 updateCount( titleDiv, titleCount );164 regExMatch = titleRegEx.exec( document.title );165 if ( regExMatch ) {166 newTitle = document.title.replace( regExMatch[0], 'Comments (' + titleDiv.text() + ') ' );167 }168 } else {169 regExMatch = titleRegEx.exec( newTitle );170 if ( regExMatch ) {171 newTitle = newTitle.replace( regExMatch[0], 'Comments' );172 }173 }174 document.title = newTitle;175 };176 177 updatePending = function( diff, commentPostId ) {178 var postSelector = '.post-com-count-' + commentPostId,179 noClass = 'comment-count-no-pending',180 noParentClass = 'post-com-count-no-pending',181 pendingClass = 'comment-count-pending',182 pending,183 noPending;184 185 if ( ! isDashboard ) {186 updateHtmlTitle( diff );187 }188 189 $( 'span.pending-count' ).each(function() {190 var a = $(this), n = getCount(a) + diff;191 if ( n < 1 )192 n = 0;193 a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');194 updateCount( a, n );195 });196 197 if ( ! commentPostId ) {198 return;199 }200 201 // cache selectors to not get dupes202 pending = $( 'span.' + pendingClass, postSelector );203 noPending = $( 'span.' + noClass, postSelector );204 205 pending.each(function() {206 var a = $(this), n = getCount(a) + diff;207 if ( n < 1 )208 n = 0;209 210 if ( 0 === n ) {211 a.parent().addClass( noParentClass );212 a.removeClass( pendingClass ).addClass( noClass );213 } else {214 a.parent().removeClass( noParentClass );215 a.addClass( pendingClass ).removeClass( noClass );216 }217 updateCount( a, n );218 });219 220 noPending.each(function() {221 var a = $(this);222 if ( diff > 0 ) {223 a.parent().removeClass( noParentClass );224 a.removeClass( noClass ).addClass( pendingClass );225 } else {226 a.parent().addClass( noParentClass );227 a.addClass( noClass ).removeClass( pendingClass );228 }229 updateCount( a, diff );230 });231 };232 233 updateApproved = function( diff, commentPostId ) {234 var postSelector = '.post-com-count-' + commentPostId,235 noClass = 'comment-count-no-comments',236 approvedClass = 'comment-count-approved',237 approved,238 noComments;239 240 updateCountText( 'span.approved-count', diff );241 242 if ( ! commentPostId ) {243 return;244 }245 246 // cache selectors to not get dupes247 approved = $( 'span.' + approvedClass, postSelector );248 noComments = $( 'span.' + noClass, postSelector );249 250 approved.each(function() {251 var a = $(this), n = getCount(a) + diff;252 if ( n < 1 )253 n = 0;254 255 if ( 0 === n ) {256 a.removeClass( approvedClass ).addClass( noClass );257 } else {258 a.addClass( approvedClass ).removeClass( noClass );259 }260 updateCount( a, n );261 });262 263 noComments.each(function() {264 var a = $(this);265 if ( diff > 0 ) {266 a.removeClass( noClass ).addClass( approvedClass );267 } else {268 a.addClass( noClass ).removeClass( approvedClass );269 }270 updateCount( a, diff );271 });272 };273 274 updateCountText = function( selector, diff ) {275 $( selector ).each(function() {276 var a = $(this), n = getCount(a) + diff;277 if ( n < 1 ) {278 n = 0;279 }280 updateCount( a, n );281 });282 };283 284 updateDashboardText = function ( response ) {285 if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {286 return;287 }288 289 var rightNow = $( '#dashboard_right_now' );290 291 $( '.comment-count a', rightNow ).text( response.i18n_comments_text );292 $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )293 .parent()294 [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );295 298 }; 296 299 … … 795 798 796 799 if ( r.supplemental.i18n_comments_text ) { 797 updateDashboardText( r.supplemental ); 800 if ( isDashboard ) { 801 updateDashboardText( r.supplemental ); 802 } else { 803 updateApproved( 1, r.supplemental.parent_post_id ); 804 updateCountText( 'span.all-count', 1 ); 805 } 798 806 } 799 807
Note: See TracChangeset
for help on using the changeset viewer.