planning-poker/assets/js/app.js

82 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-05-02 21:55:20 +00:00
// We need to import the CSS so that webpack will load it.
// The MiniCssExtractPlugin is used to separate it out into
// its own CSS file.
import "../css/app.scss"
// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import deps with the dep name or local files with a relative path, for example:
//
// import {Socket} from "phoenix"
// import socket from "./socket"
//
import "phoenix_html"
2020-05-06 05:30:43 +00:00
import { Socket, Presence } from "phoenix"
import { Elm } from "../src/Main.elm"
import uuid4 from "uuid4"
var player_id = uuid4()
2020-05-07 02:02:44 +00:00
var room_id = uuid4()
2020-05-06 05:30:43 +00:00
var socket = new Socket("/socket", {params: {player_id: player_id}})
socket.connect()
2020-05-02 21:55:20 +00:00
2021-01-23 05:03:27 +00:00
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
2020-05-02 21:55:20 +00:00
var app = Elm.Main.init({
2020-05-06 05:30:43 +00:00
node: document.getElementById("elm-main"),
2020-05-07 02:02:44 +00:00
flags: {
2020-05-08 04:34:42 +00:00
player: player_id,
room: room_id,
width: window.innerWidth,
2021-01-23 05:03:27 +00:00
height: window.innerHeight,
theme: getCookie("theme")
2020-05-07 02:02:44 +00:00
}
2020-05-06 05:30:43 +00:00
})
app.ports.joinRoom.subscribe(options => {
2020-05-07 00:03:31 +00:00
let channel = socket.channel("room:" + options.room, {})
2020-05-09 06:06:30 +00:00
// Presence events
channel.on("presence_state", app.ports.gotPresenceState.send)
channel.on("presence_diff", app.ports.gotPresenceDiff.send)
2020-05-09 06:06:30 +00:00
// Incoming room events
2020-05-15 01:48:00 +00:00
channel.on("vote", app.ports.gotVote.send)
channel.on("reset", app.ports.gotReset.send)
2020-05-15 02:18:38 +00:00
channel.on("reveal", app.ports.gotReveal.send)
2020-05-09 06:06:30 +00:00
// Outgoing room events
app.ports.roomActions.subscribe(action => {
channel.push(action.type, action.data)
2020-05-08 05:57:55 +00:00
})
2020-05-09 06:06:30 +00:00
2021-01-23 05:03:27 +00:00
// Theme changes
app.ports.saveTheme.subscribe(theme => {
document.cookie = "theme=" + theme
})
2020-05-07 00:03:31 +00:00
channel.join()
.receive("ok", resp => {
console.log("Joined successfully", resp);
})
.receive("error", resp => { console.log("Unable to join", resp) })
2020-05-06 05:30:43 +00:00
})