Skip to content
Snippets Groups Projects
poll-editor.vue 2.6 KiB
Newer Older
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
<template>
<div class="mk-poll-editor">
	<p class="caution" v-if="choices.length < 2">
syuilo's avatar
syuilo committed
		%fa:exclamation-triangle%%i18n:@no-only-one-choice%
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
	</p>
	<ul ref="choices">
syuilo's avatar
syuilo committed
		<li v-for="(choice, i) in choices">
syuilo's avatar
syuilo committed
			<input :value="choice" @input="onInput(i, $event)" :placeholder="'%i18n:!@choice-n%'.replace('{}', i + 1)">
syuilo's avatar
syuilo committed
			<button @click="remove(i)" title="%i18n:@remove%">
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
				%fa:times%
			</button>
		</li>
	</ul>
syuilo's avatar
syuilo committed
	<button class="add" v-if="choices.length < 10" @click="add">%i18n:@add%</button>
	<button class="destroy" @click="destroy" title="%i18n:@destroy%">
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
		%fa:times%
	</button>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
	data() {
		return {
			choices: ['', '']
		};
	},
syuilo's avatar
syuilo committed
	watch: {
		choices() {
			this.$emit('updated');
		}
	},
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
	methods: {
		onInput(i, e) {
syuilo's avatar
syuilo committed
			Vue.set(this.choices, i, e.target.value);
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
		},

		add() {
			this.choices.push('');
syuilo's avatar
syuilo committed
			this.$nextTick(() => {
				(this.$refs.choices as any).childNodes[this.choices.length - 1].childNodes[0].focus();
			});
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
		},

		remove(i) {
			this.choices = this.choices.filter((_, _i) => _i != i);
		},

		destroy() {
			this.$emit('destroyed');
		},

		get() {
			return {
				choices: this.choices.filter(choice => choice != '')
			}
		},

		set(data) {
			if (data.choices.length == 0) return;
			this.choices = data.choices;
syuilo's avatar
syuilo committed
			if (data.choices.length == 1) this.choices = this.choices.concat('');
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
		}
	}
});
</script>

<style lang="stylus" scoped>
syuilo's avatar
syuilo committed
@import '~const.styl'

syuilo's avatar
syuilo committed
root(isDark)
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
	padding 8px

	> .caution
		margin 0 0 8px 0
		font-size 0.8em
		color #f00

		> [data-fa]
			margin-right 4px

	> ul
		display block
		margin 0
		padding 0
		list-style none

		> li
			display block
			margin 8px 0
			padding 0
			width 100%

			&:first-child
				margin-top 0

			&:last-child
				margin-bottom 0

			> input
syuilo's avatar
syuilo committed
				padding 6px 8px
				width 300px
				font-size 14px
syuilo's avatar
syuilo committed
				color isDark ? #fff : #000
				background isDark ? #191b22 : #fff
こぴなたみぽ's avatar
wip
こぴなたみぽ committed
				border solid 1px rgba($theme-color, 0.1)
				border-radius 4px

				&:hover
					border-color rgba($theme-color, 0.2)

				&:focus
					border-color rgba($theme-color, 0.5)

			> button
				padding 4px 8px
				color rgba($theme-color, 0.4)

				&:hover
					color rgba($theme-color, 0.6)

				&:active
					color darken($theme-color, 30%)

	> .add
		margin 8px 0 0 0
		vertical-align top
		color $theme-color

	> .destroy
		position absolute
		top 0
		right 0
		padding 4px 8px
		color rgba($theme-color, 0.4)

		&:hover
			color rgba($theme-color, 0.6)

		&:active
			color darken($theme-color, 30%)

syuilo's avatar
syuilo committed
.mk-poll-editor[data-darkmode]
	root(true)

.mk-poll-editor:not([data-darkmode])
	root(false)

こぴなたみぽ's avatar
wip
こぴなたみぽ committed
</style>