Migrate to mithril.js

This commit is contained in:
2023-02-18 09:22:43 +03:00
parent 024d7fb10d
commit bdc8f8496f
79 changed files with 1697 additions and 149 deletions

View File

@@ -42,9 +42,12 @@ from . import lib, models
def inject_data():
result = {}
result['STATIC'] = app.config['STATIC']
result['ITEMS_ON_PAGE'] = app.config['ITEMS_ON_PAGE']
result['user'] = None
if lib.get_user():
result['user'] = lib.get_user()
result['user'] = lib.get_user().as_dict()
return result
@@ -56,6 +59,9 @@ def shutdown_session(exception=None):
# API
from . import ns_api # noqa F401
# Общее
from . import ns_common # noqa F401
# Авторизация
from . import ns_login # noqa F401

View File

@@ -22,8 +22,8 @@ def login_required(func):
models.User.id == session['user_id']
).first()
if user:
user.last_activity = datetime.datetime.now()
models.db_session.commit()
# user.last_activity = datetime.datetime.now()
# models.db_session.commit()
return func(*args, **kwargs)
else:
session.pop('logged_in', None)

57
myapp/mutations/user.py Normal file
View File

@@ -0,0 +1,57 @@
__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
import logging
from .. import lib, models
log = logging.getLogger(__name__)
def user_as_dict(
user: models.User,
fields: list = ['id', 'name']
):
"""Пользователя как словарь (в JSON)
"""
def field_favorite(user):
# Избранное
assert lib.get_user(), 'favorite only authorized users'
result = False
favorite = models.db_session.query(
models.FavoritePage
).filter(
models.FavoritePage.user_id == user.id,
models.FavoritePage.user_id == lib.get_user().id
).first()
if favorite:
result = True
return result
def field_tags(user):
# Теги
result = []
for tagLink in user.tags:
newTag = tagLink.tag.as_dict()
result.append(newTag)
return result
funcs = {
'favorite': field_favorite,
'tags': field_tags
}
result = {}
for column in user.__table__.columns:
if column.name in fields:
if column.name not in ['password']:
result[column.name] = getattr(user, column.name)
for field in fields:
if field in funcs:
func = funcs[field]
result[field] = func(user)
log.info(result)
return result

View File

@@ -35,6 +35,7 @@ jsonrpc = JSONRPC()
from . import ( # noqa F401
login,
note,
profile,
page,
tag,
user

View File

@@ -28,6 +28,12 @@ def login(username: str, password: str) -> bool:
return True
@jsonrpc.method('auth.logout')
def logout() -> bool:
session.clear()
return True
@jsonrpc.method('auth.register')
def login_register(username: str, password: str) -> bool:
"""Регистрация

View File

@@ -0,0 +1,8 @@
__author__ = 'RemiZOffAlex'
__copyright__ = '(c) RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
from . import ( # noqa F401
common
)

View File

@@ -0,0 +1,19 @@
__author__ = 'RemiZOffAlex'
__copyright__ = '(c) RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
from .. import jsonrpc, login_required
from ... import lib, models
from ...mutations.user import user_as_dict
@jsonrpc.method('profile')
def profile(
fields: list = ['id', 'name']
) -> dict:
"""Показать список пользователей
"""
user = lib.get_user()
result = user_as_dict(user, fields)
return result

View File

@@ -4,6 +4,26 @@ __email__ = 'remizoffalex@mail.ru'
from . import jsonrpc
from .. import app, lib, models
from ..mutations.user import user_as_dict
@jsonrpc.method('user')
def user_id(
id: int,
fields: list = ['id', 'title']
) -> dict:
"""Статья
"""
user = models.db_session.query(
models.User
).filter(
models.User.id == id
).first()
if user is None:
raise ValueError
result = user_as_dict(user, fields)
return result
@jsonrpc.method('user.add')
@@ -109,7 +129,8 @@ def user_pages_count(id: int) -> int:
@jsonrpc.method('users')
def users_list(
page: int = 1,
order_by: dict = {'field': 'name', 'order': 'asc'}
order_by: dict = {'field': 'name', 'order': 'asc'},
fields: list = ['id', 'name']
) -> list:
"""Показать список пользователей
"""
@@ -118,7 +139,7 @@ def users_list(
)
# Сортировка
if order_by['field'] not in ['name', 'created']:
if order_by['field'] not in ['id', 'name', 'created']:
raise ValueError
if order_by['order'] not in ['asc', 'desc']:
raise ValueError
@@ -136,7 +157,7 @@ def users_list(
result = []
for item in users:
result.append(item.as_dict())
result.append(user_as_dict(item, fields))
return result

View File

@@ -0,0 +1,18 @@
__author__ = 'RemiZOffAlex'
__copyright__ = '(c) RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
import os
import jinja2
from . import views # noqa F401
from .. import app
my_loader = jinja2.ChoiceLoader([
app.jinja_loader,
jinja2.FileSystemLoader(
os.path.dirname(os.path.abspath(__file__)) + "/templates"
),
])
app.jinja_loader = my_loader

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/public/skeleton.html" %}
{% block content %}
<h1>{{ pagedata['info'] }}</h1>

View File

@@ -0,0 +1,8 @@
{% extends "/private/skeleton.html" %}
{% block content %}
<h1>{{ pagedata['info'] }}</h1>
<hr />
<p>Самурай без меча подобен самураю с мечом, но только без меча, однако как-будто с мечом, которого у него нет, но и без него он как с ним...</p>
{% endblock %}

20
myapp/ns_common/views.py Normal file
View File

@@ -0,0 +1,20 @@
__author__ = 'RemiZOffAlex'
__copyright__ = '(c) RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
from flask import render_template, session, redirect
from .. import app, lib
@app.route('/')
def index():
pagedata = {}
pagedata['title'] = app.config['TITLE']
if lib.get_user():
pagedata['info'] = 'Закрытый раздел'
body = render_template('user/index.html', pagedata=pagedata)
else:
pagedata['info'] = 'Открытый раздел'
body = render_template('guest/index.html', pagedata=pagedata)
return body

View File

@@ -11,8 +11,6 @@
<span v-html="note.body"></span>
<backtotop-component></backtotop-component>
{% endraw %}
{% endblock %}
@@ -27,10 +25,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/tags.js"></script>
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -37,9 +37,6 @@
{% endblock %}
{% block script %}
{% import 'inc/editor.js' as editor %}
{{ editor.plugin('tinymce') }}
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -38,9 +38,6 @@
{% endblock %}
{% block script %}
{% import 'inc/editor.js' as editor %}
{{ editor.plugin('tinymce') }}
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -17,12 +17,9 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getNotes"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script>

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/public/skeleton.html" %}
{% block content %}
<h3>Статьи</h3>
@@ -16,13 +16,9 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getPages"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script>

View File

@@ -1,4 +1,4 @@
{% extends "private/skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
{% raw %}
@@ -11,8 +11,6 @@
<span v-html="page.body"></span>
<backtotop-component></backtotop-component>
{% endraw %}
{% endblock %}
@@ -27,8 +25,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/tags.js"></script>
<script type="text/javascript">

View File

@@ -1,4 +1,4 @@
{% extends "private/skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
@@ -37,9 +37,6 @@
{% endblock %}
{% block script %}
{% import 'inc/editor.js' as editor %}
{{ editor.plugin('tinymce') }}
<script type="text/javascript">
var app = new Vue({
el: '#app',
@@ -53,7 +50,7 @@ var app = new Vue({
send: function () {
/* Сохранить */
let vm = this;
{{ editor.getValue('"body"', 'vm.page.body', type='tinymce') }}
/* editor.getValue('"body"', 'vm.page.body', type='tinymce') */
axios.post(
'/api',
{
@@ -74,9 +71,6 @@ var app = new Vue({
);
}
},
mounted: function () {
{{ editor.tinymce('"body"') }}
}
})
</script>
{% endblock %}

View File

@@ -1,4 +1,4 @@
{% extends "private/skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
<h3>
@@ -38,9 +38,6 @@
{% endblock %}
{% block script %}
{% import 'inc/editor.js' as editor %}
{{ editor.plugin('tinymce') }}
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -1,4 +1,4 @@
{% extends "private/skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
<h3>
@@ -45,13 +45,9 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getPages"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script>

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
<h3>Профиль</h3>

View File

@@ -12,13 +12,9 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getPages"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock content %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -27,8 +27,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -14,8 +14,6 @@
</div>
{% endraw %}
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -26,9 +24,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript">
Object.assign(root.data, {
tags: [],

View File

@@ -28,10 +28,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">
Object.assign(root.data, {
menuitem: null,

View File

@@ -30,8 +30,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -26,8 +26,6 @@
</div>
{% endraw %}
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -38,9 +36,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript">
Object.assign(root.data, {
tags: [],

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/public/skeleton.html" %}
{% block content %}
{% raw %}
@@ -29,9 +29,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -16,8 +16,6 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getPages"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -31,8 +29,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/public/skeleton.html" %}
{% block content %}
<h3>Список пользователей</h3>
@@ -10,8 +10,6 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getUsers"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -22,8 +20,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -1,4 +1,4 @@
{% extends "skeleton.html" %}
{% extends "/private/skeleton.html" %}
{% block content %}
{% raw %}
@@ -29,9 +29,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript">
var app = new Vue({
el: '#app',

View File

@@ -16,8 +16,6 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getPages"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -31,10 +29,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">
Object.assign(root.data, {
menuitem: 'pages',

View File

@@ -10,8 +10,6 @@
<pagination-component v-bind:pagination="pagination" v-bind:click-handler="getUsers"></pagination-component>
<backtotop-component></backtotop-component>
{% endblock %}
{% block breadcrumb %}
@@ -22,8 +20,6 @@
{% endblock %}
{% block script %}
<script type="text/javascript" src="/static/components/backtotop.js"></script>
<link rel="stylesheet" href="/static/components/backtotop.css"></link>
<script type="text/javascript" src="/static/components/pagination.js"></script>
<script type="text/javascript">

View File

@@ -0,0 +1,19 @@
body, td, p {
color:#000;
font-family: 'Liberation Sans', Arial, Helvetica, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
h1, h2, h3, h4, h5, h6 {
color:#000;
font-family: 'Liberation Sans', Arial, Helvetica, sans-serif;
}
code, pre {
color:#000;
font-family: 'Liberation Mono';
font-size: 1rem;
background-color: #c5c5c5;
border-radius: 3px;
padding: 5px;
line-height: 1.5;
}

View File

@@ -1,4 +1,5 @@
{% include '/components/backtotop.js' %}
{% include '/components/filter.js' %}
{% include '/components/footer.js' %}
{% include '/components/order_by.js' %}
{% include '/components/pagination.js' %}

View File

@@ -35,10 +35,6 @@ function Pagination(arguments) {
return data.page > 1;
};
function has_next() {
console.log('has_next');
console.log(data.page);
console.log(pages());
console.log(data.page < pages());
return data.page < pages();
};
function iter_pages() {
@@ -65,12 +61,11 @@ function Pagination(arguments) {
return {
data: data,
view: function(vnode) {
// console.log(data.page);
console.log('Pagination.view');
let result = [
m('div', {class: "row"},
m('div', {class: "col py-2 text-center"},
(function() {
// console.log(`pages: ${pages()}`)
if (pages()<=1) {
return m('button', {class: "btn btn-outline-secondary", type: "button", onclick: function() { data.clickHandler(1)}}, m('i', {class: "fa fa-refresh"}));
} else {
@@ -98,7 +93,6 @@ function Pagination(arguments) {
m('button', {type: "button", class: "btn btn-outline-secondary float-end", onclick: function() { handlePageSelected(data.page+1)}}, 'Следующая')
);
}
// console.log(`result: ${result}`)
return result;
})()
),

View File

@@ -3,8 +3,19 @@
<meta name="author" content="Ремизов Александр" />
<meta name="copyright" lang="ru" content="RemiZOffAlex" />
<link rel="shortcut icon" href="/static/favicon.ico">
<link rel="stylesheet" href="{{ STATIC }}/css/bootstrap.min.css" />
<link rel="stylesheet" href="{{ STATIC }}/css/font-awesome.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC }}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC }}/css/font-awesome.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC }}/css/backtotop.css" />
<script type="text/javascript" src="{{ STATIC }}/js/mithril.min.js"></script>
<title>{{ pagedata['title'] }}</title>
<style>
@font-face {
font-family: "Liberation Sans"; /* Имя шрифта */
src: url({{ STATIC }}/fonts/liberation/LiberationSans-Regular.ttf); /* Путь к файлу со шрифтом */
}
@font-face {
font-family: "Liberation Mono"; /* Имя шрифта */
src: url({{ STATIC }}/fonts/liberation/LiberationMono-Regular.ttf); /* Путь к файлу со шрифтом */
}
</style>
</head>

View File

@@ -0,0 +1,22 @@
function EventBus() {
let listeners = {};
return {
on: function(event, callback) {
console.log('on');
if (!(event in listeners)) {
listeners[event] = [];
}
listeners[event].push(callback)
},
off: function(event, id) {
console.log('off');
},
emit: function(event, arguments) {
console.log('emit');
listeners[event].forEach(element => {
console.log(element);
let result = element(arguments);
});
}
};
};

View File

@@ -0,0 +1,3 @@
{% include '/lib/common.js' %}
{% include '/lib/event-bus.js' %}
{% include '/lib/tinymce.js' %}

View File

@@ -0,0 +1,44 @@
function tinymce_init(id) {
let result = tinymce.init({
selector: id,
height: 400,
language: 'ru',
plugins: 'anchor autolink charmap code directionality fullscreen image importcss link lists media pagebreak preview save searchreplace table visualblocks visualchars',
toolbar: 'code | undo redo | styles blocks | bold italic underline strikethrough removeformat | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | pagebreak | fullscreen | link image anchor charmap',
font_family_formats: 'Liberation Sans=Liberation Sans; Mono=Liberation Mono;',
formats: {
img: { selector: 'img', classes: 'img-thumbnail', styles: {} },
paragraph: { selector: 'p,h1,h2,h3,h4,h5,h6', classes: '', styles: {} },
table: { selector: 'table', classes: 'table', styles: {} },
note: { classes: 'alert alert-primary', styles: {} },
danger: { classes: 'alert alert-danger', styles: {} },
bold: [
{ inline: 'strong', remove: 'all' },
{ inline: 'span', remove: 'all' },
{ inline: 'b', remove: 'all' }
],
},
style_formats: [
{ title: 'Картинка', selector: 'img', format: 'img' },
// { title: 'Абзац', format: 'paragraph' }, // С этим не работает список стилей
{ title: 'Таблица', selector: 'table', format: 'table' },
{ title: 'Примечание', format: 'note' },
{ title: 'Внимание', format: 'danger' },
],
content_css: '/static/css/tinymce-code.css',
relative_urls: false,
convert_urls: false,
// Table
table_default_attributes: {},
table_default_styles: {},
table_class_list: [
{title: 'default', value: 'table'},
{title: 'None', value: ''},
],
});
return result;
};
function tinymce_remove(id) {
tinymce.get(id).remove();
};

View File

@@ -0,0 +1,15 @@
{% include '/private/settings.js' %}
//let vr = document.body;
let vroot = document.getElementById("app");
let routes = {};
{% include '/lib/inc.j2' %}
{% include '/private/layout.js' %}
{% include '/components/inc.j2' %}
{% include '/private/components/inc.j2' %}
{% include '/private/domains/inc.j2' %}
{% include '/routes.js' %}

View File

@@ -0,0 +1,3 @@
{% include '/private/components/menu-general.js' %}
{% include '/private/components/pages.js' %}
{% include '/private/components/users.js' %}

View File

@@ -0,0 +1,43 @@
function MenuGeneral() {
function logout() {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'auth.logout',
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
window.location.href = '/';
}
}
);
};
return {
oninit: function(vnode) {
document.title = SETTINGS.TITLE;
},
view: function(vnode) {
result = [];
result.push(
m('div', {class: 'row'},
m('div', {class: 'col py-2'}, [
m(m.route.Link, {class: 'btn btn-outline-secondary', href: '/'}, m('i', {class: 'fa fa-home'})),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/pages'}, 'Статьи'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/tags'}, 'Метки'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/users'}, 'Пользователи'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/api/browse'}, 'API JSON-RPC'),
m('div', {class: 'btn-group float-end'},
m(m.route.Link, {class: 'btn btn-outline-secondary', href: '/profile'}, m('i', {class: 'fa fa-user'})),
m('button', {class: "btn btn-outline-danger", onclick: logout}, m('i', {class: "fa fa-sign-out"})),
)
])
)
)
return result;
}
};
};

View File

@@ -0,0 +1,57 @@
function ComponentPages() {
let data = {
pages: null,
};
function page_render(page, pageIdx) {
let odd = '';
if (pageIdx % 2) {
odd = ' bg-light'
};
let tags = page.tags.map(
function(tag, tagIdx) {
return [
m('i', {class: "fa fa-tag"}),
{tag: '<', children: '&nbsp;'},
m(m.route.Link, {class: "font-monospace text-decoration-none", href: `/tag/${tag.id}`}, tag.name),
{tag: '<', children: '&nbsp;'},
]
}
);
return m('div', {class: 'row'},
m('div', {class: "col py-2" + odd}, [
m(m.route.Link, {class: "text-decoration-none", href: `/page/${page.id}`}, m.trust(page.title)),
m('div', {class: 'row'},
m('div', {class: 'col text-muted'},
m('small', [...tags])
)
)
])
)
};
function pages_render() {
return data.pages.map(page_render);
};
return {
oninit: function(vnode) {
console.log('ComponentPages.oninit');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
onupdate: function(vnode) {
console.log('ComponentPages.onupdate');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
view: function() {
console.log('ComponentPages.view');
if (data.pages!=null) {
let result = [];
result.push(pages_render());
return result;
};
}
};
};

View File

@@ -0,0 +1,41 @@
function ComponentUsers() {
let data = {
users: null,
};
function user_render(user, userIdx) {
let odd = '';
if (userIdx % 2) {
odd = ' bg-light'
};
return m('div', {class: 'row'},
m('div', {class: "col py-2" + odd}, [
m(m.route.Link, {class: "text-decoration-none", href: `/user/${user.id}`}, m.trust(user.name)),
])
)
};
function users_render() {
return data.users.map(user_render);
};
return {
oninit: function(vnode) {
console.log('ComponentUsers.oninit');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
onupdate: function(vnode) {
console.log('ComponentUsers.onupdate');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
view: function() {
console.log('ComponentUsers.view');
if (data.users!=null) {
let result = [];
result.push(users_render());
return result;
};
}
};
};

View File

@@ -0,0 +1,14 @@
let Home = {
oninit: function(vnode) {
document.title = SETTINGS.TITLE;
},
view: function(vnode) {
result = [];
result.push(
m('h1', '{{ pagedata['info'] }}'),
m('hr'),
m('p', 'Самурай без меча подобен самураю с мечом, но только без меча, однако как-будто с мечом, которого у него нет, но и без него он как с ним...')
)
return result;
}
};

View File

@@ -0,0 +1,11 @@
{% include '/private/domains/page/inc.j2' %}
{% include '/private/domains/profile/inc.j2' %}
{% include '/private/domains/user/inc.j2' %}
{% include '/private/domains/home.js' %}
Object.assign(
routes,
{
"/": layout_decorator(Home),
}
);

View File

@@ -0,0 +1,101 @@
function PageAdd() {
let data = {
page: {
title: '',
body: '',
},
editor: null,
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/pages'}, 'Список статей')),
m('li', {class: 'breadcrumb-item active'}, 'Новая статья'),
]);
return result;
};
function form_submit(e) {
e.preventDefault();
page_add();
};
function page_add() {
if (data.page.title.length<2) {
return;
};
tinymce_get_value();
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'page.add',
"params": data.page,
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
m.route.set(`/page/${response['result'].id}`);
}
}
)
};
function tinymce_remove() {
tinymce.get('page_body').remove();
};
function tinymce_get_value() {
let value = tinymce.get('page_body').getContent();
if (value != data.page.body) {
data.page.body = value;
}
};
function tinymce_set_value() {
tinymce.get('page_body').setContent(data.page.body);
};
return {
oncreate(vnode) {
console.log('PageAdd.oncreate');
if (data.editor==null) {
data.editor = tinymce_init('#page_body');
}
},
onbeforeremove: function(vnode) {
console.log('PageAdd.onbeforeremove');
if (data.editor!=null) {
tinymce_remove();
};
},
view: function(vnode) {
console.log('PageAdd.view');
result = [];
result.push([
m('div', {class: 'row'},
m('div', {class: 'col py-2 h1'}, [
m(m.route.Link, {class: "btn btn-outline-secondary", href: "/pages", title: "Список статей"}, m('i', {class: 'fa fa-chevron-left'})),
'Новая страница'
])
),
m('hr')
]);
result.push(
m('form', {onsubmit: form_submit}, [
m('div', {class: 'mb-2'}, [
m('label', {class: 'form-label'}, 'Заголовок'),
m('input', {class: 'form-control', type: 'text', oninput: function (e) {data.page.title = e.target.value}, value: data.page.title}),
]),
m('div', {class: 'mb-2'}, [
m('label', {class: 'form-label'}, 'Текст'),
m('textarea', {class: 'form-control', cols: '40', rows: '8', id: 'page_body', oninput: function (e) {data.page.body = e.target.value}, value: data.page.body})
]),
m('div', {class: 'row'},
m('div', {class: 'col py-2'}, [
m('button', {class: 'btn btn-outline-success btn-lg float-end', type: 'submit'}, [m('i', {class: 'fa fa-save'}), ' Сохранить']),
])
),
])
);
result.push(breadcrumbs_render());
return result;
}
};
};

View File

@@ -0,0 +1,13 @@
{% include '/private/domains/page/add.js' %}
{% include '/private/domains/page/page.js' %}
{% include '/private/domains/page/pages.js' %}
Object.assign(
routes,
{
"/page/add": layout_decorator(PageAdd),
"/page/:id": layout_decorator(Page),
"/pages": layout_decorator(Pages),
"/pages/:page": layout_decorator(Pages),
}
);

View File

@@ -0,0 +1,83 @@
function Page() {
let data = {
page: null,
panels: {
standart: {
visible: false
},
},
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/pages'}, 'Список статей')),
m('li', {class: 'breadcrumb-item active'}, m.trust(data.page.title)),
]);
return result;
};
function page_get(id) {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'page',
"params": {
"id": id,
"fields": ["id", "title", "body", "parent_id", "created", "updated", "tags"]
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
data.page = response['result'];
document.title = `${data.page.title} - ${SETTINGS.TITLE}`;
}
}
);
};
function page_prev() {
if (data.page.parent_id) {
return m(m.route.Link, {class: "btn btn-outline-secondary", href: `/page/${data.page.parent_id}`, title: "Список статей"}, m('i', {class: 'fa fa-chevron-left'}));
} else {
return m(m.route.Link, {class: "btn btn-outline-secondary", href: "/pages", title: "Список статей"}, m('i', {class: 'fa fa-chevron-left'}));
}
};
return {
oninit: function(vnode) {
console.log('Page.oninit');
page_get(vnode.attrs.id);
},
onupdate: function(vnode) {
console.log('Page.onupdate');
if (data.page!=null) {
if (data.page.id.toString()!==vnode.attrs.id) {
page_get(vnode.attrs.id);
}
}
},
view: function(vnode) {
console.log('Page.view');
let result = [];
if (data.page!=null) {
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
page_prev(),
m('button', {class: 'btn btn-outline-secondary', title: 'Инструменты', onclick: function() {panel_show(data.panels.standart)}}, m('i', {class: 'fa fa-cog'})),
]),
m.trust(data.page.title),
])
),
m('hr'),
);
result.push(m.trust(data.page.body));
result.push(breadcrumbs_render());
};
return result
}
}
};

View File

@@ -0,0 +1,122 @@
function Pages() {
let data = {
filter: PanelFilter(),
order_by: PanelOrderBy({
field: 'title',
fields: [
{value: 'id', text: 'ID'},
{value: 'title', text: 'заголовку'},
{value: 'created', text: 'дате создания'},
{value: 'updated', text: 'дате обновления'}
],
clickHandler: pages_get,
order: 'asc',
}),
pages: [],
pagination: Pagination({
clickHandler: pages_get,
prefix_url: '/pages'
}),
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item active'}, 'Список статей'),
]);
return result;
};
function pages_get() {
let order_by = data.order_by.data;
let pagination = data.pagination.data;
m.request({
url: '/api',
method: "POST",
body: [
{
"jsonrpc": "2.0",
"method": 'pages',
"params": {
"page": pagination.page,
"order_by": {
"field": data.order_by.data.field,
'order': data.order_by.data.order
},
"fields": ["id", "title", "tags"]
},
"id": 1
},
{
"jsonrpc": "2.0",
"method": 'pages.count',
"id": 1
}
]
}).then(
function(response) {
if ('result' in response[0]) {
data.pages = response[0]['result'];
}
if ('result' in response[1]) {
data.pagination.size = response[1]['result'];
}
}
);
};
function page_render(page, pageIdx) {
let odd = '';
if (pageIdx % 2) {
odd = ' bg-light'
};
return m('div', {class: 'row'},
m('div', {class: `col py-2 ${odd}`}, [
m(m.route.Link, {href: `/page/${page.id}`}, m.trust(page.title)),
m('div', {class: 'row'},
),
])
);
};
function pages_render() {
return data.pages.map(page_render);
};
return {
oninit: function(vnode) {
let pagination = data.pagination.data;
if (vnode.attrs.page!==undefined) {
pagination.page = Number(vnode.attrs.page);
};
document.title = `Список статей - ${SETTINGS.TITLE}`;
pages_get();
},
view: function(vnode) {
let result = [];
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m(m.route.Link, {class: "btn btn-outline-success btn-lg float-end", href: "/page/add", title: "Добавить статью"}, m('i', {class: 'fa fa-plus'})),
m('div', {class: "btn-group btn-group-lg me-2"}, [
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.filter.data) }},
m('i', {class: "fa fa-filter"})
),
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.order_by.data) }},
m('i', {class: "fa fa-sort-alpha-asc"})
)
]),
'Статьи',
{tag: '<', children: '<hr />'}
])
)
);
result.push(m(data.filter));
result.push(m(data.order_by));
result.push(m(data.pagination));
if (data.pages.length>0) {
result.push(m(ComponentPages, {pages: data.pages}));
result.push(m(data.pagination));
};
result.push(breadcrumbs_render());
return result
}
}
};

View File

@@ -0,0 +1,65 @@
function Profile() {
let data = {
user: null,
panels: {
standart: {
visible: false
},
},
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item active'}, 'Профиль пользователя'),
]);
return result;
};
function profile_get() {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'profile',
"params": {
"fields": ["id", "name", "created", "disabled"]
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
data.user = response['result'];
}
}
);
};
return {
oninit: function(vnode) {
console.log('Profile.oninit');
document.title = `Мой профиль - ${SETTINGS.TITLE}`;
profile_get();
},
view: function(vnode) {
console.log('Profile.view');
let result = [];
if (data.user!=null) {
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
m('button', {class: 'btn btn-outline-secondary', title: 'Инструменты', onclick: function() {panel_show(data.panels.standart)}}, m('i', {class: 'fa fa-cog'})),
]),
'Мой профиль',
])
),
m('hr'),
);
result.push(data.user.name);
result.push(breadcrumbs_render());
};
return result
}
}
};

View File

@@ -0,0 +1,8 @@
{% include '/private/domains/profile/common.js' %}
Object.assign(
routes,
{
"/profile": layout_decorator(Profile),
}
);

View File

@@ -0,0 +1,11 @@
{% include '/public/domains/user/user.js' %}
{% include '/public/domains/user/users.js' %}
Object.assign(
routes,
{
"/user/:id": layout_decorator(User),
"/users": layout_decorator(Users),
"/users/:page": layout_decorator(Users),
}
);

View File

@@ -0,0 +1,75 @@
function User() {
let data = {
user: null,
panels: {
standart: {
visible: false
},
},
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/users'}, 'Список пользователей')),
m('li', {class: 'breadcrumb-item active'}, m.trust(data.user.name)),
]);
return result;
};
function user_get(id) {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'user',
"params": {
"id": id,
"fields": ["id", "name", "created"]
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
data.user = response['result'];
document.title = `${data.user.title} - ${SETTINGS.TITLE}`;
}
}
);
};
return {
oninit: function(vnode) {
console.log('User.oninit');
user_get(vnode.attrs.id);
},
onupdate: function(vnode) {
console.log('User.onupdate');
if (data.user!=null) {
if (data.user.id.toString()!==vnode.attrs.id) {
user_get(vnode.attrs.id);
}
}
},
view: function(vnode) {
console.log('User.view');
let result = [];
if (data.user!=null) {
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
m(m.route.Link, {class: "btn btn-outline-secondary", href: "/users", title: "Список пользователей"}, m('i', {class: 'fa fa-chevron-left'})),
m('button', {class: 'btn btn-outline-secondary', title: 'Инструменты', onclick: function() {panel_show(data.panels.standart)}}, m('i', {class: 'fa fa-cog'})),
]),
m.trust(data.user.name),
])
),
m('hr'),
);
result.push(breadcrumbs_render());
};
return result
}
}
};

View File

@@ -0,0 +1,156 @@
function Users() {
let data = {
filter: PanelFilter(),
order_by: PanelOrderBy({
field: 'name',
fields: [
{value: 'id', text: 'ID'},
{value: 'name', text: 'имени'},
{value: 'created', text: 'дате создания'},
{value: 'updated', text: 'дате обновления'}
],
clickHandler: users_get,
order: 'asc',
}),
raw_users: [],
get users() {
/* Отфильтрованный список */
let value = data.filter.data.value;
if ( value.length<1 ) {
return data.raw_users;
}
if (data.filter.data.isregex) {
try {
let regex = new RegExp(value, 'ig');
} catch (e) {
console.log(e);
return data.raw_users;
}
}
let result = data.raw_users.filter(user_filter);
return result;
},
pagination: Pagination({
clickHandler: users_get,
prefix_url: '/users'
}),
};
function user_filter(user) {
/* Фильтр статей */
let value = data.filter.data.value;
if ( value.length<1 ) {
return true;
}
let isTitle = null;
if ( data.filter.data.isregex) {
let regex = new RegExp(value, 'ig');
isTitle = regex.test(user.name.toLowerCase());
} else {
isTitle = user.name.toLowerCase().includes(value.toLowerCase());
}
if ( isTitle ) {
return true;
}
return false;
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item active'}, 'Список пользователей'),
]);
return result;
};
function users_get() {
let order_by = data.order_by.data;
let pagination = data.pagination.data;
m.request({
url: '/api',
method: "POST",
body: [
{
"jsonrpc": "2.0",
"method": 'users',
"params": {
"page": pagination.page,
"order_by": {
"field": order_by.field,
'order': order_by.order
},
"fields": ["id", "name"]
},
"id": 1
},
{
"jsonrpc": "2.0",
"method": 'users.count',
"id": 1
}
]
}).then(
function(response) {
if ('result' in response[0]) {
data.raw_users = response[0]['result'];
}
if ('result' in response[1]) {
data.pagination.size = response[1]['result'];
}
}
);
};
function user_render(user, userIdx) {
let odd = '';
if (userIdx % 2) {
odd = ' bg-light'
};
return m('div', {class: 'row'},
m('div', {class: `col py-2 ${odd}`}, [
m(m.route.Link, {href: `/user/${user.id}`}, m.trust(user.name)),
m('div', {class: 'row'},
),
])
);
};
function users_render() {
return data.users.map(user_render);
};
return {
oninit: function(vnode) {
let pagination = data.pagination.data;
if (vnode.attrs.page!==undefined) {
pagination.page = Number(vnode.attrs.page);
};
document.title = `Список пользователей - ${SETTINGS.TITLE}`;
users_get();
},
view: function(vnode) {
let result = [];
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.filter.data) }},
m('i', {class: "fa fa-filter"})
),
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.order_by.data) }},
m('i', {class: "fa fa-sort-alpha-asc"})
)
]),
'Пользователи',
])
),
m('hr'),
);
result.push(m(data.filter));
result.push(m(data.order_by));
result.push(m(data.pagination));
if (data.users.length>0) {
result.push(m(ComponentUsers, {users: data.users}));
result.push(m(data.pagination));
};
result.push(breadcrumbs_render());
return result
}
}
};

View File

@@ -0,0 +1,21 @@
function layout_decorator(controller) {
return {
render: function(vnode) {
return m(Layout, m(controller, vnode.attrs))
}
}
};
function Layout() {
return {
view: function(vnode) {
let result = [
m(MenuGeneral),
// m(SearchBar),
m("section", vnode.children),
m(Footer),
m(Backtotop),
];
return result;
}
}
};

View File

@@ -7,8 +7,7 @@
<a class="btn btn-outline-secondary border-0" href="/users">Пользователи</a>
<a class="btn btn-outline-secondary border-0" href="/api/browse">API JSON-RPC</a>
<div class="btn-group float-right">
<a class="btn btn-outline-secondary" href="/notes"><i class="fa fa-sticky-note-o"></i></a>
<div class="btn-group float-end">
<a class="btn btn-outline-secondary" href="/profile"><i class="fa fa-user"></i></a>
<a class="btn btn-outline-danger" href="/logout"><i class="fa fa-sign-out"></i></a>
</div>

View File

@@ -0,0 +1,5 @@
const SETTINGS = {
ITEMS_ON_PAGE: {{ ITEMS_ON_PAGE }},
TITLE: 'Моя панель',
ME: {{ user|tojson|safe }},
}

View File

@@ -1,8 +1,7 @@
<!DOCTYPE html>
<html lang="ru">
{% include 'header.html' %}
{% include 'header.html' %}
<body>
{% include '/inc/init.html' %}
<section id="app" class="container">
{% include '/private/navbar.html' %}
@@ -16,9 +15,10 @@
{% include 'footer.html' %}
</section>
{% block script %}{% endblock %}
<script type="text/javascript" src="{{ STATIC }}/tinymce/tinymce.min.js"></script>
{% include '/inc/run.html' %}
<script type="text/javascript" src="{{ STATIC }}/codemirror/codemirror.js"></script>
<script type="text/javascript" src="/app.js"></script>
</body>
</html>

View File

@@ -4,7 +4,7 @@
let vroot = document.getElementById("app");
let routes = {};
{% include '/lib.js' %}
{% include '/lib/inc.j2' %}
{% include '/public/layout.js' %}
{% include '/components/inc.j2' %}

View File

@@ -0,0 +1,3 @@
{% include '/public/components/menu-general.js' %}
{% include '/public/components/pages.js' %}
{% include '/public/components/users.js' %}

View File

@@ -0,0 +1,21 @@
let MenuGeneral = {
oninit: function(vnode) {
document.title = SETTINGS.TITLE;
},
view: function(vnode) {
result = [];
result.push(
m('div', {class: 'row'},
m('div', {class: 'col py-2'}, [
m(m.route.Link, {class: 'btn btn-outline-secondary', href: '/'}, m('i', {class: 'fa fa-home'})),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/pages'}, 'Статьи'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/tags'}, 'Метки'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/users'}, 'Пользователи'),
m(m.route.Link, {class: 'btn btn-outline-secondary border-0', href: '/api/browse'}, 'API JSON-RPC'),
m(m.route.Link, {class: 'btn btn-outline-success float-end', href: '/login'}, m('i', {class: 'fa fa-sign-in'})),
])
)
)
return result;
}
};

View File

@@ -0,0 +1,57 @@
function ComponentPages() {
let data = {
pages: null,
};
function page_render(page, pageIdx) {
let odd = '';
if (pageIdx % 2) {
odd = ' bg-light'
};
let tags = page.tags.map(
function(tag, tagIdx) {
return [
m('i', {class: "fa fa-tag"}),
{tag: '<', children: '&nbsp;'},
m(m.route.Link, {class: "font-monospace text-decoration-none", href: `/tag/${tag.id}`}, tag.name),
{tag: '<', children: '&nbsp;'},
]
}
);
return m('div', {class: 'row'},
m('div', {class: "col py-2" + odd}, [
m(m.route.Link, {class: "text-decoration-none", href: `/page/${page.id}`}, m.trust(page.title)),
m('div', {class: 'row'},
m('div', {class: 'col text-muted'},
m('small', [...tags])
)
)
])
)
};
function pages_render() {
return data.pages.map(page_render);
};
return {
oninit: function(vnode) {
console.log('ComponentPages.oninit');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
onupdate: function(vnode) {
console.log('ComponentPages.onupdate');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
view: function() {
console.log('ComponentPages.view');
if (data.pages!=null) {
let result = [];
result.push(pages_render());
return result;
};
}
};
};

View File

@@ -0,0 +1,41 @@
function ComponentUsers() {
let data = {
users: null,
};
function user_render(user, userIdx) {
let odd = '';
if (userIdx % 2) {
odd = ' bg-light'
};
return m('div', {class: 'row'},
m('div', {class: "col py-2" + odd}, [
m(m.route.Link, {class: "text-decoration-none", href: `/user/${user.id}`}, user.name),
])
)
};
function users_render() {
return data.users.map(user_render);
};
return {
oninit: function(vnode) {
console.log('ComponentUsers.oninit');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
onupdate: function(vnode) {
console.log('ComponentUsers.onupdate');
for (let key in vnode.attrs){
data[key] = vnode.attrs[key];
};
},
view: function() {
console.log('ComponentUsers.view');
if (data.users!=null) {
let result = [];
result.push(users_render());
return result;
};
}
};
};

View File

@@ -0,0 +1,79 @@
function Register() {
let data = {
username: '',
password: '',
repeat: ''
};
function register() {
if (data.username.length==0 || data.password.length==0) {
let error = 'Пароли не совпадают';
return;
}
if (data.password!=data.repeat) {
let error = 'Пароли не совпадают';
return;
}
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": "auth.register",
"params": {
"username": data.username,
"password": data.password
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
window.location.href = '/login';
} else if ('error' in response) {
vm.error = response.data['error'];
}
}
);
};
function form_submit(e) {
e.preventDefault();
register();
};
return {
view: function(vnode) {
let result = [];
result.push(
m('div', {class: 'row justify-content-center my-3'},
m('div', {class: 'col-md-6'}, [
m('h3', [
{tag: '<', children: '<a class="btn btn-outline-secondary float-end" href="/forgot-password">Забыл пароль</a>'},
'Регистрация',
m('hr'),
]),
m('form', {onsubmit: form_submit}, [
m('div', {class: "input-group mb-3"}, [
{tag: '<', children: '<span class="input-group-text"><i class="fa fa-user"></i></span>'},
m('input', {class: 'form-control', placeholder: 'Логин', type: 'text', oninput: function (e) {data.username = e.target.value}, value: data.username}),
]),
m('div', {class: "input-group mb-3"}, [
{tag: '<', children: '<span class="input-group-text"><i class="fa fa-lock"></i></span>'},
m('input', {class: 'form-control', placeholder: 'Пароль', type: 'password', oninput: function (e) {data.password = e.target.value}, value: data.password}),
]),
m('div', {class: "input-group mb-3"}, [
{tag: '<', children: '<span class="input-group-text"><i class="fa fa-lock"></i></span>'},
m('input', {class: 'form-control', placeholder: 'Повтор пароля', type: 'repeat', oninput: function (e) {data.repeat = e.target.value}, value: data.repeat}),
]),
m('div', {class: 'row'},
m('div', {class: "col py-2"}, [
m(m.route.Link, {class: 'btn btn-outline-secondary', href: '/login'}, 'Вход'),
m('button', {class: 'btn btn-outline-success float-end', type: 'submit'}, 'Зарегистрироваться')
]),
),
])
])
)
)
return result;
}
};
};

View File

@@ -0,0 +1,14 @@
let Home = {
oninit: function(vnode) {
document.title = SETTINGS.TITLE;
},
view: function(vnode) {
result = [];
result.push(
m('h1', '{{ pagedata['info'] }}'),
m('hr'),
m('p', 'Самурай без меча подобен самураю с мечом, но только без меча, однако как-будто с мечом, которого у него нет, но и без него он как с ним...')
)
return result;
}
};

View File

@@ -0,0 +1,11 @@
{% include '/public/domains/auth/inc.j2' %}
{% include '/public/domains/page/inc.j2' %}
{% include '/public/domains/user/inc.j2' %}
{% include '/public/domains/home.js' %}
Object.assign(
routes,
{
"/": layout_decorator(Home),
}
);

View File

@@ -0,0 +1,11 @@
{% include '/public/domains/page/page.js' %}
{% include '/public/domains/page/pages.js' %}
Object.assign(
routes,
{
"/page/:id": layout_decorator(Page),
"/pages": layout_decorator(Pages),
"/pages/:page": layout_decorator(Pages),
}
);

View File

@@ -0,0 +1,83 @@
function Page() {
let data = {
page: null,
panels: {
standart: {
visible: false
},
},
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/pages'}, 'Список статей')),
m('li', {class: 'breadcrumb-item active'}, m.trust(data.page.title)),
]);
return result;
};
function page_get(id) {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'page',
"params": {
"id": id,
"fields": ["id", "title", "body", "parent_id", "created", "updated", "tags"]
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
data.page = response['result'];
document.title = `${data.page.title} - ${SETTINGS.TITLE}`;
}
}
);
};
function page_prev() {
if (data.page.parent_id) {
return m(m.route.Link, {class: "btn btn-outline-secondary", href: `/page/${data.page.parent_id}`, title: "Список статей"}, m('i', {class: 'fa fa-chevron-left'}));
} else {
return m(m.route.Link, {class: "btn btn-outline-secondary", href: "/pages", title: "Список статей"}, m('i', {class: 'fa fa-chevron-left'}));
}
};
return {
oninit: function(vnode) {
console.log('Page.oninit');
page_get(vnode.attrs.id);
},
onupdate: function(vnode) {
console.log('Page.onupdate');
if (data.page!=null) {
if (data.page.id.toString()!==vnode.attrs.id) {
page_get(vnode.attrs.id);
}
}
},
view: function(vnode) {
console.log('Page.view');
let result = [];
if (data.page!=null) {
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
page_prev(),
m('button', {class: 'btn btn-outline-secondary', title: 'Инструменты', onclick: function() {panel_show(data.panels.standart)}}, m('i', {class: 'fa fa-cog'})),
]),
m.trust(data.page.title),
])
),
m('hr'),
);
result.push(m.trust(data.page.body));
result.push(breadcrumbs_render());
};
return result
}
}
};

View File

@@ -0,0 +1,11 @@
{% include '/public/domains/user/user.js' %}
{% include '/public/domains/user/users.js' %}
Object.assign(
routes,
{
"/user/:id": layout_decorator(User),
"/users": layout_decorator(Users),
"/users/:page": layout_decorator(Users),
}
);

View File

@@ -0,0 +1,76 @@
function User() {
let data = {
user: null,
panels: {
standart: {
visible: false
},
},
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/users'}, 'Список пользователей')),
m('li', {class: 'breadcrumb-item active'}, m.trust(data.user.name)),
]);
return result;
};
function user_get(id) {
m.request({
url: '/api',
method: "POST",
body: {
"jsonrpc": "2.0",
"method": 'user',
"params": {
"id": id,
"fields": ["id", "name", "created", "disabled"]
},
"id": 1
}
}).then(
function(response) {
if ('result' in response) {
data.user = response['result'];
document.title = `${data.user.title} - ${SETTINGS.TITLE}`;
}
}
);
};
return {
oninit: function(vnode) {
console.log('User.oninit');
user_get(vnode.attrs.id);
},
onupdate: function(vnode) {
console.log('User.onupdate');
if (data.user!=null) {
if (data.user.id.toString()!==vnode.attrs.id) {
user_get(vnode.attrs.id);
}
}
},
view: function(vnode) {
console.log('User.view');
let result = [];
if (data.user!=null) {
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
m(m.route.Link, {class: "btn btn-outline-secondary", href: "/users", title: "Список пользователей"}, m('i', {class: 'fa fa-chevron-left'})),
m('button', {class: 'btn btn-outline-secondary', title: 'Инструменты', onclick: function() {panel_show(data.panels.standart)}}, m('i', {class: 'fa fa-cog'})),
]),
m.trust(data.user.name),
])
),
m('hr'),
);
result.push(m.trust(data.user.body));
result.push(breadcrumbs_render());
};
return result
}
}
};

View File

@@ -0,0 +1,156 @@
function Users() {
document.title = `Список пользователей - ${SETTINGS.TITLE}`;
let data = {
filter: PanelFilter(),
order_by: PanelOrderBy({
field: 'name',
fields: [
{value: 'id', text: 'ID'},
{value: 'name', text: 'имени'},
{value: 'created', text: 'дате создания'},
{value: 'updated', text: 'дате обновления'}
],
clickHandler: users_get,
order: 'asc',
}),
raw_users: [],
get users() {
/* Отфильтрованный список */
let value = data.filter.data.value;
if ( value.length<1 ) {
return data.raw_users;
}
if (data.filter.data.isregex) {
try {
let regex = new RegExp(value, 'ig');
} catch (e) {
console.log(e);
return data.raw_users;
}
}
let result = data.raw_users.filter(user_filter);
return result;
},
pagination: Pagination({
clickHandler: users_get,
prefix_url: '/users'
}),
};
function user_filter(user) {
/* Фильтр статей */
let value = data.filter.data.value;
if ( value.length<1 ) {
return true;
}
let isTitle = null;
if ( data.filter.data.isregex) {
let regex = new RegExp(value, 'ig');
isTitle = regex.test(user.name.toLowerCase());
} else {
isTitle = user.name.toLowerCase().includes(value.toLowerCase());
}
if ( isTitle ) {
return true;
}
return false;
};
function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item active'}, 'Список пользователей'),
]);
return result;
};
function users_get() {
let order_by = data.order_by.data;
let pagination = data.pagination.data;
m.request({
url: '/api',
method: "POST",
body: [
{
"jsonrpc": "2.0",
"method": 'users',
"params": {
"page": pagination.page,
"order_by": {
"field": order_by.field,
'order': order_by.order
},
"fields": ["id", "name"]
},
"id": 1
},
{
"jsonrpc": "2.0",
"method": 'users.count',
"id": 1
}
]
}).then(
function(response) {
if ('result' in response[0]) {
data.raw_users = response[0]['result'];
}
if ('result' in response[1]) {
data.pagination.size = response[1]['result'];
}
}
);
};
function user_render(user, userIdx) {
let odd = '';
if (userIdx % 2) {
odd = ' bg-light'
};
return m('div', {class: 'row'},
m('div', {class: `col py-2 ${odd}`}, [
m(m.route.Link, {href: `/user/${user.id}`}, m.trust(user.name)),
m('div', {class: 'row'},
),
])
);
};
function users_render() {
return data.users.map(user_render);
};
return {
oninit: function(vnode) {
let pagination = data.pagination.data;
if (vnode.attrs.page!==undefined) {
pagination.page = Number(vnode.attrs.page);
};
users_get();
},
view: function(vnode) {
let result = [];
result.push(
breadcrumbs_render(),
m('div', {class: 'row'},
m('div', {class: 'col h1 py-1'}, [
m('div', {class: "btn-group btn-group-lg me-2"}, [
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.filter.data) }},
m('i', {class: "fa fa-filter"})
),
m('button', {type: "button", class: "btn btn-outline-secondary", onclick: function() { panel_show(data.order_by.data) }},
m('i', {class: "fa fa-sort-alpha-asc"})
)
]),
'Пользователи',
])
),
m('hr'),
);
result.push(m(data.filter));
result.push(m(data.order_by));
result.push(m(data.pagination));
if (data.users.length>0) {
result.push(m(ComponentUsers, {users: data.users}));
result.push(m(data.pagination));
};
result.push(breadcrumbs_render());
return result
}
}
};

View File

@@ -15,8 +15,6 @@
{% include 'footer.html' %}
</section>
{% block script %}{% endblock %}
<script type="text/javascript" src="/app.js"></script>
</body>
</html>

View File

@@ -1,23 +0,0 @@
<!DOCTYPE html>
<html lang="ru">
{% include 'header.html' %}
<body>
<section id="app" class="container">
{% include '/public/navbar.html' %}
{% block content %}
{% endblock content %}
{% block breadcrumb %}
{% endblock %}
{% include 'footer.html' %}
</section>
<script type="text/javascript" src="/app.js"></script>
</body>
</html>

View File

@@ -19,15 +19,6 @@ def app_js():
return Response(body, mimetype='application/javascript')
@app.route('/')
def index():
pagedata = {}
pagedata['title'] = app.config['TITLE']
pagedata['info'] = 'Привет мир!'
body = render_template('index.html', pagedata=pagedata)
return body
@app.route("/robots.txt")
def robots_txt():
body = render_template("robots.txt")