view mgallery.js @ 209:671b7cfebf87

Various fixes and adjustments to style.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 22 Mar 2018 13:41:36 +0200
parents cdccda315a0f
children 0a0a2936d779
line wrap: on
line source

//
// Yet Another Image Gallery
// -- Main Javascript utility functions file
// Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org>
// (C) Copyright 2015-2018 Tecnic Software productions (TNSP)
//


function mgalAddEventOb(obname, evobj, evtype, evcallback)
{
  if (evobj == null || typeof(evobj) == 'undefined')
  {
    console.log("Event object '"+ obname +"' == null.");
    return;
  }

  if (evobj.addEventListener)
    evobj.addEventListener(evtype, evcallback, false);
  else
  if (evobj.attachEvent)
    evobj.attachEvent("on"+evtype, evcallback);
  else
    evobj["on"+evtype] = evcallback;
}


function mgalAddEventsToClass(clname, evtype, evcallback)
{
  var elist = document.getElementsByClassName(clname);
  for (var index = 0; index < elist.length; index++)
  {
    mgalAddEventOb(clname, elist[index], evtype, evcallback);
  }
}


function mgalAddEvent(obname, evtype, evcallback)
{
  mgalAddEventOb(obname, document.getElementById(obname), evtype, evcallback);
}


function mgalNavigateTo(url)
{
  if (url != "")
    window.location = url;
}


function mgalProcessKeyPress(ev)
{
  ev = ev || window.event;
  var key = ev.keyCode ? ev.keyCode : ev.which;
  switch (key)
  {
    case 37:
    case 65:
    case 52:
      // left
      mgalNavigateTo(mgalPrevURL);
      break;

    case 39:
    case 68:
    case 54:
      // right
      mgalNavigateTo(mgalNextURL);
      break;

    case 38:
    case 56:
      // up
      mgalNavigateTo(mgalUpURL);
      break;

    default:
      return true;
  }

  ev.preventDefault();
  return false;
}


function mgalGetElementOrWindowSize(nelem)
{
  if (nelem)
  {
    var elem = document.getElementById(nelem);
    if (elem)
      return [elem.clientWidth, elem.clientHeight];
  }

  if (typeof(window.innerWidth) == 'number')
  {
    // Non-MSIE
    return [window.innerWidth, window.innerHeight];
  }

  if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    // MSIE 6+ in 'standards compliant mode'
    return [document.documentElement.clientWidth, document.documentElement.clientHeight];
  }

  if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    // MSIE 4 compatible
    return [document.body.clientWidth, document.body.clientHeight];
  }

  return null;
}


function mgalAdjustImageDo()
{
  var eimg = document.getElementById("imageImage");
  var win = mgalGetElementOrWindowSize("pageImageBox");
  var madj = 0.99;
  if (eimg && win != null)
  {
    if (eimg.width > eimg.height)
    {
      eimg.style.width = "100%";
      eimg.style.height = "auto";
      if (eimg.height > win[1] * madj)
      {
        eimg.style.width = "auto";
        eimg.style.height = (win[1] * madj)+"px";
      }
    }
    else
    {
      eimg.style.width = "auto";
      eimg.style.height = "100%";
      if (eimg.height > win[1] * madj)
        eimg.style.height = (win[1] * madj)+"px";
    }
  }
  adjustPID = -1;
}


function mgalAdjustImage()
{
  if (adjustPID == -1)
    adjustPID = setTimeout(mgalAdjustImageDo, 10);
}


function mgalDisplayInfo(mvstate)
{
  var mvbut = document.getElementById("pageInfoButton");
  var mvnbut = document.getElementById("pageUpNaviButton");
  var mvelem = document.getElementById("pageInfoHeader");
  mvelem.style.display =  mvstate ? "block" : "none";
  mvbut.style.display  = !mvstate ? "block" : "none";
  mvnbut.style.display = !mvstate ? "block" : "none";
  mvInfoOpen = mvstate;
}


function mgalOpenInfo()
{
  mgalDisplayInfo(true);
}


function mgalCloseInfo()
{
  mgalDisplayInfo(false);
}


function mgalPreventDefault(ev)
{
  ev.preventDefault();
  ev.target.click();
}