Difference between revisions of "User: Xavier/common.js"

From Genesys Documentation
Jump to: navigation, search
(Created page with "// Highlighting HTML Access Keys by Christian Schmidt // This code may be freely used, copied, modified, distributed etc. // Iterates through all elements that support the AC...")
 
Line 1: Line 1:
// Highlighting HTML Access Keys by Christian Schmidt
+
/*
// This code may be freely used, copied, modified, distributed etc.
+
  QuestionMark.js 2.0 by Louis Lazaris
 +
  http://impressivewebs.github.io/QuestionMark.js/
 +
  Use it for whatever you want, no credit needed.
 +
  This script should work everywhere, including IE9+.
 +
*/
  
// Iterates through all elements that support the ACCESSKEY attribute.
+
(function (d) {
function highlightAccessKeys() {
+
  'use strict';
    //don't do anything in old browsers
 
    if (!document.getElementsByTagName) return;
 
  
    var labels = document.getElementsByTagName('LABEL');
+
  function removeModal(helpUnderlay) {
     for (var i = 0; i < labels.length; i++) {
+
     helpUnderlay.classList.remove('help-isVisible');
        var control = document.getElementById(labels[i].htmlFor);
+
  }
        if (control && control.accessKey) {
 
            highlightAccessKey(labels[i], control.accessKey);
 
        } else if (labels[i].accessKey) {
 
            highlightAccessKey(labels[i], labels[i].accessKey);
 
        }
 
    }
 
  
    var tagNames = new Array('A', 'BUTTON', 'LEGEND');
+
  function doWhichKey(e) {
     for (var j = 0; j < tagNames.length; j++) {
+
     let charCode = e.keyCode;
        var elements = document.getElementsByTagName(tagNames[j]);
+
    // String.fromCharCode(charCode) gets the keycode if you want
        for (var i = 0; i < elements.length; i++) {
+
    return charCode;
            if (elements[i].accessKey) {
+
  }
                highlightAccessKey(elements[i], elements[i].accessKey);
 
            }
 
        }
 
    }
 
}
 
  
// Highlights the specified character in the specified element
+
  // Primary function, called in checkServerResponse()
function highlightAccessKey(e, accessKey) {
+
  function doQuestionMark() {
     if (e.hasChildNodes()) {
+
     let helpUnderlay = d.getElementById('helpUnderlay'),
        var childNode, txt;
+
         helpModal = d.getElementById('helpModal'),
       
+
        helpClose = d.getElementById('helpClose'),
        //find the first text node that contains the access character
+
         classCol;
         for (var i = 0; i < e.childNodes.length; i++) {
 
            txt = e.childNodes[i].nodeValue;
 
            if (e.childNodes[i].nodeType == 3 &&
 
                txt.toLowerCase().indexOf(accessKey.toLowerCase()) != -1) {
 
           
 
                childNode = e.childNodes[i];
 
                break;
 
            }
 
        }
 
       
 
         if (!childNode) {
 
            //access character was not found
 
            return;
 
        }
 
  
        var pos = txt.toLowerCase().indexOf(accessKey.toLowerCase());
+
    d.addEventListener('keydown', function (e) {
        var span = document.createElement('SPAN');
+
      // 191 = '?' key
        var spanText = document.createTextNode(txt.substr(pos, 1));
+
      // '?' key toggles the modal
         span.className = 'accesskey';
+
      if (doWhichKey(e) === 191 && e.shiftKey === true) {
        span.appendChild(spanText);
+
         helpUnderlay.classList.add('help-isVisible');
 +
      }
 +
    }, false);
  
        //the text before the access key
+
    d.addEventListener('keyup', function (e) {
        var text1 = document.createTextNode(txt.substr(0, pos));
+
      // 27 = ESC key
        //the text after the access key
+
      if (doWhichKey(e) === 27) {
        var text2 = document.createTextNode(txt.substr(pos + 1));
+
         removeModal(helpUnderlay);
          
+
      }
        if (text1.length > 0) e.insertBefore(text1, childNode);
+
    }, false);
        e.insertBefore(span, childNode);
 
        if (text2.length > 0) e.insertBefore(text2, childNode);
 
  
        e.removeChild(childNode);
+
    // Modal is removed if the background is clicked
     }
+
    helpUnderlay.addEventListener('click', function () {
}
+
      removeModal(helpUnderlay);
 +
    }, false);
 +
 
 +
    // this prevents click on modal from removing the modal
 +
    helpModal.addEventListener('click', function (e) {
 +
      e.stopPropagation();
 +
     }, false);
 +
 
 +
    // the close button
 +
    helpClose.addEventListener('click', function () {
 +
      removeModal(helpUnderlay);
 +
    }, false);
 +
  }
 +
 
 +
  function doFetch() {
 +
    fetch('question.mark.html')
 +
      .then((response) => {
 +
        return response.text();
 +
      })
 +
      .then((data) => {
 +
        d.body.innerHTML += data;
 +
        doQuestionMark();
 +
      });
 +
  }
 +
 
 +
  // This fires the Fetch request and, in turn,
 +
  // the primary function for the modal.
 +
  doFetch();
 +
}(document));

Revision as of 05:25, January 27, 2022

/*
  QuestionMark.js 2.0 by Louis Lazaris
  http://impressivewebs.github.io/QuestionMark.js/
  Use it for whatever you want, no credit needed.
  This script should work everywhere, including IE9+.
*/

(function (d) {
  'use strict';

  function removeModal(helpUnderlay) {
    helpUnderlay.classList.remove('help-isVisible');
  }

  function doWhichKey(e) {
    let charCode = e.keyCode;
    // String.fromCharCode(charCode) gets the keycode if you want
    return charCode;
  }

  // Primary function, called in checkServerResponse()
  function doQuestionMark() {
    let helpUnderlay = d.getElementById('helpUnderlay'),
        helpModal = d.getElementById('helpModal'),
        helpClose = d.getElementById('helpClose'),
        classCol;

    d.addEventListener('keydown', function (e) {
      // 191 = '?' key
      // '?' key toggles the modal
      if (doWhichKey(e) === 191 && e.shiftKey === true) {
        helpUnderlay.classList.add('help-isVisible');
      }
    }, false);

    d.addEventListener('keyup', function (e) {
      // 27 = ESC key
      if (doWhichKey(e) === 27) {
        removeModal(helpUnderlay);
      }
    }, false);

    // Modal is removed if the background is clicked
    helpUnderlay.addEventListener('click', function () {
      removeModal(helpUnderlay);
    }, false);

    // this prevents click on modal from removing the modal
    helpModal.addEventListener('click', function (e) {
      e.stopPropagation();
    }, false);

    // the close button
    helpClose.addEventListener('click', function () {
      removeModal(helpUnderlay);
    }, false);
  }

  function doFetch() {
    fetch('question.mark.html')
      .then((response) => {
        return response.text();
      })
      .then((data) => {
        d.body.innerHTML += data;
        doQuestionMark();
      });
  }

  // This fires the Fetch request and, in turn,
  // the primary function for the modal.
  doFetch();
}(document));
Retrieved from "https://all.docs.genesys.com/User:Xavier/common.js (2025-06-19 20:50:24)"