From c389e0863c41706af4bde9977dcf1fc6ed1b23d0 Mon Sep 17 00:00:00 2001 From: Averrin Date: Mon, 19 Aug 2013 17:29:23 +0400 Subject: [PATCH] underscore and templates for activity. issue #51 --- emergent/settings.py | 1 + static/js/activity.js | 51 ++++++++++++++++++++++++---------- static/js/main.js | 16 ----------- templates/activity/stream.html | 17 ++++++++---- templates/base.html | 6 ++-- 5 files changed, 53 insertions(+), 38 deletions(-) diff --git a/emergent/settings.py b/emergent/settings.py index 4d11e1f..b07a4fc 100644 --- a/emergent/settings.py +++ b/emergent/settings.py @@ -104,6 +104,7 @@ BOWER_INSTALLED_APPS = ( 'bootstrap', + 'underscore', ) AUTH_USER_MODEL = 'rpg.Hero' diff --git a/static/js/activity.js b/static/js/activity.js index 577e218..9a43233 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -1,18 +1,41 @@ +function chat_send(editor){ + $.post("/activity/send", {message: editor.exportFile()}, function(){ + editor.importFile(); + }); +} + $(function(){ -var editor = new EpicEditor({ -theme: { - base: "http://"+location.host+'/static/epiceditor/themes/base/epiceditor.css', - preview: "http://"+location.host+'/static/epiceditor/themes/preview/preview-dark.css', - editor: "http://"+location.host+'/static/epiceditor/themes/editor/epic-dark.css' - }, -focusOnLoad: true -}).load(); + var chat_message = _.template("
<%= user %> (<%= now %>):
<%= message %>
") -$("#msg_form").on("submit", function(e){ - e.preventDefault(); - $.post("/activity/send", {message: editor.exportFile()}, function(){}) - editor.importFile() - return false; -}) + var pusher = new Pusher('6bb5412badf09454aa87'); + var channel = pusher.subscribe('activity'); + channel.bind('my_event', function(data) { + console.log(data); + var now = new Date().toLocaleTimeString(); + data.now = now; + $("#events").append(chat_message(data)); + }); + + var editor = new EpicEditor({ + + theme: { + base: "http://"+location.host+'/static/epiceditor/themes/base/epiceditor.css', + preview: "http://"+location.host+'/static/epiceditor/themes/preview/preview-dark.css', + editor: "http://"+location.host+'/static/epiceditor/themes/editor/epic-dark.css' + }, + focusOnLoad: true + }).load(); + + $("#msg_form").on("submit", function(e){ + e.preventDefault(); + chat_send(editor); + return false; + }); + + //TODO: shortcut in editor + Mousetrap.bind(['ctrl+enter'], function(e) { + console.log("Ctrl + Enter"); + chat_send(editor); + }); }); diff --git a/static/js/main.js b/static/js/main.js index c5fc260..ffa1dcd 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,17 +1 @@ $(".level_info").tooltip({placement: "bottom", html: true}) - -var pusher = new Pusher('6bb5412badf09454aa87'); - var channel = pusher.subscribe('activity'); - channel.bind('my_event', function(data) { - console.log(data) - var now = new Date().toLocaleTimeString(); - $("#events").append(data.user + ' (' + now + "): " + data.message + "
"); - $("#msg_text").val("") - }); - -$("#msg_form").on("submit", function(e){ - e.preventDefault(); - $.post("/activity/send", {message:$('#msg_text').val()}, function(){}) - $('#msg_text').val('') - return false; -}) diff --git a/templates/activity/stream.html b/templates/activity/stream.html index fa9ee24..8b1e3ca 100644 --- a/templates/activity/stream.html +++ b/templates/activity/stream.html @@ -2,14 +2,21 @@ {% load static %} {% block page %} - - -

Websocket test:

+ +
- {% csrf_token %} + {% csrf_token %}
- +
+ +

Messages

{% endblock page %} + +{% block js_includes %} + + + +{% endblock js_includes %} diff --git a/templates/base.html b/templates/base.html index 34ee506..cbefddf 100644 --- a/templates/base.html +++ b/templates/base.html @@ -60,14 +60,14 @@ - + - - + + {% block js_includes %}{% endblock js_includes %}