API Improve and Pages class

This commit is contained in:
Diego Najar 2019-02-20 08:45:55 +01:00
parent 3058904fd2
commit a5180135f6
5 changed files with 103 additions and 56 deletions

View File

@ -36,6 +36,10 @@ function editorInitialize(content) {
initialValue: content initialValue: content
}); });
// Get the tags from the content
// When the content is setted the tags need to be setted
_tags = parser.tags(content);
// Editor event change // Editor event change
_editor.codemirror.on("change", function(){ _editor.codemirror.on("change", function(){
// If the content doesn't changed is not need to autosave // If the content doesn't changed is not need to autosave
@ -69,28 +73,21 @@ function editorInitialize(content) {
}); });
} }
function editorSetContent(text) {
// Get the tags from the content
// When the content is setted the tags need to be setted
_tags = parser.tags(text);
// Set the current content to the variable
// This variable helps to know when the content was changed
_content = text;
// Set the new content into the editor
_editor.value(text);
}
function editorGetContent() { function editorGetContent() {
return _editor.value(); return _editor.value();
} }
function createPage() {
let response = ajax.createPage();
response.then(function(key) {
// Log
log('createPage() => ajax.createPage => key',key);
_key = key;
editorInitialize('# Title \n');
});
}
// MAIN // MAIN
// Init editor area
editorInitialize("# Title \n");
$(document).ready(function() { $(document).ready(function() {
showAlert("Welcome to Bludit"); showAlert("Welcome to Bludit");
}); });

View File

@ -18,14 +18,14 @@ function displayTags() {
// Init array for current tags // Init array for current tags
_currentTags = []; _currentTags = [];
// Remove all tags from the <ul> // Remove all tags from the <ul>
$("#currentTags").html('<li class="tagItem list-group-item tagSelected"><i class="fa fa-star-o"></i> Untagged</li>'); $("#currentTags").html('<li class="tagItem list-group-item tagSelected" data-action="untagged"><i class="fa fa-star-o"></i> Untagged</li>');
// Add all tags to the <ul> // Add all tags to the <ul>
tags.forEach(function(tag) { tags.forEach(function(tag) {
_currentTags[tag.key] = tag.list; _currentTags[tag.key] = tag.list;
if (tagSelected == tag.key) { if (tagSelected == tag.key) {
$("#currentTags").append('<li class="tagItem list-group-item tagSelected" data-key="'+tag.key+'"># '+tag.name+'</li>'); $("#currentTags").append('<li class="tagItem list-group-item tagSelected" data-action="tag" data-key="'+tag.key+'"># '+tag.name+'</li>');
} else { } else {
$("#currentTags").append('<li class="tagItem list-group-item" data-key="'+tag.key+'"># '+tag.name+'</li>'); $("#currentTags").append('<li class="tagItem list-group-item" data-action="tag" data-key="'+tag.key+'"># '+tag.name+'</li>');
} }
}); });
}); });
@ -39,10 +39,18 @@ $(document).ready(function() {
$(this).addClass("tagSelected"); $(this).addClass("tagSelected");
// Get the tag key clicked // Get the tag key clicked
let tagKey = $(this).data("key"); let tagKey = $(this).data("key");
let action = $(this).data("action");
// Log // Log
log('click li.tagItem => action',action);
log('click li.tagItem => tagKey',tagKey); log('click li.tagItem => tagKey',tagKey);
// Display pages by the tag
displayPagesByTag(tagKey); if (action=="untagged") {
displayPagesUntagged();
} else {
// Display pages by the tag
displayPagesByTag(tagKey);
}
}); });
// Retrive and show the tags // Retrive and show the tags
@ -78,6 +86,23 @@ function displayPagesByTag(tagKey) {
}); });
} }
function displayPagesUntagged() {
let response = ajax.getPagesUntagged();
response.then(function(pages) {
// Log
log('displayPagesUntagged() => ajax.getPagesUntagged => pages',pages);
// Init array for current pages by tag
_currentPages = [];
// Remove all pages from the <ul>
$("#currentPages").html("");
pages.forEach(function(page) {
_currentPages[page.key] = page;
// Add all pages to the <ul>
$("#currentPages").append('<li class="pageItem list-group-item" data-key="'+page.key+'"><div class="pageItemTitle">'+page.title+'</div><div class="pageItemContent">'+page.contentRaw.substring(0, 50)+'</div></li>');
});
});
}
// Set the page selected // Set the page selected
function loadPage(pageKey) { function loadPage(pageKey) {
// Check the current key if the same as the page is editing // Check the current key if the same as the page is editing
@ -98,7 +123,7 @@ function loadPage(pageKey) {
content += "# "+page.title.trim()+"\n"; content += "# "+page.title.trim()+"\n";
} }
content += page.contentRaw; content += page.contentRaw;
editorSetContent(content); editorInitialize(content);
}); });
} }

View File

@ -21,29 +21,27 @@ class Ajax {
} }
} }
createPage() { async createPage() {
var url = this.apiURL+"pages"; var url = this.apiURL+"pages";
return fetch(url, { try {
credentials: 'same-origin', const response = await fetch(url, {
method: "POST", credentials: 'same-origin',
body: JSON.stringify({ method: "POST",
token: this.token, body: JSON.stringify({
authentication: this.authentication token: this.token,
}), authentication: this.authentication
headers: new Headers({ }),
'Content-Type': 'application/json' headers: new Headers({
}), 'Content-Type': 'application/json'
}) }),
.then(function(response) { });
return response.json(); const json = await response.json();
})
.then(function(json) {
return json.data.key; return json.data.key;
}) }
.catch(err => { catch (err) {
console.log(err); console.log(err);
return false; return true;
}); }
} }
updatePage(key, title, content, tags) { updatePage(key, title, content, tags) {
@ -104,4 +102,25 @@ class Ajax {
return false; return false;
} }
} }
async getPagesUntagged() {
let parameters = {
token: this.token,
untagged: true,
published: true,
draft: true
}
let url = this.apiURL+"pages?"+$.param(parameters);
try {
const response = await fetch(url, {
method: "GET"
});
const json = await response.json();
return json.data;
}
catch (err) {
console.log(err);
return true;
}
}
} }

View File

@ -474,19 +474,19 @@ class Pages extends dbJSON {
} }
if ($static) { if ($static) {
$list += $pages->getStaticDB(); $list += $this->getStaticDB();
} }
if ($sticky) { if ($sticky) {
$list += $pages->getStickyDB(); $list += $this->getStickyDB();
} }
if ($draft) { if ($draft) {
$list += $pages->getDraftDB(); $list += $this->getDraftDB();
} }
if ($scheduled) { if ($scheduled) {
$list += $pages->getScheduledDB(); $list += $this->getScheduledDB();
} }
if ($numberOfItems==-1) { if ($numberOfItems==-1) {

View File

@ -296,11 +296,15 @@ class pluginAPI extends Plugin {
private function getPages($args) private function getPages($args)
{ {
global $pages; global $pages;
$published = $args['published'];
$static = $args['static']; // Parameters and the default values
$draft = $args['draft']; $published = (isset($args['published'])?$args['published']:true);
$sticky = $args['sticky']; $static = (isset($args['static'])?$args['static']:false);
$scheduled = $args['scheduled']; $draft = (isset($args['draft'])?$args['draft']:false);
$sticky = (isset($args['sticky'])?$args['sticky']:false);
$scheduled = (isset($args['scheduled'])?$args['scheduled']:false);
$untagged = (isset($args['untagged'])?$args['untagged']:false);
$numberOfItems = $this->getValue('numberOfItems'); $numberOfItems = $this->getValue('numberOfItems');
$pageNumber = 1; $pageNumber = 1;
$list = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled); $list = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled);
@ -315,10 +319,12 @@ class pluginAPI extends Plugin {
try { try {
// Create the page object from the page key // Create the page object from the page key
$page = new Page($pageKey); $page = new Page($pageKey);
if ($args['untagged'] && (empty($page->tags()))) { if ($untagged) {
// Push the page to the data array for the response if (empty($page->tags())) {
array_push($tmp['data'], $page->json($returnsArray=true)); // Push the page to the data array for the response
} else { array_push($tmp['data'], $page->json($returnsArray=true));
}
} else{
array_push($tmp['data'], $page->json($returnsArray=true)); array_push($tmp['data'], $page->json($returnsArray=true));
} }
} catch (Exception $e) { } catch (Exception $e) {