note citation script

var ajaxFail = function ($popupWindow,xhr, ajaxOptions, thrownError){

  var html = $(xhr.responseText);
  $popupWindow.html(html); 
}

var relatedOverviewReturned = function($popupWindow,data){
  //get the topic list

  $popupWindow.html(data); 
  $popupWindow.trigger('slotReady');
}

var citationBoxOnClick = function(){
  var $_this = $(this);
  var noteName = $_this.attr("id").split("+")[0];

  //loading image 
  var loadingImageUrl = "https://wikirate-production-storage.fra1.cdn.digitaloceanspaces.com/files/20531/12604360-medium.gif";

  //create pop up
  var popupId = 'citation-popup-window';
  var $popupWindow = $('#'+popupId);
  if($popupWindow.length==0){
    $("#main").prepend('<div id="'+popupId+'" style="display:none;"></div>');//create one
    $popupWindow = $('#'+popupId);
  }
  //show pop up of loading
  $popupWindow.html("<img src='"+loadingImageUrl+"' />");
 
  $popupWindow.dialog({
    height: 'auto',
    position: {
      of: $_this,
      my: "right top",
      at: "left top",
      collision: "none none"
    },
    minWidth: 500,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    close: function( event, ui ) {
      $popupWindow.dialog("destroy");
    }
  });

  //get company and topic list

  var companyUrl = "/"+noteName+"+related overview?view=content";
  
  var jqxhr = $.ajax(companyUrl)
    .done(function(data){
      relatedOverviewReturned($popupWindow,data);
    })
    .fail(function(xhr, ajaxOptions, thrownError){
      ajaxFail($popupWindow,xhr, ajaxOptions, thrownError);
    });
  return false;

}

var citeSyntaxHelper = function(){
 var $general_overview = $('.RIGHT-overview');
 var note_name=  $(this).closest('.card-slot').data('card-name');
 if($general_overview.find('.tinymce-textarea').length > 0 ){
    var cite_syntax = '{'+'{'+note_name+'|cite}}';
    $('.sample-citation .note-tip').show();
    $('.sample-citation textarea').val(cite_syntax);
    $('.RIGHT-overview')[0].scrollIntoView( true );
 }else{
    var param_key = window.location.href.indexOf("?")>0 ? window.location.search+'&':'?'; 
    var url = window.location.pathname+param_key+'citable='+note_name+'&edit_general_overview=true';
    window.location = url; 
 }
}

wagn.slotReady(function(slot) {
  slot.find('.titled-view.RIGHT-citation_count').each(function(){
    // $(this).off("click").click(citationBoxOnClick);
  });
  slot.find('.general_overview_cite_btn').on('click', citeSyntaxHelper);
});

//Because notes tab is laoded through ajax , slot ready not working :(
$( document ).ajaxComplete(function() {
  $('.general_overview_cite_btn').on('click', citeSyntaxHelper);
});