Move templates selection client side
This commit is contained in:
parent
6652bf02ed
commit
9062be9f54
2 changed files with 60 additions and 79 deletions
72
app.py
72
app.py
|
|
@ -19,57 +19,10 @@ app = Flask(__name__)
|
|||
|
||||
app.secret_key = "blahaj"
|
||||
|
||||
templates = {
|
||||
"DNH": {
|
||||
"text1": {
|
||||
"string": "Do Not Hack",
|
||||
"size": 130,
|
||||
"pos": 0,
|
||||
"bold": True
|
||||
},
|
||||
"text2": {
|
||||
"string": "bottom text",
|
||||
"size": 50,
|
||||
"pos": 130,
|
||||
"bold": False
|
||||
},
|
||||
"cut": True
|
||||
},
|
||||
"Food": {
|
||||
"text1": {
|
||||
"string": "Nickname",
|
||||
"size": 130,
|
||||
"pos": 0,
|
||||
"bold": True
|
||||
},
|
||||
"text2": {
|
||||
"string": "",
|
||||
"size": 50,
|
||||
"pos": 130,
|
||||
"bold": False
|
||||
},
|
||||
"cut": True
|
||||
},
|
||||
"Remove by": {
|
||||
"text1": {
|
||||
"string": "Remove by",
|
||||
"size": 130,
|
||||
"pos": 0,
|
||||
"bold": True
|
||||
},
|
||||
"text2": {
|
||||
"string": "",
|
||||
"size": 50,
|
||||
"pos": 130,
|
||||
"bold": False
|
||||
},
|
||||
"cut": True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def render_text_template(info=None, info_color=None, scrollDown=None):
|
||||
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"], info=info, info_color=info_color, scrollDown=scrollDown, fonts=session["fonts"])
|
||||
return render_template('text.html', filename=session["text image path"], cut=session["cut"], info=info, info_color=info_color, scrollDown=scrollDown, fonts=session["fonts"])
|
||||
|
||||
|
||||
|
||||
|
|
@ -90,9 +43,7 @@ def on_new_user(session):
|
|||
session["text image path"] = None
|
||||
session["uploaded image path"] = None
|
||||
session["cut"] = True
|
||||
session["text1"] = templates["DNH"]["text1"]
|
||||
session["text2"] = templates["DNH"]["text2"]
|
||||
session["cut"] = templates["DNH"]["cut"]
|
||||
session["cut"] = True
|
||||
session["fonts"] = fonts()
|
||||
|
||||
|
||||
|
|
@ -155,25 +106,6 @@ def image_print():
|
|||
return render_image_template()
|
||||
|
||||
|
||||
@app.route('/text-template', methods=['GET', 'POST'])
|
||||
def text_template():
|
||||
check_for_new_user(session)
|
||||
|
||||
if request.method == 'POST':
|
||||
template = templates[request.form["template"]]
|
||||
|
||||
if request.form["template"] == "Food":
|
||||
template["text2"]["string"] = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
if request.form["template"] == "Remove by":
|
||||
template["text2"]["string"] = (datetime.now() + timedelta(weeks=3)).strftime('%Y-%m-%d')
|
||||
|
||||
session["text1"] = template["text1"]
|
||||
session["text2"] = template["text2"]
|
||||
session["cut"] = template["cut"]
|
||||
|
||||
return render_text_template()
|
||||
|
||||
|
||||
@app.route('/text-form', methods=['GET', 'POST'])
|
||||
def text_form():
|
||||
|
|
|
|||
|
|
@ -7,12 +7,12 @@
|
|||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form method="POST" action="/text-template">
|
||||
<form id="template">
|
||||
<label for="template">Template:</label>
|
||||
<select name="template">
|
||||
<option value="DNH">Do not hack</option>
|
||||
<option value="Food">Food</option>
|
||||
<option value="Remove by">Remove by</option>
|
||||
<option value="Remove by">Remove by</option>
|
||||
</select>
|
||||
<input type="submit" value="Apply">
|
||||
</form>
|
||||
|
|
@ -79,8 +79,38 @@
|
|||
return html;
|
||||
}
|
||||
|
||||
const templates = new Map ([
|
||||
["DNH", [{
|
||||
text: "Do Not Hack",
|
||||
size: 130,
|
||||
y_pos: 0
|
||||
}, {
|
||||
text: "bottom text",
|
||||
size: 50,
|
||||
y_pos: 130
|
||||
}]],
|
||||
["Food", [{
|
||||
text: "Nickname",
|
||||
size: 130,
|
||||
y_pos: 0
|
||||
}, {
|
||||
text: new Date().toISOString().slice(0, 10),
|
||||
size: 50,
|
||||
y_pos: 130
|
||||
}]],
|
||||
["Remove by", [{
|
||||
text: "Remove by",
|
||||
size: 130,
|
||||
y_pos: 0
|
||||
}, {
|
||||
text: new Date(Date.now() + 3 * 7 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10),
|
||||
size: 50,
|
||||
y_pos: 130
|
||||
}]],
|
||||
]);
|
||||
|
||||
function generate_fields() {
|
||||
|
||||
function generate_and_populate_fields() {
|
||||
let html = ""
|
||||
|
||||
let lineCnt = get_persistent("lineCount", 2);
|
||||
|
|
@ -88,10 +118,12 @@
|
|||
let modified_line_form = line_form;
|
||||
modified_line_form
|
||||
|
||||
template = templates.get("DNH");
|
||||
|
||||
modified_line_form = modified_line_form.replace("[title]", "Line " + i);
|
||||
modified_line_form = init_field("text", "Text", i, modified_line_form);
|
||||
modified_line_form = init_field("size", 130, i, modified_line_form);
|
||||
modified_line_form = init_field("y_pos", i*130, i, modified_line_form);
|
||||
modified_line_form = init_field("text", template[i].text, i, modified_line_form);
|
||||
modified_line_form = init_field("size", template[i].size, i, modified_line_form);
|
||||
modified_line_form = init_field("y_pos", template[i].y_pos, i, modified_line_form);
|
||||
html += modified_line_form;
|
||||
console.log(i);
|
||||
}
|
||||
|
|
@ -103,7 +135,7 @@
|
|||
|
||||
document.getElementById("input").innerHTML = html;
|
||||
}
|
||||
generate_fields()
|
||||
generate_and_populate_fields()
|
||||
|
||||
const text_form = document.getElementById("text_form")
|
||||
text_form.addEventListener('submit', function(e) {
|
||||
|
|
@ -124,13 +156,30 @@
|
|||
|
||||
function add_line() {
|
||||
localStorage.setItem("lineCount", Number(localStorage.getItem("lineCount")) + 1);
|
||||
generate_fields();
|
||||
generate_and_populate_fields();
|
||||
}
|
||||
function remove_line() {
|
||||
localStorage.setItem("lineCount", Number(localStorage.getItem("lineCount")) - 1);
|
||||
generate_fields();
|
||||
generate_and_populate_fields();
|
||||
}
|
||||
|
||||
const template_selector = document.getElementById("template")
|
||||
template_selector.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
template = templates.get(template_selector.elements.template.value);
|
||||
|
||||
|
||||
for (let i = 0; i < template.length; i++) {
|
||||
localStorage.setItem("text" + i, template[i].text);
|
||||
localStorage.setItem("size" + i, template[i].size);
|
||||
localStorage.setItem("y_pos" + i, template[i].y_pos);
|
||||
}
|
||||
localStorage.setItem("lineCount", template.length);
|
||||
generate_and_populate_fields()
|
||||
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in a new issue