Commit a18c2262 authored by Thomas Mignot's avatar Thomas Mignot

maj frontend + statics files + urls

parent 1a6b1577
No preview for this file type
......@@ -87,3 +87,33 @@ class Text(Component):
def __init__(self, content=[],
parent='body', _id=uuid.uuid1().hex):
super().__init__('text', content, parent=parent, _id=_id)
class Form(Component):
def __init__(self, content=[], attr={}, events={},
parent='body', _id=uuid.uuid1().hex):
super().__init__('form', content, attr, events, parent, _id)
class Input(Component):
def __init__(self, content=[], attr={}, events={},
parent='body', _id=uuid.uuid1().hex):
super().__init__('input', content, attr, events, parent, _id)
class Button(Component):
def __init__(self, content=[], attr={}, events={},
parent='body', _id=uuid.uuid1().hex):
super().__init__('button', content, attr, events, parent, _id)
class Nav(Component):
def __init__(self, content=[], attr={}, events={},
parent='body', _id=uuid.uuid1().hex):
super().__init__('nav', content, attr, events, parent, _id)
class H1(Component):
def __init__(self, content=[], attr={}, events={},
parent='body', _id=uuid.uuid1().hex):
super().__init__('h1', content, attr, events, parent, _id)
......@@ -126,4 +126,5 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_URL = 'static/'
STATIC_ROOT = 'static/'
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
from project.component import Div, Ul, Li, Span
from project.component import Div, Ul, Li, Span, Input, Button
from project.component import Nav, H1
from todos.models import Tasks
class Task(Li):
def __init__(self, task_object):
content = [Span(task_object.about)]
super().__init__(content, _id=f'task_{task_object.id}')
attr = {'class': 'list-group-item'}
content = [
Span(task_object.about),
Button('Done', {'class': 'btn float-right'}, {
'click': f'call("remove_task", {{id: {task_object.id}}})'
})
]
super().__init__(content, attr, _id=f'task_{task_object.id}')
class Tasklist(Ul):
......@@ -16,10 +23,57 @@ class Tasklist(Ul):
Task(t)
for t in Tasks.objects.filter()
]
super().__init__(content, _id='tasklist')
super().__init__(content, {'class': 'list-group'}, _id='tasklist')
class Taskform(Div):
def __init__(self):
attr = {
'class': 'form-group',
'style': 'margin:20px',
'action': '#'
}
content = [
Div(
attr={'class': 'input-group'},
content=[
Input(attr={
'id': 'task_input',
'type': 'text',
'class': 'form-control'
}),
Div(
attr={'class': 'input-group-append'},
content=[
Button(
content='Add',
attr={
'class': 'btn btn-primary'
},
events={
'click': 'call("insert_task", { \
about: $("#task_input").value \
}); $("#task_input").value = ""'
}
)
]
)
]
)
]
super().__init__(content, attr, _id='task_form')
class Base(Div):
def __init__(self):
content = [Tasklist()]
super().__init__(content, _id='tasklist_container')
attr = {
'class': 'container'
}
content = [
Nav([H1('Todos')], {'class': 'navbar navbar-light bg-light'}),
Div([
Tasklist(),
Taskform()
])
]
super().__init__(content, attr, _id='tasklist_container')
......@@ -6,6 +6,7 @@ from channels.auth import get_user
from django.contrib.auth.models import User
from asgiref.sync import async_to_sync
from todos.methods import Methods
from todos.models import Clients, Subscriptions
......@@ -77,8 +78,31 @@ class Consumer(JsonWebsocketConsumer, object):
}
self.send(json.dumps(data))
def recv_getcomponent(self, data):
pass
def recv_method(self, data):
to_send = {'_id': data['_id']}
params = data['params']
method = getattr(Methods, params['name'], None)
if method is None:
to_send.update({
'type': 'Error',
'params': {
'name': 'Not found',
'message': f'Method {params["name"]} not found'
}
})
self.send(json.dumps(to_send))
else:
ret = method(params['params'])
if ret:
to_send.update({
'type': 'Result',
'params': ret
})
else:
to_send.update({
'type': 'Error',
'params': ret
})
def insert_component(self, data, change=False):
self.send(json.dumps({
......
from todos.models import Tasks
class Methods():
def insert_task(params):
if 'about' in params:
t = Tasks(about=params['about'])
t.save()
return True
return False
def remove_task(params):
if 'id' in params:
t = Tasks.objects.get(id=params['id'])
if t:
t.delete()
return True
return False
......@@ -3,6 +3,7 @@
<head>
<meta charset="utf-8"/>
<title>Todos POC</title>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
</head>
<body>
</body>
......@@ -43,7 +44,11 @@
elem.setAttribute(k, component.attr[k]);
});
Object.keys(component.events).forEach(function(k) {
elem.addEventListener(k, window[component.events[k]]);
console.log('adding event ' + k);
console.log(component.events[k]);
elem.addEventListener(k, function(e) {
eval(component.events[k]);
});
});
}
......@@ -147,9 +152,13 @@
}, function(r, e) {
if (e)
console.log(e);
else
else {
constructDOM(r.params);
history.pushState(null, null, '/' + url + '?' + q)
state = '/' + url;
if (q && q.length)
state += '?' + q;
history.pushState(null, null, state)
}
});
};
......@@ -171,6 +180,22 @@
route(current_url, query_string);
});
call = function(name, params) {
ws_send({
type: 'method',
params: {
name: name,
params: params
}
}, function(r, e) {
if (e)
console.log(e);
});
};
$ = function(q) {
return document.querySelector(q);
};
</script>
</html>
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('<path:url>', views.index, name='index'),
urlpatterns = static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT) + [
path('', views.index, name='index'),
path('<path:url>', views.index, name='index')
]
......@@ -2,7 +2,7 @@ import json
from django.shortcuts import render
def index(request, url):
def index(request, url=''):
print(json.dumps(request.GET))
return render(request, 'index.html', {
'url': url,
......
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