Changeset 12713
- Timestamp:
- 07/05/2023 10:45:32 AM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/js/editor.js
r12673 r12713 1 /* global $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, $gp_editor_options, fetch, TextDecoderStream, URL, URLSearchParams, window */1 /* global document, $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, $gp_editor_options, fetch, TextDecoderStream, URL, URLSearchParams, window */ 2 2 /* eslint camelcase: "off" */ 3 3 jQuery( function( $ ) { 4 /** 5 * Stores (caches) the content of the translation helpers, to avoid making the query another time. 6 * 7 * @type {Array} 8 */ 9 // eslint-disable-next-line prefer-const 10 let translationHelpersCache = []; 4 11 let focusedRowId = ''; 5 12 // When a user clicks on a sidebar tab, the visible tab and div changes. … … 20 27 const rowId = tr.attr( 'row' ); 21 28 const translation_status = tr.find( '.panel-header' ).find( 'span' ).html(); 29 const nextEditor = $gp.editor.current.nextAll( 'tr.editor' ).first(); 22 30 const chatgpt_review_status = JSON.parse( window.localStorage.getItem( 'translate-details-state' ) ); 23 31 const chatgpt_review_enabled = ( chatgpt_review_status && 'open' === chatgpt_review_status[ 'details-chatgpt' ] ) || ! chatgpt_review_status; … … 28 36 focusedRowId = rowId; 29 37 loadTabsAndDivs( tr ); 38 39 if ( nextEditor.length ) { 40 cacheTranslationHelpersForARow( nextEditor ); 41 } 42 30 43 if ( chatgpt_review_enabled && $gp_comment_feedback_settings.openai_key && $gp_editor_options.can_approve && ( 'waiting' === translation_status || 'fuzzy' === translation_status ) ) { 31 44 fetchOpenAIReviewResponse( rowId, tr, false ); 32 45 } else { 33 if ( ! $gp_comment_feedback_settings.openai_key ) { 34 tr.find( '.details-chatgpt' ).hide(); 35 } 36 tr.find( '.openai-review' ).hide(); 46 tr.find( '.details-chatgpt, .openai-review' ).hide(); 37 47 } 38 48 } ); … … 194 204 } 195 205 206 $( document ).ready( function() { 207 // Gets the translation helpers for the first row and caches them. 208 const firstEditor = $( '#translations' ).find( 'tr.editor' ).first(); 209 cacheTranslationHelpersForARow( firstEditor ); 210 } ); 211 196 212 /** 197 213 * Hides all tabs and show one of them, the last clicked. … … 237 253 238 254 /** 239 * Load the content in the tabs (header tab and content) for the opened row.255 * Loads the content in the tabs (header tab and content) for the opened row. 240 256 * 241 257 * @param {Object} element The element that triggers the action. 242 258 */ 243 259 function loadTabsAndDivs( element ) { 244 const originalId = element.closest( 'tr' ).attr( 'id' ).substring( 7 ); 245 const requestUrl = $gp_translation_helpers_editor.translation_helper_url + originalId + '?nohc'; 246 $.getJSON( requestUrl, function( data ) { 260 const rowId = element.closest( 'tr.editor' ).attr( 'id' ).substring( 7 ); 261 const requestUrl = $gp_translation_helpers_editor.translation_helper_url + rowId + '?nohc'; 262 if ( translationHelpersCache[ rowId ] !== undefined ) { 263 updateDataInTabs( translationHelpersCache[ rowId ], rowId ); 264 } else { 265 $.getJSON( requestUrl, function( data ) { 266 translationHelpersCache[ rowId ] = data; 267 updateDataInTabs( data, rowId ); 268 } ); 269 } 270 } 271 272 /** 273 * Updates the content of the tabs and divs. 274 * 275 * @param {Object} data The content to update the tabs and divs. 276 * @param {number} originalId The id of the original string to translate. 277 * 278 * @return {void} 279 */ 280 function updateDataInTabs( data, originalId ) { 281 if ( data[ 'helper-translation-discussion-' + originalId ] !== undefined ) { 247 282 $( '[data-tab="sidebar-tab-discussion-' + originalId + '"]' ).html( 'Discussion (' + data[ 'helper-translation-discussion-' + originalId ].count + ')' ); 248 283 $( '#sidebar-div-discussion-' + originalId ).html( data[ 'helper-translation-discussion-' + originalId ].content ); 284 } 285 if ( data[ 'helper-history-' + originalId ] !== undefined ) { 249 286 $( '[data-tab="sidebar-tab-history-' + originalId + '"]' ).html( 'History (' + data[ 'helper-history-' + originalId ].count + ')' ); 250 287 $( '#sidebar-div-history-' + originalId ).html( data[ 'helper-history-' + originalId ].content ); 288 } 289 if ( data[ 'helper-other-locales-' + originalId ] !== undefined ) { 251 290 $( '[data-tab="sidebar-tab-other-locales-' + originalId + '"]' ).html( 'Other locales (' + data[ 'helper-other-locales-' + originalId ].count + ')' ); 252 291 $( '#sidebar-div-other-locales-' + originalId ).html( data[ 'helper-other-locales-' + originalId ].content ); 253 292 add_copy_button( '#sidebar-div-other-locales-' + originalId ); 254 } ); 293 } 294 } 295 296 /** 297 * Caches the translation helpers for a row. 298 * 299 * @param {Object} editor The editor row. 300 * 301 * @return {void} 302 */ 303 function cacheTranslationHelpersForARow( editor ) { 304 const rowId = editor.attr( 'row' ); 305 const requestUrl = $gp_translation_helpers_editor.translation_helper_url + rowId + '?nohc'; 306 if ( ! rowId ) { 307 return; 308 } 309 310 if ( translationHelpersCache[ rowId ] === undefined ) { 311 // Store a string with a space to avoid making the same request another time. 312 translationHelpersCache[ rowId ] = ' '; 313 $.getJSON( requestUrl, function( data ) { 314 translationHelpersCache[ rowId ] = data; 315 updateDataInTabs( data, rowId ); 316 } ); 317 } 255 318 } 256 319
Note: See TracChangeset
for help on using the changeset viewer.