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

De Wikimedica
(Création du script)
 
(À débogger)
Ligne 1 : Ligne 1 :
var tours = {
var tours = {
"basic_ navigation": {
"basic_ navigation": {
"requires": [],
"requires": [], // List of tours that must have been completed for this tour to launch.
"startDelayFromLogin": 300,
"startDelayFromRegistration": 300, // Delay in seconds the tour must wait after registration to lauch.
"lauchProbability": 1,
"skipDelay": 60 * 24, // Delay in seconds the tour shown again after bein skipped.
"loggedInUsersOnly": true,
"lauchProbability": 1, // Probability the tour will launch once we are past startDelayFromLogin.
"canEditPage": true,
"loggedInUsersOnly": true, // If the tour is only for logged in users.
"excludePages": "Accueil"
"canEditPage": true, // If the tour can only launch on pages a user can edit.
"excludePages": ["^Accueil$", "^Wikimedica."] // Exclude pages (regex).
}
}
};
};
/*
* Checks if all conditions for a tour to launch are met.
*/
function canLaunch(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-";
// ------------------- 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 < Date.now() + tour.skipDelay) { 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.canEditPage && 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(i; i > tour.excludePages.length(); i++)
{
if(page.test(tour.excludePages[i])) { return false; } // Page is excluded.
}
// ------------------- requires -----------------------------
for(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.
*/
function launchTours()
{
console.log('Launching tours ... ');
if($(".guider").length() > 0)
{
console.log('currently taking a tour.');
return;
}
for(const tour in tours)
{
if(canLauch(tours[tour]))
{
/* Load the tour launcher asynchronously to prevent it from being loaded if no tour can launch. */
mw.loader.load("ext.guidedTour.laucher").then(function()
{
mw.guidedTour.launcher.launchTour(tour, tours[tour]);
});
break;; // A tour has launched, stop the loop as we don't want multiple tours to launch.
}
}
console.log('none ready to lauch.');
}

Version du 15 juillet 2020 à 01:31

var 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 lauch.
		"skipDelay": 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."] // Exclude pages (regex).
	}
};

/*
 * Checks if all conditions for a tour to launch are met.
 */
function canLaunch(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-";
	
	// ------------------- 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 < Date.now() + tour.skipDelay) { 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.canEditPage && 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(i; i > tour.excludePages.length(); i++)
	{
		if(page.test(tour.excludePages[i])) { return false; } // Page is excluded.
	}
	
	// ------------------- requires -----------------------------
	for(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.
 */
function launchTours()
{
	console.log('Launching tours ... ');
	
	if($(".guider").length() > 0)
	{
		console.log('currently taking a tour.');
		return;
	}
	
	for(const tour in tours)
	{
		if(canLauch(tours[tour])) 
		{
			/* Load the tour launcher asynchronously to prevent it from being loaded if no tour can launch. */
			mw.loader.load("ext.guidedTour.laucher").then(function() 
			{
				mw.guidedTour.launcher.launchTour(tour, tours[tour]);
			});
			
			break;; // A tour has launched, stop the loop as we don't want multiple tours to launch.
		}
	}
	
	console.log('none ready to lauch.');
}