All files / ext.wikilambda.app/components/widgets/publish Publish.vue

94.42% Statements 271/287
81.81% Branches 27/33
81.25% Functions 13/16
94.42% Lines 271/287

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 279 280 281 282 283 284 285 286 287 2881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 8x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 5x 5x 5x 8x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 718x 718x 32x 32x 718x 1x 1x 1x 1x 1x 1x 1x 1x 33x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 10x 10x 10x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 22x 22x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 22x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	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-app-publish-widget" data-testid="publish-widget">
		<template #main>
			<cdx-button
				class="ext-wikilambda-app-publish-widget__cancel-button"
				weight="primary"
				@click.stop="handleCancel"
			>
				{{ $i18n( 'wikilambda-cancel' ).text() }}
			</cdx-button>
			<cdx-button
				class="ext-wikilambda-app-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 { CdxButton } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	WidgetBase = require( '../../base/WidgetBase.vue' ),
	LeaveEditorDialog = require( './LeaveEditorDialog.vue' ),
	PublishDialog = require( './PublishDialog.vue' ),
	eventLogger = require( '../../../mixins/eventLogUtils.js' ).methods,
	urlUtils = require( '../../../mixins/urlUtils.js' ),
	{ mapActions } = require( 'vuex' ),
	{ mapGetters } = require( 'vuex' );
 
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
	},
	mixins: [ urlUtils ],
	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.
		 * If true, this enables the Publish button without needing an event.
		 *
		 * @return {boolean}
		 */
		revertToEdit: function () {
			return !!( this.getParameterByName( 'oldid' ) || this.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
		 * clicking 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,
			 * - target property is _blank,
			 * - the link is to the current page, (usually when it's a hash link)
			 * - the link is a button
			 * we are staying in this page, so there's no need to handle cancelation
			 */
			if ( !target.href || target.target === '_blank' || this.isLinkCurrentPath( target.href ) || target.role === 'button' ) {
				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();
				// Log an event using Metrics Platform's 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();
			}
		},
 
		/**
		 * Add event listeners.
		 */
		addListeners: function () {
			window.addEventListener( 'click', this.handleClickAway );
			window.addEventListener( 'beforeunload', this.handleUnload );
		},
 
		/**
		 * Remove event listeners.
		 */
		removeListeners: function () {
			window.removeEventListener( 'click', this.handleClickAway );
			window.removeEventListener( 'beforeunload', this.handleUnload );
		}
	} ),
	mounted: function () {
		this.addListeners();
	},
	beforeUnmount: function () {
		this.removeListeners();
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-publish-widget {
	.ext-wikilambda-app-publish-widget__cancel-button {
		margin-right: @spacing-50;
	}
}
</style>