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

100% Statements 466/466
83.72% Branches 36/43
100% Functions 16/16
100% Lines 466/466

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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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x 6x 6x 6x 6x 6x 6x 6x 6x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for the Publish Dialog.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-publishdialog"
		data-testid="confirm-publish-dialog"
	>
		<cdx-dialog
			data-testid="publish-dialog"
			:open="showDialog"
			:title="publishDialogTitle"
			:close-button-label="$i18n( 'wikilambda-dialog-close' ).text()"
			:primary-action="primaryAction"
			:default-action="defaultAction"
			@default="closeDialog"
			@primary="publishZObject"
			@update:open="closeDialog"
		>
			<!-- Error and Warning section -->
			<div
				v-if="hasErrors"
				class="ext-wikilambda-publishdialog__errors"
			>
				<cdx-message
					v-for="( error, index ) in errors"
					:key="'dialog-error-' + index"
					class="ext-wikilambda-publishdialog__error"
					:type="error.type"
				>
					<!-- eslint-disable vue/no-v-html -->
					<div v-html="getErrorMessage( error )"></div>
				</cdx-message>
			</div>
 
			<!-- Summary section -->
			<cdx-field :status="status">
				<cdx-text-input
					v-model="summary"
					class="ext-wikilambda-publishdialog__summary-input"
					:aria-label="summaryLabel"
					:placeholder="summaryPlaceholder"
					@keydown="handleSummaryKeydown"
				></cdx-text-input>
				<template #label>
					{{ $i18n( 'wikilambda-editor-publish-dialog-summary-help-text' ).text() }}
				</template>
 
				<cdx-message
					v-if="hasKeyboardSubmitWarning"
					class="ext-wikilambda-publishdialog__keyboard-submit-warning"
					type="warning"
					:inline="true"
				>
					<div v-html="keyboardSubmitMessage"></div>
				</cdx-message>
			</cdx-field>
 
			<!-- Legal text -->
			<template #footer-text>
				<div
					class="ext-wikilambda-publishdialog__legal-text"
					v-html="legalText"
				></div>
			</template>
		</cdx-dialog>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	CdxField = require( '@wikimedia/codex' ).CdxField,
	CdxTextInput = require( '@wikimedia/codex' ).CdxTextInput,
	CdxMessage = require( '@wikimedia/codex' ).CdxMessage,
	CdxDialog = require( '@wikimedia/codex' ).CdxDialog,
	eventLogUtils = require( '../../mixins/eventLogUtils.js' ),
	errorUtils = require( '../../mixins/errorUtils.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions;
 
const enterKeyChar = `
	<kbd class="ext-wikilambda-publishdialog__kbd">
		<span class="ext-wikilambda-publishdialog__kbd-enter">&#x21B5;</span>
	</kbd>
`;
const cmdKeyChar = `
	<kbd class="ext-wikilambda-publishdialog__kbd">
		<span class="ext-wikilambda-publishdialog__kbd-cmd">&#x2318;</span>
	</kbd>
`;
 
const ctrlKeyChar = `
	<kbd class="ext-wikilambda-publishdialog__kbd">
		<span class="ext-wikilambda-publishdialog__kbd-ctrl">Ctrl</span>
	</kbd>
`;
module.exports = exports = defineComponent( {
	name: 'wl-publish-dialog',
	components: {
		'cdx-field': CdxField,
		'cdx-text-input': CdxTextInput,
		'cdx-message': CdxMessage,
		'cdx-dialog': CdxDialog
	},
	mixins: [ eventLogUtils, errorUtils ],
	inject: {
		viewmode: { default: false }
	},
	props: {
		showDialog: {
			type: Boolean,
			required: true,
			default: false
		},
		functionSignatureChanged: {
			type: Boolean,
			required: false,
			default: false
		}
	},
	data: function () {
		return {
			summary: '',
			hasKeyboardSubmitWarning: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getUserLangCode',
		'getCurrentZObjectId',
		'getCurrentZObjectType',
		'getCurrentZImplementationType',
		'getErrors',
		'getUserLangZid',
		'isCreateNewPage'
	] ), {
 
		/**
		 * Returns the status of the summary text field.
		 *
		 * @return {string}
		 */
		status: function () {
			return this.hasKeyboardSubmitWarning ? 'warning' : 'default';
		},
 
		/**
		 * Returns the array of errors and warnings of the page
		 *
		 * @return {Array}
		 */
		errors: function () {
			return this.getErrors( 0 );
		},
 
		/**
		 * Returns whether there are any errors in the page
		 * to show in the publish dialog
		 *
		 * @return { boolean }
		 */
		hasErrors: function () {
			return this.errors.length !== 0;
		},
 
		/**
		 * Returns an object of type PrimaryDialogAction that describes
		 * the action of the primary (save or publish) dialog button.
		 *
		 * @return {Object}
		 */
		primaryAction: function () {
			return {
				actionType: 'progressive',
				label: this.$i18n( 'wikilambda-publishnew' ).text()
			};
		},
 
		/**
		 * Returns an object of type DialogAction that describes
		 * the action of the secondary (cancel) button.
		 *
		 * @return {Object}
		 */
		defaultAction: function () {
			return {
				label: this.$i18n( 'wikilambda-cancel' ).text()
			};
		},
 
		/**
		 * Returns the title for the Publish dialog
		 *
		 * @return {string}
		 */
		publishDialogTitle: function () {
			return this.$i18n( 'wikilambda-editor-publish-dialog-header' ).text();
		},
 
		/**
		 * Returns the label for the summary text field
		 *
		 * @return {string}
		 */
		summaryLabel: function () {
			return this.$i18n( 'wikilambda-editor-publish-dialog-summary-label' ).text();
		},
 
		/**
		 * Returns the placeholder for the summary text field
		 *
		 * @return {string}
		 */
		summaryPlaceholder: function () {
			return this.$i18n( 'wikilambda-editor-publish-dialog-summary-placeholder' ).text();
		},
 
		/**
		 * Returns the legal text to display in the Publish Dialog, depending
		 * on the type of object that is being submitted:
		 * * Special message for implementations (Apache 2.0 licence for code).
		 * * General message for all other kinds of ZObjects (CC0).
		 *
		 * @return { string }
		 */
		legalText: function () {
			return ( this.getCurrentZObjectType === Constants.Z_IMPLEMENTATION ) ?
				this.$i18n( 'wikifunctions-edit-copyrightwarning-implementation' ).text() :
				this.$i18n( 'wikifunctions-edit-copyrightwarning-function' ).text();
		},
 
		/**
		 * Returns a warning message which informs the user that they can submit using Ctrl/CMD + Enter;
		 *
		 * @return {string}
		 */
		keyboardSubmitMessage: function () {
			const isMac = /Mac|iPod|iPhone|iPad/.test( navigator.userAgent );
			return this.$i18n( 'wikilambda-editor-publish-dialog-keyboard-submit-warning', isMac ? cmdKeyChar : ctrlKeyChar, enterKeyChar );
		}
	} ),
	methods: Object.assign( mapActions( [
		'submitZObject',
		'setError',
		'setDirty',
		'clearAllErrors'
	] ),
	{
 
		/**
		 * Handle pressing the Enter key on the summary field.
		 *
		 * @param {Event} event The keydown event.
		 */
		handleSummaryEnter: function ( event ) {
			event.preventDefault();
			this.hasKeyboardSubmitWarning = true;
		},
 
		/**
		 * Handles the keydown event on the summary text field.
		 * - If the user presses Ctrl/Cmd + Enter, publishes the ZObject.
		 * - If the user presses Enter, shows a warning message.
		 *
		 * @param {Event} event The keydown event.
		 */
		handleSummaryKeydown: function ( event ) {
			const enterKey = event.key === 'Enter';
 
			// If the user presses Ctrl/Cmd + Enter, publish the ZObject
			if ( ( event.metaKey || event.ctrlKey ) && enterKey ) {
				this.publishZObject();
				return;
			}
 
			// If the user presses Enter, show a warning message
			if ( enterKey ) {
				this.handleSummaryEnter( event );
			}
		},
 
		/**
		 * Clears the error notifications and emits a close-dialog
		 * event for the Publish widget to close the dialog.
		 */
		closeDialog: function () {
			this.hasKeyboardSubmitWarning = false;
 
			if ( this.hasErrors ) {
				this.clearAllErrors();
			}
			this.$emit( 'close-dialog' );
		},
 
		/**
		 * Before exiting the page after successful publishing, handle
		 * state and remove exit event listeners.
		 *
		 * @param {string | undefined} pageTitle
		 */
		successfulExit: function ( pageTitle ) {
			this.$emit( 'before-exit' );
			this.setDirty( false );
			this.closeDialog();
			window.location.href = !pageTitle ?
				new mw.Title( Constants.PATHS.MAIN_PAGE ).getUrl() :
				`/view/${ this.getUserLangCode }/${ pageTitle }?success=true`;
		},
 
		/**
		 * Submits the ZObject to the wikilambda_edit API
		 * and handles the return value:
		 * 1. If the response contains an error, saves the error
		 *    in the store/errors module and displays the error messages
		 *    in the Publish Dialog notification block.
		 * 2. If the response is successful, navigates to the ZObject
		 *    page.
		 */
		publishZObject: function () {
			const summary = this.summary;
			const disconnectFunctionObjects = this.functionSignatureChanged;
 
			this.submitZObject( {
				summary,
				disconnectFunctionObjects
			} ).then( ( response ) => {
				this.successfulExit( response.page );
			} ).catch( ( response ) => {
				this.clearAllErrors();
				// If response.error.message: known ZError
				// Else, PHP error or exception captured in response.error.info
				// Additionally, if nothing available, show generic unknown error message
				const errorMessage = ( response && response.error ) ?
					( response.error.message || response.error.info ) :
					undefined;
 
				const payload = {
					rowId: 0,
					errorType: Constants.errorTypes.ERROR,
					errorMessage,
					errorCode: !errorMessage ? Constants.errorCodes.UNKNOWN_ERROR : undefined
				};
 
				this.setError( payload );
			} ).finally( () => {
				// After receiving the response, log a publish event
				const eventNamespace = this.getNamespace( this.getCurrentZObjectType );
				const customData = {
					isnewzobject: this.isCreateNewPage,
					zobjectid: this.getCurrentZObjectId,
					zobjecttype: this.getCurrentZObjectType || null,
					implementationtype: this.getCurrentZImplementationType || null,
					zlang: this.getUserLangZid || null,
					haserrors: this.hasErrors
				};
				this.dispatchEvent( `wf.ui.${ eventNamespace }.publish`, customData );
				// T350497 Update the WikiLambda instrumentation to use core interaction events
				const interactionData = {
					zobjecttype: this.getCurrentZObjectType || null,
					zobjectid: this.getCurrentZObjectId || null,
					zlang: this.getUserLangZid || null,
					implementationtype: this.getCurrentZImplementationType || null,
					haserrors: this.hasErrors
				};
				this.submitInteraction( 'publish', interactionData );
			} );
		}
	} )
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-publishdialog {
	&__errors {
		margin-bottom: @spacing-200;
	}
 
	&__summary {
		display: flex;
		flex-direction: column;
		padding: @spacing-50 0;
		color: @color-placeholder;
	}
 
	&__summary-label {
		padding-bottom: @spacing-25;
	}
 
	&__kbd {
		font-size: @font-size-x-small;
		font-weight: @font-weight-normal;
		line-height: @line-height-x-small;
		color: @color-base;
		background-color: @background-color-interactive-subtle;
		border: @border-width-base @border-style-base @border-color-subtle;
		border-radius: @border-radius-base;
		padding: (@spacing-12 / 2) @spacing-25;
		box-shadow: @box-shadow-drop-small;
		font-family: inherit;
		display: inline-block;
	}
 
	&__kbd-enter {
		position: relative;
		top: (@spacing-12 / 2);
	}
 
	&__keyboard-submit-warning {
		margin-top: @spacing-25;
	}
 
	&__divider {
		margin-top: @spacing-100;
	}
 
	&__actions {
		display: flex;
		flex-direction: row-reverse;
 
		&__button-cancel {
			margin-right: @spacing-50;
		}
	}
 
	&__legal-text {
		color: @color-placeholder;
	}
 
	&__body {
		padding: 0 @spacing-100 @spacing-100;
	}
 
	&__header {
		display: flex;
		justify-content: space-between;
		padding: @spacing-50 0;
		position: sticky;
		top: @spacing-100;
		background: @background-color-base;
 
		&__title {
			width: 100%;
			font-weight: bold;
			font-size: 1.15em;
			margin: auto;
		}
 
		&__close-button {
			display: flex;
			color: @color-notice;
			justify-content: center;
			align-items: center;
			height: @size-200;
			width: @size-200;
			background: none;
			border: 0;
			margin: auto;
		}
	}
}
</style>