All files / ext.wikilambda.edit/views ZObjectEditor.vue

60.6% Statements 20/33
37.5% Branches 3/8
54.54% Functions 6/11
60.6% Lines 20/33

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                                                                          13x 13x 13x 13x 13x 13x 13x 13x     13x                     8x                           8x     21x                                                 7x     7x 130x 130x 7x                             8x 8x     4x                                
<template>
	<!--
		WikiLambda Vue interface module for top-level editor encapsulation.
 
		@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
		@license MIT
	-->
	<!-- TODO (T300537): Add a loading indicator, once T300538 is done upstream. -->
	<div id="ext-wikilambda-editor">
		<wl-z-object
			:persistent="true"
		></wl-z-object>
		<template v-if="showEditCommand">
			<wl-z-object-publish :is-disabled="!isDirty"></wl-z-object-publish>
			<cdx-button
				class="ext-wikilambda-expertModeToggle"
				@click="$store.dispatch( 'toggleExpertMode' )">
				<template v-if="$store.getters.isExpertMode">
					{{ $i18n( 'wikilambda-disable-expert-mode' ).text() }}
				</template>
				<template v-else>
					{{ $i18n( 'wikilambda-enable-expert-mode' ).text() }}
				</template>
			</cdx-button>
			<cdx-message v-if="message.text" :type="message.type">
				{{ message }}
			</cdx-message>
		</template>
		<wl-leave-editor-dialog
			:show-dialog="showLeaveEditorDialog"
			:continue-callback="leaveEditorCallback"
			@close-dialog="closeLeaveEditorDialog"
		></wl-leave-editor-dialog>
	</div>
</template>
 
<script>
var ZObject = require( '../components/ZObject.vue' ),
	ZObjectPublish = require( '../components/ZObjectPublish.vue' ),
	LeaveEditorDialog = require( '../components/base/LeaveEditorDialog.vue' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxMessage = require( '@wikimedia/codex' ).CdxMessage,
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	typeUtils = require( '../mixins/typeUtils.js' );
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-object-editor',
	components: {
		'wl-z-object': ZObject,
		'wl-z-object-publish': ZObjectPublish,
		'wl-leave-editor-dialog': LeaveEditorDialog,
		'cdx-button': CdxButton,
		'cdx-message': CdxMessage
	},
	mixins: [ typeUtils ],
	data: function () {
		return {
			showLeaveEditorDialog: false,
			leaveEditorCallback: ''
		};
	},
	computed: $.extend( mapGetters( {
		createNewPage: 'isCreateNewPage',
		message: 'getZObjectMessage',
		getZObjectChildrenById: 'getZObjectChildrenById',
		getIsZObjectDirty: 'getIsZObjectDirty'
	} ), {
		showEditCommand: function () {
			// TODO: Move this into its own vuex store as things gets more complicated and more view settings are set
			// we currently hide the save command for evaluate function call.
			return mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'EvaluateFunctionCall';
		},
		isDirty: function () {
			return this.getIsZObjectDirty;
		}
	} ),
	methods: $.extend( {},
		mapActions( [ 'submitZObject', 'changeType', 'validateZObject' ] ),
		mapActions( 'router', [ 'navigate' ] ),
		{
			submit: function () {
				const context = this;
				this.validateZObject().then( function ( isValid ) {
					Iif ( isValid ) {
						context.submitZObject( { summary: context.summary } ).then( function ( pageTitle ) {
							Iif ( pageTitle ) {
								window.location.href = new mw.Title( pageTitle ).getUrl();
							}
						} );
					}
				} );
			},
 
			closeLeaveEditorDialog: function () {
				this.showLeaveEditorDialog = false;
			},
 
			handleClickAway: function ( e ) {
				let target = e.target;
 
				// Find if what was clicked was a link.
				while ( target && target.tagName !== 'A' ) {
					target = target.parentNode;
					if ( !target ) {
						return;
					}
				}
				Iif ( target.href && this.isDirty ) {
					this.showLeaveEditorDialog = true;
					e.preventDefault();
					this.leaveEditorCallback = function () {
						window.removeEventListener( 'click', this.handleClickAway );
						window.location.href = target.href;
					}.bind( this );
				}
			}
		}
	),
	mounted: function () {
		this.$emit( 'mounted' );
		window.addEventListener( 'click', this.handleClickAway );
	},
	beforeUnmount: function () {
		window.removeEventListener( 'click', this.handleClickAway );
	}
};
</script>
 
<style lang="less">
@import 'mediawiki.mixins';
 
.ext-wikilambda-expertModeToggle {
	margin: 1em 0 0 0;
}
 
.ext-wikilambda-editor-nojswarning {
	display: none;
}
</style>