mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
1 line
9.3 KiB
JavaScript
1 line
9.3 KiB
JavaScript
$(document).ready(function(){$("#logout").click(function(a){return a.preventDefault(),$.ajax({type:"GET",url:"/admin",async:!1,username:"username",password:"password",headers:{Authorization:"Basic xxx"}}).fail(function(){window.location="/"}),!1}),$(document).pjax("a[data-pjax]","#content")}),$(document).on("ready pjax:success",function(){function a(){this.style.height="5px",this.style.height=this.scrollHeight+"px"}return document.title=document.getElementById("site-title").innerHTML,$("textarea").each(a),$("textarea").keyup(a),$(window).resize(function(){$("textarea").each(a)}),$("main").hasClass("browse")&&$(document).trigger("page:browse"),$(".editor")[0]&&$(document).trigger("page:editor"),!1}),$(document).on("page:browse",function(){$("body").off("click",".rename").on("click",".rename",function(a){a.preventDefault(),button=$(this);var b=prompt("New file name:");if(""==b)return!1;"/"!=b.substring(0,1)&&(b=window.location.pathname.replace("/admin/browse/","")+"/"+b);var c='{"filename": "'+b+'"}';return $.ajax({type:"PUT",url:button.data("file"),data:c,dataType:"json",encode:!0}).done(function(a){$.pjax({url:window.location.pathname,container:"#content"}),notification({text:button.data("message"),type:"success",timeout:5e3})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}),$("body").off("click",".delete").on("click",".delete",function(a){return a.preventDefault(),button=$(this),$.ajax({type:"DELETE",url:button.data("file")}).done(function(a){button.parent().parent().fadeOut(),notification({text:button.data("message"),type:"success",timeout:5e3})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}),$(".new").off("click").click(function(a){return a.preventDefault(),$(this).data("opened")?($("#new-file").fadeOut(200),$(this).data("opened",!1)):($("#new-file").fadeIn(200),$(this).data("opened",!0)),!1}),$("#new-file").on("keypress","input",function(a){if(13==a.keyCode){a.preventDefault();var b=$(this).val(),c=b.split(":"),d="",e="";if(""==b)return notification({text:"You have to write something. If you want to close the box, click the button again.",type:"warning",timeout:5e3}),!1;if(1==c.length)d=b;else{if(2!=c.length)return notification({text:"Hmm... I don't understand you. Try writing something like 'name[:archetype]'.",type:"error"}),!1;d=c[0],e=c[1]}var f='{"filename": "'+d+'", "archetype": "'+e+'"}';return $.ajax({type:"POST",url:window.location.pathname,data:f,dataType:"json",encode:!0}).done(function(a){notification({text:"File created successfully.",type:"success",timeout:5e3}),$.pjax({url:window.location.pathname.replace("browse","edit")+d,container:"#content"})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}}),$("#upload").click(function(a){return a.preventDefault(),$('.actions input[type="file"]').click(),!1}),$('input[type="file"]').on("change",function(a){a.preventDefault(),files=a.target.files;var b=new FormData;return $.each(files,function(a,c){b.append(a,c)}),$.ajax({url:window.location.pathname,type:"POST",data:b,cache:!1,dataType:"json",headers:{"X-Upload":"true"},processData:!1,contentType:!1}).done(function(a){notification({text:"File(s) uploaded successfully.",type:"success",timeout:5e3}),$.pjax({url:window.location.pathname,container:"#content"})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1})}),$(document).on("page:editor",function(){var a=$("#editor-source").data("mode"),b=$('textarea[name="content"]').hide(),c=ace.edit("editor-source");c.getSession().setMode("ace/mode/"+a),c.getSession().setValue(b.val()),c.getSession().on("change",function(){b.val(c.getSession().getValue())}),c.setOptions({wrap:!0,maxLines:1/0,theme:"ace/theme/github",showPrintMargin:!1,fontSize:"1em"}),$("body").off("keypress","input").on("keypress","input",function(a){return 13==a.keyCode?(a.preventDefault(),$('input[value="Save"]').focus().click(),!1):void 0}),$("#site-title").keyup(function(){$(".frontmatter #title").val($(this).val())});var d=$("#editor-preview"),e=$("#editor-source");$("#see-source").off("click").click(function(a){a.preventDefault(),d.hide(),e.fadeIn(),$("#see-preview").data("previewing","false")}),$("#see-preview").off("click").click(function(a){if(a.preventDefault(),"true"==$(this).data("previewing"))d.hide(),e.fadeIn(),$(this).data("previewing","false");else{var b=new showdown.Converter,f=c.getValue(),g=b.makeHtml(f);e.hide(),d.html(g).fadeIn(),$(this).data("previewing","true")}return!1}),$("form").submit(function(a){a.preventDefault(),d.html("").fadeOut(),$("#see-preview").data("previewing","false"),e.fadeIn();var b=JSON.stringify($(this).serializeJSON()),c=$(this).find("input[type=submit]:focus");return $.ajax({type:"POST",url:window.location,data:b,headers:{"X-Regenerate":c.data("regenerate"),"X-Schedule":c.data("schedule"),"X-Content-Type":c.data("type")},dataType:"json",encode:!0,contentType:"application/json; charset=utf-8"}).done(function(a){notification({text:c.data("message"),type:"success",timeout:5e3})}).fail(function(a){notification({text:"Something went wrong.",type:"error"}),console.log(a)}),!1}),$("body").on("click",".add",function(a){if(a.preventDefault(),defaultID="lorem-ipsum-sin-dolor-amet",$("#"+defaultID).length)return!1;if(block=$(this).parent().parent(),blockType=block.data("type"),blockID=block.attr("id"),block.is("div")&&block.hasClass("frontmatter")&&(block=$(".blocks"),block.append('<div class="block" id="'+defaultID+'"></div>'),blockType="object"),"array"==blockType&&(newID=blockID+"[]",input=blockID,input=input.replace(/\[/,"\\["),input=input.replace(/\]/,"\\]"),block.append('<div id="'+newID+"-"+$("#"+input+" > div").length+'" data-type="array-item"><input name="'+newID+':auto" id="'+newID+'"></input><span class="actions"> <button class="delete">−</button></span></div></div>')),"object"==blockType){if(newItem=$("#"+defaultID),newItem.html('<input id="name-'+defaultID+'" placeholder="Write the field name and press enter..."></input>'),field=$("#name-"+defaultID),!document.cookie.replace(/(?:(?:^|.*;\s*)placeholdertip\s*\=\s*([^;]*).*$)|^.*$/,"$1")){var b=new Date;b.setDate(b.getDate()+365),document.cookie="placeholdertip=true; expires="+b.toUTCString+"; path=/",notification({text:'Write the field name and then press enter. If you want to create an array or an object, end the name with ":array" or ":object".',type:"information"})}$(field).keypress(function(a){return 13==a.which?(a.preventDefault(),value=field.val(),""==value?(newItem.remove(),!1):(elements=value.split(":"),elements.length>2?(notification({text:"Invalid syntax. It must be 'name[:type]'.",type:"error"}),!1):2==elements.length&&"array"!=elements[1]&&"object"!=elements[1]?(notification({text:"Only arrays and objects are allowed.",type:"error"}),!1):(field.remove(),"undefined"==typeof blockID?blockID=elements[0]:blockID=blockID+"["+elements[0]+"]",1==elements.length?(newItem.attr("id","block-"+blockID),newItem.append('<input name="'+blockID+':auto" id="'+blockID+'"></input><br>'),newItem.prepend('<label for="'+blockID+'">'+value+'</label> <span class="actions"><button class="delete">−</button></span>')):(type="","array"==elements[1]?type="array":type="object",template='<fieldset id="${blockID}" data-type="${type}"> <h3>${elements[0]}</h3> <span class="actions"> <button class="add">+</button> <button class="delete">−</button> </span> </fieldset>',template=template.replace("${blockID}",blockID),template=template.replace("${elements[0]}",elements[0]),template=template.replace("${type}",type),newItem.after(template),newItem.remove(),console.log('"'+blockID+'" block of type "'+type+'" added.')),!1))):void 0})}return!1}),$("body").on("click",".delete",function(a){return a.preventDefault(),button=$(this),name=button.parent().parent().attr("for")||button.parent().parent().attr("id")||button.parent().parent().parent().attr("id"),name=name.replace(/\[/,"\\["),name=name.replace(/\]/,"\\]"),console.log(name),$('label[for="'+name+'"]').fadeOut().remove(),$("#"+name).fadeOut().remove(),!1})}),$.noty.themes.admin={name:"admin",helpers:{},modal:{css:{position:"fixed",width:"100%",height:"100%",backgroundColor:"#000",zIndex:1e4,opacity:.6,display:"none",left:0,top:0}}},$.noty.defaults={layout:"topRight",theme:"admin",dismissQueue:!0,animation:{open:"animated bounceInRight",close:"animated fadeOut",easing:"swing",speed:500},timeout:!1,force:!1,modal:!1,maxVisible:5,killer:!1,closeWith:["click"],callback:{onShow:function(){},afterShow:function(){},onClose:function(){},afterClose:function(){},onCloseClick:function(){}},buttons:!1},notification=function(a){var b;switch(a.type){case"success":b='<i class="fa fa-check"></i>';break;case"error":b='<i class="fa fa-times"></i>';break;case"warning":b='<i class="fa fa-exclamation"></i>';break;case"information":b='<i class="fa fa-info"></i>';break;default:b='<i class="fa fa-bell"></i>'}var c={template:'<div class="noty_message"><span class="noty_icon">'+b+'</span><span class="noty_text"></span></div>'};if(a=$.extend({},c,a),noty(a),!document.cookie.replace(/(?:(?:^|.*;\s*)stickynoties\s*\=\s*([^;]*).*$)|^.*$/,"$1")&&!a.timeout){var d=new Date;d.setDate(d.getDate()+365),document.cookie="stickynoties=true; expires="+d.toUTCString+"; path=/",notification({text:"Some notifications are sticky. If it doesn't go away, click to dismiss it.",type:"information"})}}; |