general popup script

var sourceListHtmlReturned = function (header,data){
  var $popupWindow = $('#popup-window');
  $_data = $(data);
  if(header)
    $_data.find('.card-header').remove();
  
  $popupWindow.html($_data);
  var height = ($(window).height() - $('.navbar:first').height()-1);
  if($popupWindow.height()>height)
    $popupWindow.dialog( "option", "height", height ); 
  wagn.initializeEditors($popupWindow.find("div:first"));
  $popupWindow.find("div:first").trigger('slotReady');
};
var gettingSourceListHtmlFail = function (xhr, ajaxOptions, thrownError){
  var $popupWindow = $('#popup-window');
  var html = $(xhr.responseText);
  $popupWindow.html(html); 
};
var linkOnClick = function(e){
  e.preventDefault();
  var $_this = $(this);
  var href = $_this.attr('href');

  // show the loading image for this
  var loadingImageUrl = "https://wikirate-production-storage.fra1.cdn.digitaloceanspaces.com/files/20531/12604360-large.gif";
  var position = 'center';
  if($_this.hasClass("position-right"))
    position = {
      of: ".navbar",
      my: "right bottom", 
      at: "right top",
      collision: 'flipfit',
    };
  if($_this.hasClass("position-left"))
    position = {
      my: "left bottom", at: "left top", of: ".navbar",collision: 'flipfit',
      };
  var title ='<i class="fa fa-arrows"></i>';
  if($_this.data('popup-title')) {
    title = $_this.data('popup-title');
  }
  var $popupWindow = $('#popup-window');
  if($popupWindow.length===0)
    $("#main").prepend('<div id="popup-window" style="display:none;"></div>');//create one
  $popupWindow = $('#popup-window');
  $popupWindow.html("<img src='"+loadingImageUrl+"' />");
  $popupWindow.removeAttr('style');
  $popupWindow.dialog({
    
    height: 'auto',
    minWidth: 700,
    position: position,
    title: title,
    closeOnEscape: false,
    resizable: false,
    draggable: true,
    close: function( event, ui ) {
      $popupWindow.dialog("destroy");
    }
  });

  var header = $_this.hasClass("no-header");
  

  //create a ajax call to get content and show
  
  var originalLink = $_this.hasClass("popup-original-link");
  var jqxhr = $.ajax(href+(originalLink?"":"?view=content"))
    .done(function(data){
      sourceListHtmlReturned(header,data);
    })
    .fail(gettingSourceListHtmlFail);
  return false;
};


wagn.slotReady(function(slot) {
  slot.find('.show-link-in-popup').each(function(){
    $(this).off("click").click(linkOnClick);
  });
});