All files / ext.wikilambda.edit/components/widgets Publish.vue

92.3% Statements 60/65
68.75% Branches 22/32
90% Functions 18/20
92.18% Lines 59/64

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278    18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x                                         22x                         7x                             8x           2x                               13x             13x 13x 13x 9x 9x                   12x   12x 12x                   51x   51x 1041x 1041x 50x         1x 1x                                                                 10x 7x 7x 7x   7x           7x 7x   10x 5x 5x   5x               16x 16x       22x 22x     18x   18x     18x 18x   42x 42x 42x 42x 42x       62x           38x                     38x                                 18x                                                              
<!--
	WikiLambda Vue component for publishing a zobject.
	Contains both the publish button and the dialog pop-up flow prior
	to submission.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-widget-base class="ext-wikilambda-publish-widget">
		<template #main>
			<cdx-button
				class="ext-wikilambda-publish-widget__cancel-button"
				weight="primary"
				@click.stop="handleCancel"
			>
				{{ $i18n( 'wikilambda-cancel' ).text() }}
			</cdx-button>
			<cdx-button
				class="ext-wikilambda-publish-widget__publish-button"
				action="progressive"
				:disabled="!isDirty && !revertToEdit"
				data-testid="publish-button"
				@click.stop="waitAndHandlePublish"
			>
				{{ $i18n( 'wikilambda-publishnew' ).text() }}
			</cdx-button>
			<wl-publish-dialog
				:show-dialog="showPublishDialog"
				:function-signature-changed="functionSignatureChanged"
				@close-dialog="closePublishDialog"
				@before-exit="removeListeners"
			></wl-publish-dialog>
			<wl-leave-editor-dialog
				:show-dialog="showLeaveEditorDialog"
				:continue-callback="leaveEditorCallback"
				@close-dialog="closeLeaveDialog"
				@before-exit="removeListeners"
			></wl-leave-editor-dialog>
		</template>
	</wl-widget-base>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	WidgetBase = require( '../base/WidgetBase.vue' ),
	LeaveEditorDialog = require( './LeaveEditorDialog.vue' ),
	PublishDialog = require( './PublishDialog.vue' ),
	eventLogger = require( '../../mixins/eventLogUtils.js' ).methods,
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters,
	getParameterByName = require( '../../mixins/urlUtils.js' ).methods.getParameterByName;
 
module.exports = exports = defineComponent( {
	name: 'wl-publish-widget',
	components: {
		'cdx-button': CdxButton,
		'wl-leave-editor-dialog': LeaveEditorDialog,
		'wl-publish-dialog': PublishDialog,
		'wl-widget-base': WidgetBase
	},
	props: {
		functionSignatureChanged: {
			type: Boolean,
			required: false,
			default: false
		},
		isDirty: {
			type: Boolean,
			required: false,
			default: false
		}
	},
	data: function () {
		return {
			leaveEditorCallback: undefined,
			showLeaveEditorDialog: false,
			showPublishDialog: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getCurrentZObjectId',
		'getCurrentZObjectType',
		'getCurrentZImplementationType',
		'getUserLangZid',
		'getUserLangCode',
		'isCreateNewPage',
		'waitForRunningParsers'
	] ), {
		/**
		 * Returns the eventLog data object
		 *
		 * @return {Object}
		 */
		eventData: function () {
			return {
				isnewzobject: this.isCreateNewPage,
				zobjectid: this.getCurrentZObjectId,
				zobjecttype: this.getCurrentZObjectType || null,
				implementationtype: this.getCurrentZImplementationType || null,
				zlang: this.getUserLangZid || null,
				isdirty: this.isDirty
			};
		}
	} ),
	methods: Object.assign( mapActions( [
		'clearValidationErrors',
		'validateZObject'
	] ), {
		/**
		 * Handle cancel event from Publish dialog
		 */
		closePublishDialog: function () {
			this.showPublishDialog = false;
		},
 
		/**
		 * Handle cancel event from Leave dialog
		 */
		closeLeaveDialog: function () {
			this.showLeaveEditorDialog = false;
		},
 
		/**
		 * If 'oldid' exists in the query, return true.
		 * IEf true, this enables the Publish button without needing an event.
		 *
		 * @return {boolean}
		 */
		revertToEdit: function () {
			return !!( getParameterByName( 'oldid' ) || getParameterByName( 'undo' ) );
		},
 
		/**
		 * Waits for running parsers to return and persist
		 * changes before going ahead and running the function call
		 */
		waitAndHandlePublish: function () {
			this.waitForRunningParsers.then( () => this.handlePublish() );
		},
 
		/**
		 * Handle click event on Publish button: opens
		 * the publish dialog.
		 */
		handlePublish: function () {
			this.clearValidationErrors();
			this.validateZObject().then( ( isValid ) => {
				if ( isValid ) {
					this.$emit( 'start-publish' );
					this.showPublishDialog = true;
				}
			} );
		},
 
		/**
		 * Handle click event on Cancel button: opens
		 * the leave editor confirmation dialog.
		 */
		handleCancel: function () {
			// Emit click cancel event
			this.$emit( 'start-cancel' );
			// Get redirect url
			const cancelTargetUrl = this.isCreateNewPage ?
				new mw.Title( Constants.PATHS.MAIN_PAGE ).getUrl() :
				`/view/${ this.getUserLangCode }/${ this.getCurrentZObjectId }`;
			this.leaveTo( cancelTargetUrl );
		},
 
		/**
		 * Handles navigation away from the page.
		 * Currently only handles navigation out when
		 * chicking a link.
		 *
		 * @param {Object} e the click event
		 */
		handleClickAway: function ( e ) {
			let target = e.target;
			// If the click element is not a link, exit
			while ( target && target.tagName !== 'A' ) {
				target = target.parentNode;
				if ( !target ) {
					return;
				}
			}
			// If the link doesn't have a target or target property is _blank,
			// we are staying in this page, so there's no need to handle cancelation
			if ( !target.href || ( target.target === '_blank' ) ) {
				return;
			}
			// Else, abandon the page
			e.preventDefault();
			this.leaveTo( target.href );
		},
 
		/**
		 * Handles navigation away from the page using the browser
		 * beforeunload event. The dialog shown will be the browser
		 * provided dialog, and when existing through this way we
		 * won't be able to track cancel events with our event
		 * logging system. The beforeunload event has compatibility
		 * and performance issues.
		 *
		 * See:
		 * https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
		 * https://developer.chrome.com/blog/page-lifecycle-api/#the-beforeunload-event
		 *
		 * @param {Object} e the beforeunload event
		 */
		handleUnload: function ( e ) {
			if ( this.isDirty ) {
				e.preventDefault();
			}
		},
 
		/**
		 * Handle actions before leaving the edit page:
		 * Show confirmation dialog if there are unsaved changes
		 * and sends a cancelation event when/if we finally leave.
		 *
		 * @param {string} targetUrl
		 */
		leaveTo: function ( targetUrl ) {
			const leaveAction = () => {
				this.removeListeners();
				const eventNamespace = eventLogger.getNamespace( this.getCurrentZObjectType );
				eventLogger.dispatchEvent( `wf.ui.${ eventNamespace }.cancel`, this.eventData );
				// T350497 Update the WikiLambda instrumentation to use core interaction events
				const interactionData = {
					zobjecttype: this.getCurrentZObjectType || null,
					zobjectid: this.getCurrentZObjectId,
					zlang: this.getUserLangZid || null,
					implementationtype: this.getCurrentZImplementationType || null
				};
				eventLogger.submitInteraction( 'cancel', interactionData );
				window.location.href = targetUrl;
			};
 
			if ( this.isDirty ) {
				this.leaveEditorCallback = leaveAction;
				this.showLeaveEditorDialog = true;
			} else {
				leaveAction();
			}
		},
 
		/**
		 * On successfully exiting the page, remove event
		 * listeners.
		 */
		removeListeners: function () {
			window.removeEventListener( 'click', this.handleClickAway );
			window.removeEventListener( 'beforeunload', this.handleUnload );
		}
	} ),
	mounted: function () {
		window.addEventListener( 'click', this.handleClickAway );
		window.addEventListener( 'beforeunload', this.handleUnload );
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-publish-widget {
	&__cancel-button {
		margin-right: @spacing-50;
	}
 
	.cdx-card__text__title {
		display: none;
	}
}
</style>