mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 12:18:45 -05:00
110 lines
4.0 KiB
HTML
110 lines
4.0 KiB
HTML
{% extends "base.html" %}
|
|
{% load crispy_forms_tags %}
|
|
{% load i18n %}
|
|
{% load custom_tags %}
|
|
|
|
{% block title %}{% trans 'Edit Recipe' %}{% endblock %}
|
|
|
|
{% block extra_head %}
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/tabulator/4.4.3/js/tabulator.min.js"
|
|
integrity="sha256-u2YCVBkzzkIuLh6bMHUmqv6uuuHLxGgc6XF+rCJUV5k=" crossorigin="anonymous"></script>
|
|
<link rel="stylesheet"
|
|
href="https://cdnjs.cloudflare.com/ajax/libs/tabulator/4.4.3/css/bootstrap/tabulator_bootstrap4.min.css"
|
|
integrity="sha256-+AmauyGZPl0HNTBQ5AMZBxfzP+rzXJjraezMKpWwWSE=" crossorigin="anonymous"/>
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
|
|
<h3>{% trans 'Edit Recipe' %}</h3>
|
|
|
|
<form action="." method="post">
|
|
{% csrf_token %}
|
|
|
|
{% for field in form %}
|
|
<div class="fieldWrapper">
|
|
{{ field|as_crispy_field }}
|
|
</div>
|
|
{% if field.name == 'name' %}
|
|
{% trans 'Ingredients' %}
|
|
<div id="ingredients-table"></div>
|
|
<br>
|
|
<div class="table-controls">
|
|
<button class="btn" id="new_empty" type="button"><i class="fas fa-plus-circle"></i></button>
|
|
</div>
|
|
{% endif %}
|
|
{% endfor %}
|
|
|
|
<input type="hidden" id="ingredients_data_input" name="ingredients">
|
|
<hr>
|
|
<input type="submit" value="Submit" class="btn btn-success">
|
|
<a href="{% url 'redirect_delete' form.instance|get_class|lower form.instance.pk %}"
|
|
class="btn btn-danger">{% trans 'Delete' %}</a>
|
|
{% if view_url %}
|
|
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
|
|
{% endif %}
|
|
</form>
|
|
|
|
<script>
|
|
//converts multiselct in recipe edit to searchable multiselect
|
|
//shitty solution that needs to be redone at some point
|
|
$(document).ready(function () {
|
|
$('#id_keywords').select2();
|
|
|
|
var ingredients = {{ ingredients|safe }}
|
|
|
|
ingredients.forEach(function (cur, i) {
|
|
cur.delete = false
|
|
})
|
|
|
|
var data = ingredients
|
|
|
|
var table = new Tabulator("#ingredients-table", {
|
|
index: "id",
|
|
layout: "fitColumns",
|
|
reactiveData: true,
|
|
data: data,
|
|
columns: [
|
|
{
|
|
title: "{% trans 'Ingredient' %}",
|
|
field: "name",
|
|
validator: "required",
|
|
editor: "input"
|
|
},
|
|
{title: "{% trans 'Amount' %}", field: "amount", validator: "required", editor: "input"},
|
|
{title: "{% trans 'Unit' %}", field: "unit", validator: "required", editor: "input"},
|
|
{
|
|
title: "{% trans 'Delete' %}",
|
|
field: "delete",
|
|
align: "center",
|
|
editor: true,
|
|
formatter: "tickCross"
|
|
},
|
|
{title: "id", field: "id", visible: false}
|
|
],
|
|
dataEdited: function (data) {
|
|
$('#ingredients_data_input').val(JSON.stringify(data))
|
|
|
|
data.forEach(function (cur, i) {
|
|
if (cur.delete) {
|
|
table.deleteRow(cur.id);
|
|
}
|
|
})
|
|
}
|
|
});
|
|
|
|
// load initial value
|
|
$('#ingredients_data_input').val(JSON.stringify(data))
|
|
|
|
document.getElementById("new_empty").addEventListener("click", function () {
|
|
data.push({
|
|
name: "{% trans 'Ingredient' %}",
|
|
amount: "100",
|
|
unit: "g",
|
|
id: Math.floor(Math.random() * 10000000),
|
|
delete: false,
|
|
});
|
|
});
|
|
});
|
|
|
|
</script>
|
|
{% endblock %} |