|
6 | 6 | if($lockElementId > 0) { |
7 | 7 | ?> |
8 | 8 | <script> |
9 | | - // Trigger unlock when leaving window |
10 | | - var form_save = false; |
11 | | - jQuery(window).on('beforeunload', function(){ |
12 | | - var stay = jQuery('#stay').val(); |
13 | | - var lastClickedElement = localStorage.getItem('MODX_lastClickedElement'); |
14 | | - var sameElement = false; |
15 | | - if(lastClickedElement != null) { |
16 | | - try { |
17 | | - lastClickedElement = JSON.parse( lastClickedElement ); |
18 | | - sameElement = lastClickedElement[0]==<?php echo $lockElementType;?> && lastClickedElement[1]==<?php echo $lockElementId;?> ? true : false; |
19 | | - } catch(err) { |
20 | | - console.log(err); |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - // Trigger unlock |
25 | | - if((stay != 2 || !form_save) && !sameElement) { |
26 | | - // console.log('unlock triggered:', 'stay='+stay, 'form_save='+form_save, 'sameElement='+sameElement, lastClickedElement); |
27 | | - var unlockRequest = new Ajax('index.php?a=67&type=<?php echo $lockElementType;?>&id=<?php echo $lockElementId;?>&o=' + Math.random(), { |
28 | | - method: 'get' |
29 | | - }).request(); |
| 9 | + // Polyfill for Navigator.sendBeacon |
| 10 | + (function(root) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + function sendBeacon(url, data) { |
| 14 | + var xhr = ('XMLHttpRequest' in window) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); |
| 15 | + xhr.open('POST', url, false); |
| 16 | + xhr.setRequestHeader('Accept', '*/*'); |
| 17 | + if (typeof data === 'string') { |
| 18 | + xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); |
| 19 | + xhr.responseType = 'text/plain'; |
| 20 | + } else if (Object.prototype.toString.call(data) === '[object Blob]') { |
| 21 | + if (data.type) { |
| 22 | + xhr.setRequestHeader('Content-Type', data.type); |
| 23 | + } |
| 24 | + } |
| 25 | + xhr.send(data); |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + if (typeof exports !== 'undefined') { |
| 30 | + if (typeof module !== 'undefined' && module.exports) { |
| 31 | + exports = module.exports = sendBeacon; |
| 32 | + } |
| 33 | + exports.sendBeacon = sendBeacon; |
| 34 | + } else if (typeof define === 'function' && define.amd) { |
| 35 | + define([], function() { |
| 36 | + return sendBeacon; |
| 37 | + }); |
| 38 | + } else if ('navigator' in root && !('sendBeacon' in root.navigator)) { |
| 39 | + root.navigator.sendBeacon = sendBeacon; |
| 40 | + } |
| 41 | + })(this); |
| 42 | + |
| 43 | + // Trigger unlock when leaving window |
| 44 | + var form_save = false; |
| 45 | + window.addEventListener('unload', unlockThisElement, false); |
| 46 | + |
| 47 | + function unlockThisElement() { |
| 48 | + var stay = jQuery('#stay').val(); |
| 49 | + var lastClickedElement = localStorage.getItem('MODX_lastClickedElement'); |
| 50 | + var sameElement = false; |
| 51 | + if(lastClickedElement != null) { |
| 52 | + try { |
| 53 | + lastClickedElement = JSON.parse( lastClickedElement ); |
| 54 | + sameElement = lastClickedElement[0]==<?php echo $lockElementType;?> && lastClickedElement[1]==<?php echo $lockElementId;?> ? true : false; |
| 55 | + } catch(err) { |
| 56 | + console.log(err); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Trigger unlock |
| 61 | + if((stay != 2 || !form_save) && !sameElement) { |
| 62 | + // console.log('unlock triggered:', 'stay='+stay, 'form_save='+form_save, 'sameElement='+sameElement, lastClickedElement); |
| 63 | + navigator.sendBeacon('index.php?a=67&type=<?php echo $lockElementType;?>&id=<?php echo $lockElementId;?>&o=' + Math.random()); |
30 | 64 | top.mainMenu.reloadtree(); |
31 | | - } |
32 | | - }); |
| 65 | + } |
| 66 | + } |
33 | 67 | </script> |
34 | 68 | <?php |
35 | 69 | } |
|
0 commit comments