chatWidget :: (YesodChat master, ChatMessage master ~ ChatMessage master0)
           => (Route Chat -> Route master) -> WidgetT master IO ()
chatWidget toMaster = do
    let disabledChat = do
            master <- liftHandlerT getYesod
            [whamlet|
<h1 .icon-chat> Chat
<p>
    You must be #
    $maybe ar <- authRoute master
        <a href=@{ar}>logged in
    $nothing
        logged in
    \ to chat.
|]
    mident <- liftHandlerT chatIdent -- check if we're already logged in
    flip (maybe disabledChat) mident $ \_ -> do
        recent  <- liftHandlerT chatGet
        chat    <- liftHandlerT newIdent   -- the containing div
        [whamlet|
<h1 .icon-chat> Chat
<div.clearfix ##{chat}>
    <div ##{chat}-output>
      $forall msg <- recent
        #{preEscapedToMarkup $ decodeUtf8 $ toByteString $ chatRenderMsg msg}
    <input ##{chat}-input type=text placeholder="Enter Message">
|] >> toWidget [lucius|
 ##{chat}-output { width: 100%; max-height: 300px; overflow: auto; }
 ##{chat}-input { width: 100%; padding-left:0; padding-right:0; }
 ##{chat}-output p {
    margin:0;
}
|] >> toWidgetBody [julius|
// Set up the receiving end
var output = document.getElementById("#{rawJS chat}-output");
var src = new EventSource("@{toMaster ReceiveR}");
src.onmessage = function(msg) {
    // This function will be called for each new message.
    output.innerHTML = output.innerHTML + msg.data;

    // And now scroll down within the output div so the most recent message
    // is displayed.
    output.scrollTop = output.scrollHeight;
};

// Set up the sending end: send a message via Ajax whenever the user hits
// enter.
var input = document.getElementById("#{rawJS chat}-input");
input.onkeyup = function(event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == '13') {
        var xhr = new XMLHttpRequest();
        var val = input.value;
        input.value = "";
        var params = "?message=" + encodeURIComponent(val);
        xhr.open("POST", "@{toMaster SendR}" + params);
        xhr.send(null);
    }
}
|]
chatWidget :: (YesodChat master, ChatMessage master ~ ChatMessage master0)
           => (Route Chat -> Route master) -> WidgetT master IO ()
chatWidget toMaster = do
    let disabledChat = do
            master <- liftHandlerT getYesod
            [whamlet|
<h1 .icon-chat> Chat
<p>
    You must be #
    $maybe ar <- authRoute master
        <a href=@{ar}>logged in
    $nothing
        logged in
    \ to chat.
|]
    mident <- liftHandlerT chatIdent -- check if we're already logged in
    flip (maybe disabledChat) mident $ \_ -> do
        recent  <- liftHandlerT chatGet
        chat    <- liftHandlerT newIdent   -- the containing div
        [whamlet|
<h1 .icon-chat> Chat
<div.clearfix ##{chat}>
    <div ##{chat}-output>
      $forall msg <- recent
        #{preEscapedToMarkup $ decodeUtf8 $ toByteString $ chatRenderMsg msg}
    <input ##{chat}-input type=text placeholder="Enter Message">
|] >> toWidget [lucius|
 ##{chat}-output { width: 100%; max-height: 300px; overflow: auto; }
 ##{chat}-input { width: 100%; padding-left:0; padding-right:0; }
 ##{chat}-output p {
    margin:0;
}
|] >> toWidgetBody [julius|
// Set up the receiving end
var output = document.getElementById("#{rawJS chat}-output");
var src = new EventSource("@{toMaster ReceiveR}");
src.onmessage = function(msg) {
    // This function will be called for each new message.
    output.innerHTML = output.innerHTML + msg.data;

    // And now scroll down within the output div so the most recent message
    // is displayed.
    output.scrollTop = output.scrollHeight;
};

// Set up the sending end: send a message via Ajax whenever the user hits
// enter.
var input = document.getElementById("#{rawJS chat}-input");
input.onkeyup = function(event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == '13') {
        var xhr = new XMLHttpRequest();
        var val = input.value;
        input.value = "";
        var params = "?message=" + encodeURIComponent(val);
        xhr.open("POST", "@{toMaster SendR}" + params);
        xhr.send(null);
    }
}
|]
chatWidget toMaster = do
    let disabledChat = do
            master <- liftHandlerT getYesod
            [whamlet|
<h1 .icon-chat> Chat
<p>
    You must be #
    $maybe ar <- authRoute master
        <a href=@{ar}>logged in
    $nothing
        logged in
    \ to chat.
|]
    mident <- liftHandlerT chatIdent -- check if we're already logged in
    flip (maybe disabledChat) mident $ \_ -> do
        recent  <- liftHandlerT chatGet
        chat    <- liftHandlerT newIdent   -- the containing div
        [whamlet|
<h1 .icon-chat> Chat
<div.clearfix ##{chat}>
    <div ##{chat}-output>
      $forall msg <- recent
        #{preEscapedToMarkup $ decodeUtf8 $ toByteString $ chatRenderMsg msg}
    <input ##{chat}-input type=text placeholder="Enter Message">
|] >> toWidget [lucius|
 ##{chat}-output { width: 100%; max-height: 300px; overflow: auto; }
 ##{chat}-input { width: 100%; padding-left:0; padding-right:0; }
 ##{chat}-output p {
    margin:0;
}
|] >> toWidgetBody [julius|
// Set up the receiving end
var output = document.getElementById("#{rawJS chat}-output");
var src = new EventSource("@{toMaster ReceiveR}");
src.onmessage = function(msg) {
    // This function will be called for each new message.
    output.innerHTML = output.innerHTML + msg.data;

    // And now scroll down within the output div so the most recent message
    // is displayed.
    output.scrollTop = output.scrollHeight;
};

// Set up the sending end: send a message via Ajax whenever the user hits
// enter.
var input = document.getElementById("#{rawJS chat}-input");
input.onkeyup = function(event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == '13') {
        var xhr = new XMLHttpRequest();
        var val = input.value;
        input.value = "";
        var params = "?message=" + encodeURIComponent(val);
        xhr.open("POST", "@{toMaster SendR}" + params);
        xhr.send(null);
    }
}
|]