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

100% Statements 179/179
100% Branches 29/29
100% Functions 13/13
100% Lines 179/179

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 18049x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 7x 49x 49x 1x 49x 49x 18x 18x 18x 49x 49x 49x 7x 49x 49x 1x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 6x 6x 6x 6x 6x 6x 6x 6x 6x 24x 5x 5x 6x 6x 6x 6x 6x 1x 1x 6x 6x 6x 1x 1x 1x 6x 6x 6x 6x 6x 6x 49x 49x 49x 49x 49x 49x 49x 34x 34x 34x 34x 4x 4x 4x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 34x 34x 34x 34x 34x 34x 34x 34x 29x 34x 34x 34x 34x 3x 3x 3x 3x 34x 34x 34x 34x 34x 34x 34x 34x 27x 34x 34x 34x 15x 15x 15x 15x 15x 15x 15x 15x 34x 34x 34x 49x 49x 49x 49x 49x 49x 49x 49x 49x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 10x 49x 49x  
/*!
 * WikiLambda Vue editor: Application store router
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' );
 
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;
		},
		getViewMode: function () {
			const editingData = mw.config.get( 'wgWikiLambda' );
			return editingData.viewmode;
		}
	},
	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 ) {
			/**
			 * 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( ( viewKey ) => {
					if ( Constants.VIEWS[ viewKey ] === view ) {
						viewExist = true;
					}
				} );
				return !viewExist;
			};
 
			if ( viewIsInvalid( payload.to ) ) {
				return;
			}
 
			context.commit( 'CHANGE_CURRENT_VIEW', payload.to );
			if ( payload.params ) {
				const queryParamsObject = Object.assign( {}, context.state.queryParams, payload.params );
				context.commit( 'CHANGE_QUERY_PARAMS', queryParamsObject );
			}
 
			const path = context.state.currentPath;
			const query = Object.assign( {}, context.state.queryParams, { view: context.state.currentView } );
			const newUriString = path + '?' + $.param( query );
 
			window.history.pushState( { path, query }, null, newUriString );
		},
		/**
		 * 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;
 
			/**
			 * 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 );
			};
 
			// 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;
 
				// Change view and end?
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			/**
			 * 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 );
			};
 
			// 2. if Special page Run Function
			if ( isEvaluateFunctionCallPath( uri.query ) ) {
				currentView = Constants.VIEWS.FUNCTION_EVALUATOR;
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			/**
			 * Whether the Root ZObject presented in the view or edit page is a Z8/Function
			 *
			 * @param {Object} objectContext The ZObject context in which we're operating
			 * @return {boolean}
			 */
			const isFunctionRootObject = function ( objectContext ) {
				return objectContext.getters.getCurrentZObjectType === Constants.Z_FUNCTION;
			};
 
			if ( isFunctionRootObject( context ) ) {
				currentView = context.getters.getViewMode ?
					Constants.VIEWS.FUNCTION_VIEWER :
					Constants.VIEWS.FUNCTION_EDITOR;
 
				// Change view and end?
				context.dispatch( 'changeCurrentView', currentView );
				return;
			}
 
			currentView = Constants.VIEWS.DEFAULT;
			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 path = uri.path;
				const query = Object.assign( uri.query, { view: view } );
				const newUriString = path + '?' + $.param( query );
				window.history.replaceState( { path, query }, null, newUriString );
			}
		}
	}
};