2016-06-28 13:59:33 +00:00
'use strict' ;
2016-06-28 16:12:02 +00:00
document . addEventListener ( 'DOMContentLoaded' , event => {
2016-08-28 09:37:09 +00:00
document . querySelector ( 'footer' ) . innerHTML += 'With a flavour of <a rel="noopener noreferrer" href="https://github.com/hacdias/caddy-filemanager">Hugo</a>.' ;
document . querySelector ( '.only-side > p > a' ) . innerHTML = "Hugo" ;
2016-08-28 09:15:55 +00:00
let link = document . querySelector ( '.only-side > p a:first-child' ) . getAttribute ( 'href' ) + "/settings/"
2016-07-01 15:10:47 +00:00
document . getElementById ( 'logout' ) . insertAdjacentHTML ( 'beforebegin' , ` <a href=" ${ link } ">
2016-06-28 16:12:02 +00:00
< div class = "action" >
< i class = "material-icons" > settings < / i >
< / d i v >
< / a > ` ) ;
2016-06-28 21:20:42 +00:00
} ) ;
document . addEventListener ( 'listing' , event => {
2016-06-29 13:57:40 +00:00
if ( window . location . pathname . includes ( '/content/' ) ) {
document . getElementById ( 'newdir' ) . placeholder = "file[:archetype]..." ;
document . getElementById ( 'newdir' ) . removeEventListener ( 'keydown' , newDirEvent ) ;
document . getElementById ( 'newdir' ) . addEventListener ( 'keydown' , event => {
if ( event . keyCode == 27 ) {
document . getElementById ( 'newdir' ) . classList . toggle ( 'enabled' ) ;
setTimeout ( ( ) => {
document . getElementById ( 'newdir' ) . value = '' ;
} , 200 ) ;
}
if ( event . keyCode == 13 ) {
event . preventDefault ( ) ;
let value = document . getElementById ( 'newdir' ) . value ;
let index = value . lastIndexOf ( ':' ) ;
let name = value . substring ( 0 , index ) ;
let archetype = value . substring ( index + 1 , value . length ) ;
if ( name == "" ) name = archetype ;
if ( index == - 1 ) archetype = "" ;
let button = document . getElementById ( 'new' ) ;
let html = button . changeToLoading ( ) ;
let request = new XMLHttpRequest ( ) ;
request . open ( "POST" , window . location ) ;
request . setRequestHeader ( 'Filename' , name ) ;
2016-07-05 16:54:54 +00:00
request . setRequestHeader ( 'Token' , token ) ;
2016-06-29 13:57:40 +00:00
request . setRequestHeader ( 'Archetype' , archetype ) ;
request . send ( ) ;
request . onreadystatechange = function ( ) {
if ( request . readyState == 4 ) {
button . changeToDone ( ( request . status != 200 ) , html ) ;
if ( request . status == 200 ) {
window . location = window . location . pathname + name ;
}
}
}
}
} ) ;
}
2016-06-28 21:20:42 +00:00
} ) ;
document . addEventListener ( 'editor' , event => {
2016-06-29 13:57:40 +00:00
document . getElementById ( 'submit' ) . insertAdjacentHTML ( 'afterend' , ` <div class="right">
2016-06-28 21:20:42 +00:00
< button id = "publish" >
< span >
< i class = "material-icons" > send < / i >
< / s p a n >
< span > publish < / s p a n >
< / b u t t o n >
< / d i v > ` ) ;
2016-06-28 16:12:02 +00:00
2016-06-29 19:39:29 +00:00
if ( ( document . getElementById ( 'date' ) || document . getElementById ( 'publishdate' ) ) && document . getElementById ( 'editor' ) . dataset . kind == "complete" ) {
2016-06-29 13:57:40 +00:00
document . querySelector ( '#editor .right' ) . insertAdjacentHTML ( 'afterbegin' , ` <button id="schedule">
2016-06-28 21:20:42 +00:00
< span >
< i class = "material-icons" > alarm < / i >
< / s p a n >
< span > Schedule < / s p a n >
< / b u t t o n > ` ) ;
2016-06-29 13:57:40 +00:00
document . getElementById ( 'schedule' ) . addEventListener ( 'click' , event => {
event . preventDefault ( ) ;
2016-06-29 19:39:29 +00:00
let date = document . getElementById ( 'date' ) . value ;
if ( document . getElementById ( 'publishDate' ) ) {
date = document . getElementById ( 'publishDate' ) . value ;
}
let container = document . getElementById ( 'editor' ) ;
let kind = container . dataset . kind ;
let button = document . querySelector ( '#schedule span:first-child' ) ;
let data = form2js ( document . querySelector ( 'form' ) ) ;
let html = button . changeToLoading ( ) ;
let request = new XMLHttpRequest ( ) ;
request . open ( "PUT" , window . location ) ;
request . setRequestHeader ( 'Kind' , kind ) ;
2016-07-05 16:54:54 +00:00
request . setRequestHeader ( 'Token' , token ) ;
2016-06-29 19:39:29 +00:00
request . setRequestHeader ( 'Schedule' , date ) ;
request . send ( JSON . stringify ( data ) ) ;
request . onreadystatechange = function ( ) {
if ( request . readyState == 4 ) {
button . changeToDone ( ( request . status != 200 ) , html ) ;
}
}
2016-06-29 13:57:40 +00:00
} ) ;
}
document . getElementById ( 'publish' ) . addEventListener ( 'click' , event => {
event . preventDefault ( ) ;
if ( document . getElementById ( 'draft' ) ) {
document . getElementById ( 'block-draft' ) . remove ( ) ;
}
let container = document . getElementById ( 'editor' ) ;
let kind = container . dataset . kind ;
let button = document . querySelector ( '#publish span:first-child' ) ;
let data = form2js ( document . querySelector ( 'form' ) ) ;
let html = button . changeToLoading ( ) ;
let request = new XMLHttpRequest ( ) ;
request . open ( "PUT" , window . location ) ;
request . setRequestHeader ( 'Kind' , kind ) ;
2016-07-05 16:54:54 +00:00
request . setRequestHeader ( 'Token' , token ) ;
2016-06-29 13:57:40 +00:00
request . setRequestHeader ( 'Regenerate' , "true" ) ;
request . send ( JSON . stringify ( data ) ) ;
request . onreadystatechange = function ( ) {
if ( request . readyState == 4 ) {
button . changeToDone ( ( request . status != 200 ) , html ) ;
}
}
} ) ;
2016-06-29 19:39:29 +00:00
} ) ;