« MediaWiki:Gadget-tour-loader.js » : différence entre les versions

De Wikimedica
Aucun résumé des modifications
(Dépendances déplacées dans la définition du gadget)
Ligne 1 : Ligne 1 :
// Load the tour launcher asynchronously.
mw.guidedTour.loader = { // Hook to the mw.guidedTour to make the launcher globally accessible.
mw.loader.using("ext.guidedTour.launcher", function()
tours: {
{
basic_navigation: {
mw.guidedTour.loader = { // Hook to the mw.guidedTour to make the launcher globally accessible.
requires: [], // List of tours that must have been completed for this tour to launch.
tours: {
startDelayFromRegistration: 300, // Delay in seconds the tour must wait after registration to launch.
basic_navigation: {
skipDelay: 60 * 60 * 24, // Delay in seconds the tour shown again after bein skipped.
requires: [], // List of tours that must have been completed for this tour to launch.
lauchProbability: 1, // Probability the tour will launch once we are past startDelayFromLogin.
startDelayFromRegistration: 300, // Delay in seconds the tour must wait after registration to launch.
loggedInUsersOnly: true, // If the tour is only for logged in users.
skipDelay: 60 * 60 * 24, // Delay in seconds the tour shown again after bein skipped.
canEditPage: true, // If the tour can only launch on pages a user can edit.
lauchProbability: 1, // Probability the tour will launch once we are past startDelayFromLogin.
excludePages: ["^Accueil$", "^Wikimedica.", "^Classe.", "^Modèle."], // Exclude pages (regex).
loggedInUsersOnly: true, // If the tour is only for logged in users.
debug: true // Tour is in debug mode (not lauched when ready)
canEditPage: true, // If the tour can only launch on pages a user can edit.
}
excludePages: ["^Accueil$", "^Wikimedica.", "^Classe.", "^Modèle."], // Exclude pages (regex).
},
debug: true // Tour is in debug mode (not lauched when ready)
}
/*
},
* Checks if all conditions for a tour to launch are met.
*/
canLaunch: function(name, tour)
{
// Normalize configuration.
if(tour.startDelayFromRegistration === undefined) { tour.startDelayFromRegistration = 0; }
if(tour.skipDelay === undefined) { tour.skipDelay = false; }
if(tour.launchProbability === undefined) { tour.lauchProbability = 1; }
if(tour.loggedInUsersOnly === undefined) { tour.loggedInUsersOnly = true; }
if(tour.canEditPage === undefined) { tour.canEditPage = true; }
if(tour.excludePages === undefined) { tour.excludePages = []; }
if(tour.requires === undefined) { tour.requires = []; }
cookiePrefix = mw.config.get("wgCookiePrefix") + "-mw-tour-";
// If the tour has been completed or abandoned.
if($.cookie(cookiePrefix + name + "-completed") || $.cookie(cookiePrefix + name + "-abandoned"))
{
return false;
}
// ------------------- startDelayfromRegistration -----------------------------
d = new Date(mw.user.getRegistration());
// Not past the delay yet.
if(d.getTime() + tour.startDelayFromRegistration > Date.now()) { return false; }
/*
// ------------------- skipDelay -----------------------------
* Checks if all conditions for a tour to launch are met.
if(tour.skipDelay !== false)
*/
canLaunch: function(name, tour)
{
{
// Normalize configuration.
skipped = $.cookie(cookiePrefix + name + "-skipped");
if(tour.startDelayFromRegistration === undefined) { tour.startDelayFromRegistration = 0; }
if(tour.skipDelay === undefined) { tour.skipDelay = false; }
if(tour.launchProbability === undefined) { tour.lauchProbability = 1; }
if(tour.loggedInUsersOnly === undefined) { tour.loggedInUsersOnly = true; }
if(tour.canEditPage === undefined) { tour.canEditPage = true; }
if(tour.excludePages === undefined) { tour.excludePages = []; }
if(tour.requires === undefined) { tour.requires = []; }
cookiePrefix = mw.config.get("wgCookiePrefix") + "-mw-tour-";
if(skipped && (skipped + tour.skipDelay > Date.now())) { return false; } // Tour has been skipped for now.
else if(skipped) { $.removeCookie(cookiePrefix + name + "-skipped"); }
// If the tour has been completed or abandoned.
}
if($.cookie(cookiePrefix + name + "-completed") || $.cookie(cookiePrefix + name + "-abandoned"))
// ------------------- launchProbability -----------------------------
if(tour.launchProbability < Math.random()) { return false; }
// ------------------- loggedInUsersOnly -----------------------------
// The tour required the user to be logged in.
if(tour.loggedInUsersOnly && mw.user.isAnon()) { return false; }
// ------------------- canEditPage -----------------------------
// The tour requires the user to be able to edit the current page.
if(tour.canEditPage && !mw.config.get('wgIsProbablyEditable')) { return false; }
// ------------------- excludePages -----------------------------
page = mw.config.get("wgPageName");
for(var i; i > tour.excludePages.length; i++)
{
if(page.test(tour.excludePages[i])) { return false; } // Page is excluded.
}
// ------------------- requires -----------------------------
for(var i; i > tour.requires.length; i++)
{
if($.cookie(cookiePrefix + tour.requires[i] + "-done") === null)
{
{
return false;
return false; // Tour requires a tour that has not been taken yet.
}
}
}
// ------------------- startDelayfromRegistration -----------------------------
d = new Date(mw.user.getRegistration());
return true; // The tour can run
},
// Not past the delay yet.
 
if(d.getTime() + tour.startDelayFromRegistration > Date.now()) { return false; }
/*
* Iterates over tours to launch them.
// ------------------- skipDelay -----------------------------
*/
if(tour.skipDelay !== false)
launch: function()
{
{
skipped = $.cookie(cookiePrefix + name + "-skipped");
console.log('Launching tours ... ');
if(skipped && (skipped + tour.skipDelay > Date.now())) { return false; } // Tour has been skipped for now.
d = $(".guider").css("display");
else if(skipped) { $.removeCookie(cookiePrefix + name + "-skipped"); }
if(d !== undefined && d != "none")
}
{
console.log('currently taking a tour.');
// ------------------- launchProbability -----------------------------
return;
if(tour.launchProbability < Math.random()) { return false; }
}
// ------------------- loggedInUsersOnly -----------------------------
for(const tour in this.tours)
// The tour required the user to be logged in.
{
if(tour.loggedInUsersOnly && mw.user.isAnon()) { return false; }
if(this.canLaunch(tour, this.tours[tour]))  
// ------------------- canEditPage -----------------------------
// The tour requires the user to be able to edit the current page.
if(tour.canEditPage && !mw.config.get('wgIsProbablyEditable')) { return false; }
// ------------------- excludePages -----------------------------
page = mw.config.get("wgPageName");
for(var i; i > tour.excludePages.length; i++)
{
{
if(page.test(tour.excludePages[i])) { return false; } // Page is excluded.
if(this.tours[tour].debug) // Tour is in debug mode.
}
// ------------------- requires -----------------------------
for(var i; i > tour.requires.length; i++)
{
if($.cookie(cookiePrefix + tour.requires[i] + "-done") === null)
{
{
return false; // Tour requires a tour that has not been taken yet.
console.log(tour + ' would have launched (but is in debug mode)');
continue;
}
}
console.log(tour + ' available for launching');
mw.guidedTour.launcher.launchTour(tour);
return; // A tour has launched, stop the loop as we don't want multiple tours to launch.
}
}
}
return true; // The tour can run
},
console.log('none ready to lauch.');
}
};
 
/*
* Override the mw.guidedTour.launcher to add an override to the guiders library.
* This allows cookie handling wether a tour has been launched using this library, from a URL
* or from a cookie while resuming a tour.
*/
mw.guidedTour.launcher.launchTour = function(tourName, tourId)
{
// Prevent tours from being launched within an iframe.
if(window.self != window.top) { return; }
/*
mw.loader.using( 'ext.guidedTour.lib', function ()
* Iterates over tours to launch them.
{
*/
// Override the createGuider method from the guiders library to record tour completion or exit.
launch: function()
var parent = mw.libs.guiders.createGuider;
mw.libs.guiders.createGuider = function ( passedSettings )
{
{
console.log('Launching tours ... ');
parent(passedSettings);
d = $(".guider").css("display");
prefix = mw.config.get("wgCookiePrefix") + "-mw-tour-" + tourName + '-';
if(d !== undefined && d != "none")
{
$.removeCookie(prefix + "skipped");  
console.log('currently taking a tour.');
$.removeCookie(prefix + "abandoned");
return;
$.removeCookie(prefix + "completed");
}
for(const tour in this.tours)
$(".x_button").click(function() {
{
// If there was a complete button on the step.
if(this.canLaunch(tour, this.tours[tour]))  
if($(".mw-tour-complete, .guidedtour-end-button").length)
{
{
if(this.tours[tour].debug) // Tour is in debug mode.
// This counts as a completion.
{
console.log(tourName + ' tour complete');
console.log(tour + ' would have launched (but is in debug mode)');
$.cookie(prefix + "complete", Date.now()); // Tour has been completed.
continue;
}
console.log(tour + ' available for launching');
return;
mw.guidedTour.launcher.launchTour(tour);
return; // A tour has launched, stop the loop as we don't want multiple tours to launch.
}
}
}
console.log(tourName +  ' tour abandoned');
$.cookie(prefix + "abandoned", Date.now()); // Tour has been abandoned.
});
$(".mw-tour-later").click(function() {
console.log(tourName +  ' tour skipped');
$.cookie(prefix + "skipped", Date.now()); // Tour has been postponed.
mw.guidedTour.endTour();
});
$(".mw-tour-complete, .guidedtour-end-button").click(function() {
console.log(tourName +  ' tour completed');
$.cookie(prefix + "completed", Date.now()); // Tour has been completed.
});
console.log('none ready to lauch.');
// Make the guiders draggable so users can move them when they are interfering with an element.
}
// Adapted from: https://www.sanwebe.com/2014/10/draggable-element-with-jquery-no-jquery-ui
};
$('.guider').
css('cursor', 'move').
hover(function() { $(this).css('border-width', '3px'); }, function() { $(this).css('border-width', '1px'); }).
on('mousedown', function(e) {
var dr = $(this).addClass("drag");
height = dr.outerHeight();
width = dr.outerWidth();
ypos = dr.offset().top + height - e.pageY,
xpos = dr.offset().left + width - e.pageX;
$(document.body).on('mousemove', function(e){
var itop = e.pageY + ypos - height;
var ileft = e.pageX + xpos - width;
if(dr.hasClass("drag")){
dr.offset({top: itop,left: ileft});
}
}).on('mouseup', function(e){
dr.removeClass("drag");
});
});
};
mw.guidedTour.launchTour( tourName, tourId );
});
};


/*
console.log('Loaded tour loader gadget');
* Override the mw.guidedTour.launcher to add an override to the guiders library.
* This allows cookie handling wether a tour has been launched using this library, from a URL
* or from a cookie while resuming a tour.
*/
mw.guidedTour.launcher.launchTour = function(tourName, tourId)
{
// Prevent tours from being launched within an iframe.
if(window.self != window.top) { return; }
mw.loader.using( 'ext.guidedTour.lib', function ()
{
// Override the createGuider method from the guiders library to record tour completion or exit.
var parent = mw.libs.guiders.createGuider;
mw.libs.guiders.createGuider = function ( passedSettings )
{
parent(passedSettings);
prefix = mw.config.get("wgCookiePrefix") + "-mw-tour-" + tourName + '-';
$.removeCookie(prefix + "skipped");
$.removeCookie(prefix + "abandoned");
$.removeCookie(prefix + "completed");
$(".x_button").click(function() {
// If there was a complete button on the step.
if($(".mw-tour-complete, .guidedtour-end-button").length)
{
// This counts as a completion.
console.log(tourName +  ' tour complete');
$.cookie(prefix + "complete", Date.now()); // Tour has been completed.
return;
}
console.log(tourName +  ' tour abandoned');
$.cookie(prefix + "abandoned", Date.now()); // Tour has been abandoned.
});
$(".mw-tour-later").click(function() {
console.log(tourName +  ' tour skipped');
$.cookie(prefix + "skipped", Date.now()); // Tour has been postponed.
mw.guidedTour.endTour();
});
$(".mw-tour-complete, .guidedtour-end-button").click(function() {
console.log(tourName +  ' tour completed');
$.cookie(prefix + "completed", Date.now()); // Tour has been completed.
});
// Make the guiders draggable so users can move them when they are interfering with an element.
// Adapted from: https://www.sanwebe.com/2014/10/draggable-element-with-jquery-no-jquery-ui
$('.guider').
css('cursor', 'move').
hover(function() { $(this).css('border-width', '3px'); }, function() { $(this).css('border-width', '1px'); }).
on('mousedown', function(e) {
var dr = $(this).addClass("drag");
height = dr.outerHeight();
width = dr.outerWidth();
ypos = dr.offset().top + height - e.pageY,
xpos = dr.offset().left + width - e.pageX;
$(document.body).on('mousemove', function(e){
var itop = e.pageY + ypos - height;
var ileft = e.pageX + xpos - width;
if(dr.hasClass("drag")){
dr.offset({top: itop,left: ileft});
}
}).on('mouseup', function(e){
dr.removeClass("drag");
});
});
};
mw.guidedTour.launchTour( tourName, tourId );
});
};
console.log('Loaded tour loader gadget');
});

Version du 22 juillet 2020 à 01:03

mw.guidedTour.loader = { // Hook to the mw.guidedTour to make the launcher globally accessible.
	tours: {
		basic_navigation: {
			requires: [], // List of tours that must have been completed for this tour to launch.
			startDelayFromRegistration: 300, // Delay in seconds the tour must wait after registration to launch.
			skipDelay: 60 * 60 * 24, // Delay in seconds the tour shown again after bein skipped.
			lauchProbability: 1, // Probability the tour will launch once we are past startDelayFromLogin.
			loggedInUsersOnly: true, // If the tour is only for logged in users.
			canEditPage: true, // If the tour can only launch on pages a user can edit.
			excludePages: ["^Accueil$", "^Wikimedica.", "^Classe.", "^Modèle."], // Exclude pages (regex).
			debug: true // Tour is in debug mode (not lauched when ready)
		}
	},
	
	/*
	 * Checks if all conditions for a tour to launch are met.
	 */
	canLaunch: function(name, tour)
	{
		// Normalize configuration.
		if(tour.startDelayFromRegistration === undefined) { tour.startDelayFromRegistration = 0; }
		if(tour.skipDelay === undefined) { tour.skipDelay = false; }
		if(tour.launchProbability === undefined) { tour.lauchProbability = 1; }
		if(tour.loggedInUsersOnly === undefined) { tour.loggedInUsersOnly = true; }
		if(tour.canEditPage === undefined) { tour.canEditPage = true; }
		if(tour.excludePages === undefined) { tour.excludePages = []; }
		if(tour.requires === undefined) { tour.requires = []; }
		
		cookiePrefix = mw.config.get("wgCookiePrefix") + "-mw-tour-";
		
		// If the tour has been completed or abandoned.
		if($.cookie(cookiePrefix + name + "-completed") || $.cookie(cookiePrefix + name + "-abandoned"))
		{
			return false;
		}
		
		// ------------------- startDelayfromRegistration -----------------------------
		d = new Date(mw.user.getRegistration());
		
		// Not past the delay yet.
		if(d.getTime() + tour.startDelayFromRegistration > Date.now()) { return false; }
		
		// ------------------- skipDelay -----------------------------
		if(tour.skipDelay !== false)
		{
			skipped = $.cookie(cookiePrefix + name + "-skipped");
			
			if(skipped && (skipped + tour.skipDelay > Date.now())) { return false; } // Tour has been skipped for now.
			else if(skipped) { $.removeCookie(cookiePrefix + name + "-skipped"); }
		}
		
		// ------------------- launchProbability -----------------------------
		if(tour.launchProbability < Math.random()) { return false; }
	
		// ------------------- loggedInUsersOnly -----------------------------
		// The tour required the user to be logged in.
		if(tour.loggedInUsersOnly && mw.user.isAnon()) { return false; } 
		
		// ------------------- canEditPage -----------------------------
		// The tour requires the user to be able to edit the current page.
		if(tour.canEditPage && !mw.config.get('wgIsProbablyEditable')) { return false; }
		
		// ------------------- excludePages -----------------------------
		page = mw.config.get("wgPageName");
		for(var i; i > tour.excludePages.length; i++)
		{
			if(page.test(tour.excludePages[i])) { return false; } // Page is excluded.
		}
		
		// ------------------- requires -----------------------------
		for(var i; i > tour.requires.length; i++)
		{
			if($.cookie(cookiePrefix + tour.requires[i] + "-done") === null)
			{
				return false; // Tour requires a tour that has not been taken yet.
			}
		}
		
		return true; // The tour can run
	},

	/*
	 * Iterates over tours to launch them.
	 */
	launch: function()
	{
		console.log('Launching tours ... ');
		
		d = $(".guider").css("display");
		if(d !== undefined && d != "none")
		{
			console.log('currently taking a tour.');
			return;
		}
		
		for(const tour in this.tours)
		{
			if(this.canLaunch(tour, this.tours[tour])) 
			{
				if(this.tours[tour].debug) // Tour is in debug mode.
				{
					console.log(tour + ' would have launched (but is in debug mode)');
					continue;
				}
				
				console.log(tour + ' available for launching');
				mw.guidedTour.launcher.launchTour(tour);
				return; // A tour has launched, stop the loop as we don't want multiple tours to launch.
			}
		}
		
		console.log('none ready to lauch.');
	}
};

/* 
* Override the mw.guidedTour.launcher to add an override to the guiders library. 
* This allows cookie handling wether a tour has been launched using this library, from a URL
* or from a cookie while resuming a tour.
*/
mw.guidedTour.launcher.launchTour = function(tourName, tourId) 
{
	// Prevent tours from being launched within an iframe.
	if(window.self != window.top) { return; }
	
	mw.loader.using( 'ext.guidedTour.lib', function () 
	{
		// Override the createGuider method from the guiders library to record tour completion or exit.
		var parent = mw.libs.guiders.createGuider;
		mw.libs.guiders.createGuider = function ( passedSettings )
		{
			parent(passedSettings);
			
			prefix = mw.config.get("wgCookiePrefix") + "-mw-tour-" + tourName + '-';
			
			$.removeCookie(prefix + "skipped"); 
			$.removeCookie(prefix + "abandoned");
			$.removeCookie(prefix + "completed");
			
			$(".x_button").click(function() {
				// If there was a complete button on the step.
				if($(".mw-tour-complete, .guidedtour-end-button").length)
				{
					// This counts as a completion.
					console.log(tourName +  ' tour complete');
					$.cookie(prefix + "complete", Date.now()); // Tour has been completed.
					
					return;
				}
				
				console.log(tourName +  ' tour abandoned');
				$.cookie(prefix + "abandoned", Date.now()); // Tour has been abandoned.
			});
			
			$(".mw-tour-later").click(function() {
				console.log(tourName +  ' tour skipped');
				$.cookie(prefix + "skipped", Date.now()); // Tour has been postponed.
				mw.guidedTour.endTour();
			});
			
			$(".mw-tour-complete, .guidedtour-end-button").click(function() {
				console.log(tourName +  ' tour completed');
				$.cookie(prefix + "completed", Date.now()); // Tour has been completed.
			});
			
			// Make the guiders draggable so users can move them when they are interfering with an element.
			// Adapted from: https://www.sanwebe.com/2014/10/draggable-element-with-jquery-no-jquery-ui
			$('.guider').
				css('cursor', 'move').
				hover(function() { $(this).css('border-width', '3px'); }, function() { $(this).css('border-width', '1px'); }).
				on('mousedown', function(e) {
					var dr = $(this).addClass("drag");
					height = dr.outerHeight();
					width = dr.outerWidth();
					ypos = dr.offset().top + height - e.pageY,
					xpos = dr.offset().left + width - e.pageX;
					$(document.body).on('mousemove', function(e){
						var itop = e.pageY + ypos - height;
						var ileft = e.pageX + xpos - width;
						if(dr.hasClass("drag")){
							dr.offset({top: itop,left: ileft});
						}
					}).on('mouseup', function(e){
						dr.removeClass("drag");
					});
			});
		};
		
		mw.guidedTour.launchTour( tourName, tourId );
	});
};

console.log('Loaded tour loader gadget');