Extension:GuidedTour/Write an on-wiki tour

From mediawiki.org

This page explains how to write an on-wiki tour. These types of tours are stored directly on a wiki, in the MediaWiki namespace. To edit this namespace, you generally must be an administrator. However, anyone can propose a tour be added.

The tour will go in MediaWiki:Guidedtour-tour-tourname.js.

A series of steps[edit]

First, you have to consider when and how your tour should start.

The tour itself is a sequence of dialog windows that appear on the page, called steps. (It is possible to have a single-step tour that operates as a fancy tooltip, and to jump into the tour at different points.)

Each step can optionally have a next button, a back button, or both. You can also have the tour automatically move (transition) from one step to another in response to a user action (for instance, when the user makes their first change to an open VisualEditor window). You define the tour in a JavaScript function, calling API methods to construct and link the steps together.

The extension keeps track of the current step in each tour in a browser cookie. By default, a tour will continue across multiple web pages.

Implementation[edit]

  1. Consult the design recommendations and the API documentation.
  2. Choose a tour name. This and shouldLog (if applicable) should be set when constructing the TourBuilder (see example tours below).
    name: 'tourname',
    
  3. If you want to log certain events with Schema:GuidedTour, add:
    shouldLog: true,
    
  4. For each step in your tour:
    1. Choose title and description text.
    2. Then, choose whether the step will have an attachment or a central overlay:
      • Attachment positions a step near a page element such as the Edit tab, a link, or the save button. You identify the page element using a selector. Any jQuery selector can be used. In general, this includes all CSS selectors. This example positions near the Edit tab using an id selector:
        attachTo: '#ca-edit',
        
        For attachments, you must choose an attachment position. The choices are topLeft, top, topRight, rightTop, right, rightBottom, bottomRight, bottom, bottomLeft, leftBottom, left, leftTop. You can experiment with these values, and may want to test in a skin beside your primary one (using the useskin parameter). For on-wiki tours, you should test in the wiki's site direction (which does not vary by user or according to uselang). If you're not sure what this is, you can check in a JavaScript console with:
        $( document.body ).is( '.sitedir-ltr' ) ? 'ltr' : 'rtl'
        Extension-defined tours should always be written left-to-right. In either case (on-wiki or extension tours), if the user is using another direction (for example, they are browsing English Wikipedia but have their interface language set to Hebrew in preferences), steps will automatically flip horizontally.
      • By default (if you do not choose one of the directions above) the step shows in the center of the screen. This allows you to explain something without attaching to an element. In this case, it is recommended that you use an overlay to draw focus to the step:
        overlay: true,
        
    3. To link steps together, you can use Next and Back buttons. These will automatically be added if you tell the tour how to find the Next and Back steps. For example:
      • For Next:
        step.next( 'nextStep' );
        
      • For Back:
        step.back( 'previousStep' );
        
      Both Next and Back can be used on the same step. Also, more powerful behavior is available (such as choosing a different next or back step depending on context); see the 'next' and 'back' methods in the API documentation.
    4. Choose any further button actions you want to add to the step. If you want the user to click something (such as the edit tab), you can provide no explicit buttons (an Okay button that dismisses the step will still show). If you only want Next and/or Back buttons, you do not need to specify anything here (see above). Otherwise, you will want to specify one or more. You can specify arbitrary button actions. However, there are helpers included for common button behaviors. For okay and end, no custom button text is necessary. For the others, use name or namemsg (see below sections):
      • okay - Shows as an Okay button. Will run the specified function when the button is clicked.
         { action: 'okay', onclick: function () { /* ... */ } }
        
      • end - Shows as an Okay button. Will end the tour when clicked.
        { action: 'end' }
        
      • wikiLink - Shows as a textual button. Will go to the specified wiki page when clicked.
        { action: 'wikiLink', page: 'Name of page' }
        
      • externalLink - Shows as a textual button. Will go the specified external web site when clicked.
        { action: 'externalLink', url: 'http://example.com' }
        
      • For any of the buttons, you can specify the type of button as neutral, progressive, constructive, destructive, or quiet. See the Living Style Guide for an explanation of the meanings. For example:
        { action: 'wikiLink', page: 'Name of page', type: 'neutral' }
        
      • You can add arbitrary CSS classes to the button using the classString parameter. For example:
        { action: 'wikiLink', page: 'Name of page', type: 'neutral', cssClass: 'class_1 class_2' }
        
    5. By default, the step will close when they click outside it. In some cases, such as on the edit page, you may want to disable this by including:
      closeOnClickOutside: false,
      
    6. Decide if you want transition behavior. In some cases, you will want to automatically transition to another step when an event (including page load) occurs. For example, if you have a step asking the user to click Edit, you may want to transition to a step pointing to the Preview button ("explainPreviewButton" in this example) once they're on the edit page. For more information about transitions, see the API documentation on the 'transition' method.
       step.transition( function () { if ( gt.isEditing() ) { return 'explainPreviewButton'; } } )
      

Note that if a step doesn't have a button to take them somewhere, you'll generally want to implement a transition() function for the current step that allows them to go somewhere. Otherwise the tour will "stall" on it — every time the user reloads the page or visits any page that loads your tour, the current step will reappear until the user exits the tour.

Tips[edit]

  • Use debug mode while developing your tours.
  • Inspect the tour cookie (named wgCookiePrefix-mw-tour) to see what step your tour is on.
  • Even when writing an on-wiki tour, use a JavaScript checker such as jshint to check for errors in your tour definition.
  • When stepping through your code in a browser debugger, the show() function in guiders.js is one of the most useful functions in which to set a breakpoint.

Test tour code walkthrough[edit]

This is a walkthrough of the test tour built into Extension:GuidedTour, modified to be an on-wiki tour. The test tour does not use any multi-page functionality (see #Firstedit tour code walkthrough). It demonstrates multiple approaches to the description, either inlined completely, inlined but parsed from wikitext (gt.parseDescription), and parsed from a dedicated page (gt.getPageAsDescription). For an actual tour, you can use one option for all steps (simplest), or use different options for different steps.

/*
 * Guided Tour to test guided tour features.
 */
// Copy the next line into the start of your tour.
( function ( window, document, $, mw, gt ) {

	// Declare a variable for use later
	var pageName = 'Help:Guided tours/guider',
		tour;

	tour = new gt.TourBuilder( {
		/*
		 * This is the name of the tour.  It must be lowercase, without any hyphen (-) or
		 * period (.) characters.
		 *
		 * The page where you save an on-wiki tour must be named
		 * MediaWiki:Guidedtour-tour-{name}.js , in this example MediaWiki:Guidedtour-tour-mytest.js
		 */
		name: 'mytest'
	} );

	// Information defining each tour step

	// This tour shows a central overlay at the start of the tour.
	// Guiders appear in the center if another position is not specified.
	// To specify the first step of the tour, use .firstStep instead of .step
	tour.firstStep( {
		name: 'overlay',
		// Note that for on-wiki tours, we use title and description with the actual text.
		// The title appears in the title bar of the guider.
		title: 'Testing',

		// The description appears in the body
		description: 'This is a test of the description. Lorem ipsum dolor sit!',

		// This specifies that there is an overlay behind the guider.
		overlay: true
	} )
	// This specifies the next step of the tour, and will automatically generate a next button.
	// 'callout' refers to the name used in the step immediately below.  Although putting the steps
	// in a meaningful order is recommended, any step can be specified as next/back.
	.next( 'callout' );

	tour.step( {
		/*
		 * Callout of left menu
		 */
		name: 'callout',
		title: 'Test callouts',
		description: 'This is the community portal page.',

                // This positions the guider next to a page element, in this
                // case the portal link (which is "Community portal" on English
                // Wikipedia, but varies by site).
                // The string is a jQuery selector.  "#n-portal" means the HTML
                // element with this id attribute, and "a" means an a, or link,
                // element inside that.
		attachTo: '#n-portal a',

                // This means the guider shows to the right of the Community Portal link
		position: 'right',
	} )
	.next( 'description' )
	// The 'back' property specifies that you can go back from this step, and where to go
	// if the back button is clicked.
	.back( 'overlay' );

	tour.step( {
		/*
		 * Test out mediawiki description pages
		 */
		name: 'description',
		title: 'Test MediaWiki description pages',

		// Name of the page to parse
		description: pageName,

		overlay: true,

		// This means the wikitext for the description will be loaded from the
		// page name in the description field.
		onShow: gt.getPageAsDescription,

		buttons: [ {
			// This makes a button which acts like a wikilink to 'Help:Guided tours/guider'
			action: 'wikiLink',
			page: pageName,
			name: 'Go to description page',
			// This specifies that the button takes you to the next step of a process,
			// which affects its appearance.
			type: 'progressive'
		}, {
			// This makes the okay button on this step end the tour.
			action: 'end'
		} ]
	} )
	.back( 'callout' );

// The following should be the last line of your tour.
} ( window, document, jQuery, mediaWiki, mediaWiki.guidedTour ) );

Firstedit tour code walkthrough[edit]

This is a walkthrough of a simplified version of the 'firstedit' tour, if it were written as an on-wiki tour. It covers more advanced features not described above:

// Guided Tour to help users make their first edit.
// Designed to work on any Wikipedia article, and can work for other sites with minor message changes.

( function ( window, document, $, mw, gt ) {
	var hasEditSection, tour;

	// Check if there are section edit links (used later)
	hasEditSection = $( '.mw-editsection' ).length > 0;

	tour = new gt.TourBuilder( {
		name: 'myfirstedit',
		// Specify that we want logging for this tour
		shouldLog: true
	} );

	tour.firstStep( {
		name: 'intro',
		title: 'Edit the whole page…',
		description: 'Click the "Edit" button to make your changes.',
		attachTo: '#ca-edit',
		position: 'bottom',
		// This indicates that we don't want an automatic next button,
		// even though we are specifying which step comes next.
		allowAutomaticNext: false,
		buttons: [ {
			// Custom logic to specify a button and its behavior
			// depending on whether there are sections on the page.
			action: hasEditSection ? 'next' : 'okay',
			onclick: function () {
				if ( hasEditSection ) {
					mw.libs.guiders.next();
				} else {
					mw.libs.guiders.hideAll();
				}
			}
		} ]
	} )
	// At certain times, called transition points, the callback passed to .transition
	// will be called.  At those times, this tour checks if the user is editing.  If so,
	// the tour returns 'preview', indicating that the tour should transition to the
	// 'preview' step automatically.
	.transition( function () {
		if ( gt.isEditing() ) {
			return 'preview';
		}
	} )
	.next( 'editSection' );

	tour.step( {
		name: 'editSection',
		title: 'Or edit a section',
		description: 'There are "edit" links for each major section in a page, so you can focus on just that part.",',
		position: 'right',
		attachTo: '.mw-editsection',
		// Automatically scroll to this step
		autoFocus: true,
		// Custom width, in pixels
		width: 300
	} )
	.transition( function () {
		if ( gt.isEditing() ) {
			return 'preview';
		} else if ( !hasEditSection ) {
			// Returning HIDE means that the tour should be hidden, but not ended.
			return gt.TransitionAction.HIDE;
		}
	} )
	.back( 'intro' );

	tour.step( {
		name: 'preview',
		title: 'Preview your changes (optional)',
		description: 'Clicking "Show preview" allows you to check what the page will look like with your changes. Just don\'t forget to save!",',
		attachTo: '#wpPreview',
		autoFocus: true,
		position: 'top',
		// This specifies that, unlike the default, the guider should not close when the user clicks outside of it.
		closeOnClickOutside: false
	} )
	.transition( function () {
		// isReviewing checks whether the user is either previewing or showing changes.
		if ( gt.isReviewing() ) {
			return 'save';
		} else if ( !gt.isEditing() ) {
			// When the user is on this step, but neither editing nor reviewing, this tour automatically ends.
			return gt.TransitionAction.END;
		}
	} )
	.next( 'save' );

	tour.step( {
		name: 'save',
		title: 'You\'re almost done!',
		description: 'When you\'re ready, clicking "Save pages" will make your changes visible for everyone.',
		attachTo: '#wpSave',
		autoFocus: true,
		position: 'top',
		closeOnClickOutside: false
	} )
	.transition( function () {
		if ( !gt.isReviewing() ) {
			return gt.TransitionAction.END;
		}
	} )
	.back( 'preview' );

} ( window, document, jQuery, mediaWiki, mediaWiki.guidedTour ) );