Browse Source

CustomCategory: replace 'ajax' functions by own entry point

master
parent
commit
dec58babdd
  1. 36
      customCategory.php
  2. 34
      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;
}

34
src/customCategory.js

@ -26,7 +26,9 @@ class CustomCategoryRepository {
return callback(null, yaml.load(cache[id]))
}
ajax('customCategory', { id }, (result) => {
fetch('customCategory.php?id=' + id)
.then(res => res.text())
.then(result => {
let data
cache[id] = result
@ -57,9 +59,26 @@ class CustomCategory {
load (id, callback) {
this.id = id
ajax('customCategory', { id }, (result) => {
fetch('customCategory.php?id=' + id)
.then(res => res.text())
.then(result => {
let data
cache[id] = result
this.content = result
callback(null, 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)
})
}
@ -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,7 +229,9 @@ 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) => {
fetch('customCategory.php?action=list')
.then(res => res.json())
.then(result => {
browser.dom.innerHTML = ''
const ul = document.createElement('ul')

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