Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"network.min.js","sources":["../src/network.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Poll the server to keep the session alive.\n *\n * @module     core/network\n * @copyright  2019 Damyon Wiese\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],\n        function($, Ajax, Config, Notification, Str) {\n\n    var started = false;\n    var warningDisplayed = false;\n    var keepAliveFrequency = 0;\n    var requestTimeout = 0;\n    var keepAliveMessage = false;\n    var sessionTimeout = false;\n    // 1/10 of session timeout, max of 10 minutes.\n    var checkFrequency = Math.min((Config.sessiontimeout / 10), 600) * 1000;\n    // Check if sessiontimeoutwarning is set or double the checkFrequency.\n    var warningLimit = (Config.sessiontimeoutwarning > 0) ? (Config.sessiontimeoutwarning * 1000) : (checkFrequency * 2);\n    // First wait is minimum of remaining time or half of the session timeout.\n    var firstWait = (Config.sessiontimeoutwarning > 0) ?\n        Math.min((Config.sessiontimeout - Config.sessiontimeoutwarning) * 1000, checkFrequency * 5) : checkFrequency * 5;\n    /**\n     * The session time has expired - we can't extend it now.\n     * @param {Modal} modal\n     */\n    var timeoutSessionExpired = function(modal) {\n        sessionTimeout = true;\n        warningDisplayed = false;\n        closeModal(modal);\n        displaySessionExpired();\n    };\n\n    /**\n     * Close modal - this relies on modal object passed from Notification.confirm.\n     *\n     * @param {Modal} modal\n     */\n    var closeModal = function(modal) {\n        modal.destroy();\n    };\n\n    /**\n     * The session time has expired - we can't extend it now.\n     * @return {Promise}\n     */\n    var displaySessionExpired = function() {\n        // Check again if its already extended before displaying session expired popup in case multiple tabs are open.\n        var request = {\n            methodname: 'core_session_time_remaining',\n            args: { }\n        };\n\n        return Ajax.call([request], true, true, true)[0].then(function(args) {\n            if (args.timeremaining * 1000 > warningLimit) {\n                return false;\n            } else {\n                return Str.get_strings([\n                    {key: 'sessionexpired', component: 'error'},\n                    {key: 'sessionerroruser', component: 'error'},\n                    {key: 'loginagain', component: 'moodle'},\n                    {key: 'cancel', component: 'moodle'}\n                ]).then(function(strings) {\n                    Notification.confirm(\n                        strings[0], // Title.\n                        strings[1], // Message.\n                        strings[2], // Login Again.\n                        strings[3], // Cancel.\n                        function() {\n                            location.reload();\n                            return true;\n                        }\n                    );\n                    return true;\n                }).catch(Notification.exception);\n            }\n        });\n    };\n\n    /**\n     * Ping the server to keep the session alive.\n     *\n     * @return {Promise}\n     */\n    var touchSession = function() {\n        var request = {\n            methodname: 'core_session_touch',\n            args: { }\n        };\n\n        if (sessionTimeout) {\n            // We timed out before we extended the session.\n            return displaySessionExpired();\n        } else {\n            return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {\n                if (keepAliveFrequency > 0) {\n                    setTimeout(touchSession, keepAliveFrequency);\n                }\n                return true;\n            }).catch(function() {\n                Notification.alert('', keepAliveMessage);\n            });\n        }\n    };\n\n    /**\n     * Ask the server how much time is remaining in this session and\n     * show confirm/cancel notifications if the session is about to run out.\n     *\n     * @return {Promise}\n     */\n    var checkSession = function() {\n        var request = {\n            methodname: 'core_session_time_remaining',\n            args: { }\n        };\n        sessionTimeout = false;\n        return Ajax.call([request], true, true, true)[0].then(function(args) {\n            if (args.userid <= 0) {\n                return false;\n            }\n            if (args.timeremaining <= 0) {\n                return displaySessionExpired();\n            } else if (args.timeremaining * 1000 <= warningLimit && !warningDisplayed) {\n                warningDisplayed = true;\n                Str.get_strings([\n                    {key: 'norecentactivity', component: 'moodle'},\n                    {key: 'sessiontimeoutsoon', component: 'moodle'},\n                    {key: 'extendsession', component: 'moodle'},\n                    {key: 'cancel', component: 'moodle'}\n                ]).then(function(strings) {\n                     return Notification.confirm(\n                        strings[0], // Title.\n                        strings[1], // Message.\n                        strings[2], // Extend session.\n                        strings[3], // Cancel.\n                        function() {\n                            touchSession();\n                            warningDisplayed = false;\n                            // First wait is minimum of remaining time or half of the session timeout.\n                            setTimeout(checkSession, firstWait);\n                            return true;\n                        },\n                        function() {\n                            // User has cancelled notification.\n                            setTimeout(checkSession, checkFrequency);\n                        }\n                    );\n                }).then(modal => {\n                    // If we don't extend the session before the timeout - warn.\n                    setTimeout(timeoutSessionExpired, args.timeremaining * 1000, modal);\n                    return;\n                }).catch(Notification.exception);\n            } else {\n                setTimeout(checkSession, checkFrequency);\n            }\n            return true;\n        });\n        // We do not catch the fails from the above ajax call because they will fail when\n        // we are not logged in - we don't need to take any action then.\n    };\n\n    /**\n     * Start calling a function to check if the session is still alive.\n     */\n    var start = function() {\n        if (keepAliveFrequency > 0) {\n            setTimeout(touchSession, keepAliveFrequency);\n        } else {\n            // First wait is minimum of remaining time or half of the session timeout.\n            setTimeout(checkSession, firstWait);\n        }\n    };\n\n    /**\n     * Are we in an iframe and the parent page is from the same Moodle site?\n     *\n     * @return {boolean} true if we are in an iframe in a page from this Moodle site.\n     */\n    const isMoodleIframe = function() {\n        if (window.parent === window) {\n            // Not in an iframe.\n            return false;\n        }\n\n        // We are in an iframe. Is the parent from the same Moodle site?\n        let parentUrl;\n        try {\n            parentUrl = window.parent.location.href;\n        } catch (e) {\n            // If we cannot access the URL of the parent page, it must be another site.\n            return false;\n        }\n\n        return parentUrl.startsWith(M.cfg.wwwroot);\n    };\n\n    /**\n     * Don't allow more than one of these polling loops in a single page.\n     */\n    var init = function() {\n        // We only allow one concurrent instance of this checker.\n        if (started) {\n            return;\n        }\n        started = true;\n\n        if (isMoodleIframe()) {\n            window.console.log('Not starting Moodle session timeout warning in this iframe.');\n            return;\n        }\n\n        window.console.log('Starting Moodle session timeout warning.');\n\n        start();\n    };\n\n    /**\n     * Start polling with more specific values for the frequency, timeout and message.\n     *\n     * @param {number} freq How ofter to poll the server.\n     * @param {number} timeout The time to wait for each request to the server.\n     * @param {string} identifier The string identifier for the message to show if session is going to time out.\n     * @param {string} component The string component for the message to show if session is going to time out.\n     */\n    var keepalive = async function(freq, timeout, identifier, component) {\n        // We only allow one concurrent instance of this checker.\n        if (started) {\n            window.console.warn('Ignoring session keep-alive. The core/network module was already initialised.');\n            return;\n        }\n        started = true;\n\n        if (isMoodleIframe()) {\n            window.console.warn('Ignoring session keep-alive in this iframe inside another Moodle page.');\n            return;\n        }\n\n        window.console.log('Starting Moodle session keep-alive.');\n\n        keepAliveFrequency = freq * 1000;\n        keepAliveMessage = await Str.get_string(identifier, component);\n        requestTimeout = timeout * 1000;\n        start();\n    };\n\n    return {\n        keepalive: keepalive,\n        init: init\n    };\n});\n"],"names":["define","$","Ajax","Config","Notification","Str","started","warningDisplayed","keepAliveFrequency","requestTimeout","keepAliveMessage","sessionTimeout","checkFrequency","Math","min","sessiontimeout","warningLimit","sessiontimeoutwarning","firstWait","timeoutSessionExpired","modal","closeModal","displaySessionExpired","destroy","call","methodname","args","then","timeremaining","get_strings","key","component","strings","confirm","location","reload","catch","exception","touchSession","setTimeout","alert","checkSession","userid","start","isMoodleIframe","window","parent","parentUrl","href","e","startsWith","M","cfg","wwwroot","keepalive","async","freq","timeout","identifier","console","warn","log","get_string","init"],"mappings":";;;;;;;AAsBAA,sBAAO,CAAC,SAAU,YAAa,cAAe,oBAAqB,aAC3D,SAASC,EAAGC,KAAMC,OAAQC,aAAcC,SAExCC,SAAU,EACVC,kBAAmB,EACnBC,mBAAqB,EACrBC,eAAiB,EACjBC,kBAAmB,EACnBC,gBAAiB,EAEjBC,eAA+D,IAA9CC,KAAKC,IAAKX,OAAOY,eAAiB,GAAK,KAExDC,aAAgBb,OAAOc,sBAAwB,EA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