All files / ext.wikilambda.app/store/stores router.js

100% Statements 217/217
100% Branches 45/45
100% Functions 14/14
100% Lines 217/217

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 297x 126x 126x 126x 126x 126x 126x 126x 126x 126x 23x 126x 126x 126x 126x 126x 126x 126x 126x 5x 126x 126x 126x 126x 126x 126x 126x 126x 126x 42x 42x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 6x 1x 1x 5x 5x 5x 5x 5x 6x 1x 1x 5x 5x 5x 6x 9x 9x 5x 5x 126x 126x 126x 126x 126x 126x 51x 51x 51x 51x 51x 51x 51x 21x 21x 51x 51x 51x 16x 16x 16x 16x 16x 16x 16x 35x 35x 39x 1x 1x 1x 34x 34x 39x 3x 3x 3x 31x 31x 38x 23x 23x 23x 23x 23x 23x 8x 8x 8x 126x 126x 126x 126x 126x 126x 126x 126x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 1x 1x 126x 126x 126x 126x 126x 126x 126x 126x 126x 6x 25x 5x 5x 25x 1x 126x 126x 126x 126x 126x 126x 126x 126x 126x 51x 126x 126x 126x 126x 126x 126x 126x 126x 434x 126x 126x 126x 126x 126x 126x 126x 126x 126x 3x 126x 126x 126x 126x 126x 126x 126x 126x 126x 34x 126x 126x 126x 126x 126x 126x 126x 126x 31x 31x 126x 126x  
/*!
 * WikiLambda Vue editor: Application store router (Pinia)
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' );
const urlUtils = require( '../../utils/urlUtils.js' );
 
module.exports = {
	state: {
		currentPath: window.location.pathname,
		currentView: Constants.VIEWS.Z_OBJECT_VIEWER,
		queryParams: urlUtils.getQueryParamsFromUrl( window.location.href )
	},
 
	getters: {
		getWikilambdaConfig: function () {
			return mw.config.get( 'wgWikiLambda' ) || {};
		},
		/**
		 * Returns current loaded view if wikilambda Repo mode.
		 * Else returns undefined.
		 *
		 * @param {Object} state
		 * @return {string|undefined}
		 */
		getCurrentView: function ( state ) {
			return state.currentView;
		},
		/**
		 * Returns Uri query parameters
		 *
		 * @param {Object} state
		 * @return {Object}
		 */
		getQueryParams: function ( state ) {
			return state.queryParams;
		},
		/**
		 * Returns view mode flag if wikilambda Repo mode.
		 * Else returns undefined.
		 *
		 * @param {Object} state
		 * @return {boolean|undefined}
		 */
		getViewMode: function () {
			return this.getWikilambdaConfig.viewmode;
		}
	},
 
	actions: {
		/**
		 * Changes the current View and Query params and updates the history state.
		 *
		 * @param {Object} payload
		 * @param {string} payload.to - Target view.
		 * @param {Object} [payload.params] - Additional query parameters.
		 */
		navigate: function ( { to, params } ) {
			if ( !this.isValidView( to ) ) {
				return;
			}
 
			this.currentView = to;
 
			const url = new URL( window.location.href );
 
			if ( params ) {
				this.queryParams = Object.assign( {}, this.queryParams, params );
			}
			const newQuery = Object.assign( {}, this.queryParams, { view: this.currentView } );
 
			url.pathname = this.currentPath;
			for ( const key in newQuery ) {
				url.searchParams.set( key, newQuery[ key ] );
			}
			const query = urlUtils.searchParamsToObject( url.searchParams );
			window.history.pushState( { path: url.pathname, query }, null, url.toString() );
		},
 
		/**
		 * Evaluate the Uri path and determine what View should be displayed.
		 */
		evaluateUri: function () {
			const url = new URL( window.location.href );
			const params = url.searchParams;
			const path = url.pathname;
 
			// Set title if URL is in `/wiki/{{ title }}` format
			let title = params.get( 'title' );
			if ( !title && path.includes( '/wiki' ) ) {
				title = path.slice( path.lastIndexOf( '/' ) + 1 );
			}
 
			// 1. if Special page Create Object
			if ( this.isCreatePath( title ) ) {
				const zid = params.get( 'zid' );
				const specialCreateView = zid && zid.toUpperCase() === Constants.Z_FUNCTION ?
					Constants.VIEWS.FUNCTION_EDITOR :
					Constants.VIEWS.DEFAULT;
				this.changeCurrentView( specialCreateView );
				return;
			}
 
			// 2. if Special page Create Abstract
			if ( this.isAbstractContent() ) {
				this.changeCurrentView( Constants.VIEWS.ABSTRACT );
				return;
			}
 
			// 3. if Special page Run Function
			if ( this.isEvaluateFunctionCallPath( title ) ) {
				this.changeCurrentView( Constants.VIEWS.FUNCTION_EVALUATOR );
				return;
			}
 
			// 4. if Function page (edit or view)
			if ( this.isFunctionRootObject() ) {
				const functionView = this.getViewMode ?
					Constants.VIEWS.FUNCTION_VIEWER :
					Constants.VIEWS.FUNCTION_EDITOR;
				this.changeCurrentView( functionView );
				return;
			}
 
			// 5. Default view
			this.changeCurrentView( Constants.VIEWS.DEFAULT );
		},
 
		/**
		 * Handle the changes of a view and replace the history state.
		 *
		 * @param {string} newView - The new view to set.
		 */
		changeCurrentView: function ( newView ) {
			this.currentView = newView;
 
			const url = new URL( window.location.href );
			const path = url.pathname;
			const params = url.searchParams;
			const currentView = params.get( 'view' );
 
			// should only replace history state if path query view is set and is different from new view
			if ( currentView && currentView !== newView ) {
				params.set( 'view', newView );
				const query = urlUtils.searchParamsToObject( params );
				window.history.replaceState( { path, query }, null, url.toString() );
			}
		},
 
		/**
		 * Check if  the given url path 'view' string is a known view in Constants.VIEWS
		 *
		 * @param {string} view
		 * @return {boolean}
		 */
		isValidView: function ( view ) {
			for ( const key in Constants.VIEWS ) {
				if ( Constants.VIEWS[ key ] === view ) {
					return true;
				}
			}
			return false;
		},
 
		/**
		 * Check if the path is for Special:CreateZObject
		 *
		 * @param {string} title
		 * @return {boolean}
		 */
		isCreatePath: function ( title ) {
			return title === Constants.PATHS.CREATE_OBJECT_TITLE;
		},
 
		/**
		 * Check if the path is for Special:CreateAbstract
		 *
		 * @return {boolean}
		 */
		isAbstractContent: function () {
			return this.getWikilambdaConfig.abstractContent;
		},
 
		/**
		 * Check if we are creating a new Abstract Wiki page
		 * (i.e. abstractContent is true and createNewPage is true).
		 *
		 * @return {boolean}
		 */
		isAbstractCreatePage: function () {
			return this.getWikilambdaConfig.abstractContent && this.getWikilambdaConfig.createNewPage;
		},
 
		/**
		 * Check if the path is for Special:EvaluateFunctionCall
		 *
		 * @param {string} title
		 * @return {boolean}
		 */
		isEvaluateFunctionCallPath: function ( title ) {
			return title === Constants.PATHS.RUN_FUNCTION_TITLE;
		},
 
		/**
		 * Check if the current ZObject is a Z8/Function
		 *
		 * @return {boolean}
		 */
		isFunctionRootObject: function () {
			return this.getCurrentZObjectType === Constants.Z_FUNCTION;
		}
	}
};