view mgallery.js @ 166:d8387b48b08d

Improve yes/no prompt check.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 01 Mar 2018 14:01:49 +0200
parents 0b87e7c1675c
children ba02d12cb4be
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-2017 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 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 mgalGetWindowSize()
{
  var winW = 0, winH = 0;
  if (typeof(window.innerWidth) == 'number')
  {
    // Non-MSIE
    winW = window.innerWidth;
    winH = window.innerHeight;
  }
  else
  if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    // MSIE 6+ in 'standards compliant mode'
    winW = document.documentElement.clientWidth;
    winH = document.documentElement.clientHeight;
  }
  else
  if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    // MSIE 4 compatible
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
  }

  return [winW, winH];
}


function mgalAdjustImageDo()
{
  var eimg = document.getElementById("imageImage");
  var win = mgalGetWindowSize();
  var madj = 0.92;
  if (eimg)
  {
    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";
      if (eimg.height > win[1] * madj)
        eimg.style.height = (win[1] * madj)+"px";
      else
        eimg.style.height = "100%";
    }
  }
  adjustPID = -1;
}


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


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


function mgalOpenInfo()
{
  mgalDisplayInfo(true);
}


function mgalCloseInfo()
{
  mgalDisplayInfo(false);
}