Commit dd11ba4e authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

implement "save as"

parent 50fc736c
......@@ -79,3 +79,41 @@ button {
#editor .ace_active-line {
background: #FFFFFF10;
}
div.modal {
display:none;
position:fixed;
left:0; top:0; width:100%;height:100%;
background-color:rgba(0,0,0,0.5);
z-index:1000;
}
div.modal>div {
position: fixed;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
background-color: #321700;
border:2px solid #613600;
font-size:14pt;
color:white;
width: 20em;
height:10em;
}
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;
}
div.modal>div>footer {
position:fixed;
bottom:0; left:0; width:100%;
background-color: #613600;
text-align:right;
}
......@@ -15,12 +15,14 @@ $(function() {
}
}
});
$('div.modal .bt_cancel').click(hideModal);
$('div.modal .bt_ok').click(hideModal);
});
window.onresize = function()
{
$(window).resize(function() {
game.vis.Resize();
};
});
function setupEditor()
{
......@@ -46,7 +48,9 @@ function setupPreview()
function setupToolbar()
{
$('#bt_run').click(ajaxRun);
$('#bt_run').click(function() {
save('run', null);
});
$('#bt_restart').click(function() {
$.post('/snake/restart', '', function(data) {
......@@ -54,20 +58,58 @@ function setupToolbar()
console.log(data.message)
});
});
$('#bt_save_as').click(function() {
showModal($('#safe_as_dialog'), function() {
save('save', $('#save_as_title').val());
});
$('#save_as_title').val(snake_title).focus().select();
});
}
function showModal(el, ok_func)
{
let dialog = $(el);
let blocker = dialog.parents('.modal');
blocker.show();
function close_ok()
{
ok_func();
hideModal(dialog);
}
dialog.find('.bt_ok').off("modal.ok").on("click", "modal.ok", close_ok);
$(document).off('keydown.modal').on('keydown.modal', function(event) {
if (event.which === 27) { hideModal(el); }
if (event.which === 13) { close_ok(); }
});
}
function hideModal(el)
{
$(document).off('keydown.modal');
let blocker = $(el).parents('.modal');
blocker.find('.bt_ok').off("modal.ok");
blocker.hide();
}
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function ajaxRun()
function save(action, title)
{
let json_req = {
'action': 'run',
'code': editor.getSession().getValue()
'action': action,
'code': editor.getSession().getValue(),
'comment': title,
'parent': snake_id
};
$.post('/snake/edit/save', JSON.stringify(json_req), function(data) {
game.vis.FollowDbId(data.snake_id);
snake_id = data.snake_id;
snake_title = data.comment;
game.vis.FollowDbId(snake_id);
});
}
......
......@@ -20,21 +20,36 @@
<div id="toolbar">
<button id="bt_run">save & run</button>
<button id="bt_restart">restart</button>
<button>save as</button>
<button id="bt_save_as">save as</button>
<button>load</button>
<button>preferences</button>
</div>
</div>
</div>
<div class="modal">
<div id="safe_as_dialog">
<header>save current version as</header>
<div class="main">
<label for="save_as_title">Title:</label>
<input id="save_as_title">
</div>
<footer>
<button class="bt_ok">Save</button>
<button class="bt_cancel">Cancel</button>
</footer>
</div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
let snake_id = {{ snake.id | default:"null" }};
let viewer_key = "{{ profile.viewer_key }}";
let csrftoken = $("[name=csrfmiddlewaretoken]").val();
</script>
<script type="text/javascript">
let snake_title = "{{ snake.comment | default:"" }}";
let snake_id = {{ snake.id | default:"null" }};
let viewer_key = "{{ profile.viewer_key }}";
let csrftoken = $("[name=csrfmiddlewaretoken]").val();
</script>
<script src="{% static "ide/ace/src-noconflict/ace.js" %}" type="text/javascript" charset="utf-8"></script>
<script src="{% static "ide/ide.js" %}" type="text/javascript" charset="utf-8"></script>
{% include "visualization/js.html" %}
......
......@@ -68,14 +68,19 @@ def snake_save(request):
comment = json_req.get('comment')
snake = SnakeVersion(user=request.user, code=code, comment=comment)
try:
parent = SnakeVersion.objects.get(pk=json_req.get('parent'), user=request.user)
except SnakeVersion.DoesNotExist:
parent = None
snake = SnakeVersion(user=request.user, code=code, comment=comment, parent=parent)
snake.save()
if action == "run":
snake.activate()
send_kill_command(snake.user)
return JsonResponse({'success': True, 'snake_id': snake.id})
return JsonResponse({'success': True, 'snake_id': snake.id, 'comment': snake.comment})
def send_kill_command(user):
......
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