All files / ext.wikilambda.edit/components ZObjectJson.vue

67.5% Statements 27/40
62.5% Branches 5/8
76.92% Functions 10/13
67.5% Lines 27/40

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                                      24x 24x 24x 24x 24x     24x               7x           7x 7x     4x 4x                                             7x                 2x     35x 33x 33x           2x 2x         35x                   16x         16x     16x 16x                                                           28x 28x       7x 7x                        
<template>
	<!--
		WikiLambda Vue component for the JSON rendering for ZObjects.
 
		@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
		@license MIT
	-->
	<div class="ext-wikilambda-json">
		<wl-code-editor
			v-clickout="onClickoutHandler"
			mode="json"
			:value="initialJson"
			:read-only="readonly || viewmode"
			@change="codeChangeHandler"
		></wl-code-editor>
	</div>
</template>
 
<script>
var mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	typeUtils = require( '../mixins/typeUtils.js' ),
	schemata = require( '../mixins/schemata.js' ),
	CodeEditor = require( './base/CodeEditor.vue' );
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-object-json',
	components: {
		'wl-code-editor': CodeEditor
	},
	directives: {
		clickout: {
			beforeMount: function ( el, binding ) {
				el.clickout = {
					stop: function ( e ) {
						e.stopPropagation();
					}
				};
 
				document.body.addEventListener( 'click', binding.value );
				el.addEventListener( 'click', el.clickout.stop );
			},
			unmounted: function ( el, binding ) {
				document.body.removeEventListener( 'click', binding.value );
				el.removeEventListener( 'click', el.clickout.stop );
			}
		}
	},
	mixins: [ typeUtils, schemata ],
	inject: {
		viewmode: { default: false }
	},
	props: {
		zobjectId: {
			type: Number,
			required: false,
			default: 0
		},
		zobjectRaw: {
			type: [ Object, String ]
		},
		readonly: {
			type: Boolean,
			default: false
		}
	},
	data: function () {
		return {
			codeEditorState: '',
			initialJson: ''
		};
	},
	computed: $.extend( {},
		mapGetters( [ 'getZObjectAsJsonById', 'getZObjectById' ] ),
		{
			zobject: function () {
				return this.getZObjectById( this.zobjectId );
			},
			zobjectJson: function () {
				if ( this.zobjectRaw !== undefined ) {
					try {
						return typeof this.zobjectRaw === 'string' ?
							JSON.parse( this.zobjectRaw ) :
							this.zobjectRaw;
					} catch ( err ) {
						return this.zobjectRaw;
					}
				} else if ( this.zobject ) {
					return this.getZObjectAsJsonById( this.zobjectId, this.zobject.value === 'array' );
				}
				return '';
			},
			canonicalJson: function () {
				return JSON.stringify( this.canonicalizeZObject( this.zobjectJson ), null, 4 );
			}
		}
	),
	methods: $.extend( {},
		mapActions( [
			'setIsZObjectDirty'
		] ),
		{
			codeChangeHandler: function ( val ) {
				this.codeEditorState = val;
				// this will ensure non-clickout events
				// (like clicking the submit button directly) still get processed the same way
				// however this will trigger a lot of false positives so there is probably a smarter way to do this
				// TODO (T301286): find  a more performant solution
				this.onClickoutHandler();
			},
			onClickoutHandler: function () {
				if ( this.readonly ) {
					return;
				}
				var json,
					self = this;
				Iif ( this.zobjectId !== undefined ) {
					try {
						json = JSON.parse( this.codeEditorState );
					} catch ( error ) {
						// JSON parse failed, do nothing
						return;
					}
					this.$store.dispatch( 'injectZObject', {
						zobject: json,
						key: this.zobject.key,
						id: this.zobjectId,
						parent: this.zobject.parent
					} ).then( function ( newType ) {
						Iif ( self.isValidZidFormat( newType ) ) {
							self.$emit( 'change-literal', newType );
						}
					} ).catch( function ( error ) {
						throw error;
					} );
				}
				this.setIsZObjectDirty( true );
			}
		}
	),
	watch: {
		zobjectRaw: function () {
			this.codeEditorState = this.canonicalJson;
			this.initialJson = this.canonicalJson;
		}
	},
	mounted: function () {
		this.codeEditorState = this.canonicalJson;
		this.initialJson = this.canonicalJson;
	}
};
</script>
 
<style lang="less">
.ext-wikilambda-json {
	white-space: pre-wrap;
	font-family: 'Courier New', 'Courier', monospace;
	width: 100%;
}
</style>