Merge pull request #1000 from ostcar/javascript

Fixed some javascript related problems
This commit is contained in:
Oskar Hahn 2013-11-06 07:40:51 -08:00
commit 5040de8dcb
8 changed files with 22 additions and 11 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from json import dumps
from time import time
from django.conf import settings
@ -116,7 +117,7 @@ def get_projector_overlays():
return render_to_string('projector/all_overlays.html', {'overlays': overlays})
def get_projector_overlays_js():
def get_projector_overlays_js(as_json=False):
"""
Returns JS-Code for the active overlays.
@ -127,6 +128,8 @@ def get_projector_overlays_js():
if overlay.is_active():
overlay_js = overlay.get_javascript()
if overlay_js:
if as_json:
overlay_js = dumps(overlay_js)
javascript.append(overlay_js)
return javascript

View File

@ -106,6 +106,7 @@ def countdown(sender, **kwargs):
return {
'load_file': static('javascript/countdown.js'),
'call': 'update_countdown();',
'projector_countdown_start': start,
'projector_countdown_duration': duration,
'projector_countdown_pause': pause,
@ -164,8 +165,9 @@ def projector_clock(sender, **kwargs):
"""
Returns JavaScript for the projector
"""
javascript = 'projector.set_server_time(%d);update_clock();' % int(time())
return {'load_file': static('javascript/clock.js'),
'server_time': int(time())}
'call': javascript}
return Overlay(name, None, get_projector_html, get_projector_js,
allways_active=True)

View File

@ -5,10 +5,9 @@ function update_clock() {
currentHours = normalise(currentHours);
currentMinutes = normalise(currentMinutes);
$('#currentTime').html(currentHours + ':' + currentMinutes);
setTimeout('update_clock()', 200);
}
update_clock();
setInterval('update_clock()', 200);
function normalise(i) {
if (i < 10) {

View File

@ -38,10 +38,9 @@ function update_countdown() {
if(seconds !== undefined) {
$('#overlay_countdown_inner').html(seconds);
}
setTimeout('update_countdown()', 200);
}
update_countdown();
setInterval('update_countdown()', 200);
function normalise(i) {
if(i < 10) {

View File

@ -37,13 +37,19 @@ var projector = {
return date;
},
set_server_time: function(value) {
var local_time = Date.parse(new Date().toUTCString());
projector.server_time_offset = local_time - value * 1000;
},
update_data: function(data) {
$.each(data, function (key, value) {
if (key === 'load_file')
projector.load_file(value);
else if (key === 'server_time') {
var local_time = Date.parse(new Date().toUTCString());
projector.server_time_offset = local_time - value * 1000;
else if (key === 'call') {
try {
eval(value);
} catch (e) {}
} else
projector[key] = value;
});

View File

@ -14,12 +14,14 @@
<script type="text/javascript" src="{% static 'javascript/sockjs-0.3.min.js' %}"></script>
<script type="text/javascript" src="{% static 'javascript/projector.js' %}"></script>
<script type="text/javascript">
$(function() {
{% for js in overlay_js %}
projector.update_data({{ js|safe }});
{% endfor %}
{% for key, value in calls.items %}
projector.{{ key }}({{ value }});
{% endfor %}
});
</script>
</head>

View File

@ -51,7 +51,7 @@ class ProjectorView(TemplateView):
kwargs.update({
'content': get_projector_content(),
'overlays': get_projector_overlays(),
'overlay_js': get_projector_overlays_js(),
'overlay_js': get_projector_overlays_js(as_json=True),
'reload': True,
'calls': config['projector_js_cache']})
# For the Preview

View File

@ -33,7 +33,7 @@ class ProjectorViewTest(TestCase):
context = view.get_context_data()
mock_get_projector_content.assert_called_with()
mock_get_projector_overlays.assert_called_with()
mock_get_projector_overlays_js.assert_called_with()
mock_get_projector_overlays_js.assert_called_with(as_json=True)
self.assertTrue(context['reload'])
self.assertEqual(context['calls'], 'js_cache')