//var ngram_list_table = $('#ngram-list-table');
var ngram_list_table_body = $('#ngram-list-table-body');
var ngram_memo_table = $('#ngram-memo-table');
var ngram_details = $('#ngram-details');
var ngram_details_hanzi = $('#ngram-details-hanzi');
var ngram_add = $('#ngram-add');
var animation_memo_duration = 500;

ngram_add.on('click', function() { ngram_add.hide(); });
ngram_add.on('click', add_ngram_to_memo);

function already_in_memo(x) {
  var handle_selector = '[data-handle-id="' + x + '"]';
  return ngram_memo_table.find(handle_selector).length > 0;
}

function make_ngram_interactive(x) {
  ngram_details_hanzi.empty();
  x.make_hanzi_interactive(ngram_details_hanzi);
}

function launch_structural_query(x) {
  $.ajax({
    url: '/search/structural/' + x,
    method: 'GET'
  }).done(function(res) {
    update_hanzi_debug_table(x, res);
  })
}

function ngram_details_conditionally_show_add_button() {
  if (ngram_details_hanzi.text().trim().length > 0) {
    var handle_id = ngram_details.attr('data-handle-id');

    if (already_in_memo(handle_id)) {
      ngram_add.hide();
    } else {
      ngram_add.show();
    }
  } else {
    ngram_add.hide();
  }
}

function memo_cell_deletion() {
  return $('<td>')
           .append($('<span>')
                     .addClass('glyphicon glyphicon-remove')
                     .css('visibility', 'hidden'))
           .on('click', remove_memo_cell);
}

function remove_memo_cell() {
  $(this).parent().hide(animation_memo_duration, function() { $(this).remove() });
  ngram_details_conditionally_show_add_button();
}

function reveal_memo_remove() {
  $(this).find('.glyphicon-remove').css('visibility', 'visible');
}

function hide_memo_remove() {
  $(this).find('.glyphicon-remove').css('visibility', 'hidden');
}

function add_ngram_to_memo() {
  var handle_id = $(this).parent().parent().parent().attr('data-handle-id');
  var handle_selector = '[data-handle-id="' + handle_id + '"]';

  if (!already_in_memo(handle_id)) {
    var clone = ngram_list_table_body
                  .find(handle_selector)
                  .clone(false)
                  .append(memo_cell_deletion())
                  .on('click', add_to_ngram_details)
                  .on('mouseover', reveal_memo_remove)
                  .on('mouseout',  hide_memo_remove)
                  .hide();

    $.each(clone.find('[data-toggle="ngram-tooltip"]'), function() {
      $(this)
        .attr('title', $(this).attr('data-original-title'))
        .tooltip({
          container: 'body',
          html: true,
          placement: 'top'
         })
    });

    ngram_memo_table.prepend(clone);
    clone.show(animation_memo_duration);
  }
}
@@ -1215,24 +1215,65 @@
 n $('%3Ctd%3E')%0A
+           .addClass('memo-non-trigger')%0A
            .
@@ -1401,18 +1401,27 @@
 hidden')
-)
 %0A
+          
         
@@ -1436,32 +1436,193 @@
 click', 
-remove_memo_cell
+%0A                         function(event) %7B%0A                           remove_memo_cell($(this));%0A                           event.stopPropagation();%0A                         %7D)
 );%0A%7D%0A%0Afu
@@ -1645,28 +1645,34 @@
 mo_cell(
+el
 ) %7B%0A  
-$(this
+el.parent(
 ).parent
@@ -1673,16 +1673,21 @@
 parent()
+%0A    
 .hide(an
@@ -1709,16 +1709,27 @@
 ration, 
+%0A          
 function
@@ -1733,16 +1733,29 @@
 ion() %7B 
+%0A            
 $(this).
@@ -1762,21 +1762,28 @@
 remove()
- %7D);%0A
+;%0A          
   ngram_
@@ -1823,16 +1823,30 @@
 tton();%0A
+          %7D);%0A
 %7D%0A%0Afunct

Fix for animation on ngram-memo triggering click on row deletion.