2022-08-25 01:51:27 +02:00
|
|
|
tool
|
|
|
|
extends WebNode
|
|
|
|
class_name WebPage, "res://addons/web_pages/icons/icon_web_page.svg"
|
|
|
|
|
2022-08-25 15:35:23 +02:00
|
|
|
export(bool) var sohuld_render_menu : bool = true
|
2022-08-25 20:03:43 +02:00
|
|
|
export(bool) var allow_web_interface_editing : bool = false
|
2022-08-25 15:35:23 +02:00
|
|
|
|
2022-08-25 01:51:27 +02:00
|
|
|
export(Array, Resource) var entries : Array
|
2022-08-25 15:09:54 +02:00
|
|
|
|
|
|
|
signal entries_changed()
|
|
|
|
|
2022-08-25 15:35:23 +02:00
|
|
|
func _handle_request(request : WebServerRequest):
|
2022-08-25 18:28:01 +02:00
|
|
|
if request.get_remaining_segment_count() > 0:
|
2022-08-25 20:03:43 +02:00
|
|
|
if allow_web_interface_editing:
|
|
|
|
if web_editor_try_handle(request):
|
|
|
|
return
|
|
|
|
|
2022-08-25 18:28:01 +02:00
|
|
|
for i in range(entries.size()):
|
|
|
|
var e : WebPageEntry = entries[i]
|
|
|
|
|
|
|
|
if e && e.handle_request(request):
|
|
|
|
return
|
2022-08-25 20:03:43 +02:00
|
|
|
|
|
|
|
request.send_error(404)
|
2022-08-25 18:28:01 +02:00
|
|
|
|
2022-08-25 15:35:23 +02:00
|
|
|
if sohuld_render_menu:
|
|
|
|
render_menu(request)
|
2022-08-25 20:03:43 +02:00
|
|
|
|
|
|
|
var should_render_edit_bar : bool = allow_web_interface_editing && (request.can_edit() || request.can_delete())
|
|
|
|
|
2022-08-25 15:35:23 +02:00
|
|
|
for i in range(entries.size()):
|
|
|
|
var e : WebPageEntry = entries[i]
|
|
|
|
|
|
|
|
if e:
|
2022-08-25 20:03:43 +02:00
|
|
|
if should_render_edit_bar:
|
|
|
|
e.render_edit_bar(request)
|
|
|
|
|
2022-08-25 15:35:23 +02:00
|
|
|
e.render(request)
|
|
|
|
|
|
|
|
request.compile_and_send_body()
|
|
|
|
|
2022-08-25 20:03:43 +02:00
|
|
|
func web_editor_try_handle(request : WebServerRequest) -> bool:
|
|
|
|
var path_segment : String = request.get_current_path_segment()
|
|
|
|
|
|
|
|
if path_segment == "add":
|
|
|
|
return web_editor_handle_add(request)
|
|
|
|
elif path_segment == "edit":
|
|
|
|
return web_editor_handle_edit(request)
|
|
|
|
elif path_segment == "move_up":
|
|
|
|
return web_editor_handle_move_up(request)
|
|
|
|
elif path_segment == "move_down":
|
|
|
|
return web_editor_handle_move_down(request)
|
|
|
|
elif path_segment == "delete":
|
|
|
|
return web_editor_handle_delete(request)
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
func web_editor_handle_add(request : WebServerRequest) -> bool:
|
|
|
|
if !request.can_create():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_method() == HTTPServerEnums.HTTP_METHOD_POST:
|
|
|
|
pass
|
|
|
|
|
|
|
|
#render interface
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
func web_editor_handle_edit(request : WebServerRequest) -> bool:
|
|
|
|
if !request.can_edit():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_remaining_segment_count() < 1:
|
|
|
|
return false
|
|
|
|
|
|
|
|
request.push_path()
|
|
|
|
|
|
|
|
var resource_id_str : String = request.get_current_path_segment()
|
|
|
|
|
|
|
|
if resource_id_str.empty() || !resource_id_str.is_valid_integer():
|
|
|
|
# Todo either change the docs that this now returns "", or change it to "/"
|
|
|
|
# not yet sure which one is better (empty is probably better)
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
var resource_id : int = resource_id_str.to_int()
|
|
|
|
|
|
|
|
var entry : WebPageEntry = get_entry_with_id(resource_id)
|
|
|
|
|
|
|
|
if !entry:
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
#entry.handle_edit(request)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
func web_editor_handle_move_up(request : WebServerRequest) -> bool:
|
|
|
|
if !request.can_edit():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_remaining_segment_count() < 1:
|
|
|
|
return false
|
|
|
|
|
|
|
|
request.push_path()
|
|
|
|
|
|
|
|
var resource_id_str : String = request.get_current_path_segment()
|
|
|
|
|
|
|
|
if resource_id_str.empty() || !resource_id_str.is_valid_integer():
|
|
|
|
# Todo either change the docs that this now returns "", or change it to "/"
|
|
|
|
# not yet sure which one is better (empty is probably better)
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
var resource_id : int = resource_id_str.to_int()
|
|
|
|
|
|
|
|
var entry : WebPageEntry = get_entry_with_id(resource_id)
|
|
|
|
|
|
|
|
if !entry:
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
move_entry_up(entry)
|
|
|
|
#TODO binding missing 2nd default param
|
|
|
|
request.send_redirect(request.get_url_root_parent(1), HTTPServerEnums.HTTP_STATUS_CODE_302_FOUND)
|
|
|
|
return true
|
|
|
|
|
|
|
|
func web_editor_handle_move_down(request : WebServerRequest) -> bool:
|
|
|
|
if !request.can_edit():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_remaining_segment_count() < 1:
|
|
|
|
return false
|
|
|
|
|
|
|
|
request.push_path()
|
|
|
|
|
|
|
|
var resource_id_str : String = request.get_current_path_segment()
|
|
|
|
|
|
|
|
if resource_id_str.empty() || !resource_id_str.is_valid_integer():
|
|
|
|
# Todo either change the docs that this now returns "", or change it to "/"
|
|
|
|
# not yet sure which one is better (empty is probably better)
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
var resource_id : int = resource_id_str.to_int()
|
|
|
|
|
|
|
|
var entry : WebPageEntry = get_entry_with_id(resource_id)
|
|
|
|
|
|
|
|
if !entry:
|
|
|
|
request.send_error(404)
|
|
|
|
return true
|
|
|
|
|
|
|
|
move_entry_down(entry)
|
|
|
|
#TODO binding missing 2nd default param
|
|
|
|
request.send_redirect(request.get_url_root_parent(1), HTTPServerEnums.HTTP_STATUS_CODE_302_FOUND)
|
|
|
|
return true
|
|
|
|
|
|
|
|
func web_editor_handle_delete(request : WebServerRequest) -> bool:
|
|
|
|
if !request.can_delete():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_remaining_segment_count() < 1:
|
|
|
|
return false
|
|
|
|
|
|
|
|
request.push_path()
|
|
|
|
|
|
|
|
var resource_index_str : String = request.get_current_path_segment()
|
|
|
|
|
|
|
|
if resource_index_str.empty() || !resource_index_str.is_valid_integer():
|
|
|
|
return false
|
|
|
|
|
|
|
|
if request.get_method() == HTTPServerEnums.HTTP_METHOD_POST:
|
|
|
|
pass
|
|
|
|
|
|
|
|
#entry.handle_delete(request)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
func get_next_id() -> int:
|
|
|
|
var id : int = 0
|
|
|
|
for i in range(entries.size()):
|
|
|
|
var e : WebPageEntry = entries[i]
|
|
|
|
|
|
|
|
if e:
|
|
|
|
if e.id > id:
|
|
|
|
id = e.id
|
|
|
|
|
|
|
|
return id + 1
|
|
|
|
|
2022-08-25 15:09:54 +02:00
|
|
|
func add_entry(var entry : WebPageEntry, var after : WebPageEntry = null) -> void:
|
2022-08-25 20:03:43 +02:00
|
|
|
var id : int = get_next_id()
|
|
|
|
entry.id = id
|
|
|
|
|
2022-08-25 15:09:54 +02:00
|
|
|
if after != null:
|
|
|
|
for i in range(entries.size()):
|
|
|
|
if entries[i] == after:
|
|
|
|
entries.insert(i + 1, entry)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
entries.push_back(entry)
|
|
|
|
|
|
|
|
emit_signal("entries_changed")
|
|
|
|
|
2022-08-25 20:03:43 +02:00
|
|
|
func get_entry_with_index(index : int) -> WebPageEntry:
|
|
|
|
if index < 0 || index >= entries.size():
|
|
|
|
return null
|
|
|
|
|
|
|
|
return entries[index]
|
|
|
|
|
|
|
|
func get_entry_with_id(id : int) -> WebPageEntry:
|
|
|
|
for i in range(entries.size()):
|
|
|
|
var e : WebPageEntry = entries[i]
|
|
|
|
|
|
|
|
if e && e.id == id:
|
|
|
|
return e
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
2022-08-25 15:09:54 +02:00
|
|
|
func remove_entry(var entry : WebPageEntry) -> void:
|
|
|
|
entries.erase(entry)
|
|
|
|
|
|
|
|
emit_signal("entries_changed")
|
|
|
|
|
|
|
|
func move_entry_up(var entry : WebPageEntry) -> void:
|
|
|
|
# Skips checking the first entry (1, entries.size())
|
|
|
|
for i in range(1, entries.size()):
|
|
|
|
if entries[i] == entry:
|
|
|
|
entries[i] = entries[i - 1]
|
|
|
|
entries[i - 1] = entry
|
|
|
|
emit_signal("entries_changed")
|
|
|
|
return
|
|
|
|
|
|
|
|
func move_entry_down(var entry : WebPageEntry) -> void:
|
|
|
|
# Skips checking the last entry (size() - 1)
|
|
|
|
for i in range(entries.size() - 1):
|
|
|
|
if entries[i] == entry:
|
|
|
|
entries[i] = entries[i + 1]
|
|
|
|
entries[i + 1] = entry
|
|
|
|
emit_signal("entries_changed")
|
|
|
|
return
|
|
|
|
|
|
|
|
func get_entry_before(var entry : WebPageEntry) -> WebPageEntry:
|
|
|
|
if entries.size() < 2:
|
|
|
|
return null
|
|
|
|
|
|
|
|
if entries[0] == entry:
|
|
|
|
return null
|
|
|
|
|
|
|
|
for i in range(1, entries.size()):
|
|
|
|
if entries[i] == entry:
|
|
|
|
return entries[i - 1]
|
|
|
|
|
|
|
|
return null
|