MarkDoc/js/functions.js

189 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-01-01 12:08:59 +01:00
/*
* MarkDoc
* PHP MarkDown document manager
* Hordé Nicolas
* https://github.com/dahut87/MarkDoc
* Release under GPLv3.0 license
* Base on Pheditor "PHP file editor" By Hamid Samak Release under MIT license
*/
var external = RegExp('^((f|ht)tps?:)?//');
var javascript = RegExp('^javascript:');
2021-03-26 13:47:57 +01:00
var textual = RegExp('\.md$');
var internal = RegExp('^\/:');
2021-01-01 12:08:59 +01:00
var emoji;
2021-01-03 14:32:47 +01:00
var LANG;
2021-01-01 12:08:59 +01:00
$(function(){
$("#files").jstree({
themes: { responsive: true },
plugins: [ "types" ],
core:
{
strings:{ loading: LANG['LOADING'] },
2021-01-01 12:08:59 +01:00
check_callback: true,
data : {
type: "POST",
dataType: "json",
url : "/index.php",
data : function (node) {
return { 'action' : 'children' };
}
}
}
});
emoji = new EmojiConvertor();
$("#content").html(emoji.replace_colons($("#content").html()));
2021-01-01 12:08:59 +01:00
$("#editor").hide();
$("#image").hide();
$("#save").hide();
setTimeout(function(){ $("#forkongithub").fadeOut(1500); }, 5000);
$('#content > p> img').magnifik({ratio:1.0});
tocgenerate();
2021-01-01 12:08:59 +01:00
$("#files").on("select_node.jstree", function (e, nodes) {
file="/"+$("#files").jstree("get_path",nodes.node,"/").replace(/^.+?[/]/, '');
if ($("#files").jstree("is_leaf",nodes.node))
openlink(file+".md",false);
2021-01-01 12:08:59 +01:00
});
$("input[name=submit]").click(function(e) {
e.preventDefault();
search($("#search").val());
});
2021-01-03 14:17:40 +01:00
$("input[name=toc]").hover(function(e) {
e.preventDefault();
tocshow();
});
2021-03-26 13:47:57 +01:00
$(window).on('popstate', function(e){
window.location.reload();
});
2021-01-01 12:08:59 +01:00
majlink('head');
});
function openlink(dest,majtree)
2021-01-01 12:08:59 +01:00
{
2021-03-26 13:47:57 +01:00
$("#content").html('<div class="container h-100"><div class="row h-100 justify-content-center align-items-center"><div class="col-12 fa-3x container"><i class="fas fa-spinner fa-spin"></i></div></div></div>');
document.title = $("#title").text()+" - "+dest;
history.pushState({href: dest}, "", dest);
2021-01-01 12:08:59 +01:00
$.ajax({
type: "POST",
url: "/index.php",
data: { action: "open", file: encodeURIComponent(dest) },
success: function(data){
$("#content").html(emoji.replace_colons(data));
2021-01-01 12:08:59 +01:00
Prism.highlightAll();
majlink('content');
$(window).scrollTop(0);
if (majtree) searchtree(dest);
$('#content > p> img').magnifik({ratio:1.00});
tocgenerate();
2021-01-01 12:08:59 +01:00
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (dest!="special/404.md")
openlink("special/404.md",false);
2021-01-01 12:08:59 +01:00
else
2021-01-03 14:32:47 +01:00
$("#content").html("<b>"+LANG['404X2']+"</b>");
2021-01-01 12:08:59 +01:00
}
});
}
function tocgenerate()
{
tocshow();
$('.toc').toc({
'selectors': 'h2,h3,h4',
'container': '#content'
})
$('.toc').append('<i class="fa fa-2x fa-caret-up" aria-hidden="true"></i>');
$('.toc > i').off().hover(function(e) {
e.preventDefault();
tochide();
});
}
function tochide()
{
$('.toc').slideUp()
$('#toc').show();
}
function tocshow()
{
$('.toc').slideDown()
$('#toc').hide();
}
2021-01-01 12:08:59 +01:00
function alertBox(message, className) {
$(".alert").removeClass("alert-success alert-warning alert-danger");
$(".alert").html(message).addClass("alert-" + className).fadeIn();
setTimeout(function(){
$(".alert").fadeOut();
}, 5000);
}
function majlink(context)
{
$('#'+context+' a').click(function(e) {
dest=$(this).attr('href');
if (!external.test(dest) && !javascript.test(dest) && (textual.test(dest) || internal.test(dest)))
2021-01-01 12:08:59 +01:00
{
e.preventDefault();
openlink(dest,true);
2021-01-01 12:08:59 +01:00
}
});
}
function searchtree(file)
{
var flag;
$("#files").jstree("deselect_all");
node=$("#files").jstree("get_node", "ul > li:first");
file.split('/').forEach(function (item) {
flag=false;
subnodes=$("#files").jstree("get_children_dom",node);
subnodes.each(function (i,subnode)
{
text=$("#files").jstree("get_text",subnode);
if ((text==item) || (text+".md"==item))
{
$("#files").jstree("open_node",subnode);
if ($("#files").jstree("is_leaf",subnode))
{
$("#files").jstree("select_node",subnode,true);
flag=false;
return false;
}
node=subnode;
flag=true;
return false;
}
});
if (!flag) return false;
});
}
2021-01-01 12:08:59 +01:00
function search(arg)
{
$("#search").val(arg);
$.ajax({
type: "POST",
url: "/index.php",
data: { action: "search", search: encodeURIComponent(arg), type: "js" },
success: function(data){
$("#content").html(data);
2021-01-01 12:08:59 +01:00
Prism.highlightAll();
majlink('content');
$(window).scrollTop(0);
tocgenerate();
2021-01-01 12:08:59 +01:00
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (dest!="special/404.md")
openlink("special/404.md",false);
2021-01-01 12:08:59 +01:00
else
2021-01-03 14:32:47 +01:00
$("#content").html("<b>"+LANG['404X2']+"</b>");
2021-01-01 12:08:59 +01:00
}
});
}