openkb
Version:
openKB is an Open Source Nodejs Markdown based knowledge base/FAQ/Wiki app with powerful lunr search
1 lines • 7.85 kB
JavaScript
function file_delete_confirm(e,t){window.confirm("Are you sure you want to delete the file?")&&$.ajax({method:"POST",url:$("#app_context").val()+"/file/delete",data:{img:e}}).done(function(e){$("#file-"+t).remove(),show_notification(e,"success")}).fail(function(e){show_notification(e,"danger")})}function show_notification(e,t,i){i=i||!1,$("#notify_message").removeClass(),$("#notify_message").addClass("notify_message-"+t),$("#notify_message").html(e),$("#notify_message").slideDown(600).delay(1200).slideUp(600,function(){!0===i&&location.reload()})}function search_form(e){$("form#"+e).submit()}function convertToSlug(e){return e.toLowerCase().replace(/ /g,"-").replace(/[^\w-]+/g,"")}$(document).ready(function(){function e(){var e=window.markdownItClassy,t=window.markdownit({html:!0,linkify:!0,typographer:!0,breaks:!0});t.use(e);var i=t.render(n.value()).replace(/<img/g,"<img class='img-responsive' ");i=i.replace(/<table/g,"<table class='table table-hover' "),$("#preview").html(i),$("pre code").each(function(e,t){hljs.highlightBlock(t)})}function t(e){return $.trim(e).replace(/[^a-z0-9-æøå]/gi,"-").replace(/-+/g,"-").replace(/^-|-$/g,"").replace(/æ/gi,"ae").replace(/ø/gi,"oe").replace(/å/gi,"a").toLowerCase()}if($(".body_text img").each(function(){$(this).addClass("img-responsive")}),!0===config.links_blank_page&&$(".body_text a").attr("target","_blank"),"undefined"!=typeof mermaid&&mermaid.initialize({startOnLoad:!0}),$("table").each(function(){$(this).addClass("table table-hover")}),$(document).on("change","#kb_versions",function(){$.ajax({method:"POST",url:$("#app_context").val()+"/api/getArticleJson",data:{kb_id:$(this).val()}}).done(function(e){$("#frm_kb_title").val(e.kb_title),n.value(e.kb_body),$("#btnSettingsMenu").trigger("click")}).fail(function(e){show_notification(e.responseText,"danger")})}),!0===config.typeahead_search&&$("#frm_search").length&&$.ajax({method:"POST",url:$("#app_context").val()+"/search_api",data:{id:this.id,state:this.checked}}).done(function(e){var t=lunr.Index.load(JSON.parse(e.index)),i=JSON.parse(e.store);$("#frm_search").on("keyup",function(){var e=$(this).val();if(e.length>2){var n=t.search(e);if(0===n.length)$("#searchResult").addClass("hidden");else{$(".searchResultList").empty(),$(".searchResultList").append('<li class="list-group-item list-group-heading">Search results</li>');for(var o in n){var a=n[o].ref,l='<li class="list-group-item"><a href="'+$("#app_context").val()+"/"+config.route_name+"/"+i[a].p+'">'+i[a].t+"</a></li>";$(".searchResultList").append(l)}$("#searchResult").removeClass("hidden")}}})}).fail(function(e){show_notification(e.responseText,"danger")}),$(".toggle-menu").length&&$(".toggle-menu").jPushMenu({closeOnClickOutside:!1}),$("pre code").each(function(e,t){hljs.highlightBlock(t)}),!0===config.add_header_anchors&&$(".body_text > h1, .body_text > h2, .body_text > h3, .body_text > h4, .body_text > h5").each(function(){$(this).attr("id",convertToSlug($(this).text())),$(this).prepend('<a class="headerAnchor" href="#'+convertToSlug($(this).text())+'">#</a> ')}),window.location.hash&&$(window.location.hash).length){var i=$(window.location.hash);$(window).scrollTop(i.offset().top).scrollLeft(i.offset().left)}if($("#frm_kb_keywords").length&&$("#frm_kb_keywords").tokenfield(),$("#editor").length){var n=new SimpleMDE({element:$("#editor")[0],spellChecker:config.enable_spellchecker,toolbar:["bold","italic","heading","|","quote","unordered-list","ordered-list","|","link","image","|","table","horizontal-rule","code","guide"]});inlineAttachment.editors.codemirror4.attach(n.codemirror,{uploadUrl:$("#app_context").val()+"/file/upload_file"}),e(),n.codemirror.on("change",function(){e()});var o=document.getElementById("preview");if(null!==o){var a=!1,l=!1;n.codemirror.on("scroll",function(e){if(a)a=!1;else{l=!0;var t=e.getScrollInfo().height-e.getScrollInfo().clientHeight,i=parseFloat(e.getScrollInfo().top)/t,n=(o.scrollHeight-o.clientHeight)*i;o.scrollTop=n}}),o.onscroll=function(){if(l)l=!1;else{a=!0;var e=o.scrollHeight-o.clientHeight,t=parseFloat(o.scrollTop)/e,i=(n.codemirror.getScrollInfo().height-n.codemirror.getScrollInfo().clientHeight)*t;n.codemirror.scrollTo(0,i)}}}}if($(document).on("click","#frm_edit_kb_save",function(e){e.preventDefault(),$("#versionSidebar").length&&""===$("#frm_kb_edit_reason").val()?(show_notification("Please enter a reason for editing article","danger"),$("#btnVersionMenu").trigger("click"),$("#frm_kb_edit_reason").focus()):$("#edit_form").submit()}),$(document).on("click",".btnEditVersion",function(e){$("#btnVersionMenu").trigger("click"),$.LoadingOverlay("show",{zIndex:9999}),$.ajax({method:"POST",url:$("#app_context").val()+"/api/getArticleJson",data:{kb_id:$(this).parent().attr("id")}}).done(function(e){$.LoadingOverlay("hide"),$("#frm_kb_title").val(e.kb_title),n.value(e.kb_body)}).fail(function(e){$.LoadingOverlay("hide"),show_notification(e,"danger")})}),$(document).on("click",".btnDeleteVersion",function(e){var t=$(this).closest(".versionWrapper");$("#btnVersionMenu").trigger("click"),$.ajax({method:"POST",url:$("#app_context").val()+"/api/deleteVersion",data:{kb_id:$(this).parent().attr("id")}}).done(function(e){t.remove(),show_notification("Version removed successfully","success")}).fail(function(e){show_notification(JSON.parse(e.responseText).message,"danger")})}),"true"===$("#frm_editor").val()&&$(window).bind("keydown",function(e){(e.ctrlKey||e.metaKey)&&"s"===String.fromCharCode(e.which).toLowerCase()&&(e.preventDefault(),$("#frm_edit_kb_save").click())}),$("input[class='published_state']").change(function(){$.ajax({method:"POST",url:$("#app_context").val()+"/published_state",data:{id:this.id,state:this.checked}}).done(function(e){show_notification(e,"success")}).fail(function(e){show_notification(e.responseText,"danger")})}),$(document).on("click","#btnUpvote",function(){$.ajax({method:"POST",url:$("#app_context").val()+"/vote",data:{doc_id:$("#doc_id").val(),vote_type:"upvote"}}).done(function(e){show_notification(e,"success",!0)}).fail(function(e){show_notification(e.responseText,"danger")})}),$(document).on("click","#btnDownvote",function(){$.ajax({method:"POST",url:$("#app_context").val()+"/vote",data:{doc_id:$("#doc_id").val(),vote_type:"downvote"}}).done(function(e){show_notification(e,"success",!0)}).fail(function(e){show_notification(e.responseText,"danger")})}),$("#validate_permalink").click(function(){""!==$("#frm_kb_permalink").val()?$.ajax({method:"POST",url:$("#app_context").val()+"/api/validate_permalink",data:{permalink:$("#frm_kb_permalink").val(),doc_id:$("#frm_kb_id").val()}}).done(function(e){show_notification(e,"success")}).fail(function(e){show_notification(e.responseText,"danger")}):show_notification("Please enter a permalink to validate","danger")}),$("#generate_permalink").click(function(){var e=Math.floor(9e5*Math.random())+1e5;$("#frm_kb_permalink").val(e)}),$("#frm_kb_title").change(function(){var e=$(this).val();e&&e.length>5?($("#generate_permalink_from_title").removeClass("disabled"),$("#generate_permalink_from_title").click(function(){var e=$("#frm_kb_title").val();e&&e.length>5&&$("#frm_kb_permalink").val(t(e))})):$("#generate_permalink_from_title").addClass("disabled")}),$("#btn_articles_filter").click(function(){window.location.href=$("#app_context").val()+"/articles/"+encodeURIComponent($("#article_filter").val())}),$("#btn_articles_reset").click(function(){window.location.href=$("#app_context").val()+"/articles"}),$("#btn_search").click(function(e){""===$("#frm_search").val()&&(show_notification("Please enter a search value","danger"),e.preventDefault())}),""!==$("#input_notify_message").val()){var r=$("#input_notify_message").val(),c=$("#input_notify_message_type").val();$("#input_notify_message").val(""),$("#input_notify_message_type").val(""),show_notification(r,c,!1)}});