Skip to content

Commit

Permalink
fix frontend bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Aug 1, 2017
1 parent c007e4a commit aa4596f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 51 deletions.
1 change: 0 additions & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<body>
<script src="js/ws.js"></script>
<script src="js/main.js"></script>
<div id="temp"></div>
</body>
</html>

116 changes: 66 additions & 50 deletions web/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var Connection = new Client("ws://localhost:8080/ws");
function getWebSocketEndpoint() {
var h = window.location.href.split("/")
return "ws" + h[0].replace("http", "") + "//" + h[2] + "/ws";
}

var Connection = new Client(getWebSocketEndpoint());

var Messages = {
list: [],
Expand Down Expand Up @@ -101,54 +106,60 @@ var App = {
m("a", { href: route, oncreate: m.route.link }, caption)
])
}

var items = [
m(".col-xs-7", [
m("ul.nav.nav-pills", [
m("li.nav-header", {role: "presentation"}, [
m("a#chat", { href: "/chat", oncreate: m.route.link }, "GoChat"),
]),
nav("/about", "about"),
]),
])
]

if (Bootstrap.ready) {
items.push(m(".col-xs-5.text-right", [
m("form.user", {
onsubmit: function(e) {
e.preventDefault()
if (User.name.length != 0) {
Bootstrap.rename()
}
}
}, [
m("span.glyphicon.glyphicon-user"),
m("span.user-prefix", "@"),
m("input.user-name", {
value: User.name,
oncreate: function(vnode) {
vnode.dom.style.width = textWidth(vnode.dom.value)
},
onfocus: function(e) {
var prev = this.value
setTimeout(function() {
e.target.value = prev
}, 1)
},
oninput: function(e) {
var el = e.target
User.name = el.value
el.style.width = textWidth(el.value)
},
onchange: function(e) {
if (User.name.length != 0) {
Bootstrap.rename()
}
}
}),
])
]))
}

return [
m("header.header", [
m("div.container", [
m(".row", [
m(".col-xs-7", [
m("ul.nav.nav-pills", [
m("li.nav-header", {role: "presentation"}, [
m("a#chat", { href: "/chat", oncreate: m.route.link }, "GoChat"),
]),
nav("/about", "about"),
]),
]),
m(".col-xs-5.text-right", [
m("form.user", {
onsubmit: function(e) {
e.preventDefault()
if (User.name.length != 0) {
Bootstrap.rename()
}
}
}, [
m("span.glyphicon.glyphicon-user"),
m("span.user-prefix", "@"),
m("input.user-name", {
value: User.name,
oncreate: function(vnode) {
vnode.dom.style.width = textWidth(vnode.dom.value)
},
onfocus: function(e) {
var prev = this.value
setTimeout(function() {
e.target.value = prev
}, 1)
},
oninput: function(e) {
var el = e.target
User.name = el.value
el.style.width = textWidth(el.value)
},
onchange: function(e) {
if (User.name.length != 0) {
Bootstrap.rename()
}
}
}),
])
])
])
m(".row", items)
])
]),
m("div.container.content", [
Expand All @@ -160,8 +171,9 @@ var App = {

function textWidth(text) {
var ret = 0;
var temp = document.getElementById("temp");
m.render(temp, m("div", {
var div = document.createElement('div');
document.body.appendChild(div);
m.render(div, m("div", {
oncreate: function(vnode) {
ret = vnode.dom.clientWidth;
},
Expand All @@ -177,8 +189,12 @@ function textWidth(text) {
"width": "auto",
"white-space": "nowrap"
},
}, text))
return ret + 5 + "px"
}, text));

ret = ret + 5 + "px";
document.body.removeChild(div);

return ret
}

var Message = {
Expand Down

0 comments on commit aa4596f

Please sign in to comment.