« MediaWiki:Gadget-tour-loader.js » : différence entre les versions
Aucun résumé des modifications |
(Gestion des cookies) |
||
Ligne 27 : | Ligne 27 : | ||
cookiePrefix = mw.config.get("wgCookiePrefix") + "-mw-tour-"; | 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 ----------------------------- | // ------------------- startDelayfromRegistration ----------------------------- | ||
Ligne 91 : | Ligne 97 : | ||
{ | { | ||
console.log(tour + ' available for launching'); | 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; // A tour has launched, stop the loop as we don't want multiple tours to launch. | ||
} | } | ||
Ligne 105 : | Ligne 105 : | ||
} | } | ||
}; | }; | ||
// Load the tour launcher asynchronously. | |||
mw.loader.using("ext.guidedTour.launcher").then(function() | |||
{ | |||
/* | |||
* 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) | |||
{ | |||
mw.loader.using( 'ext.guidedTour.lib', function () | |||
{ | |||
mw.guidedTour.launchTour( tourName, tourId ); | |||
// Override _addXButton method from the guiders library to record tour completion or exit. | |||
var parent = mw.libs.guiders._addXButton; | |||
mw.libs.guiders._addXButton = function(guider) | |||
{ | |||
parent(guider); | |||
prefix = mw.config.get("wgCookiePrefix") + "-mw-tour-" + tour + '-'; | |||
$.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").length) | |||
{ | |||
// This counts as a completion. | |||
console.log(tour + 'tour complete'); | |||
$.cookie(prefix + "complete", Date.now()); // Tour has been completed. | |||
return; | |||
} | |||
console.log(tour + 'tour abandoned'); | |||
$.cookie(prefix + "abandoned", Date.now()); // Tour has been skipped. | |||
}); | |||
$("mw-tour-later").click(function() { | |||
console.log(tour + 'tour skipped'); | |||
$.cookie(prefix + "skipped", Date.now()); // Tour has been postponed. | |||
}) | |||
$("mw-tour-complete").click(function() { | |||
console.log(tour + 'tour completed'); | |||
$.cookie(prefix + "completed", Date.now()); // Tour has been completed. | |||
}); | |||
}; | |||
}); | |||
}; | |||
}); | |||
console.log('Loaded tour loader gadget'); | console.log('Loaded tour loader gadget'); |
Version du 16 juillet 2020 à 23:40
mw.tours = { // Hook to the mw object 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 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.
*/
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.
*/
launchTours: function()
{
console.log('Launching tours ... ');
if($(".guider").length > 0)
{
console.log('currently taking a tour.');
return;
}
for(const tour in this.tours)
{
if(this.canLaunch(tour, this.tours[tour]))
{
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.');
}
};
// Load the tour launcher asynchronously.
mw.loader.using("ext.guidedTour.launcher").then(function()
{
/*
* 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)
{
mw.loader.using( 'ext.guidedTour.lib', function ()
{
mw.guidedTour.launchTour( tourName, tourId );
// Override _addXButton method from the guiders library to record tour completion or exit.
var parent = mw.libs.guiders._addXButton;
mw.libs.guiders._addXButton = function(guider)
{
parent(guider);
prefix = mw.config.get("wgCookiePrefix") + "-mw-tour-" + tour + '-';
$.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").length)
{
// This counts as a completion.
console.log(tour + 'tour complete');
$.cookie(prefix + "complete", Date.now()); // Tour has been completed.
return;
}
console.log(tour + 'tour abandoned');
$.cookie(prefix + "abandoned", Date.now()); // Tour has been skipped.
});
$("mw-tour-later").click(function() {
console.log(tour + 'tour skipped');
$.cookie(prefix + "skipped", Date.now()); // Tour has been postponed.
})
$("mw-tour-complete").click(function() {
console.log(tour + 'tour completed');
$.cookie(prefix + "completed", Date.now()); // Tour has been completed.
});
};
});
};
});
console.log('Loaded tour loader gadget');