diff --git a/htdocs/settings.js b/htdocs/settings.js index 6263d478..b228aa34 100644 --- a/htdocs/settings.js +++ b/htdocs/settings.js @@ -22,4 +22,16 @@ $(function(){ }); } }); + $('.sdr-device-list').draggableList({ + dataType: 'application/x-sdr-device', + itemSelector: '.sdr-device', + idProperty: 'device-id', + performMove: function(deviceId, index) { + return $.ajax(document.location.href + '/movedevice', { + data: JSON.stringify({device_id: deviceId, index: index}), + contentType: 'application/json', + method: 'POST' + }); + } + }) }); \ No newline at end of file diff --git a/owrx/controllers/settings/sdr.py b/owrx/controllers/settings/sdr.py index 49f18478..02292c93 100644 --- a/owrx/controllers/settings/sdr.py +++ b/owrx/controllers/settings/sdr.py @@ -73,7 +73,7 @@ class SdrDeviceListController(AuthorizationMixin, BreadcrumbMixin, WebpageContro ) return """ -
  • +
  • """.format( + device_id=device_id, device_name=config["name"] if config["name"] else "[Unnamed device]", device_link="{}settings/sdr/{}".format(self.get_document_root(), quote(device_id)), state=state_info, @@ -107,6 +108,26 @@ class SdrDeviceListController(AuthorizationMixin, BreadcrumbMixin, WebpageContro def indexAction(self): self.serve_template("settings/general.html", **self.template_variables()) + def moveDevice(self): + try: + data = json.loads(self.get_body().decode("utf-8")) + if "device_id" not in data or "index" not in data: + self.send_response("{}", content_type="application/json", code=400) + return + device_id = data["device_id"] + index = data["index"] + config = Config.get() + devices = config["sdrs"] + device = next(d for d in devices if d["id"] == device_id) + devices.remove(device) + devices.insert(index, device) + config.store() + self.send_response("{}", content_type="application/json", code=203) + except json.JSONDecodeError: + self.send_response("{}", content_type="application/json", code=400) + except StopIteration: + self.send_response("{}", content_type="application/json", code=404) + class SdrFormController(SettingsFormController, metaclass=ABCMeta): def __init__(self, handler, request, options): diff --git a/owrx/http.py b/owrx/http.py index 9781033b..5fcfa2c2 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -104,6 +104,12 @@ class Router(object): "/settings/general", GeneralSettingsController, method="POST", options={"action": "processFormData"} ), StaticRoute("/settings/sdr", SdrDeviceListController), + StaticRoute( + "/settings/sdr/movedevice", + SdrDeviceListController, + method="POST", + options={"action": "moveDevice"} + ), StaticRoute("/settings/newsdr", NewSdrDeviceController), StaticRoute( "/settings/newsdr", NewSdrDeviceController, method="POST", options={"action": "processFormData"}