2020-11-18 09:11:12 +00:00
|
|
|
// originally taken from https://stackoverflow.com/questions/1517924/javascript-mapping-touch-events-to-mouse-events
|
2020-03-29 20:36:10 +00:00
|
|
|
function touchEventConverter( event ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const first = event.changedTouches[ 0 ];
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
event.preventDefault();
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2023-08-24 08:59:26 +00:00
|
|
|
const type = {
|
|
|
|
touchstart: 'mousedown',
|
|
|
|
touchmove: 'mousemove',
|
|
|
|
touchend: 'mouseup'
|
|
|
|
}[ event.type ];
|
|
|
|
if ( !type ) {
|
|
|
|
return;
|
2020-11-18 09:11:12 +00:00
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2023-06-22 09:41:37 +00:00
|
|
|
let simulatedEvent;
|
2020-11-18 09:11:12 +00:00
|
|
|
if ( typeof MouseEvent !== 'undefined' ) {
|
|
|
|
simulatedEvent = new MouseEvent( type, {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
view: window,
|
|
|
|
detail: 1,
|
|
|
|
screenX: first.screenX,
|
|
|
|
screenY: first.screenY,
|
|
|
|
clientX: first.clientX,
|
|
|
|
clientY: first.clientY,
|
|
|
|
button: 0,
|
|
|
|
relatedTarget: null
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
simulatedEvent = document.createEvent( 'MouseEvent' );
|
|
|
|
simulatedEvent.initMouseEvent(
|
|
|
|
type, true, true, window, 1,
|
|
|
|
first.screenX, first.screenY,
|
|
|
|
first.clientX, first.clientY,
|
|
|
|
false, false, false, false,
|
|
|
|
0, null
|
|
|
|
);
|
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
first.target.dispatchEvent( simulatedEvent );
|
2020-03-29 20:36:10 +00:00
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
// fixes issues with zoomed Chrome on touch see https://github.com/jquery/jquery/issues/3187
|
2020-03-29 20:36:10 +00:00
|
|
|
function correctElementOffsets( offset ) {
|
2024-01-27 12:12:09 +00:00
|
|
|
const isChrome = /Chrom(e|ium)/i.test( navigator.userAgent );
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
// since this problem only seems to appear with Chrome just use this in Chrome
|
|
|
|
if ( !isChrome ) {
|
|
|
|
return offset;
|
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
// get document element width without scrollbar
|
2023-06-22 09:41:37 +00:00
|
|
|
const prevStyle = document.body.style.overflow || '';
|
2020-11-18 09:11:12 +00:00
|
|
|
document.body.style.overflow = 'hidden';
|
2023-06-22 09:41:37 +00:00
|
|
|
const docWidth = document.documentElement.clientWidth;
|
2020-11-18 09:11:12 +00:00
|
|
|
document.body.style.overflow = prevStyle;
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
// determine if the viewport has been scaled
|
|
|
|
if ( docWidth / window.innerWidth !== 1 ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const docRect = document.documentElement.getBoundingClientRect();
|
2020-11-18 09:11:12 +00:00
|
|
|
offset = {
|
|
|
|
top: offset.top - window.pageYOffset - docRect.top,
|
|
|
|
left: offset.left - window.pageXOffset - docRect.left
|
|
|
|
};
|
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
return offset;
|
2020-03-29 20:36:10 +00:00
|
|
|
}
|
2017-05-03 13:54:30 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Based on jQuery RTL Scroll Type Detector plugin by othree: https://github.com/othree/jquery.rtl-scroll-type
|
|
|
|
*
|
|
|
|
* @return {string} - 'default', 'negative' or 'reverse'
|
|
|
|
*/
|
2020-03-29 20:36:10 +00:00
|
|
|
function determineRtlScrollType() {
|
2023-06-22 09:41:37 +00:00
|
|
|
const $dummy = $( '<div>' )
|
2020-11-18 09:11:12 +00:00
|
|
|
.css( {
|
|
|
|
dir: 'rtl',
|
|
|
|
width: '4px',
|
|
|
|
height: '1px',
|
|
|
|
position: 'absolute',
|
|
|
|
top: '-1000px',
|
|
|
|
overflow: 'scroll'
|
|
|
|
} )
|
2024-02-07 16:00:14 +00:00
|
|
|
.text( 'ABCDE' )
|
2020-11-18 09:11:12 +00:00
|
|
|
.appendTo( 'body' )[ 0 ];
|
|
|
|
if ( $dummy.scrollLeft > 0 ) {
|
|
|
|
return 'default';
|
|
|
|
} else {
|
|
|
|
$dummy.scrollLeft = 1;
|
|
|
|
if ( $dummy.scrollLeft === 0 ) {
|
|
|
|
return 'negative';
|
2017-07-11 11:51:56 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
}
|
|
|
|
return 'reverse';
|
2020-03-29 20:36:10 +00:00
|
|
|
}
|
2017-07-11 11:51:56 +00:00
|
|
|
|
2020-03-29 20:36:10 +00:00
|
|
|
function calculateRevisionsPerWindow( margin, revisionWidth ) {
|
2020-11-18 09:11:12 +00:00
|
|
|
return Math.floor( ( $( '#mw-content-text' ).width() - margin ) / revisionWidth );
|
2020-03-29 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
calculateRevisionsPerWindow: calculateRevisionsPerWindow,
|
|
|
|
correctElementOffsets: correctElementOffsets,
|
|
|
|
determineRtlScrollType: determineRtlScrollType,
|
|
|
|
touchEventConverter: touchEventConverter
|
2020-11-18 09:11:12 +00:00
|
|
|
};
|