{"version":3,"file":"network.min.js","sources":["https:\/\/lms.spbstu.ru\/lib\/amd\/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 .\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 \/\/ 1\/5 of sessiontimeout.\n var warningLimit = checkFrequency * 2;\n\n \/**\n * The session time has expired - we can't extend it now.\n *\/\n var timeoutSessionExpired = function() {\n sessionTimeout = true;\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 Str.get_strings([\n {key: 'sessionexpired', component: 'error'},\n {key: 'sessionerroruser', component: 'error'}\n ]).then(function(strings) {\n Notification.alert(\n strings[0], \/\/ Title.\n strings[1] \/\/ Message.\n );\n return true;\n }).fail(Notification.exception);\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 }).fail(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\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 Str.get_strings([\n {key: 'sessionexpired', component: 'error'},\n {key: 'sessionerroruser', component: 'error'}\n ]).then(function(strings) {\n Notification.alert(\n strings[0], \/\/ Title.\n strings[1] \/\/ Message.\n );\n return true;\n }).fail(Notification.exception);\n\n } else if (args.timeremaining * 1000 < warningLimit && !warningDisplayed) {\n \/\/ If we don't extend the session before the timeout - warn.\n setTimeout(timeoutSessionExpired, args.timeremaining * 1000);\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 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 half the session timeout.\n setTimeout(checkSession, checkFrequency * 5);\n return true;\n },\n function() {\n warningDisplayed = false;\n setTimeout(checkSession, checkFrequency);\n }\n );\n return true;\n }).fail(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 half the session timeout.\n setTimeout(checkSession, checkFrequency * 5);\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} message The message to display if the session is going to time out.\n *\/\n var keepalive = function(freq, timeout, message) {\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 = message;\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","timeoutSessionExpired","touchSession","get_strings","key","component","then","strings","alert","fail","exception","call","methodname","args","setTimeout","checkSession","userid","timeremaining","confirm","start","isMoodleIframe","window","parent","parentUrl","location","href","e","startsWith","M","cfg","wwwroot","keepalive","freq","timeout","message","console","warn","log","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,aAAgC,EAAjBJ,eAKfK,sBAAwB,WACxBN,gBAAiB,GAQjBO,aAAe,SAAfA,sBAMIP,eAEON,IAAIc,YAAY,CACnB,CAACC,IAAK,iBAAkBC,UAAW,SACnC,CAACD,IAAK,mBAAoBC,UAAW,WACtCC,MAAK,SAASC,gBACbnB,aAAaoB,MACTD,QAAQ,GACRA,QAAQ,KAEL,KACRE,KAAKrB,aAAasB,WAEdxB,KAAKyB,KAAK,CAlBP,CACVC,WAAY,qBACZC,KAAM,MAgBsB,GAAM,GAAM,EAAOpB,gBAAgB,GAAGa,MAAK,kBAC\/Dd,mBAAqB,GACrBsB,WAAWZ,aAAcV,qBAEtB,KACRiB,MAAK,WACJrB,aAAaoB,MAAM,GAAId,sBAW\/BqB,aAAe,SAAfA,sBAMApB,gBAAiB,EACVT,KAAKyB,KAAK,CANH,CACVC,WAAY,8BACZC,KAAM,MAIkB,GAAM,GAAM,GAAM,GAAGP,MAAK,SAASO,cACvDA,KAAKG,QAAU,KAGfH,KAAKI,cAAgB,EACrB5B,IAAIc,YAAY,CACZ,CAACC,IAAK,iBAAkBC,UAAW,SACnC,CAACD,IAAK,mBAAoBC,UAAW,WACtCC,MAAK,SAASC,gBACbnB,aAAaoB,MACTD,QAAQ,GACRA,QAAQ,KAEL,KACRE,KAAKrB,aAAasB,WAEO,IAArBG,KAAKI,cAAuBjB,eAAiBT,kBAEpDuB,WAAWb,s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