/* global $gp_editor_options, $gp */ /* eslint camelcase: "off" */ $gp.editor = ( function( $ ) { return { current: null, orginal_translations: null, init: function( table ) { var $previewRows; $gp.init(); $gp.editor.table = table; $gp.editor.install_hooks(); // Open the first editor if the current table has only one. $previewRows = $gp.editor.table.find( 'tr.preview' ); if ( 1 === $previewRows.length ) { $gp.editor.show( $previewRows.eq( 0 ) ); } }, original_id_from_row_id: function( row_id ) { return row_id.split( '-' )[ 0 ]; }, translation_id_from_row_id: function( row_id ) { return row_id.split( '-' )[ 1 ]; }, show: function( element ) { var row_id = element.closest( 'tr' ).attr( 'row' ); var editor = $( '#editor-' + row_id ); var gmt_date_added = $( '#gmt-date-added-' + row_id ); var local_date_added = $( '#local-date-added-' + row_id ); var offset = new Date().getTimezoneOffset(); var gmt_date = new Date( gmt_date_added.text() ); var local_date = new Date( ( gmt_date - ( offset * 60 * 1000 ) ) ); if ( ! editor.length ) { return; } if ( $gp.editor.current ) { $gp.editor.hide(); } editor.preview = $( '#preview-' + row_id ); editor.row_id = row_id; editor.original_id = $gp.editor.original_id_from_row_id( row_id ); editor.translation_id = $gp.editor.translation_id_from_row_id( row_id ); editor.orginal_translations = $( 'textarea[name="translation[' + editor.original_id + '][]"]', editor ).map( function() { return this.value; } ).get(); $gp.editor.current = editor; local_date_added.text( local_date.toLocaleDateString() + ' ' + local_date.toLocaleTimeString() ); editor.show(); editor.preview.hide(); $( 'textarea:first', editor ).focus(); }, prev: function() { var prev; if ( ! $gp.editor.current ) { return; } // TODO: go to previous page if needed prev = $gp.editor.current.prevAll( 'tr.editor' ); if ( prev.length ) { $gp.editor.show( prev.eq( 0 ) ); } else { $gp.editor.hide(); } }, next: function() { var next; if ( ! $gp.editor.current ) { return; } // TODO: go to next page if needed. next = $gp.editor.current.nextAll( 'tr.editor' ); if ( next.length ) { $gp.editor.show( next.eq( 0 ) ); } else { $gp.editor.hide(); } }, hide: function( editor ) { editor = editor ? editor : $gp.editor.current; if ( ! editor ) { return; } editor.hide(); editor.preview.show(); $gp.editor.current = null; }, install_hooks: function() { $( $gp.editor.table ) .on( 'click', 'a.edit', $gp.editor.hooks.show ) .on( 'dblclick', 'tr.preview td', $gp.editor.hooks.show ) .on( 'change', 'select.priority', $gp.editor.hooks.set_priority ) .on( 'click', 'button.close', $gp.editor.hooks.cancel ) .on( 'click', 'a.discard-warning', $gp.editor.hooks.discard_warning ) .on( 'click', 'button.copy', $gp.editor.hooks.copy ) .on( 'click', 'button.inserttab', $gp.editor.hooks.tab ) .on( 'click', 'button.insertnl', $gp.editor.hooks.newline ) .on( 'click', 'button.approve', $gp.editor.hooks.set_status_current ) .on( 'click', 'button.reject', $gp.editor.hooks.set_status_rejected ) .on( 'click', 'button.fuzzy', $gp.editor.hooks.set_status_fuzzy ) .on( 'click', 'button.ok', $gp.editor.hooks.ok ) .on( 'keydown', 'tr.editor textarea', $gp.editor.hooks.keydown ); $( '#translations' ).tooltip( { items: '.glossary-word', content: function() { var content = $( '