Browse Source

CustomCategory: replace 'ajax' functions by own entry point

master
parent
commit
dec58babdd
  1. 36
      customCategory.php
  2. 106
      src/customCategory.js
  3. 29
      src/customCategory.php

36
customCategory.php

@ -0,0 +1,36 @@
<?php include "conf.php"; /* load a local configuration */ ?>
<?php session_start(); ?>
<?php require 'vendor/autoload.php'; /* composer includes */ ?>
<?php include "modulekit/loader.php"; /* loads all php-includes */ ?>
<?php call_hooks("ajax_start"); /* initialize submodules */ ?>
<?php
if (!isset($db)) {
exit(0);
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'list') {
$result = $customCategoryRepository->list($_REQUEST);
Header("Content-Type: application/json; charset=utf-8");
print json_readable_encode($result);
}
if (isset($_REQUEST['id'])) {
$category = $customCategoryRepository->getCategory($_REQUEST['id']);
if ($category) {
$customCategoryRepository->recordAccess($_REQUEST['id']);
}
Header("Content-Type: application/yaml; charset=utf-8");
print $category;
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'save') {
$content = file_get_contents("php://input");
$id = $customCategoryRepository->saveCategory($content);
$customCategoryRepository->recordAccess($id);
Header("Content-Type: text/plain; charset=utf-8");
print $id;
}

106
src/customCategory.js

@ -26,23 +26,25 @@ class CustomCategoryRepository {
return callback(null, yaml.load(cache[id]))
}
ajax('customCategory', { id }, (result) => {
let data
cache[id] = result
try {
data = yaml.load(result)
}
catch (e) {
return global.alert(e)
}
fetch('customCategory.php?id=' + id)
.then(res => res.text())
.then(result => {
let data
cache[id] = result
try {
data = yaml.load(result)
}
catch (e) {
return global.alert(e)
}
if (Object.is(data) && !('name' in data)) {
data.name = 'Custom ' + id.substr(0, 6)
}
if (Object.is(data) && !('name' in data)) {
data.name = 'Custom ' + id.substr(0, 6)
}
callback(null, data)
})
callback(null, data)
})
}
getTemplate (id, options, callback) {
@ -57,10 +59,27 @@ class CustomCategory {
load (id, callback) {
this.id = id
ajax('customCategory', { id }, (result) => {
this.content = result
callback(null, result)
})
fetch('customCategory.php?id=' + id)
.then(res => res.text())
.then(result => {
let data
cache[id] = result
this.content = result
try {
data = yaml.load(result)
}
catch (e) {
return global.alert(e)
}
if (Object.is(data) && !('name' in data)) {
data.name = 'Custom ' + id.substr(0, 6)
}
callback(null, data)
})
}
edit () {
@ -115,7 +134,10 @@ class CustomCategory {
applyContent (content) {
this.content = content
ajax('customCategory', { action: 'save' }, this.content, () => {})
fetch('customCategory.php?action=save', {
method: 'POST',
body: this.content
})
if (this.textarea) {
this.textarea.value = content
@ -207,33 +229,35 @@ hooks.register('browser-more-categories', (browser, parameters) => {
function customCategoriesList (browser, options) {
browser.dom.innerHTML = '<i class="fa fa-spinner fa-pulse fa-fw"></i> ' + lang('loading')
ajax('customCategory', { action: 'list' }, (result) => {
browser.dom.innerHTML = ''
fetch('customCategory.php?action=list')
.then(res => res.json())
.then(result => {
browser.dom.innerHTML = ''
const ul = document.createElement('ul')
browser.dom.appendChild(ul)
const ul = document.createElement('ul')
browser.dom.appendChild(ul)
result.forEach(cat => {
const li = document.createElement('li')
result.forEach(cat => {
const li = document.createElement('li')
const a = document.createElement('a')
a.href = '#categories=custom/' + cat.id
a.appendChild(document.createTextNode(cat.name))
li.appendChild(a)
const a = document.createElement('a')
a.href = '#categories=custom/' + cat.id
a.appendChild(document.createTextNode(cat.name))
li.appendChild(a)
const edit = document.createElement('a')
edit.onclick = (e) => {
editCustomCategory(cat.id)
e.preventDefault()
}
edit.innerHTML = ' <i class="fa fa-pen"></i>'
li.appendChild(edit)
const edit = document.createElement('a')
edit.onclick = (e) => {
editCustomCategory(cat.id)
e.preventDefault()
}
edit.innerHTML = ' <i class="fa fa-pen"></i>'
li.appendChild(edit)
ul.appendChild(li)
})
ul.appendChild(li)
})
browser.catchLinks()
})
browser.catchLinks()
})
}
hooks.register('init', () => {

29
src/customCategory.php

@ -79,32 +79,3 @@ class CustomCategoryRepository {
}
$customCategoryRepository = new CustomCategoryRepository();
function ajax_customCategory ($param, $content) {
global $db;
global $customCategoryRepository;
if (!$db) {
return null;
}
if (isset($param['action']) && $param['action'] === 'list') {
return $customCategoryRepository->list($param);
}
if (isset($param['id'])) {
$category = $customCategoryRepository->getCategory($param['id']);
if ($category) {
$customCategoryRepository->recordAccess($param['id']);
}
return $category;
}
if (isset($param['action']) && $param['action'] === 'save') {
$id = $customCategoryRepository->saveCategory($content);
$customCategoryRepository->recordAccess($id);
return $id;
}
}
Loading…
Cancel
Save