[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/js/ -> editor.js (source)

   1  $gp.editor = function($){ return {
   2      current: null,
   3      init: function(table) {
   4          $gp.init();
   5          $gp.editor.table = table;
   6          $gp.editor.install_hooks();
   7      },
   8      original_id_from_row_id: function(row_id) {
   9          return row_id.split('-')[0];
  10      },
  11      translation_id_from_row_id: function(row_id) {
  12          return row_id.split('-')[1];
  13      },
  14      show: function(element) {
  15          var row_id = element.closest('tr').attr('row');
  16          var editor = $('#editor-' + row_id);
  17          if (!editor.length) return;
  18          if ($gp.editor.current) $gp.editor.hide();
  19          editor.preview = $('#preview-' + row_id);
  20          editor.row_id = row_id;
  21          editor.original_id = $gp.editor.original_id_from_row_id(row_id);
  22          editor.translation_id = $gp.editor.translation_id_from_row_id(row_id);
  23          $gp.editor.current = editor;
  24          editor.show();
  25          editor.preview.hide();
  26          $('tr:first', $gp.editor.table).hide();
  27          $('textarea:first', editor).focus();
  28      },
  29      next: function() {
  30          if (!$gp.editor.current) return;
  31          //TODO: go to next page if needed
  32          var next = $gp.editor.current.nextAll('tr.editor');
  33          if (next.length)
  34              $gp.editor.show(next.eq(0));
  35          else
  36              $gp.editor.hide();
  37      },
  38      hide: function(editor) {
  39          editor = editor? editor : $gp.editor.current;
  40          if (!editor) return;
  41          editor.hide();
  42          editor.preview.show();
  43          $('tr:first', $gp.editor.table).show();
  44          $gp.editor.current = null;
  45      },
  46      install_hooks: function() {
  47          $($gp.editor.table).on('click', 'a.edit', $gp.editor.hooks.show)
  48              .on('dblclick', 'tr.preview td', $gp.editor.hooks.show)
  49              .on('change', 'select.priority', $gp.editor.hooks.set_priority)
  50              .on('click', 'a.close', $gp.editor.hooks.hide)
  51              .on('click', 'a.copy', $gp.editor.hooks.copy)
  52              .on('click', 'a.gtranslate', $gp.editor.hooks.google_translate)
  53              .on('click', 'a.discard-warning', $gp.editor.hooks.discard_warning)
  54              .on('click', 'button.approve', $gp.editor.hooks.set_status_current)
  55              .on('click', 'button.reject', $gp.editor.hooks.set_status_rejected)
  56              .on('click', 'button.ok', $gp.editor.hooks.ok);
  57      },
  58      replace_current: function(html) {
  59          if (!$gp.editor.current) return;
  60          $gp.editor.current.after(html);
  61          var old_current = $gp.editor.current;
  62          $gp.editor.next();
  63          old_current.preview.remove();
  64          old_current.remove();
  65          $gp.editor.current.preview.fadeIn(800);
  66      },
  67      save: function(button) {
  68          if (!$gp.editor.current) return;        
  69          var editor = $gp.editor.current;
  70          button.prop('disabled', true);
  71          $gp.notices.notice('Saving…');
  72          name = "translation["+editor.original_id+"][]";
  73          data = $("textarea[name='"+name+"']", editor).map(function() {
  74              return name+'='+encodeURIComponent($(this).val());
  75          }).get().join('&');
  76          $.ajax({type: "POST", url: $gp_editor_options.url, data: data, dataType: 'json',
  77              success: function(data){
  78                  button.prop('disabled', false);
  79                  $gp.notices.success('Saved!');
  80                  for(original_id in data) {
  81                      $gp.editor.replace_current(data[original_id]);
  82                  }
  83                  if ($gp.editor.current.hasClass('no-warnings')) {
  84                      $gp.editor.next();
  85                  } else {
  86                      $gp.editor.current.preview.hide();
  87                  }
  88              },
  89              error: function(xhr, msg, error) {
  90                  button.prop('disabled', false);
  91                  msg = xhr.responseText? 'Error: '+ xhr.responseText : 'Error saving the translation!';
  92                  $gp.notices.error(msg);
  93              }
  94          });
  95      },
  96      set_priority: function(select) {
  97          if (!$gp.editor.current) return;
  98          var editor = $gp.editor.current;
  99          select.prop('disabled', true);
 100          $gp.notices.notice('Setting priority…');
 101          data = {priority: $('option:selected', select).attr('value')};
 102          $.ajax({type: "POST", url: $gp_editor_options.set_priority_url.replace('%original-id%', editor.original_id), data: data,
 103              success: function(data){
 104                  select.prop('disabled', false);
 105                  $gp.notices.success('Priority set!');
 106                  var new_priority_class = 'priority-'+$('option:selected', select).text();
 107                  $gp.editor.current.addClass(new_priority_class);
 108                  $gp.editor.current.preview.addClass(new_priority_class);
 109              },
 110              error: function(xhr, msg, error) {
 111                  msg = xhr.responseText? 'Error: '+ xhr.responseText : 'Error setting the priority!';
 112                  $gp.notices.error(msg);
 113              }
 114          });
 115          
 116      },
 117      set_status: function(button, status) {
 118          if (!$gp.editor.current || !$gp.editor.current.translation_id) return;
 119          var editor = $gp.editor.current;
 120          button.prop('disabled', true);
 121          $gp.notices.notice('Setting status to “'+status+'”…');
 122          var data = {translation_id: editor.translation_id, status: status};
 123          $.ajax({type: "POST", url: $gp_editor_options.set_status_url, data: data,
 124              success: function(data){
 125                  button.prop('disabled', false);
 126                  $gp.notices.success('Status set!');
 127                  $gp.editor.replace_current(data);
 128                  $gp.editor.next();
 129              },
 130              error: function(xhr, msg, error) {
 131                  msg = xhr.responseText? 'Error: '+ xhr.responseText : 'Error setting the status!';
 132                  $gp.notices.error(msg);
 133              }
 134          });
 135      },
 136      discard_warning: function(link) {
 137          if (!$gp.editor.current) return;
 138          $gp.notices.notice('Discarding…');
 139          data = {translation_id: $gp.editor.current.translation_id, key: link.attr('key'), index: link.attr('index')};
 140          $.ajax({type: "POST", url: $gp_editor_options.discard_warning_url, data: data,
 141              success: function(data) {
 142                  $gp.notices.success('Saved!');
 143                  $gp.editor.replace_current(data);
 144              },
 145              error: function(xhr, msg, error) {
 146                  msg = xhr.responseText? 'Error: '+ xhr.responseText : 'Error saving the translation!';
 147                  $gp.notices.error(msg);
 148              }
 149          });
 150      },
 151      copy: function(link) {
 152          original_text = link.parents('.textareas').prev();
 153          if ( ! original_text.hasClass('original') ) original_text = original_text.find('.original');
 154          original_text = original_text.text();
 155          original_text = original_text.replace(/<span class=.invisibles.*?<\/span>/g, '');
 156          a = link.parents('.textareas').find('textarea').val(original_text).focus();
 157      },
 158      google_translate: function(link) {
 159          original_text = link.parents('.textareas').siblings('.original').html();
 160          if (!original_text) original_text = link.parents('.textareas').siblings('p:last').children('.original').html();
 161          if (!original_text) return;
 162          if (typeof google == 'undefined') {
 163              $gp.notices.error('Couldn&#8217;t load Google Translate library!');
 164              return;
 165          }
 166          $gp.notices.notice('Translating via Google Translate&hellip;');        
 167          google.language.translate({text: original_text, type: 'html'}, 'en', $gp_editor_options.google_translate_language, function(result) {
 168              if (!result.error) {
 169                  // fix common google translate misbehaviours
 170                  result.translation = result.translation.replace(/% (s|d)/gi, function(m, letter) {
 171                      return '%'+letter.toLowerCase();
 172                  });
 173                  result.translation = result.translation.replace(/% (\d+) \$ (S|D)/gi, function(m, number, letter) {
 174                      return '%'+number+'$'+letter.toLowerCase();
 175                  });
 176                  result.translation = result.translation.replace(/&lt;\/\s+([A-Z]+)&gt;/g, function(m, tag) {
 177                      return '&lt;/'+tag.toLowerCase()+'&gt;';
 178                  });
 179  
 180                  link.parent('p').siblings('textarea').html(result.translation).focus();
 181                  $gp.notices.success('Translated!');
 182              } else {
 183                  $gp.notices.error('Error in translating via Google Translate: '+result.message+'!');
 184                  link.parent('p').siblings('textarea').focus();
 185              }
 186          });        
 187      },
 188      hooks: {
 189          show: function() {
 190              $gp.editor.show($(this));
 191              return false;
 192          },
 193          hide: function() {
 194              $gp.editor.hide();
 195              return false;
 196          },
 197          ok: function() {
 198              $gp.editor.save($(this));
 199              return false;
 200          },
 201          copy: function() {
 202              $gp.editor.copy($(this));
 203              return false;
 204          },
 205          google_translate: function() {
 206              $gp.editor.google_translate($(this));
 207              return false;
 208          },
 209          discard_warning: function() {
 210              $gp.editor.discard_warning($(this));
 211              return false;
 212          },
 213          set_status_current: function() {
 214              $gp.editor.set_status($(this), 'current');
 215              return false;
 216          },
 217          set_status_rejected: function() {
 218              $gp.editor.set_status($(this), 'rejected');
 219              return false;
 220          },
 221          set_priority: function() {
 222              $gp.editor.set_priority($(this));
 223              return false;
 224          }
 225      }
 226  }}(jQuery);
 227  
 228  if (typeof google != 'undefined') google.load("language", "1");
 229  
 230  jQuery(function($) {
 231      $gp.editor.init($('#translations'));
 232  });


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.