MediaWiki:Common.js

From Game Detectives Wiki
Revision as of 03:11, 20 August 2018 by Mattyb (talk | contribs)
Jump to: navigation, search
/* Scripts here will load on all pages for all users */
/* Script for popup onclick for SMW matty tables */

(function () {

  /* Define jQuery scripts and settings */
  var jQuery = document.createElement('script');
  jQuery.setAttribute('src','//code.jquery.com/jquery-3.3.1.min.js');
  jQuery.setAttribute('async', 'true');
  jQuery.setAttribute('integrity', 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=');
  jQuery.setAttribute('crossorigin', 'anonymous');

  var jQueryUI = document.createElement('script');
  jQueryUI.setAttribute('src','//code.jquery.com/ui/1.12.1/jquery-ui.min.js');
  jQueryUI.setAttribute('integrity', 'sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=');
  jQueryUI.setAttribute('crossorigin', 'anonymous');

  var jQueryCSS = document.createElement('link');
  jQueryCSS.setAttribute('rel', 'stylesheet');
  jQueryCSS.setAttribute('href', '//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css');

  // Count to break loop
  var cnt = 0;

  // Wait for jQuery to load
  jQuery.onload = function (jQueryUI, jQueryCSS) {

    // Only insert UI and CSS once jQuery has definitely loaded (or else jQuery undefined)
    document.getElementsByTagName('head')[0].appendChild(jQueryUI);
    document.getElementsByTagName('head')[0].appendChild(jQueryCSS);

    // Wait for jQuery UI to load
    jQueryUI.onload = function () {

      // Define jQuery prefix
      var $ = window.jQuery;

      // Create variable clicked to capture data about which element is clicked
      $(document).click(function (event) {
        var clicked = $(event.target).text();
      });

      // Iterate n to loop through each table row
      for (var n = 1; cnt === 0; n++) {

        var tableRow = 'tableRow' + n;

        // Check that the table row exists
        if ($(tableRow) != null) {

          // Select the table row and set it's ID selector
          var row = document.getElementById(tableRow);
          var dialogID = '#dialog' + n;
          var uiID = 1;

          if(row === null) {
            break;
          }

          if(n%2 === 0) {
            $(row).addClass('tableRowEven');
          }
          else {
            $(row).addClass('tableRowOdd');
          }

          // Add the click event listener and popup function to the given row
          row.addEventListener('click', function (dialogID, clicked) {

            // Check if what has been clicked is a table element of some kind
            if (!clicked.srcElement.toString().match('\\[object HTMLT')) ;

            //Only proceed if not a link
            else {

              // Create the popup
              $(dialogID).dialog({ // Define popup settings
                modal: true,
                minHeight: 300,
                minWidth: 600,
                width: 900,
                dialogClass: dialogID,
                position: {my: 'center', at: 'center', of: '#' + clicked.path[1].id},
                open: function () {
                  var thisDialog = document.getElementById('ui-id-' + uiID);
                  var title = thisDialog.innerHTML;
                  thisDialog.innerHTML = '<span style="color: #b02e2e !important; padding-left:60px !important;"><a href="https://wiki.gamedetectives.net/index.php?title=' + title + '">' + title + '</a></span>';
                  uiID++;
                },
                show: {effect: 'clip', direction: 'vertical', duration: 500, easing: 'swing', delay: 100},
                hide: 'fade',
                beforeClose: function (event, ui) {
                  $(dialogID).transfer({
                    to: '#' + clicked.path[1].id,
                  });
                },
                close: function (row) {
                  return function() {
                    var thisRow = row.target.getAttribute('id');
                    var thisRowID = row.target.getAttribute('id').replace('dialog', '#tableRow');
                    if (thisRow.replace('dialog', '') % 2 === 0) {
                      $(thisRowID).addClass('tableAnimationEven');
                    }
                    else {
                      $(thisRowID).addClass('tableAnimationOdd');
                    }

                    setTimeout(function() {
                      $(thisRowID).removeClass('tableAnimationEven');
                      $(thisRowID).removeClass('tableAnimationOdd');
                    }, 500);
                  }(row);
                },
                buttons: [
                  {
                    text: 'Close',
                    icon: 'ui-icon-closethick',
                    click: function () {
                      $(this).dialog('close');
                    }
                  }
                ]
              });
            }

            // Bind dialogID to function
          }.bind(null, dialogID));

          // Define a variable to be used for the title of the popup
          var nameCol = document.getElementById('tableCol1-' + n).innerText;
          var nameColID = nameCol.replace(/ /g,'_');
          var dialogContent = '';
          var userDialogContent = '';
          if (document.getElementById(nameColID + '-popup') != null) {
            userDialogContent = document.getElementById(nameColID + '-popup').innerHTML;
          }
          else {
            dialogContent = 'Popup content for ' + nameCol + ' coming soon! {{#show:' + nameColID + '|?argDescription}}';
          }

          // Define the popup's title and content and insert the popup's HTML into the document
          document.getElementById('bodyContent').innerHTML += ('<div style="display:none" id="dialog' + n + '"' + ' title="' + nameCol + '"' + n + '"></a>' + '\n' +
            '<div>' + dialogContent + '\n' +
            userDialogContent + '</div>\n' +
            '</div>');
        }
      }
    };
  }.bind(null, jQueryUI, jQueryCSS);

  // Insert jQuery script into page's HTML
  document.getElementsByTagName('head')[0].appendChild(jQuery);
})();