All files / ext.wikilambda.edit/store/modules router.js

100% Statements 60/60
100% Branches 24/24
100% Functions 15/15
100% Lines 60/60

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                57x               57x 32x                 57x 21x 21x                 57x 44x                 57x 29x                 57x 6x 6x 24x 5x     6x             57x 5x       5x 5x             57x 1x       1x 1x     57x               17x     1x         17x     1x                           6x 1x     5x 5x 1x 1x   5x 5x               44x     44x 8x 8x           44x     12x         12x 12x       32x 3x 3x 3x     29x 21x         21x 21x     8x 8x                   20x 20x   20x 1x 1x          
/*!
 * WikiLambda Vue editor: Application store router
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' );
 
/**
 * Whether the URL is to Special Evaluate Function Call page
 *
 * @param {Object} uriQuery The contextual mw.Uri's query sub-object
 * @return {boolean}
 */
const isEvaluateFunctionCallPath = function ( uriQuery ) {
	return ( uriQuery.title === Constants.PATHS.RUN_FUNCTION_TITLE );
};
 
/**
 * Whether the current config has viewmode=true
 *
 * @param {Object} uriQuery The contextual mw.Uri's query sub-object
 * @return {boolean}
 */
const isViewMode = function () {
	const editingData = mw.config.get( 'wgWikiLambda' );
	return editingData.viewmode;
};
 
/**
 * Whether the URL is to Special Create ZObject page
 *
 * @param {Object} uriQuery The contextual mw.Uri's query sub-object
 * @return {boolean}
 */
const isCreatePath = function ( uriQuery ) {
	return ( uriQuery.title === Constants.PATHS.CREATE_OBJECT_TITLE );
};
 
/**
 * Whether the Root ZObject presented in the view or edit page is a Z8/Function
 *
 * @param {Object} context The ZObject context in which we're operating
 * @return {boolean}
 */
const isFunctionRootObject = function ( context ) {
	return context.rootGetters.getCurrentZObjectType === Constants.Z_FUNCTION;
};
 
/**
 * Whether the given URI path string is a known view in Constants.VIEWS
 *
 * @param {string} view
 * @return {boolean}
 */
const viewIsInvalid = function ( view ) {
	let viewExist = false;
	Object.keys( Constants.VIEWS ).forEach( function ( viewKey ) {
		if ( Constants.VIEWS[ viewKey ] === view ) {
			viewExist = true;
		}
	} );
	return !viewExist;
};
 
/**
 * @param {string} path
 * @param {string} query
 */
const pushToHistoryState = function ( path, query ) {
	const stateObj = {
		path: path,
		query: query
	};
	const queryString = path + '?' + $.param( query );
	window.history.pushState( stateObj, null, queryString );
};
 
/**
 * @param {string} path
 * @param {string} query
 */
const replaceToHistoryState = function ( path, query ) {
	const stateObj = {
		path: path,
		query: query
	};
	const queryString = path + '?' + $.param( query );
	window.history.replaceState( stateObj, null, queryString );
};
 
module.exports = {
	state: {
		currentPath: mw.Uri().path,
		currentView: Constants.VIEWS.Z_OBJECT_VIEWER,
		queryParams: mw.Uri().query
	},
	getters: {
		getCurrentView: function ( state ) {
			return state.currentView;
		},
		getQueryParams: function ( state ) {
			return state.queryParams;
		}
	},
	mutations: {
		CHANGE_CURRENT_VIEW: function ( state, view ) {
			state.currentView = view;
		},
		CHANGE_QUERY_PARAMS: function ( state, queryParams ) {
			state.queryParams = queryParams;
		}
	},
	actions: {
		/**
		 * Changes the current View and Query params and updates the history states.
		 * This method is used to navigate between pages within the UI.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.to
		 * @param {Object} payload.params
		 */
		navigate: function ( context, payload ) {
			if ( viewIsInvalid( payload.to ) ) {
				return;
			}
 
			context.commit( 'CHANGE_CURRENT_VIEW', payload.to );
			if ( payload.params ) {
				const queryParamsObject = $.extend( {}, context.state.queryParams, payload.params );
				context.commit( 'CHANGE_QUERY_PARAMS', queryParamsObject );
			}
			const query = $.extend( {}, context.state.queryParams, { view: context.state.currentView } );
			pushToHistoryState( context.state.currentPath, query );
		},
		/**
		 * Evaluate the Uri path to evaluate what View should be displayed.
		 *
		 * @param {Object} context
		 */
		evaluateUri: function ( context ) {
			const uri = mw.Uri();
 
			// Set title of mw query if url is in /wiki/{{ title }} format
			if ( !uri.query.title && uri.path.includes( '/wiki' ) ) {
				const lastPathIndex = uri.path.lastIndexOf( '/' );
				uri.query.title = uri.path.slice( lastPathIndex + 1 );
			}
 
			let currentView;
 
			// 1. if Special page Create
			if ( isCreatePath( uri.query ) ) {
				// I we have zid=Z8 in the uri, render function edit view
				// Else, render default view
				currentView = ( uri.query.zid === Constants.Z_FUNCTION ) ?
					Constants.VIEWS.FUNCTION_EDITOR :
					Constants.VIEWS.DEFAULT_VIEW;
 
				// Change view and end?
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			// 2. if Special page Run Function
			if ( isEvaluateFunctionCallPath( uri.query ) ) {
				currentView = Constants.VIEWS.FUNCTION_EVALUATOR;
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			if ( isFunctionRootObject( context ) ) {
				currentView = isViewMode() ?
					Constants.VIEWS.FUNCTION_VIEWER :
					Constants.VIEWS.FUNCTION_EDITOR;
 
				// Change view and end?
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			currentView = Constants.VIEWS.DEFAULT_VIEW;
			context.dispatch( 'changeCurrentView', currentView );
		},
		/**
		 * Handle the changes of a view and replace the history state.
		 * This method is usually used when the change of view is made dynamically.
		 *
		 * @param {Object} context
		 * @param {string} view
		 */
		changeCurrentView: function ( context, view ) {
			context.commit( 'CHANGE_CURRENT_VIEW', view );
			const uri = mw.Uri();
			// should only replace history state if path query view is set and is different from new view
			if ( uri.query.view && uri.query.view !== view ) {
				const query = $.extend( uri.query, { view: view } );
				replaceToHistoryState( uri.path, query );
			}
		}
	}
};