| 1 |
|
| 2 |
(function() { |
| 3 |
var platform = window.pywebview.platform; |
| 4 |
var disableText = '%(text_select)s' === 'False'; |
| 5 |
var disableTextCss = 'body {-webkit-user-select: none; -khtml-user-select: none; -ms-user-select: none; user-select: none; cursor: default;}' |
| 6 |
|
| 7 |
if (platform == 'mshtml') { |
| 8 |
window.alert = function(msg) { |
| 9 |
window.external.alert(msg); |
| 10 |
} |
| 11 |
} else if (platform == 'edgechromium') { |
| 12 |
window.alert = function (message) { |
| 13 |
window.chrome.webview.postMessage(['_pywebviewAlert', pywebview.stringify(message), 'alert']); |
| 14 |
} |
| 15 |
} else if (platform == 'gtkwebkit2') { |
| 16 |
window.alert = function (message) { |
| 17 |
window.webkit.messageHandlers.jsBridge.postMessage(pywebview.stringify({funcName: '_pywebviewAlert', params: message, id: 'alert'})); |
| 18 |
} |
| 19 |
} else if (platform == 'cocoa') { |
| 20 |
window.print = function() { |
| 21 |
window.webkit.messageHandlers.browserDelegate.postMessage('print'); |
| 22 |
} |
| 23 |
} else if (platform === 'qtwebengine') { |
| 24 |
window.alert = function (message) { |
| 25 |
window.pywebview._QWebChannel.objects.external.call('_pywebviewAlert', pywebview.stringify(message), 'alert'); |
| 26 |
} |
| 27 |
} else if (platform === 'qtwebkit') { |
| 28 |
window.alert = function (message) { |
| 29 |
window.external.invoke(JSON.stringify(['_pywebviewAlert', message, 'alert'])); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
if (disableText) { |
| 34 |
var css = document.createElement("style"); |
| 35 |
css.type = "text/css"; |
| 36 |
css.innerHTML = disableTextCss; |
| 37 |
document.head.appendChild(css); |
| 38 |
} |
| 39 |
|
| 40 |
function disableTouchEvents() { |
| 41 |
var initialX = 0; |
| 42 |
var initialY = 0; |
| 43 |
|
| 44 |
function onMouseMove(ev) { |
| 45 |
var x = ev.screenX - initialX; |
| 46 |
var y = ev.screenY - initialY; |
| 47 |
window.pywebview._jsApiCallback('pywebviewMoveWindow', [x, y], 'move'); |
| 48 |
} |
| 49 |
|
| 50 |
function onMouseUp() { |
| 51 |
window.removeEventListener('mousemove', onMouseMove); |
| 52 |
window.removeEventListener('mouseup', onMouseUp); |
| 53 |
} |
| 54 |
|
| 55 |
function onMouseDown(ev) { |
| 56 |
if ( |
| 57 |
'%(drag_region_direct_target_only)s' === 'True' && |
| 58 |
!ev.target.matches('%(drag_selector)s') |
| 59 |
) { |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
initialX = ev.clientX; |
| 64 |
initialY = ev.clientY; |
| 65 |
window.addEventListener('mouseup', onMouseUp); |
| 66 |
window.addEventListener('mousemove', onMouseMove); |
| 67 |
} |
| 68 |
|
| 69 |
function onBodyMouseDown(event) { |
| 70 |
var target = event.target; |
| 71 |
var dragSelectorElements = document.querySelectorAll('%(drag_selector)s'); |
| 72 |
|
| 73 |
while (target && target !== document.body && target !== document.documentElement) { |
| 74 |
if (target.nodeType === 1) { |
| 75 |
// Check if target matches the drag selector |
| 76 |
for (var i = 0; i < dragSelectorElements.length; i++) { |
| 77 |
if (dragSelectorElements[i] === target) { |
| 78 |
onMouseDown(event); |
| 79 |
return; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// If it doesn't match, continue up the DOM tree |
| 85 |
target = target.parentNode; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
document.body.addEventListener('mousedown', onBodyMouseDown); |
| 90 |
|
| 91 |
// easy drag for edge chromium |
| 92 |
if ('%(easy_drag)s' === 'True') { |
| 93 |
window.addEventListener('mousedown', onMouseDown); |
| 94 |
} |
| 95 |
|
| 96 |
if ('%(zoomable)s' === 'False') { |
| 97 |
document.body.addEventListener('touchstart', function(e) { |
| 98 |
if ((e.touches.length > 1) || e.targetTouches.length > 1) { |
| 99 |
e.preventDefault(); |
| 100 |
e.stopPropagation(); |
| 101 |
e.stopImmediatePropagation(); |
| 102 |
} |
| 103 |
}, {passive: false}); |
| 104 |
|
| 105 |
window.addEventListener('wheel', function (e) { |
| 106 |
if (e.ctrlKey) { |
| 107 |
e.preventDefault(); |
| 108 |
} |
| 109 |
}, {passive: false}); |
| 110 |
} |
| 111 |
|
| 112 |
// draggable |
| 113 |
if ('%(draggable)s' === 'False') { |
| 114 |
document.addEventListener('dragstart', function(e) { |
| 115 |
if (e.target.tagName === 'IMG' || e.target.tagName === 'A') { |
| 116 |
e.preventDefault(); |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
disableTouchEvents(); |
| 123 |
})(); |