textareas fit to content in UserBio and Problems
[vchess.git] / client / src / components / UserBio.vue
CommitLineData
dd10eb93 1<template lang="pug">
3c24a27d
BA
2div(:id="'player_' + uid")
3 input.modal(
4 :id="'modalBio_' + uid"
5 type="checkbox"
6 )
7 div.bio-div(
dd10eb93 8 role="dialog"
3c24a27d 9 :data-checkbox="'modalBio_' + uid"
dd10eb93
BA
10 )
11 .card
3c24a27d 12 div(v-if="st.user.id == uid")
ad16f839
BA
13 div.buttons
14 button(@click="toggleEdit()")
3c24a27d
BA
15 | {{ st.tr[modeEdit ? "Cancel" : "Edit"] }}
16 button(
17 v-show="modeEdit"
18 @click="sendBio()"
19 )
20 | {{ st.tr["Send"] }}
21 fieldset(v-if="userBio !== undefined && modeEdit")
22 textarea(
ad16f839 23 @input="adjustHeight()"
3c24a27d
BA
24 v-model="userBio"
25 )
26 h3 {{ uname }}
27 .bio-content(
dd10eb93 28 v-if="userBio !== undefined"
3c24a27d 29 v-html="parseHtml(userBio)"
dd10eb93
BA
30 @click="modeEdit = !modeEdit"
31 )
3c24a27d 32 .dialog.text-center {{ st.tr[infoMsg] }}
80b38d46
BA
33 span(
34 :class="{ clickable: !!uname }"
35 @click="showBio()"
36 )
37 | {{ uname || "@nonymous" }}
dd10eb93
BA
38</template>
39
40<script>
41import { store } from "@/store";
42import { ajax } from "@/utils/ajax";
43import { processModalClick } from "@/utils/modalClick.js";
44export default {
45 name: "my-user-bio",
80b38d46 46 props: ["uid", "uname"],
dd10eb93
BA
47 data: function() {
48 return {
49 st: store.state,
50 userBio: undefined,
51 infoMsg: "",
52 modeEdit: false
53 };
54 },
dd10eb93 55 methods: {
3c24a27d
BA
56 parseHtml: function(txt) {
57 return !txt.match(/<[/a-zA-Z]+>/)
ad16f839
BA
58 ?
59 // No HTML tag
60 txt.replace(/\n\n/g, "<br/><div class='br'></div>")
61 .replace(/\n/g, "<br/>")
3c24a27d
BA
62 : txt;
63 },
ad16f839 64 adjustHeight: function() {
3c24a27d 65 // https://stackoverflow.com/a/48460773
ad16f839 66 let t = document.querySelector("#player_" + this.uid + " textarea");
3c24a27d 67 t.style.height = "";
ad16f839
BA
68 t.style.height = (t.scrollHeight + 3) + "px";
69 },
70 toggleEdit: function() {
71 this.modeEdit = !this.modeEdit;
72 if (this.modeEdit) this.$nextTick(this.adjustHeight);
3c24a27d 73 },
dd10eb93 74 showBio: function() {
80b38d46
BA
75 if (!this.uname)
76 // Anonymous users have no bio:
77 return;
dd10eb93 78 this.infoMsg = "";
3c24a27d 79 document.querySelector("#modalBio_" + this.uid).checked = true;
dd10eb93
BA
80 if (this.userBio === undefined) {
81 ajax(
82 "/userbio",
83 "GET",
84 {
80b38d46 85 data: { id: this.uid },
dd10eb93
BA
86 success: (res) => {
87 this.userBio = res.bio;
88 }
89 }
90 );
3c24a27d
BA
91 document.querySelector("#player_" + this.uid + " > .bio-div")
92 .addEventListener("click", processModalClick);
dd10eb93 93 }
ad16f839 94 else if (this.modeEdit) this.adjustHeight();
dd10eb93
BA
95 },
96 sendBio: function() {
97 ajax(
98 "/userbio",
99 "PUT",
100 {
101 data: { bio: this.userBio },
102 success: () => {
103 this.infoMsg = this.st.tr["Modifications applied!"];
104 }
105 }
106 );
107 }
108 }
109};
110</script>
111
3c24a27d
BA
112<style lang="sass">
113// bio-content HTML elements are added after initial rendering
114.bio-content
115 text-align: left
116 margin: 0 var(--universal-margin)
117 p, ul, ol
118 margin: var(--universal-margin) 0
ad16f839
BA
119 .br
120 display: block
121 margin: 10px 0
3c24a27d
BA
122</style>
123
dd10eb93
BA
124<style lang="sass" scoped>
125[type="checkbox"].modal+div .card
3c24a27d 126 max-width: 500px
dd10eb93
BA
127 max-height: 100%
128
ad16f839
BA
129.buttons > button
130 margin-bottom: 0
131
132h3
133 margin-bottom: 5px
134
3c24a27d
BA
135textarea
136 display: block
137 margin: 0 var(--universal-margin)
138 width: calc(100% - 2 * var(--universal-margin))
139 min-height: 100px
dd10eb93 140
3c24a27d 141.dialog
dd10eb93
BA
142 padding: 5px
143 color: blue
144</style>