User:Peterneubauer/mapillary.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload

// Based on Gadget-CropTool, Gadget-Tineye.js and User:Peterneubauer/mapillary.js
(function(mw, $) {
	if (mw.config.get('wgNamespaceNumber') !== 6) return;
	if (mw.config.get('wgAction') != 'view') return;
	
	// could a new separate css file would break compatibility with current userscript?
	var customStyle = '<style>#mapillary-commons-upload {position: absolute;top: 0;right: 0;margin: auto;background: #FFF;border: 2px solid #36AF6D;padding: 5px;border-radius: 4px;color: #36AF6D;margin-top: 5px;margin-right: 5px;font-weight: bold;display: none;}</style>';
	$('head').append(customStyle);

 	var keys = $('.mapillary_key');
 	if (keys.length > 0 && $(keys[0]).text().length > 0) {
		$(function() {
			mw.loader.load('https://unpkg.com/mapillary-js@2.6.0/dist/mapillary.min.css', 'text/css');
			$.getScript('https://unpkg.com/mapillary-js@2.6.0/dist/mapillary.min.js', function () {
				$('#mw-content-text')
					.append('<h2>Mapillary View</h2>')
					.append('<div id="mapillary" style="width: 640px; height: 480px;"></div>');
				var key = $(keys[0]).text();
				var viewer = new Mapillary.Viewer('mapillary', 'WHZlUV9FNXhFZ24xZEZQRHZzUlZ3QTplNDE1Y2RiMWY3MTliZDc0', key);
				
				// upload button
				$('#mapillary').append('<a id="mapillary-commons-upload" href="">Upload</a>');
				var uploadEndpoint = 'https://tools.wmflabs.org/mapillary-commons/mapillary2commons?mapillary=';
				var uploadBtn = $('#mapillary-commons-upload');
				viewer.on(Mapillary.Viewer.nodechanged, function(node) {
					if (node.key !== key) {
						uploadBtn.attr('href', uploadEndpoint + node.key);
						uploadBtn.show();
					} else {
						uploadBtn.hide();
					}
				});
			});
		});
 	}
})(mediaWiki, jQuery);