MediaWiki:Common.js: Difference between revisions

From Hidden Mickey Wiki

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 91: Line 91:
});
});


$(document).ready(function () {
function attachTimestampHandlers() {
  // Restore saved states for existing checkboxes
   $('input.timestamp-checkbox').each(function () {
   $('input.timestamp-checkbox').each(function () {
     var checkbox = $(this);
     if (!$(this).data('tsHandler')) { // attach only once
    var tsBox = $('#' + this.id + '-ts');
      var checkbox = $(this);
    var savedData = localStorage.getItem('checkbox-' + this.id);
      var tsBox = $('#' + this.id + '-ts');
    if (savedData) {
      var savedData = localStorage.getItem('checkbox-' + this.id);
      var data = JSON.parse(savedData);
 
      checkbox.prop('checked', data.checked);
      // Restore saved state
       tsBox.text(data.timestamp);
      if (savedData) {
        var data = JSON.parse(savedData);
        checkbox.prop('checked', data.checked);
        tsBox.text(data.timestamp);
       }
 
      // Attach change handler
      checkbox.on('change', function () {
        if (this.checked) {
          var now = new Date();
          var ts = String(now.getHours()).padStart(2, '0') + ':' +
                  String(now.getMinutes()).padStart(2, '0') + ':' +
                  String(now.getSeconds()).padStart(2, '0');
          tsBox.text(ts);
          localStorage.setItem('checkbox-' + this.id, JSON.stringify({
            checked: true,
            timestamp: ts
          }));
        } else {
          tsBox.text('');
          localStorage.removeItem('checkbox-' + this.id);
        }
      });
 
      $(this).data('tsHandler', true);
     }
     }
   });
   });
}


  // Attach change handler
// Run once on page ready
  $('input.timestamp-checkbox').on('change', function () {
$(document).ready(function () {
    var tsBox = $('#' + this.id + '-ts');
  attachTimestampHandlers();
    if (this.checked) {
  // Also monitor for changes in the table, e.g., when <htmltag> renders
      var now = new Date();
  var observer = new MutationObserver(attachTimestampHandlers);
      var ts = String(now.getHours()).padStart(2, '0') + ':' +
  observer.observe(document.body, { childList: true, subtree: true });
              String(now.getMinutes()).padStart(2, '0') + ':' +
              String(now.getSeconds()).padStart(2, '0');
      tsBox.text(ts);
      localStorage.setItem('checkbox-' + this.id, JSON.stringify({
        checked: true,
        timestamp: ts
      }));
    } else {
      tsBox.text('');
      localStorage.removeItem('checkbox-' + this.id);
    }
  });
});
});

Revision as of 14:05, 29 September 2025

/* Any JavaScript here will be loaded for all users on every page load. */
// JavaScript code to save checkbox state and restore it when the page loads
$(document).ready(function() {
    // Function to save the state of checkboxes to localStorage
    function saveCheckboxState() {
        $('input[type="checkbox"]').each(function() {
            localStorage.setItem($(this).attr('id'), $(this).prop('checked'));
        });
    }

    // Function to load the state of checkboxes from localStorage
    function loadCheckboxState() {
        $('input[type="checkbox"]').each(function() {
            const savedState = localStorage.getItem($(this).attr('id'));
            if (savedState !== null) {
                $(this).prop('checked', savedState === 'true');
            }
        });
    }

    // Load the saved checkbox state when the page is loaded
    loadCheckboxState();

    // Save the checkbox state whenever a checkbox is changed
    $('input[type="checkbox"]').change(function() {
        saveCheckboxState();
    });
});

// Adjust the search box width
$(document).ready(function () {
    $('#searchInput').css('width', '600px'); // Adjust width as needed
});

// Add Edit Source to user dropdown
mw.loader.using('mediawiki.util', function () {
    mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'edit' } ), 'Edit Source', 'pt-editsource' );
    mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'history' } ), 'View History', 'pt-history' );
    mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'delete' } ), 'Delete', 'pt-delete' );
    var moveLink = document.getElementById('ca-move');
    if (moveLink) {
        var a = moveLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-move', a.title || 'Move this page' );
        moveLink.remove();
    }
    mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'protect' } ), 'Protect', 'pt-protect' );
    mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'unwatch' } ), 'Unwatch', 'pt-unwatch' );
    var talkLink = document.getElementById('pt-mytalk');
    talkLink.remove();
    var whatLinksHereLink = document.getElementById('t-whatlinkshere');
    if (whatLinksHereLink) {
        var a = whatLinksHereLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-whatlinkshere', a.title || 'What Links Here' );
        whatLinksHereLink.remove();
    }
    var relatedChangesLink = document.getElementById('t-recentchangeslinked');
    if (relatedChangesLink) {
        var a = relatedChangesLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-recentchanges', a.title || 'Recent Changes' );
        relatedChangesLink.remove();
    }
    var uploadLink = document.getElementById('t-upload');
    if (uploadLink) {
        var a = uploadLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-upload', a.title || 'Upload File' );
        uploadLink.remove();
    }
    var specialPagesLink = document.getElementById('t-specialpages');
    if (specialPagesLink) {
        var a = specialPagesLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-specialpages', a.title || 'Special Pages' );
        specialPagesLink.remove();
    }
    var permanentLink = document.getElementById('t-permalink');
    if (permanentLink) {
        var a = permanentLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-permalink', a.title || 'Permanent Link' );
        permanentLink.remove();
    }
    var pageInfoLink = document.getElementById('t-info');
    if (pageInfoLink) {
        var a = pageInfoLink.querySelector('a');
        mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-info', a.title || 'Page Info' );
        pageInfoLink.remove();
    }
    var printLink = document.getElementById('t-print');
    if (printLink) {
        var a = printLink.querySelector('a');
        printLink.remove();
    }
});

function attachTimestampHandlers() {
  $('input.timestamp-checkbox').each(function () {
    if (!$(this).data('tsHandler')) { // attach only once
      var checkbox = $(this);
      var tsBox = $('#' + this.id + '-ts');
      var savedData = localStorage.getItem('checkbox-' + this.id);

      // Restore saved state
      if (savedData) {
        var data = JSON.parse(savedData);
        checkbox.prop('checked', data.checked);
        tsBox.text(data.timestamp);
      }

      // Attach change handler
      checkbox.on('change', function () {
        if (this.checked) {
          var now = new Date();
          var ts = String(now.getHours()).padStart(2, '0') + ':' +
                   String(now.getMinutes()).padStart(2, '0') + ':' +
                   String(now.getSeconds()).padStart(2, '0');
          tsBox.text(ts);
          localStorage.setItem('checkbox-' + this.id, JSON.stringify({
            checked: true,
            timestamp: ts
          }));
        } else {
          tsBox.text('');
          localStorage.removeItem('checkbox-' + this.id);
        }
      });

      $(this).data('tsHandler', true);
    }
  });
}

// Run once on page ready
$(document).ready(function () {
  attachTimestampHandlers();
  // Also monitor for changes in the table, e.g., when <htmltag> renders
  var observer = new MutationObserver(attachTimestampHandlers);
  observer.observe(document.body, { childList: true, subtree: true });
});