Commit 1ff71b87 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

add load dialog

parent 9abdc5db
......@@ -101,14 +101,16 @@ div.modal>div {
}
div.modal>div>header {
position:fixed;
top:0; left:0; width:100%;
background-color: #613600;
text-align:center;
}
div.modal div.main {
padding: 1.5em 0.5em 1.5em 0.5em;
overflow: scroll;
position: absolute;
top: 1.2em; bottom: 1.5em;
left: 4px; right: 4px;
overflow-y:auto;
}
div.modal>div>footer {
......@@ -117,3 +119,44 @@ div.modal>div>footer {
background-color: #613600;
text-align:right;
}
#load_dialog {
width:40em;
height:20em;
}
#load_dialog .list>div {
display:flex;
flex-direction: row;
font-family:"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size:10pt;
cursor:pointer;
}
#load_dialog .list>div.selected, #load_dialog .list>div:nth-child(even).selected {
background-color:rgba(0,0,0,0.5);
}
#load_dialog .list>div>div {
overflow:hidden;
padding:2px;
}
#load_dialog .list>div:nth-child(even) {
background-color:rgba(255,255,255,0.05);
}
#load_dialog .list>div>div:nth-child(1)
{
width:3em; text-align: right;
}
#load_dialog .list>div>div:nth-child(2)
{
width:15em; text-align: center;
}
#load_dialog .list>div>div:nth-child(3)
{
width:400px; text-align: left;
}
......@@ -73,30 +73,39 @@ function setupToolbar()
});
$('#save_as_title').val(snake_title).focus().select();
});
$('#bt_load').click(function() {
$.get("/snake/list", function (data, status) {
if (status!='success') {
console.log('loading version list failed: ' + status);
return;
}
if (!data.versions.length) {
console.log('got empty version list from server');
return;
}
showLoadDialog(data);
});
});
}
function showModal(el, ok_func)
{
let dialog = $(el);
let blocker = dialog.parents('.modal');
blocker.show();
function close_ok()
{
ok_func();
hideModal(dialog);
}
let bt_ok = dialog.find('.bt_ok');
let bt_cancel = dialog.find('.bt_cancel');
function close_cancel()
{
hideModal(dialog);
}
blocker.show();
bt_ok.off().click(function() { ok_func(); hideModal(dialog); });
bt_cancel.off().click(function() { hideModal(dialog); });
dialog.find('.bt_ok').off().click(close_ok);
dialog.find('.bt_cancel').off().click(close_cancel);
$(document).off('keydown.modal').on('keydown.modal', function(event) {
if (event.which === 13) { close_ok(); }
if (event.which === 27) { close_cancel(); }
if (event.which === 13) { bt_ok.click(); }
if (event.which === 27) { bt_cancel.click(); }
});
}
......@@ -128,6 +137,35 @@ function save(action, title)
});
}
function showLoadDialog(data)
{
let list = $('#load_dialog .list');
let selected_version = 'latest';
let bt_ok = $('#load_dialog .bt_ok');
bt_ok.prop("disabled", true);
list.empty();
$.each(data.versions, function(i, version) {
let item = $('<div><div>'+version.version+'</div><div>'+version.date+'</div><div>'+version.title+'</div></div>');
item.click(function() {
list.children('div').removeClass('selected');
item.addClass('selected');
selected_version = version.id;
$('#load_dialog .bt_ok').prop("disabled", false);
});
item.dblclick(function() {
selected_version = version.id;
bt_ok.click();
});
list.append(item);
});
showModal($('#load_dialog'), function() {
window.location.href = '/snake/edit/' + selected_version;
});
}
function addLogLine(frame, msg)
{
if (logWindow==null) { return; }
......
{% extends 'base.html' %}
{% load widget_tweaks %}
{% load static %}
{% block css %}
<script src="{% static "ide/ace/src-noconflict/ace.js" %}" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
#editor {
width:100%;
height:30em;
font-size:16px;
background-color: #00000080;
}
#editor .ace_gutter {
background-color: #FFFFFF20;
}
#editor .ace_scroller {
background-color: transparent;
}
#editor .ace_active-line {
background: #FFFFFF10;
}
#log {
width:100%;
height:10em;
background-color:#00000080;
}
</style>
{% endblock %}
{% block content %}
<div class="ui left floated inverted header">
Edit Bot
</div>
<div class="ui left floated inverted header">
<div class="ui toggle checkbox">
<input type="checkbox" name="livemode">
<label>Live mode</label>
</div>
</div>
<form id="snake_edit_form" method="post" class="ui inverted form">
{% csrf_token %}
<div class="equal width fields">
<div class="field">
{{ form.code.label_tag }}
<textarea id="code" name="code">{{ snake.code }}</textarea>
<div id="editor"></div>
</div>
<div class="field" id="game" style="display: none">
<label>Preview</label>
<canvas style="background: #111"></canvas>
</div>
</div>
<div class="field">
<label>Debug output:</label>
<div id="log">
</div>
</div>
<div class="field">{{ form.comment.label_tag }}{{ form.comment }}</div>
<div class="ui buttons">
<button class="ui positive button" type="submit">Save</button>
<div class="or"></div>
<a class="ui button" href="{% url 'snake' %}">Cancel</a>
</div>
</form>
{% endblock %}
{% block js %}
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/idle_fingers");
editor.session.setMode("ace/mode/lua");
editor.setShowPrintMargin(false);
var textarea = $('#code').hide();
editor.getSession().setValue(textarea.val());
$("#snake_edit_form").submit( function(event) {
textarea.val(editor.getSession().getValue());
})
</script>
<script>
$('.ui.checkbox').checkbox({
onChecked: function() {
$('#editor').css('width', '35em');
$('#game').show()
},
onUnchecked: function() {
$('#editor').css('width', '');
$('#game').hide()
},
});
</script>
{% endblock %}
\ No newline at end of file
......@@ -23,7 +23,7 @@
<button id="bt_stop">stop</button>
<button id="bt_save">save</button>
<button id="bt_save_as">save as</button>
<button>load</button>
<button id="bt_load">load</button>
<button>preferences</button>
</div>
</div>
......@@ -43,6 +43,19 @@
</div>
</div>
<div class="modal">
<div id="load_dialog">
<header>load previous version</header>
<div class="main">
<div class="list"></div>
</div>
<footer>
<button class="bt_ok">Load</button>
<button class="bt_cancel">Cancel</button>
</footer>
</div>
</div>
{% endblock %}
{% block js %}
......
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h1>My Snakes</h1>
{% if snakes|length == 0 %}
<p>No snakes defined yet.</p>
<p>
<a href="{% url 'snake_create' %}" class="ui secondary button">Create your snake</a>
</p>
{% else %}
<p>
<a href="{% url 'snake_create' %}" class="ui secondary button">Start with a clean snake</a>
<a href="{% url 'snake_restart' %}" class="ui secondary {% if not has_active %}disabled {% endif %}button" id="restart_snake">Restart your snake</a>
<a href="{% url 'snake_disable' %}" class="ui secondary {% if not has_active %}disabled {% endif %}button" id="disable_snake">Disable your snake</a>
</p>
<table class="ui inverted table snake-list">
<thead>
<tr>
<th>Snake Version</th>
<th>Last Changed</th>
<th>Comment</th>
<th>Active</th>
</tr>
</thead>
<tbody>
{% for snake in snakes %}
<tr>
<td>{{ snake.version }}&nbsp;
<a href="{% url 'snake_edit' snake.id %}" class="tiny ui labeled icon button">
<i class="icon external alternate"></i>Improve</a>&nbsp;
<a href="{% url 'snake_delete' snake.id %}" class="tiny ui labeled icon button">
<i class="icon trash alternate"></i>Delete</a>
</td>
<td>{{ snake.created|date:"d.m.Y H:i:s" }}</td>
<td>{{ snake.comment }}</td>
<td class="collapsing">
<div class="ui fitted toggle checkbox">
<input type="radio" name="activesnake" data-snake="{{ snake.id }}"
{% if snake.active %}
checked="checked"
{% endif %}> <label></label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}
{% block js %}
<script>
$('.ui.checkbox').checkbox({
onChecked: function() {
let id = $(this).data('snake');
$.ajax({
url: "{% url 'snake_activate' 1 %}".replace('1', id),
success: function(data) {
$.growl.notice({ message: data.message });
$('#disable_snake, #restart_snake').removeClass('disabled');
},
error: function(data) {
$.growl.error({ message: data.message });
}
});
}
});
$('#restart_snake').click(function(e) {
let button_url = $(this).attr('href');
$.ajax({
url: button_url,
success: function(data) {
$.growl.notice({ message: data.message });
},
error: function(data) {
$.growl.error({ message: data.message });
}
});
e.preventDefault();
});
</script>
{% endblock %}
\ No newline at end of file
......@@ -2,13 +2,13 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.snake_list, name='snake'),
path('list', views.snake_list, name='snake'),
path('create', views.snake_create, name='snake_create'),
path('delete/<int:snake_id>', views.snake_delete, name='snake_delete'),
path('edit/latest', views.snake_edit_latest, name='snake_edit_latest'),
path('edit/<int:snake_id>', views.snake_edit, name='snake_edit_version'),
path('edit/<int:snake_id>', views.snake_edit_version, name='snake_edit_version'),
path('edit/save', views.snake_save, name='snake_save'),
path('activate/<int:snake_id>', views.snake_activate, name='snake_activate'),
path('delete/<int:snake_id>', views.snake_delete, name='snake_delete'),
path('disable', views.snake_disable, name='snake_disable'),
path('restart', views.snake_restart, name='snake_restart'),
path('edit/save', views.snake_save, name='snake_save'),
]
from datetime import datetime
import json
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.forms import ModelForm
......@@ -5,7 +7,6 @@ from django.http import JsonResponse, HttpResponseBadRequest
from django.shortcuts import render, redirect, get_object_or_404
from django.template.loader import render_to_string
from django.views.decorators.http import require_POST
import json
from core.models import SnakeVersion, ServerCommand, UserProfile
......@@ -22,10 +23,16 @@ class CreateSnakeForm(ModelForm):
@login_required
def snake_list(request):
return render(request, 'ide/list.html', {
'snakes': SnakeVersion.objects.filter(user=request.user).order_by('-version'),
'active_snake': get_user_profile(request.user).active_snake
})
snakes = SnakeVersion.objects.filter(user=request.user).order_by('-created')
response = {'versions': [
{
'id': s.id,
'version': s.version,
'title': s.comment or '',
'date': s.created.strftime("%d.%m.%Y %H:%M:%S")
} for s in snakes
]}
return JsonResponse(response)
@login_required
......@@ -44,8 +51,8 @@ def snake_edit_latest(request):
@login_required
def snake_edit_version(request, snake_version_id):
snake = get_object_or_404(SnakeVersion, pk=snake_version_id)
def snake_edit_version(request, snake_id):
snake = get_object_or_404(SnakeVersion, pk=snake_id)
if snake.user != request.user:
raise PermissionDenied
return snake_edit(request, snake)
......@@ -154,7 +161,4 @@ def snake_restart(request):
else:
response = {'message': 'requesting kill of any running snake version (no activate snake version)'}
if request.is_ajax():
return JsonResponse(response)
else:
return snake_list(request)
return JsonResponse(response)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment