[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/assets/js/ -> glossary.js (source)

   1  /* global $gp_glossary_options, $gp, confirm */
   2  /* eslint camelcase: "off", no-alert: "off" */
   3  $gp.glossary = (
   4      function( $ ) {
   5          return {
   6  
   7              current: null,
   8  
   9              init: function( table ) {
  10                  $gp.init();
  11                  if ( '' === $gp_glossary_options.can_edit ) {
  12                      return;
  13                  }
  14                  $gp.glossary.table = table;
  15                  $gp.glossary.install_hooks();
  16              },
  17  
  18              install_hooks: function() {
  19                  $( $gp.glossary.table )
  20                      .on( 'click', 'a.edit', $gp.glossary.hooks.show )
  21                      .on( 'dblclick', 'tr td', $gp.glossary.hooks.show )
  22                      .on( 'click', 'button.cancel', $gp.glossary.hooks.hide )
  23                      .on( 'click', 'button.delete', $gp.glossary.hooks.del )
  24                      .on( 'click', 'button.save', $gp.glossary.hooks.ok );
  25              },
  26  
  27              show: function( event, element ) {
  28                  var preview, row_id, editor;
  29  
  30                  event.preventDefault();
  31  
  32                  preview = element.closest( 'tr' );
  33                  row_id = preview.data( 'id' );
  34                  editor = $( '#editor-' + row_id );
  35                  if ( ! editor.length ) {
  36                      return;
  37                  }
  38                  if ( $gp.glossary.current ) {
  39                      $gp.glossary.hide();
  40                  }
  41                  editor.preview = preview;
  42                  editor.row_id = row_id;
  43                  $gp.glossary.current = editor;
  44                  editor.addClass( 'active' );
  45                  editor.show();
  46                  editor.preview.hide();
  47                  if ( $( 'a.add-entry' ).hasClass( 'open' ) ) {
  48                      $( 'a.add-entry' ).click();
  49                  }
  50                  $( 'input:first', editor ).focus();
  51              },
  52  
  53              hide: function( editor ) {
  54                  editor = editor ? editor : $gp.glossary.current;
  55                  if ( ! editor ) {
  56                      return;
  57                  }
  58                  editor.hide();
  59                  editor.preview.show();
  60                  editor.removeClass( 'active' );
  61                  $gp.glossary.current = null;
  62              },
  63  
  64              save: function( button ) {
  65                  var editor, data;
  66  
  67                  if ( ! $gp.glossary.current ) {
  68                      return;
  69                  }
  70  
  71                  button.prop( 'disabled', true );
  72                  $gp.notices.notice( 'Saving…' );
  73  
  74                  editor = $gp.glossary.current;
  75  
  76                  data = {
  77                      _gp_route_nonce: button.data( 'nonce' ),
  78                  };
  79  
  80                  $( '#editor-' + editor.row_id ).find( 'input, select, textarea' ).each( function() {
  81                      data[ $( this ).attr( 'name' ) ] = this.value;
  82                  } );
  83  
  84                  $.ajax( {
  85                      type: 'POST',
  86                      url: $gp_glossary_options.url,
  87                      data: data,
  88                      dataType: 'json',
  89                      success: function( response ) {
  90                          button.prop( 'disabled', false );
  91                          $gp.notices.success( 'Saved!' );
  92                          $gp.glossary.replace_current( response );
  93                      },
  94                      error: function( xhr, msg ) {
  95                          button.prop( 'disabled', false );
  96                          msg = xhr.responseText ? 'Error: ' + xhr.responseText : 'Error saving the glossary item!';
  97                          $gp.notices.error( msg );
  98                      },
  99                  } );
 100              },
 101  
 102              del: function( event, button ) {
 103                  var result, editor, data, preview;
 104  
 105                  event.preventDefault();
 106  
 107                  result = confirm( $gp_glossary_options.ge_delete_ays );
 108                  if ( ! result ) {
 109                      return;
 110                  }
 111                  editor = button.closest( 'tr' );
 112                  preview = editor.prev( 'tr' );
 113  
 114                  data = {
 115                      _gp_route_nonce: button.data( 'nonce' ),
 116                  };
 117  
 118                  editor.find( 'input, select, textarea' ).each( function() {
 119                      data[ $( this ).attr( 'name' ) ] = this.value;
 120                  } );
 121  
 122                  $.ajax( {
 123                      type: 'POST',
 124                      url: $gp_glossary_options.delete_url,
 125                      data: data,
 126                      success: function() {
 127                          $gp.notices.success( 'Deleted!' );
 128                          editor.fadeOut( 'fast', function() {
 129                              this.remove();
 130                          } );
 131                          preview.remove();
 132                          if ( 1 === $( 'tr', $gp.glossary.table ).length ) {
 133                              $gp.glossary.table.remove();
 134                          }
 135                      },
 136                      error: function( xhr, msg ) {
 137                          msg = xhr.responseText ? 'Error: ' + xhr.responseText : 'Error deleting the glossary item!';
 138                          $gp.notices.error( msg );
 139                      },
 140                  } );
 141              },
 142  
 143              replace_current: function( html ) {
 144                  var old_current;
 145  
 146                  if ( ! $gp.glossary.current ) {
 147                      return;
 148                  }
 149  
 150                  $gp.glossary.current.after( html );
 151                  old_current = $gp.glossary.current;
 152                  old_current.preview.remove();
 153                  old_current.remove();
 154                  $gp.glossary.current.preview.fadeIn( 800 );
 155              },
 156  
 157              hooks: {
 158                  show: function( event ) {
 159                      $gp.glossary.show( event, $( this ) );
 160                  },
 161                  del: function( event ) {
 162                      $gp.glossary.del( event, $( this ) );
 163                  },
 164                  hide: function() {
 165                      $gp.glossary.hide();
 166                      return false;
 167                  },
 168                  ok: function() {
 169                      $gp.glossary.save( $( this ) );
 170                      return false;
 171                  },
 172              },
 173  
 174          };
 175      }( jQuery )
 176  );
 177  
 178  jQuery( function( $ ) {
 179      $gp.glossary.init( $( '#glossary' ) );
 180  } );


Generated: Tue May 28 01:01:19 2024 Cross-referenced by PHPXref 0.7.1