User:MichaelSchoenitzer/updown.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(function() {
	var qid = mw.config.get( 'wbEntityId' );
    if ( !qid ) {
        return;
    }
    
	importStylesheet( 'User:MichaelSchoenitzer/updown.css' );
	
	// Jump to the next property
	function next() {
		var props = $('.wikibase-listview > .wikibase-statementgroupview');
		// Loop over all properties until we find the first one currently visible
		for( var num = 0; num < props.length; num++ ) {
			var self = props.eq(num);
			if($(window).scrollTop() + 5 < self.offset().top) {
				self = props.eq(num++);
			 	$('html, body').animate({
	        	   scrollTop: self.offset().top + 'px'
			    }, 'fast');
			    break;
			}
		}
	}
	
	// Jump to the previous property
	function previous() {
		var props = $('.wikibase-listview > .wikibase-statementgroupview');
		// Loop backward over all properties until we find the first one not visible
		for( var num = props.length-1; num >= 0; num-- ) {
			var self = props.eq(num);
			if($(window).scrollTop() - 5 > self.offset().top) {
				self = props.eq(num--);
				$('html, body').animate({
			    	scrollTop: self.offset().top + 'px'
			    }, 'fast');
			    break;
			}
		}
	}
	
	function togglemenue() {
		if($('#qTOC').length === 0) {
			$("<div></div>").prependTo('body').attr("id","qTOC");
			$("<a href=''></a>")
			.text('x')
			.appendTo('#qTOC')
			.addClass("closebtn")
			.on('click',  function(e) {
			        e.preventDefault();
			        togglemenue();
			});
		
			var props = $('.wikibase-listview > .wikibase-statementgroupview');
			// Loop over all properties until we find the first one currently visible
			for( var num = 0; num < props.length; num++ ) {
				let prop = props.eq(num);
				let id = prop.attr('id');
				let title = prop.find('.wikibase-statementgroupview-property-label > a').html();
				$("<a href='#' class='qtoc-" + id + "'></a><br/>")
				.text(title)
				.appendTo('#qTOC')
				.on('click', function(e) {
			        e.preventDefault();
			        $('html, body').animate({
			            scrollTop: prop.offset().top + 'px'
			        }, 'fast');
			    });
			}
		}
		else {
			$('#qTOC').toggle();
		}
	}
	
	$( this )
	.keydown( function ( event ) {
		if (
			!event.ctrlKey &&
			!event.altKey &&
			!event.shiftKey &&
			!event.metaKey &&
			!$( ':focus' ).is("textarea") &&
			!$( ':focus' ).is("input")
		) {
			console.log("keyevent");
			switch ( event.keyCode ) {
				case 74: next(); return false; // j
				case 75: previous(); return false; // k
				case 84: togglemenue(); return false; // t
			}
		}
	});
	
	// Add Button to open menue
	$('<div></div>')
	.appendTo('body')
	.attr("id","openqTOC")
	.text("☰")
	.on('click',  function(e) {
		e.preventDefault();
		togglemenue();
	});
	
	// Add up/down arrows to statemts with lots of values
	$('.wikibase-listview > .wikibase-statementgroupview').each(function(prop) {
	 var self = $(this);
	 if(self.find('.wikibase-statementview').length > 8) {
	 	var label = $(this).find('.wikibase-statementgroupview-property-label');
	 	// Add link to first value
	 	$("<br><a href='#'>↑</a>")
	 	.appendTo(label)
	 	.on('click', function(e) {
	        e.preventDefault();
	        console.log("up");
	        $('html, body').animate({
	            scrollTop: self.find(".wikibase-statementview").first().offset().top + 'px'
	        }, 'fast');
	    });
	    
	    $("<span></span>")
	 	.appendTo(label)
	 	.text("("+self.find('.wikibase-statementview').length+")");
	    
	    // Add link to last value
	 	$("<a href='#'>↓</a>")
	 	.appendTo(label)
	 	.on('click', function(e) {
	        e.preventDefault();
	        console.log("down");
	        $('html, body').animate({
	            scrollTop: self.find(".wikibase-statementview").last().offset().top + 'px'
	        }, 'fast');
	    });
	 }
	});
});

//# sourceURL=MichaelSchoenitzer_updown.js