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

From Genesys Documentation
Jump to: navigation, search
Line 1: Line 1:
 
/*
 
/*
  QuestionMark.js 2.0 by Louis Lazaris
+
    QuestionMark.js by Louis Lazaris
  http://impressivewebs.github.io/QuestionMark.js/
+
    https://impressivewebs.github.io/QuestionMark.js/
  Use it for whatever you want, no credit needed.
+
    Use it for whatever you want, no credit needed.
  This script should work everywhere, including IE9+.
+
    This script should work everywhere, including IE8+.
 +
    If you want IE8 support, include the following
 +
    polyfill for addEventListener() at the top:
 +
    https://gist.github.com/jonathantneal/2415137
 +
    (included in the repo as attachevent.js).
 +
    Doesn't work in IE6/7, but feel free to fork and fix.
 
*/
 
*/
  
(function (d) {
+
(function () {
  'use strict';
 
  
  function removeModal(helpUnderlay) {
+
     'use strict';
     helpUnderlay.classList.remove('help-isVisible');
 
  }
 
  
  function doWhichKey(e) {
+
    function removeModal(helpUnderlay) {
    let charCode = e.keyCode;
+
        helpUnderlay.className = helpUnderlay.className.replace(/help-isVisible*/g, '');
    // String.fromCharCode(charCode) gets the keycode if you want
+
        helpUnderlay.className = helpUnderlay.className.trim();
     return charCode;
+
     }
  }
 
  
  // Primary function, called in checkServerResponse()
+
    function getWindowWidth() {
  function doQuestionMark() {
+
        var w = window,
    let helpUnderlay = d.getElementById('helpUnderlay'),
+
            d = document,
        helpModal = d.getElementById('helpModal'),
+
            e = d.documentElement,
        helpClose = d.getElementById('helpClose'),
+
            g = d.getElementsByTagName('body')[0],
         classCol;
+
            x = w.innerWidth || e.clientWidth || g.clientWidth;
 +
            //y = w.innerHeight || e.clientHeight || g.clientHeight;
 +
         return x;
 +
    }
  
     d.addEventListener('keydown', function (e) {
+
     function doModalSize(o) {
      // 191 = '?' key
+
        // Find out how many columns there are, create array of heights
      // '?' key toggles the modal
+
        o.helpColsTotal = 0;
      if (doWhichKey(e) === 191 && e.shiftKey === true) {
+
        for (o.i = 0; o.i < o.helpLists.length; o.i += 1) {
        helpUnderlay.classList.add('help-isVisible');
+
            if (o.helpLists[o.i].className.indexOf('help-list') !== -1) {
      }
+
                o.helpColsTotal += 1;
    }, false);
+
            }
 +
            o.helpListsHeights[o.i] = o.helpLists[o.i].offsetHeight;
 +
        }
  
    d.addEventListener('keyup', function (e) {
+
        // get the tallest column from the array of heights
      // 27 = ESC key
+
        o.maxHeight = Math.max.apply(Math, o.helpListsHeights);
      if (doWhichKey(e) === 27) {
 
        removeModal(helpUnderlay);
 
      }
 
    }, false);
 
  
    // Modal is removed if the background is clicked
+
        // Quasi-responsive
    helpUnderlay.addEventListener('click', function () {
+
        if (getWindowWidth() <= 1180 && getWindowWidth() > 630 && o.helpColsTotal > 2) {
      removeModal(helpUnderlay);
+
            o.helpColsTotal = 2;
    }, false);
+
            o.maxHeight = o.maxHeight * o.helpColsTotal;
 +
        }
  
    // this prevents click on modal from removing the modal
+
        if (getWindowWidth() <= 630) {
    helpModal.addEventListener('click', function (e) {
+
            o.maxHeight = o.maxHeight * o.helpColsTotal;
      e.stopPropagation();
+
            o.helpColsTotal = 1;
    }, false);
+
        }
  
    // the close button
+
        // Change the width/height of the modal and wrapper to fit the columns
    helpClose.addEventListener('click', function () {
+
        // Sorry for the magic numbers. Whatevs.
      removeModal(helpUnderlay);
+
        o.helpListWrap.style.offsetWidth = (o.helpList.offsetWidth * o.helpColsTotal) + 'px';
    }, false);
+
        o.helpListWrap.style.height = o.maxHeight + 'px';
  }
+
        o.helpModal.style.width = (o.helpList.offsetWidth * o.helpColsTotal) + 60 + 'px';
 +
        o.helpModal.style.height = o.maxHeight + 100 + 'px';
 +
    }
  
  function doFetch() {
+
    function doWhichKey(e) {
    fetch('question.mark.html')
+
        e = e || window.event;
      .then((response) => {
+
        var charCode = e.keyCode || e.which;
        return response.text();
+
         //Line below not needed, but you can read the key with it
      })
+
        //var charStr = String.fromCharCode(charCode);
      .then((data) => {
+
        return charCode;
        d.body.innerHTML += data;
+
    }
         doQuestionMark();
 
      });
 
  }
 
  
  // This fires the Fetch request and, in turn,
+
    // Primary function, called in checkServerResponse()
  // the primary function for the modal.
+
    function doQuestionMark() {
  doFetch();
+
 
}(document));
+
        var helpUnderlay = document.getElementById('helpUnderlay'),
 +
            helpModal = document.getElementById('helpModal'),
 +
            helpClose = document.getElementById('helpClose'),
 +
            timeOut = null,
 +
            objDoSize = {
 +
                i: null,
 +
                maxHeight: null,
 +
                helpListWrap: document.getElementById('helpListWrap'),
 +
                helpList: document.querySelector('.help-list'),
 +
                helpLists: document.querySelectorAll('.help-list'),
 +
                helpModal: helpModal,
 +
                helpColsTotal: null,
 +
                helpListsHeights: []
 +
            },
 +
            classCol;
 +
 
 +
        doModalSize(objDoSize);
 +
 
 +
        document.addEventListener('keypress', function (e) {
 +
 
 +
            // 63 = '?' key
 +
            // '?' key toggles the modal
 +
            if (doWhichKey(e) === 63) {
 +
                classCol = document.getElementById('helpUnderlay').className;
 +
                if (classCol.indexOf('help-isVisible') === -1) {
 +
                    document.getElementById('helpUnderlay').className += ' help-isVisible';
 +
                }
 +
 
 +
            }
 +
 
 +
        }, false);
 +
 
 +
        document.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);
 +
 
 +
        // If the window is resized, the doModalSize() function is called again.
 +
        // If your menu includes only a single column of keyboard shortcuts,
 +
        // then you won't need this. Keep only if you have 2 columns or more.
 +
        window.onresize = function () {
 +
            if (timeOut !== null) {
 +
                clearTimeout(timeOut);
 +
            }
 +
            timeOut = setTimeout(function () {
 +
                doModalSize(objDoSize);
 +
            }, 100);
 +
        };
 +
 
 +
    }
 +
 
 +
    // All the Ajax stuff is below.
 +
    // Probably no reason to touch this unless you can optimize it.
 +
    function getXhrObject() {
 +
        var xhrObject = false;
 +
        // All browsers (except IE6) use the 3 lines below
 +
        if (window.XMLHttpRequest) {
 +
            xhrObject = new XMLHttpRequest();
 +
        }
 +
        // If you need IE6 support, uncomment the following else/if:
 +
        /*else if (window.ActiveXObject) {
 +
            try {
 +
                    xhrObject = new ActiveXObject("Msxml2.XMLHTTP");
 +
                } catch(err) {
 +
                    try {
 +
                        xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
 +
                    } catch(err) {
 +
                        xhrObject = false;
 +
                    }
 +
            }
 +
        }*/
 +
        return xhrObject;
 +
    }
 +
 
 +
    function insertHelp(respText, callback) {
 +
        // Opera kept inserting the content multiple times
 +
        // so I added a check to insert it just once... bug??
 +
        if (!document.getElementById('helpUnderlay')) {
 +
            document.getElementsByTagName('body')[0].innerHTML += respText;
 +
            callback();
 +
        }
 +
    }
 +
 
 +
    function checkServerResponse(ajaxCapable) {
 +
        if (ajaxCapable.readyState === 4) {
 +
            if (ajaxCapable.status === 200 || ajaxCapable.status === 304) {
 +
                var respText = ajaxCapable.responseText;
 +
                // here's where the help modal is inserted
 +
                insertHelp(respText, function () {
 +
                    doQuestionMark();
 +
                });
 +
            }
 +
        }
 +
    }
 +
 
 +
    function doAjax() {
 +
        var ajaxCapable = getXhrObject();
 +
        if (ajaxCapable) {
 +
            ajaxCapable.onreadystatechange = function () {
 +
                checkServerResponse(ajaxCapable);
 +
            };
 +
            ajaxCapable.open("GET", "https://all.docs.genesys.com/User:Xavier/shortcutkeys.html", true);
 +
            ajaxCapable.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 +
            ajaxCapable.send(null);
 +
        } else {
 +
            // Browser does not support ajax
 +
            document.getElementsByTagName('body')[0].innerHTML += 'Error: Your browser does not support Ajax';
 +
        }
 +
    }
 +
 
 +
    // This fires all the Ajax stuff, and, in turn,
 +
    // the primary function for the modal.
 +
    doAjax();
 +
 
 +
}());

Revision as of 05:33, January 27, 2022

/*
    QuestionMark.js by Louis Lazaris
    https://impressivewebs.github.io/QuestionMark.js/
    Use it for whatever you want, no credit needed.
    This script should work everywhere, including IE8+.
    If you want IE8 support, include the following 
    polyfill for addEventListener() at the top:
    https://gist.github.com/jonathantneal/2415137
    (included in the repo as attachevent.js).
    Doesn't work in IE6/7, but feel free to fork and fix.
*/

(function () {

    'use strict';

    function removeModal(helpUnderlay) {
        helpUnderlay.className = helpUnderlay.className.replace(/help-isVisible*/g, '');
        helpUnderlay.className = helpUnderlay.className.trim();
    }

    function getWindowWidth() {
        var w = window,
            d = document,
            e = d.documentElement,
            g = d.getElementsByTagName('body')[0],
            x = w.innerWidth || e.clientWidth || g.clientWidth;
            //y = w.innerHeight || e.clientHeight || g.clientHeight;
        return x;
    }

    function doModalSize(o) {
        // Find out how many columns there are, create array of heights
        o.helpColsTotal = 0;
        for (o.i = 0; o.i < o.helpLists.length; o.i += 1) {
            if (o.helpLists[o.i].className.indexOf('help-list') !== -1) {
                o.helpColsTotal += 1;
            }
            o.helpListsHeights[o.i] = o.helpLists[o.i].offsetHeight;
        }

        // get the tallest column from the array of heights
        o.maxHeight = Math.max.apply(Math, o.helpListsHeights);

        // Quasi-responsive
        if (getWindowWidth() <= 1180 && getWindowWidth() > 630 && o.helpColsTotal > 2) {
            o.helpColsTotal = 2;
            o.maxHeight = o.maxHeight * o.helpColsTotal;
        }

        if (getWindowWidth() <= 630) {
            o.maxHeight = o.maxHeight * o.helpColsTotal;
            o.helpColsTotal = 1;
        }

        // Change the width/height of the modal and wrapper to fit the columns
        // Sorry for the magic numbers. Whatevs.
        o.helpListWrap.style.offsetWidth = (o.helpList.offsetWidth * o.helpColsTotal) + 'px';
        o.helpListWrap.style.height = o.maxHeight + 'px';
        o.helpModal.style.width = (o.helpList.offsetWidth * o.helpColsTotal) + 60 + 'px';
        o.helpModal.style.height = o.maxHeight + 100 + 'px';
    }

    function doWhichKey(e) {
        e = e || window.event;
        var charCode = e.keyCode || e.which;
        //Line below not needed, but you can read the key with it
        //var charStr = String.fromCharCode(charCode);
        return charCode;
    }

    // Primary function, called in checkServerResponse()
    function doQuestionMark() {

        var helpUnderlay = document.getElementById('helpUnderlay'),
            helpModal = document.getElementById('helpModal'),
            helpClose = document.getElementById('helpClose'),
            timeOut = null,
            objDoSize = {
                i: null,
                maxHeight: null,
                helpListWrap: document.getElementById('helpListWrap'),
                helpList: document.querySelector('.help-list'),
                helpLists: document.querySelectorAll('.help-list'),
                helpModal: helpModal,
                helpColsTotal: null,
                helpListsHeights: []
            },
            classCol;

        doModalSize(objDoSize);

        document.addEventListener('keypress', function (e) {

            // 63 = '?' key
            // '?' key toggles the modal
            if (doWhichKey(e) === 63) {
                classCol = document.getElementById('helpUnderlay').className;
                if (classCol.indexOf('help-isVisible') === -1) {
                    document.getElementById('helpUnderlay').className += ' help-isVisible';
                }

            }

        }, false);

        document.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);

        // If the window is resized, the doModalSize() function is called again.
        // If your menu includes only a single column of keyboard shortcuts,
        // then you won't need this. Keep only if you have 2 columns or more.
        window.onresize = function () {
            if (timeOut !== null) {
                clearTimeout(timeOut);
            }
            timeOut = setTimeout(function () {
                doModalSize(objDoSize);
            }, 100);
        };

    }

    // All the Ajax stuff is below.
    // Probably no reason to touch this unless you can optimize it.
    function getXhrObject() {
        var xhrObject = false;
        // All browsers (except IE6) use the 3 lines below
        if (window.XMLHttpRequest) {
            xhrObject = new XMLHttpRequest();
        }
        // If you need IE6 support, uncomment the following else/if:
        /*else if (window.ActiveXObject) {
            try {
                    xhrObject = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(err) {
                    try {
                        xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch(err) {
                        xhrObject = false;
                    }
            }
        }*/
        return xhrObject;
    }

    function insertHelp(respText, callback) {
        // Opera kept inserting the content multiple times
        // so I added a check to insert it just once... bug??
        if (!document.getElementById('helpUnderlay')) {
            document.getElementsByTagName('body')[0].innerHTML += respText;
            callback();
        }
    }

    function checkServerResponse(ajaxCapable) {
        if (ajaxCapable.readyState === 4) {
            if (ajaxCapable.status === 200 || ajaxCapable.status === 304) {
                var respText = ajaxCapable.responseText;
                // here's where the help modal is inserted
                insertHelp(respText, function () {
                    doQuestionMark();
                });
            }
        }
    }

    function doAjax() {
        var ajaxCapable = getXhrObject();
        if (ajaxCapable) {
            ajaxCapable.onreadystatechange = function () {
                checkServerResponse(ajaxCapable);
            };
            ajaxCapable.open("GET", "https://all.docs.genesys.com/User:Xavier/shortcutkeys.html", true);
            ajaxCapable.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ajaxCapable.send(null);
        } else {
            // Browser does not support ajax
            document.getElementsByTagName('body')[0].innerHTML += 'Error: Your browser does not support Ajax';
        }
    }

    // This fires all the Ajax stuff, and, in turn,
    // the primary function for the modal.
    doAjax();

}());
Retrieved from "https://all.docs.genesys.com/User:Xavier/common.js (2025-06-19 20:50:24)"