@ -5,6 +5,48 @@
// # * _license : This project is under MIT License
// # *********************************************************************************/
async function get ( partial , callback ) {
await fetch ( window . location . protocol + "//" + window . location . host + partial ,
{
mode : "cors" ,
headers : {
'Access-Control-Allow-Origin' : window . location . host ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
} ,
method : "GET"
} )
. then ( async function ( response ) {
let result = await response . json ( ) ;
callback ( result ) ;
} )
. catch ( function ( exc ) {
console . log ( exc ) ;
return null ;
} )
}
async function post ( partial , stringified _json , callback ) {
await fetch ( window . location . protocol + "//" + window . location . host + partial ,
{
mode : "cors" ,
headers : {
'Access-Control-Allow-Origin' : window . location . host ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
} ,
method : "POST" ,
body : stringified _json
} )
. then ( async function ( response ) {
let result = await response . json ( ) ;
callback ( result ) ;
} )
. catch ( function ( exc ) {
console . log ( exc ) ;
} )
}
// ------------------------------------------------------
// Called when search for mail addresses in manage mail
// ------------------------------------------------------
@ -24,74 +66,15 @@ function dashboard_mailsearch(search_txt)
}
// ------------------------------------------------------
// Called when a new project is created,
// posts it to the server
// Deletes a project from the db
// ------------------------------------------------------
function new _project _save ( ) {
let modal _ok = document . getElementById ( 'modal-ok' ) ;
let modal _cancel = document . getElementById ( 'modal-cancel' ) ;
let short _help _short = document . getElementById ( 'new-project-too-short' ) ;
let short _help _invalid = document . getElementById ( 'new-project-invalid-name' ) ;
let name = document . getElementById ( 'project-name' ) . value
short _help _short . classList . add ( 'is-hidden' ) ;
short _help _invalid . classList . add ( 'is-hidden' ) ;
// Validate input
if ( name . length === 0 ) {
short _help _short . classList . remove ( 'is-hidden' ) ;
return false ;
}
if ( /^\w+$/ . test ( name ) === false ) {
short _help _invalid . classList . remove ( 'is-hidden' ) ;
return false ;
}
modal _ok . classList . add ( 'is-loading' ) ;
modal _cancel . classList . add ( 'is-hidden' ) ;
fetch ( window . location . protocol + "//" + window . location . host + '/api/project/new' ,
{
mode : "cors" ,
headers : {
'Access-Control-Allow-Origin' : '*' ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
} ,
method : "POST" ,
// use real location
body : JSON . stringify ( {
"name" : name
} )
} )
. then ( async function ( response ) {
let result = await response . json ( ) ;
result = result [ 'status' ] ;
modal _ok . classList . remove ( 'is-loading' ) ;
modal _cancel . classList . remove ( 'is-hidden' ) ;
if ( result === "ok" ) {
hide _modal ( 'modal-new-project' ) ;
window . location . reload ( true ) ;
}
if ( result === "too-short" ) {
short _help _short . classList . remove ( 'is-hidden' ) ;
}
if ( result === "invalid-name" ) {
short _help _invalid . classList . remove ( 'is-hidden' ) ;
}
} )
. catch ( function ( exc ) {
console . log ( exc ) ;
} )
}
function project _delete ( )
async function project _delete ( )
{
let modal = document . getElementById ( 'modal-project-delete' ) ;
let modal _ok = document . getElementById ( 'modal-delete-ok' ) ;
let modal _cancel = document . getElementById ( 'modal-delete-cancel' ) ;
const project = modal . dataset . project ;
console . log ( "Project: " + project ) ;
const project = modal . dataset . name ;
if ( project === null || project . length === 0 ) {
console . log ( "Couldn't find a valid dataset" ) ;
return ;
@ -99,58 +82,253 @@ function project_delete()
modal _ok . classList . add ( 'is-loading' ) ;
modal _cancel . classList . add ( 'is-hidden' ) ;
fetch ( window . location . protocol + "//" + window . location . host + '/api/project/delete/' + project ,
{
mode : "cors" ,
headers : {
'Access-Control-Allow-Origin' : '*' ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
} ,
method : "GET"
} )
. then ( async function ( response ) {
let result = await response . json ( ) ;
result = result [ 'status' ] ;
modal _ok . classList . remove ( 'is-loading' ) ;
modal _cancel . classList . remove ( 'is-hidden' ) ;
if ( result === "ok" ) {
hide _modal ( 'modal-project-delete' ) ;
window . location . reload ( true ) ;
}
await get ( '/api/project/delete/' + project , function ( result ) {
modal _ok . classList . remove ( 'is-loading' ) ;
modal _cancel . classList . remove ( 'is-hidden' ) ;
if ( result === "ok" ) {
console . log ( result ) ;
} )
. catch ( function ( exc ) {
console . log ( exc ) ;
} )
}
} )
modal . classList . remove ( 'is-active' ) ;
window . location . reload ( true ) ;
}
// ------------------------------------------------------
// Hides any modal
// ------------------------------------------------------
function hide _modal ( id _name )
function hide _modal ( id _name , redirect = null )
{
let el = document . getElementById ( id _name ) ;
el . classList . remove ( "is-active" ) ;
if ( redirect != null ) {
window . location = redirect ;
}
}
// ------------------------------------------------------
// Shows any modal
// Shows any modal and attach project name
// ------------------------------------------------------
function show _modal ( id _name )
function show _modal ( id _name , proj _name )
{
let el = document . getElementById ( id _name ) ;
el . classList . add ( "is-active" ) ;
el . setAttribute ( 'data-name' , proj _name ) ;
}
// ------------------------------------------------------
// Shows any modal
// Shows edit/new modal
// ------------------------------------------------------
function show _modal _with _project ( id _name , proj _name )
async function show _modal _with _project ( id _name , proj _name )
{
let el = document . getElementById ( id _name ) ;
el . classList . add ( "is-active" ) ;
el . setAttribute ( 'data-project' , proj _name )
// Get title element
let title = document . getElementById ( 'modal-title' ) ;
// Get Dialog
let modal = document . getElementById ( id _name ) ;
if ( proj _name ) {
// Get Data
await get ( '/api/project/get/' + proj _name ,
function ( r ) {
document . getElementById ( 'edit-project-blog-url' ) . value = r [ 'blogurl' ] ;
document . getElementById ( 'edit-project-output' ) . value = r [ 'output' ] ;
document . getElementById ( 'edit-project-gravatar-cache' ) . checked = r [ 'gravatar_cache' ] ;
document . getElementById ( 'edit-project-gravatar-cache-dir' ) . value = r [ 'gravatar_cache_dir' ] ;
document . getElementById ( 'edit-project-gravatar-size' ) . value = r [ 'gravatar_size' ] ;
document . getElementById ( 'edit-project-send-otp' ) . checked = r [ 'sendotp' ] ;
document . getElementById ( 'edit-project-addons-smileys' ) . checked = r [ 'addon_smileys' ] ;
} ) ;
// Set project name
let proj _el = document . getElementById ( 'edit-project-name' ) ;
proj _el . value = proj _name
// Set project name
title . innerText = "Edit project '" + proj _name + "'" ;
// Make active
modal . classList . add ( "is-active" ) ;
// Edit mode
modal . setAttribute ( 'data-mode' , 'edit' ) ;
modal . setAttribute ( 'data-name' , proj _name ) ;
}
if ( proj _name == null ) {
// Set project name
title . innerText = "New Project" ;
// Reset fields, needed when user pressed cancel on edit modal
document . getElementById ( 'edit-project-name' ) . value = "" ;
document . getElementById ( 'edit-project-blog-url' ) . value = "" ;
document . getElementById ( 'edit-project-output' ) . value = "" ;
document . getElementById ( 'edit-project-gravatar-cache' ) . checked = true ;
document . getElementById ( 'edit-project-gravatar-cache-dir' ) . value = "" ;
document . getElementById ( 'edit-project-gravatar-size' ) . value = 256 ;
document . getElementById ( 'edit-project-send-otp' ) . checked = true ;
document . getElementById ( 'edit-project-addons-smileys' ) . checked = true ;
// Edit mode
modal . setAttribute ( 'data-mode' , 'new' ) ;
// Make active
modal . classList . add ( "is-active" ) ;
}
}
async function save _project _settings ( id _name )
{
// Spin the tea cups
let btn = document . getElementById ( 'modal-save-ok' ) ;
btn . classList . add ( 'is-loading' ) ;
// Get modal
let modal = document . getElementById ( id _name ) ;
// Get field data
let json _data = {
"name" : document . getElementById ( 'edit-project-name' ) . value ,
"blogurl" : document . getElementById ( 'edit-project-blog-url' ) . value ,
"output" : document . getElementById ( 'edit-project-output' ) . value ,
"gravatar_cache" : document . getElementById ( 'edit-project-gravatar-cache' ) . checked ,
"gravatar_cache_dir" : document . getElementById ( 'edit-project-gravatar-cache-dir' ) . value ,
"gravatar_size" : document . getElementById ( 'edit-project-gravatar-size' ) . value ,
"sendotp" : document . getElementById ( 'edit-project-send-otp' ) . checked ,
"addon_smileys" : document . getElementById ( 'edit-project-addons-smileys' ) . checked
}
if ( modal . dataset . mode === "edit" ) {
let old _name = modal . dataset . name ;
await post ( '/api/project/edit/' + old _name , JSON . stringify ( json _data ) , function ( result ) {
let error = document . getElementById ( 'modal-edit-error-messages' )
error . innerText = ''
if ( result [ 'status' ] === 'too-short' ) {
error . innerText = "A required field has been left empty!"
return ;
}
if ( result [ 'status' ] === 'invalid-project-name' ) {
error . innerText = "The project name is not valid. Please only use alphanumeric characters!"
return ;
}
if ( result [ 'status' ] === 'project-exists' ) {
error . innerText = "A project with this name already exists!"
return ;
}
if ( result [ 'status' ] === 'invalid-blog-url' ) {
error . innerText = "The blog-url is invalid!"
return ;
}
if ( result [ 'status' ] === 'invalid-path-output' ) {
error . innerText = "This output path does not exist!"
return ;
}