close

  
<div><p> /*
$.blockUI.defaults.css = {'padding':0, 
                          'margin':0, 
                          'width':          '30%', 
                          'top':            '40%', 
                          'left':           '35%',
                          'textAlign':'center', 
                          'color':'#000', 
                          'backgroundColor':'#fff',
                          'cursor':'wait'};
*/

// add types to the accordion
function add_types(source, cursor, types, callback) {
  $.ajax({
    type: "GET",
    async: true,
    cache: false,
    url: "/gen/accordion",
    data: {"source":source, "cursor":cursor, "html":1, "json":1},
    dataType: "json",
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      throw errorThrown;
    },
    success: function(json) {
      if (json.html) {
        $("#accordion").append(json.html);
      }
      jQuery.each(json.json.result, function(i, type) {
        types.push(type.id);
        
        // if name in params and value is mv, check it
        var name = params.source + ":/type/object/type:" + type.id + ":-";
        if (name in params &amp;&amp; params[name] === "mv") {
          $('input[name="'+ name + '"]')[1].checked = true;
        }
        
      });
      if (json.json.cursor) {
        add_types(source, json.json.cursor, types, callback);
      } else {
        if (callback) {
          callback(types);
        };
      }
    }
  });
}

  /**
   * Inject html from suggest flyout for topic id to $("#"+elem_id) 
   */
  function get_topic_block(id, elem_id) {    
    var search_url = fb_googleapis_url +
          "/search?filter=(all mid:"+id+")&amp;" +
          "output=(notable:/client/summary (description) type)&amp;key=" + 
          // TO fast fix the bug, citation and provenance had to be removed :"output=(notable:/client/summary (description citation provenance) type)&amp;key=" + //
          API_KEY;

    var image_url = fb_googleapis_url +
        "/image"+id+"?maxwidth=75&amp;errorid=/freebase/no_image_png&amp;key=" +
        API_KEY;
    //console.log(search_url);
    $.ajax({
        url: search_url,
        success: function(data) {
          if (data['result'] &amp;&amp; data['result'].length) {
            var html = $.suggest.suggest.create_flyout(
                data['result'][0], 
                image_url);
            
            //fb.hover.cache[id] = html;
            $("#"+elem_id).html(html);
            
            $(".continue-button").removeAttr("disabled");
            $.unblockUI();

          }
        },
        dataType: "jsonp",
        cache: true
    });
  }


// load discussions
function load_discuss(topic, cursor, type, callback) {
  $.ajax({
    type: "GET",
    async: true,
    cache: false,
    url: "/gen/discuss",
    data: {"topic":JSON.stringify(topic), "cursor":cursor, "html":1, "json":1},
    dataType: "json",
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      throw errorThrown;
    },
    success: function(json) {
      if (json.html) {
        $("#" + type + "-div").append(json.html);
      }
      if (json.json.cursor) {
        load_discuss(topic, json.json.cursor, type);
      } else {
        callback();
      }
    }
  });
}

// load mutexes based on the types of the source
function load_mutex(callback) {
  $.ajax({
    type: "GET",
    async: true,
    cache: false,
    url: "/gen/mutex",
    data: {"source":source},
    dataType: "json",
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      throw errorThrown;
    },
    success: function(json) {
      if (json.html) {
        var table = $("</p><table></table>").attr({"class":"tablesorter"});
        var thead = $("<thead></thead>");
        var thead_tr = $("<tr></tr>");
        thead_tr.append("<th>Mutex</th>");
        thead_tr.append("<th>Description</th>");
        thead_tr.append("<th>Incompatibilites</th>");
        thead_tr.append("<th>Timestamp</th>");
        thead_tr.append("<th>Creator</th>");
        thead.append(thead_tr);
        table.append(thead);
        
        var tbody = $("<tbody></tbody>").append(json.html);
        
        table.append(tbody);
        $("#mutex-div").append(table);
        //$("#mutex-count").html("(" + mutexes.length + ")");
      } else {
        $("#mutex-div").append("<div>No mutexes.  " +   view_link('/dataworld/incompatible_types', 'Create a new one.') + "  <a href="">Close</a></div>");
        //$("#mutex-count").html('(0)');
      }
      if (callback) {
        callback();
      }
    }
  });
}

// create a table stub
function create_link_table(id, show_action) {
  var table = $("<table></table>").attr({"id":id, "class":"tablesorter"});
  var thead = $("<thead></thead>");
  var thead_tr = $("<tr></tr>");
  thead_tr.append("<th>Source</th>");
  thead_tr.append("<th>Master Property</th>");
  thead_tr.append("<th>Target</th>");
  thead_tr.append("<th>Target Value</th>");
  thead_tr.append("<th>Timestamp</th>");
  thead_tr.append("<th>Creator</th>");
  if (show_action) {
    thead_tr.append("<th>Action</th>");
  }
  thead.append(thead_tr);
  table.append(thead);
  table.append("<tbody></tbody>");
  
  return table;
}

// load assertions into a table
function load_assertions(out_cursor, in_cursor, div_id, display_action, recurse, callback) { 

  var id = div_id.split('-').join('/');
  
  var out_mp = {
    "id": null,
    "reverse_property": {
      "id":       null,
      "optional": true,
      "unique": {
      }
    },
    "schema": {
      "id": null
    },
    "unique": {
    },
    "expected_type": null
  };
  var in_mp = {
    "id": null,
    "reverse_property": {
      "id":       null,
      "optional": true,
      "unique": {
      }
    },
    "schema": {
      "id": null
    },
    "unique": {
    },
    "expected_type": null
  };
  
  if (div_id !== "-advanced") {
    if (display_action) {
      if (id === "/type/namespace/keys") {
        in_mp["id"] = "/type/namespace/keys";
      } else {
        out_mp['id'] = id;
      }
    } else {
      out_mp['schema']['id'] = id;
      in_mp['expected_type'] = {"id":id};
    }
  }
  
  var content = $("#" + div_id + "_content");
  var table = $("#" + div_id + "_table");
  
  $.ajax({
    type: "GET",
    cache: false,
    url: "/gen/assertions",
    data: {"source":JSON.stringify({"id":source, "guid":null, "name":null}),
           "out_mp":JSON.stringify(out_mp),
           "in_mp":JSON.stringify(in_mp),
           "out_cursor":out_cursor,
           "in_cursor":in_cursor,
           "display_action":display_action},
    dataType: "json",
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      console.log(XMLHttpRequest.status, textStatus, errorThrown);
      throw errorThrown;
    },
    success: function(json) {
      if (json.html) {
        if (!table.length) {
          table = create_link_table(div_id + "_table", display_action);
          content.append(table);
        }
        $("#" + div_id + "_table &gt; tbody").append(json.html);
      }
      
      if (recurse) {
        if (json.out_cursor || json.in_cursor) {
          load_assertions(json.out_cursor, json.in_cursor, div_id, display_action, recurse, callback);
        } else {
          if (callback) {
            callback();
          }
        }
      } else {
        var button = $("#" + div_id + "_button");
        if (!button.length) {
          if (json.out_cursor || json.in_cursor) {
            var button_div = $("<div></div>");
            button = $("").attr({"id":div_id + "_button"});
            button_div.append(button);
            $("#" + div_id + "_content").append(button_div);
            button.click(function() {
              $.blockUI();
              load_assertions(json.out_cursor,
                              json.in_cursor,
                              div_id,
                              display_action,
                              false,
                              function() {$.unblockUI();});
            });
          }
        } else {
          if (!json.out_cursor &amp;&amp; !json.in_cursor) {
            button.attr("disabled", true);
          } else {
            button
              .click(function() {
                $.blockUI();
                load_assertions(json.out_cursor,
                                json.in_cursor,
                                div_id,
                                display_action,
                                false,
                                function() {$.unblockUI();});
              });
          }
        }
        if (callback) {
          callback();
        }
      }
    }
  });
}

function set_target(target, target_name) {
  if (target) {
    $(".hidden-target").val(target);
    $(".hidden-target-name").val('');
  } else {
    $(".hidden-target").val('');
    $(".hidden-target-name").val(target_name);
  }
}

function hide(id) {
  $("#" + id).animate({ opacity: "hide" }, 750);
}

function toggle_target() {
  $("#target-suggest-div").toggle(); 
  $("#target-confirm-div").toggle();
}

function reset_suggest() {
  $("#suggest").attr("value","").focus();
  toggle_target();
}

// on a radio click event, set the hidden input element in the form(s)
function set_graph(graph) {
  $(".hidden-graph").val(graph);
}


// document ready
$(function() {
  $.blockUI.defaults.overlayCSS.opacity = 0;
  $.blockUI.defaults.css = { 
    padding:        0, 
    margin:         0, 
    width:          '30%', 
    top:            '40%', 
    left:           '35%', 
    textAlign:      'center', 
    color:          '#000', 
    border:         0, 
    backgroundColor:'transparent', 
    cursor:         'wait' 
  };
  $.blockUI.defaults.message = $("#loading");
  
  add_types(source, true, [], function(types) {
    $("#accordion")
      .accordion({collapsible:true,
                  active:false,
                  clearStyle: true,
                  header:".header"})
      .bind('accordionchangestart', function(event, ui) {
        if (ui.newHeader.attr('id') &amp;&amp; ui.newHeader.data('loaded') !== 1) {
          $.blockUI();
          
          var div_id = ui.newHeader.attr('id');
          var display_action = ui.newHeader.is('.prop');
          var out_cursor = true;
          var in_cursor = true;
          
          if (display_action) {
            if (div_id === "-type-namespace-keys") {
              out_cursor = false;
            } else {
              in_cursor = false;
            }
          }
          
          if (div_id === "-common-topic") {
            out_cursor = false;
          }
          
          load_assertions(out_cursor, in_cursor, div_id, display_action, false, function () {
            if (!$("#" + div_id + "_table").length) {
              $("#" + div_id + "_content").append($("<div></div>").html("No assertions"));
            }
            ui.newHeader.data('loaded', 1);
            $.unblockUI();
          });
        }
      })
      .show();
    
    // stop the accordion event in the type-radio is clicked
    $('.header .type-radio-div .type-radio').bind("click", function(ev) {
      ev.stopPropagation();
    });
  });
  
  
  $("#tabs").tabs({select: function(event, ui) {
    if (ui.panel.id === 'advanced' &amp;&amp; jQuery.data(ui.panel, 'loaded') != 1) {
      $.blockUI();
      load_assertions(true, true, "-advanced", true, true, function () {
        $("#-advanced_table .results").tablesorter({widgets:['zebra']});
        jQuery.data(ui.panel, 'loaded', 1);
        $.unblockUI();
      });
    }
  }});
  
  $("#advanced-mark-all").change(function(){
    $(".action-" + $(this).val() + ":visible").filter(":enabled").attr({checked:true});
  });
  
  $("#advanced-filter-property").keyup(function(){
    $("#-advanced_table tbody tr").each(function(){
      var master_property = $(this).find("td:eq(1)").html();
      if (master_property.indexOf($("#advanced-filter-property").val()) == 0) {
        $(this).show();
      } else {
        $(this).hide();
      }
    });
  });
  
  if (mode == 'advanced') {
    $("#tabs").tabs('select', 1);
  }

  $("#mutex-accordion")
    .accordion({collapsible:true,
                active:false,
                clearStyle: true,
                header:".header"})
    .bind('accordionchangestart', function(event, ui) {
      if (ui.newHeader.attr('id') &amp;&amp; ui.newHeader.data('loaded') !== 1) {
        $.blockUI();
        load_mutex(function() {
          ui.newHeader.data('loaded', 1);
          $.unblockUI();
        });
      }
    })
    .show();
  
  $("#split-discuss-accordion")
    .accordion({collapsible:true,
                active:false,
                clearStyle: true,
                header:".header"})
    .bind('accordionchangestart', function(event, ui) {
      if (ui.newHeader.attr('id') &amp;&amp; ui.newHeader.data('loaded') !== 1) {
        $.blockUI();
        load_discuss({"id":null,
                      "/freebase/review_flag/kind":{"id":"/freebase/flag_kind/split"},
                      "/freebase/review_flag/item":{"id":source}}, 
                     true, 
                     "split-discuss", function() {
                       if ($("#split-discuss-div").children().length == 0) {
                         $("#split-discuss-div").append("<div>No discussions.  <a href="">Close</a></div>");
                       }
                       $(".treeview").accordion({collapsed: true});
                       ui.newHeader.data('loaded', 1);
                       $.unblockUI();
                     });
        
      }
    })
    .show();
  
  $("#general-discuss-accordion")
    .accordion({collapsible:true,
                active:false,
                clearStyle: true,
                header:".header"})
    .bind('accordionchangestart', function(event, ui) {
      if (ui.newHeader.attr('id') &amp;&amp; ui.newHeader.data('loaded') !== 1) {
        $.blockUI();
        load_discuss({"id":source}, 
                     true , 
                     "general-discuss", 
                     function() {
                       if ($("#general-discuss-div").children().length == 0) {
                         $("#general-discuss-div").append("<div>No discussions.  <a href="">Close</a></div>");
                       }
                       $(".treeview").accordion({collapsed: true});
                       ui.newHeader.data('loaded', 1);
                       $.unblockUI();
        });
      }
    })
    .show();
  
  function get_target(id) {
      $.blockUI();
      set_target(id, null);
      $("#target-span").html("to existing topic " + edit_link(id));
      //$("#target-graph-div").show();
      //$("#assertions-div").show();
      
      // enable the confirm button

      get_topic_block(id, "target-tb");
    
      return;    
  };

  
  $("#suggest")
    .suggest({
      //"filter": "(any type:/common/topic)"
      //      "exclude_guids":source_guid,
      //"type": ["/common/topic"],
      //"type_strict": "any",
      "key" : API_KEY,
      "suggest_new": "Split to a new topic",
      //"soft": true
    })
    .bind("fb-select", function(e, data) {
      get_target(data.mid);
    })
    .bind("fb-select-new", function(e, val) {
      set_target(null, val);
      $("#target-span").html("to new topic " + val);
      //$("#target-graph-div").show();
      //$("#assertions-div").show();
      
      // enable the confirm button
      $(".continue-button").removeAttr("disabled");
      
      $.ajax({
        type: "GET",
        url: "/topicblock",
        dataType: "html",
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          throw errorThrown;
        },
        success: function(html) {
          $("#target-tb").html(html);
        }
      });
    })
    .parents("form:first").submit(function() {
      var v = $("#suggest").val();
      if (/^\/m\//.test(v)) {
        get_target(v); 
      }
      return false;
    });
  
  $("#confirm-dialog").dialog({"autoOpen":false,
                               "modal":true,
                               "width":600,
                               "maxHeight":800,
                               "title":"Confirm split of " + source
                              });
  
  $(".form").submit(function(){   
    var self = this;
    if ($(this).data("submit") != 1) {
      $(this).ajaxSubmit({
        beforeSubmit:function(formData, jqForm, options) {
          
          var graph = "";
          var target = "";
          var target_target_name = "";
          var has_target = true;
          
          // clear the table if there's any data
          $("#confirm-table &gt; tbody &gt; tr").remove();
          
          jQuery.each(formData, function(i, v) {
            switch (v.name) {
              case "source":
              case "mode":
                return true;
              case "graph":
                graph = v.value;
                break;
              case "target":
                target = v.value;
                break;
              case "target_name":
                target_name = v.value;
                break;
              default:
                if (v.value !== "keep") {
                  var tuple = v['name'].split(":");
                  var tr = $("<tr></tr>");
                  tr.append($("<td></td>").html(v.value));
                  tr.append($("<td></td>").html(tuple[0]));
                  tr.append($("<td></td>").html(tuple[1]));
                  tr.append($("<td></td>").html((tuple[2] !== "-") ? tuple[2] : ""));
                  tr.append($("<td></td>").html((tuple[3] !== "-") ? tuple[3] : ""));
                  $("#confirm-table &gt; tbody").append(tr);
                }
            }
          });
          
          $("#confirm-table").tablesorter();
          if (target !== "") {
            $("#confirm-target").html("Target: " + target).css({"color":"black"});
          } else if (target_name !== "") {
            $("#confirm-target").html("Target: New topic " + target_name).css({"color":"black"});
          } else {
            $("#confirm-target").html("No Target selected or created.").css({"color":"red"});
            has_target = false;
          }
          $("#confirm-graph").html("Graph: " + (window.location.host.indexOf("sandbox")!==-1?"SANDBOX":"OTG"));
          
          $("#confirm-dialog")
            .dialog('option', 'buttons', {"Cancel": function() { $(this).dialog("close");},
                                          "Submit": function() {
                                            if (!has_target) return;
                                            $(self).data("submit", 1); $(self).submit();
                                          }
                                         })
            .dialog("open");
          
          if (!has_target) {
            var firstButton=$('.ui-dialog-buttonpane button:last');
            firstButton.addClass('ui-state-disabled');
            firstButton.attr("disabled", "disabled");
          } 
          
          return false;
        }
      });
      return false;
    } else {
      return true;
    }
  });
}); </div>

Comments

Hide