From b98bf9fc85855405f7264734a90bd66d017428aa Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 6 Feb 2016 16:08:48 +0000 Subject: [PATCH] separate js by files --- assets/js/app.min.js | 2 +- assets/src/js/app.js | 417 +--------------------------------------- assets/src/js/browse.js | 150 +++++++++++++++ assets/src/js/editor.js | 263 +++++++++++++++++++++++++ templates/browse.tmpl | 2 +- 5 files changed, 422 insertions(+), 412 deletions(-) create mode 100644 assets/src/js/browse.js create mode 100644 assets/src/js/editor.js diff --git a/assets/js/app.min.js b/assets/js/app.min.js index d57e253c..0aa1adcc 100644 --- a/assets/js/app.min.js +++ b/assets/js/app.min.js @@ -1 +1 @@ -$(document).ready(function(){$(document).pjax("a[data-pjax]","#content")}),$(document).on("ready pjax:success",function(){function a(a){this.style.height="5px",this.style.height=this.scrollHeight+"px"}if($("textarea").each(a),$("textarea").keyup(a),$(window).resize(function(){$("textarea").each(a)}),$("#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}),$("body").on("click",".delete",function(a){return a.preventDefault(),button=$(this),button.data("file")&&confirm("Are you sure you want to delete 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)}):(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}),$("main").hasClass("browse")&&($(".new").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})),$(".editor")[0]){var b=$("#source-area").data("mode"),c=ace.edit("source-area");c.getSession().setMode("ace/mode/"+b),c.setOptions({wrap:!0,maxLines:1/0,theme:"ace/theme/github",showPrintMargin:!1,fontSize:"1em"}),preview=$("#preview-area"),textarea=$("#content-area"),$("body").on("keypress","input",function(a){return 13==a.keyCode?(a.preventDefault(),$('input[value="Save"]').focus().click(),!1):void 0}),$("form").submit(function(a){a.preventDefault(),$("#preview-area").html("").fadeOut(),$("#preview").data("previewing","false"),$(".CodeMirror").fadeIn(),"undefined"!=typeof c&&c&&c.save();var b=JSON.stringify($(this).serializeJSON()),d=$(this).find("input[type=submit]:focus");return $.ajax({type:"POST",url:window.location,data:b,headers:{"X-Regenerate":d.data("regenerate"),"X-Schedule":d.data("schedule"),"X-Content-Type":d.data("type")},dataType:"json",encode:!0,contentType:"application/json; charset=utf-8"}).done(function(a){notification({text:d.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('
'),blockType="object"),"array"==blockType&&(newID=blockID+"[]",input=blockID,input=input.replace(/\[/,"\\["),input=input.replace(/\]/,"\\]"),block.append('
div").length+'" data-type="array-item">
')),"object"==blockType){if(newItem=$("#"+defaultID),newItem.html(''),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('
'),newItem.prepend(' ')):(type="","array"==elements[1]?type="array":type="object",template='

${elements[0]}

',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}),textarea[0]&&(options={mode:textarea.data("mode"),theme:"ttcn",lineWrapping:!0,lineNumbers:!0,scrollbarStyle:null},"markdown"==textarea.data("mode")&&(options.lineNumbers=!1),c=CodeMirror.fromTextArea(textarea[0],options),codemirror=$(".CodeMirror"),$("#preview").click(function(a){if(a.preventDefault(),"true"==$(this).data("previewing"))preview.hide(),codemirror.fadeIn(),$(this).data("previewing","false"),notification({text:"Think, relax and do the better you can!",type:"information",timeout:2e3});else{c.save();var b=new showdown.Converter,d=textarea.val(),e=b.makeHtml(d);codemirror.hide(),preview.html(e).fadeIn(),$(this).data("previewing","true"),notification({text:"This is how your post looks like.",type:"information",timeout:2e3})}return!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='';break;case"error":b='';break;case"warning":b='';break;case"information":b='';break;default:b=''}var c={template:'
'+b+'
'};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"})}}; \ No newline at end of file +$(document).ready(function(){$(document).pjax("a[data-pjax]","#content")}),$(document).on("ready pjax:success",function(){function a(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)}),$("#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}),$("main").hasClass("browse")&&$(document).trigger("page:browse"),$(".editor")[0]&&$(document).trigger("page:editor"),!1}),$(document).on("page:browse",function(){$("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").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=$("#source-area").data("mode"),b=ace.edit("source-area");b.getSession().setMode("ace/mode/"+a),b.setOptions({wrap:!0,maxLines:1/0,theme:"ace/theme/github",showPrintMargin:!1,fontSize:"1em"}),preview=$("#preview-area"),textarea=$("#content-area"),$("body").on("keypress","input",function(a){return 13==a.keyCode?(a.preventDefault(),$('input[value="Save"]').focus().click(),!1):void 0}),$("form").submit(function(a){a.preventDefault(),$("#preview-area").html("").fadeOut(),$("#preview").data("previewing","false"),$(".CodeMirror").fadeIn(),"undefined"!=typeof b&&b&&b.save();var c=JSON.stringify($(this).serializeJSON()),d=$(this).find("input[type=submit]:focus");return $.ajax({type:"POST",url:window.location,data:c,headers:{"X-Regenerate":d.data("regenerate"),"X-Schedule":d.data("schedule"),"X-Content-Type":d.data("type")},dataType:"json",encode:!0,contentType:"application/json; charset=utf-8"}).done(function(a){notification({text:d.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('
'),blockType="object"),"array"==blockType&&(newID=blockID+"[]",input=blockID,input=input.replace(/\[/,"\\["),input=input.replace(/\]/,"\\]"),block.append('
div").length+'" data-type="array-item">
')),"object"==blockType){if(newItem=$("#"+defaultID),newItem.html(''),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('
'),newItem.prepend(' ')):(type="","array"==elements[1]?type="array":type="object",template='

${elements[0]}

',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}),textarea[0]&&(options={mode:textarea.data("mode"),theme:"ttcn",lineWrapping:!0,lineNumbers:!0,scrollbarStyle:null},"markdown"==textarea.data("mode")&&(options.lineNumbers=!1),b=CodeMirror.fromTextArea(textarea[0],options),codemirror=$(".CodeMirror"),$("#preview").click(function(a){if(a.preventDefault(),"true"==$(this).data("previewing"))preview.hide(),codemirror.fadeIn(),$(this).data("previewing","false"),notification({text:"Think, relax and do the better you can!",type:"information",timeout:2e3});else{b.save();var c=new showdown.Converter,d=textarea.val(),e=c.makeHtml(d);codemirror.hide(),preview.html(e).fadeIn(),$(this).data("previewing","true"),notification({text:"This is how your post looks like.",type:"information",timeout:2e3})}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='';break;case"error":b='';break;case"warning":b='';break;case"information":b='';break;default:b=''}var c={template:'
'+b+'
'};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"})}}; \ No newline at end of file diff --git a/assets/src/js/app.js b/assets/src/js/app.js index 23fce6cc..d25961c2 100644 --- a/assets/src/js/app.js +++ b/assets/src/js/app.js @@ -1,20 +1,19 @@ $(document).ready(function() { - // Start pjax $(document).pjax('a[data-pjax]', '#content'); }); $(document).on('ready pjax:success', function() { + // Update the title + document.title = document.getElementById('site-title').innerHTML; + // Auto Grow Textarea function autoGrow(element) { this.style.height = "5px"; this.style.height = (this.scrollHeight) + "px"; } - // Auto Grow textareas after loading $("textarea").each(autoGrow); - // Auto Grow textareas when changing its content $('textarea').keyup(autoGrow); - // Auto Grow textareas when resizing the window $(window).resize(function() { $("textarea").each(autoGrow); }); @@ -37,415 +36,13 @@ $(document).on('ready pjax:success', function() { return false; }); - // Delete a file or a field in editor - $("body").on('click', '.delete', function(event) { - event.preventDefault(); - button = $(this); - - if (button.data("file") && confirm("Are you sure you want to delete this?")) { - $.ajax({ - type: 'DELETE', - url: button.data("file") - }).done(function(data) { - button.parent().parent().fadeOut(); - notification({ - text: button.data("message"), - type: 'success', - timeout: 5000 - }); - }).fail(function(data) { - notification({ - text: 'Something went wrong.', - type: 'error' - }); - console.log(data); - }); - } else { - 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(); - } - - return false; - }); - if ($('main').hasClass('browse')) { - $('.new').click(function(event) { - event.preventDefault(); - - if ($(this).data("opened")) { - $('#new-file').fadeOut(200); - $(this).data("opened", false); - } else { - $('#new-file').fadeIn(200); - $(this).data("opened", true); - } - - return false; - }); - - $('#new-file').on('keypress', 'input', function(event) { - if (event.keyCode == 13) { - event.preventDefault(); - var value = $(this).val(), - splited = value.split(":"), - filename = "", - archetype = ""; - - if (value == "") { - notification({ - text: "You have to write something. If you want to close the box, click the button again.", - type: 'warning', - timeout: 5000 - }); - - return false; - } else if (splited.length == 1) { - filename = value; - } else if (splited.length == 2) { - filename = splited[0]; - archetype = splited[1]; - } else { - notification({ - text: "Hmm... I don't understand you. Try writing something like 'name[:archetype]'.", - type: 'error' - }); - - return false; - } - - var content = '{"filename": "' + filename + '", "archetype": "' + archetype + '"}'; - - $.ajax({ - type: 'POST', - url: window.location.pathname, - data: content, - dataType: 'json', - encode: true, - }).done(function(data) { - notification({ - text: "File created successfully.", - type: 'success', - timeout: 5000 - }); - - $.pjax({ - url: window.location.pathname.replace("browse", "edit") + filename, - container: '#content' - }) - }).fail(function(data) { - // error types - notification({ - text: 'Something went wrong.', - type: 'error' - }); - console.log(data); - }); - - return false; - } - }); - - $("#upload").click(function(event) { - event.preventDefault(); - $('.actions input[type="file"]').click(); - return false; - }); - - $('input[type="file"]').on('change', function(event) { - event.preventDefault(); - files = event.target.files; - - // Create a formdata object and add the files - var data = new FormData(); - $.each(files, function(key, value) { - data.append(key, value); - }); - - $.ajax({ - url: window.location.pathname, - type: 'POST', - data: data, - cache: false, - dataType: 'json', - headers: { - 'X-Upload': 'true', - }, - processData: false, - contentType: false, - }).done(function(data) { - notification({ - text: "File(s) uploaded successfully.", - type: 'success', - timeout: 5000 - }); - - $.pjax({ - url: window.location.pathname, - container: '#content' - }) - }).fail(function(data) { - notification({ - text: 'Something went wrong.', - type: 'error' - }); - console.log(data); - }); - return false; - }); + $(document).trigger("page:browse"); } - // If it's editor page if ($(".editor")[0]) { - var mode = $("#source-area").data('mode'); - var editor = ace.edit("source-area"); - editor.getSession().setMode("ace/mode/" + mode); - editor.setOptions({ - wrap: true, - maxLines: Infinity, - theme: "ace/theme/github", - showPrintMargin: false, - fontSize: "1em" - }); - - - preview = $("#preview-area"); - textarea = $("#content-area"); - - $('body').on('keypress', 'input', function(event) { - if (event.keyCode == 13) { - event.preventDefault(); - $('input[value="Save"]').focus().click(); - return false; - } - }); - - //TODO: reform this - // Submites any form in the page in JSON format - $('form').submit(function(event) { - event.preventDefault(); - - // Reset preview area and button to make sure it will - // not serialize any form inside the preview - $('#preview-area').html('').fadeOut(); - $('#preview').data("previewing", "false"); - $('.CodeMirror').fadeIn(); - - // Save editor values - if (typeof editor !== 'undefined' && editor) { - editor.save(); - } - - var data = JSON.stringify($(this).serializeJSON()), - button = $(this).find("input[type=submit]:focus"); - - $.ajax({ - type: 'POST', - url: window.location, - data: data, - headers: { - 'X-Regenerate': button.data("regenerate"), - 'X-Schedule': button.data("schedule"), - 'X-Content-Type': button.data("type") - }, - dataType: 'json', - encode: true, - contentType: "application/json; charset=utf-8", - }).done(function(data) { - notification({ - text: button.data("message"), - type: 'success', - timeout: 5000 - }); - }).fail(function(data) { - notification({ - text: 'Something went wrong.', - type: 'error' - }); - console.log(data); - }); - - return false; - }); - - // Adds one more field to the current group - $("body").on('click', '.add', function(event) { - event.preventDefault(); - defaultID = "lorem-ipsum-sin-dolor-amet"; - - if ($("#" + defaultID).length) { - return false; - } - - block = $(this).parent().parent(); - blockType = block.data("type"); - blockID = block.attr("id"); - - // Main add button, after all blocks - if (block.is('div') && block.hasClass("frontmatter")) { - block = $('.blocks'); - block.append('
'); - blockType = "object"; - } - - // If the Block Type is an array - if (blockType == "array") { - newID = blockID + "[]"; - input = blockID; - input = input.replace(/\[/, '\\['); - input = input.replace(/\]/, '\\]'); - block.append('
'); - } - - // If the Block is an object - if (blockType == "object") { - newItem = $("#" + defaultID); - newItem.html(''); - field = $("#name-" + defaultID); - - // Show a notification with some information for newbies - if (!document.cookie.replace(/(?:(?:^|.*;\s*)placeholdertip\s*\=\s*([^;]*).*$)|^.*$/, "$1")) { - var date = new Date(); - date.setDate(date.getDate() + 365); - document.cookie = 'placeholdertip=true; expires=' + date.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(event) { - // When you press enter within the new name field: - if (event.which == 13) { - event.preventDefault(); - // This var should have a value of the type "name[:array, :object]" - value = field.val(); - - if (value == "") { - newItem.remove(); - return false; - } - - elements = value.split(":") - - if (elements.length > 2) { - notification({ - text: "Invalid syntax. It must be 'name[:type]'.", - type: 'error' - }); - return false; - } - - if (elements.length == 2 && elements[1] != "array" && elements[1] != "object") { - notification({ - text: "Only arrays and objects are allowed.", - type: 'error' - }); - return false; - } - - field.remove(); - - // TODO: continue here. :) 04/02/2016 10:30pm - - if (typeof blockID === "undefined") { - blockID = elements[0]; - } else { - blockID = blockID + '[' + elements[0] + ']'; - } - - if (elements.length == 1) { - newItem.attr('id', 'block-' + blockID); - newItem.append('
'); - newItem.prepend(' '); - } else { - type = ""; - - if (elements[1] == "array") { - type = "array"; - } else { - type = "object" - } - - template = "

${elements[0]}

" - 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.'); - } - - return false; - } - }); - } - - return false; - }); - - // If it has a textarea - if (textarea[0]) { - options = { - mode: textarea.data("mode"), - theme: 'ttcn', - lineWrapping: true, - lineNumbers: true, - scrollbarStyle: null - } - - if (textarea.data("mode") == "markdown") { - options.lineNumbers = false - } - - editor = CodeMirror.fromTextArea(textarea[0], options); - codemirror = $('.CodeMirror'); - - // Toggles between preview and editing mode - $("#preview").click(function(event) { - event.preventDefault(); - - // If it currently in the preview mode, hide the preview - // and show the editor - if ($(this).data("previewing") == "true") { - preview.hide(); - codemirror.fadeIn(); - $(this).data("previewing", "false"); - notification({ - text: "Think, relax and do the better you can!", - type: 'information', - timeout: 2000 - }); - } else { - // Copy the editor content to texteare - editor.save() - - // If it's in editing mode, convert the markdown to html - // and show it - var converter = new showdown.Converter(), - text = textarea.val(), - html = converter.makeHtml(text); - - // Hide the editor and show the preview - codemirror.hide(); - preview.html(html).fadeIn(); - - $(this).data("previewing", "true"); - notification({ - text: "This is how your post looks like.", - type: 'information', - timeout: 2000 - }); - } - - return false; - }); - } + $(document).trigger("page:editor"); } + + return false; }); diff --git a/assets/src/js/browse.js b/assets/src/js/browse.js new file mode 100644 index 00000000..6086b263 --- /dev/null +++ b/assets/src/js/browse.js @@ -0,0 +1,150 @@ +$(document).on('page:browse', function() { + $("body").off('click', '.delete').on('click', '.delete', function(event) { + event.preventDefault(); + button = $(this); + + $.ajax({ + type: 'DELETE', + url: button.data("file") + }).done(function(data) { + button.parent().parent().fadeOut(); + notification({ + text: button.data("message"), + type: 'success', + timeout: 5000 + }); + }).fail(function(data) { + notification({ + text: 'Something went wrong.', + type: 'error' + }); + console.log(data); + }); + + return false; + }); + + $('.new').click(function(event) { + event.preventDefault(); + + if ($(this).data("opened")) { + $('#new-file').fadeOut(200); + $(this).data("opened", false); + } else { + $('#new-file').fadeIn(200); + $(this).data("opened", true); + } + + return false; + }); + + $('#new-file').on('keypress', 'input', function(event) { + if (event.keyCode == 13) { + event.preventDefault(); + var value = $(this).val(), + splited = value.split(":"), + filename = "", + archetype = ""; + + if (value == "") { + notification({ + text: "You have to write something. If you want to close the box, click the button again.", + type: 'warning', + timeout: 5000 + }); + + return false; + } else if (splited.length == 1) { + filename = value; + } else if (splited.length == 2) { + filename = splited[0]; + archetype = splited[1]; + } else { + notification({ + text: "Hmm... I don't understand you. Try writing something like 'name[:archetype]'.", + type: 'error' + }); + + return false; + } + + var content = '{"filename": "' + filename + '", "archetype": "' + archetype + '"}'; + + $.ajax({ + type: 'POST', + url: window.location.pathname, + data: content, + dataType: 'json', + encode: true, + }).done(function(data) { + notification({ + text: "File created successfully.", + type: 'success', + timeout: 5000 + }); + + $.pjax({ + url: window.location.pathname.replace("browse", "edit") + filename, + container: '#content' + }) + }).fail(function(data) { + // error types + notification({ + text: 'Something went wrong.', + type: 'error' + }); + console.log(data); + }); + + return false; + } + }); + + $("#upload").click(function(event) { + event.preventDefault(); + $('.actions input[type="file"]').click(); + return false; + }); + + $('input[type="file"]').on('change', function(event) { + event.preventDefault(); + files = event.target.files; + + // Create a formdata object and add the files + var data = new FormData(); + $.each(files, function(key, value) { + data.append(key, value); + }); + + $.ajax({ + url: window.location.pathname, + type: 'POST', + data: data, + cache: false, + dataType: 'json', + headers: { + 'X-Upload': 'true', + }, + processData: false, + contentType: false, + }).done(function(data) { + notification({ + text: "File(s) uploaded successfully.", + type: 'success', + timeout: 5000 + }); + + $.pjax({ + url: window.location.pathname, + container: '#content' + }) + }).fail(function(data) { + notification({ + text: 'Something went wrong.', + type: 'error' + }); + console.log(data); + }); + return false; + }); +}); diff --git a/assets/src/js/editor.js b/assets/src/js/editor.js new file mode 100644 index 00000000..454fcb73 --- /dev/null +++ b/assets/src/js/editor.js @@ -0,0 +1,263 @@ +$(document).on('page:editor', function() { + var mode = $("#source-area").data('mode'); + var editor = ace.edit("source-area"); + editor.getSession().setMode("ace/mode/" + mode); + editor.setOptions({ + wrap: true, + maxLines: Infinity, + theme: "ace/theme/github", + showPrintMargin: false, + fontSize: "1em" + }); + + + preview = $("#preview-area"); + textarea = $("#content-area"); + + $('body').on('keypress', 'input', function(event) { + if (event.keyCode == 13) { + event.preventDefault(); + $('input[value="Save"]').focus().click(); + return false; + } + }); + + //TODO: reform this + // Submites any form in the page in JSON format + $('form').submit(function(event) { + event.preventDefault(); + + // Reset preview area and button to make sure it will + // not serialize any form inside the preview + $('#preview-area').html('').fadeOut(); + $('#preview').data("previewing", "false"); + $('.CodeMirror').fadeIn(); + + // Save editor values + if (typeof editor !== 'undefined' && editor) { + editor.save(); + } + + var data = JSON.stringify($(this).serializeJSON()), + button = $(this).find("input[type=submit]:focus"); + + $.ajax({ + type: 'POST', + url: window.location, + data: data, + headers: { + 'X-Regenerate': button.data("regenerate"), + 'X-Schedule': button.data("schedule"), + 'X-Content-Type': button.data("type") + }, + dataType: 'json', + encode: true, + contentType: "application/json; charset=utf-8", + }).done(function(data) { + notification({ + text: button.data("message"), + type: 'success', + timeout: 5000 + }); + }).fail(function(data) { + notification({ + text: 'Something went wrong.', + type: 'error' + }); + console.log(data); + }); + + return false; + }); + + // Adds one more field to the current group + $("body").on('click', '.add', function(event) { + event.preventDefault(); + defaultID = "lorem-ipsum-sin-dolor-amet"; + + if ($("#" + defaultID).length) { + return false; + } + + block = $(this).parent().parent(); + blockType = block.data("type"); + blockID = block.attr("id"); + + // Main add button, after all blocks + if (block.is('div') && block.hasClass("frontmatter")) { + block = $('.blocks'); + block.append('
'); + blockType = "object"; + } + + // If the Block Type is an array + if (blockType == "array") { + newID = blockID + "[]"; + input = blockID; + input = input.replace(/\[/, '\\['); + input = input.replace(/\]/, '\\]'); + block.append('
'); + } + + // If the Block is an object + if (blockType == "object") { + newItem = $("#" + defaultID); + newItem.html(''); + field = $("#name-" + defaultID); + + // Show a notification with some information for newbies + if (!document.cookie.replace(/(?:(?:^|.*;\s*)placeholdertip\s*\=\s*([^;]*).*$)|^.*$/, "$1")) { + var date = new Date(); + date.setDate(date.getDate() + 365); + document.cookie = 'placeholdertip=true; expires=' + date.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(event) { + // When you press enter within the new name field: + if (event.which == 13) { + event.preventDefault(); + // This var should have a value of the type "name[:array, :object]" + value = field.val(); + + if (value == "") { + newItem.remove(); + return false; + } + + elements = value.split(":") + + if (elements.length > 2) { + notification({ + text: "Invalid syntax. It must be 'name[:type]'.", + type: 'error' + }); + return false; + } + + if (elements.length == 2 && elements[1] != "array" && elements[1] != "object") { + notification({ + text: "Only arrays and objects are allowed.", + type: 'error' + }); + return false; + } + + field.remove(); + + // TODO: continue here. :) 04/02/2016 + + if (typeof blockID === "undefined") { + blockID = elements[0]; + } else { + blockID = blockID + '[' + elements[0] + ']'; + } + + if (elements.length == 1) { + newItem.attr('id', 'block-' + blockID); + newItem.append('
'); + newItem.prepend(' '); + } else { + type = ""; + + if (elements[1] == "array") { + type = "array"; + } else { + type = "object" + } + + template = "

${elements[0]}

" + 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.'); + } + + return false; + } + }); + } + + return false; + }); + + // If it has a textarea + if (textarea[0]) { + options = { + mode: textarea.data("mode"), + theme: 'ttcn', + lineWrapping: true, + lineNumbers: true, + scrollbarStyle: null + } + + if (textarea.data("mode") == "markdown") { + options.lineNumbers = false + } + + editor = CodeMirror.fromTextArea(textarea[0], options); + codemirror = $('.CodeMirror'); + + // Toggles between preview and editing mode + $("#preview").click(function(event) { + event.preventDefault(); + + // If it currently in the preview mode, hide the preview + // and show the editor + if ($(this).data("previewing") == "true") { + preview.hide(); + codemirror.fadeIn(); + $(this).data("previewing", "false"); + notification({ + text: "Think, relax and do the better you can!", + type: 'information', + timeout: 2000 + }); + } else { + // Copy the editor content to texteare + editor.save() + + // If it's in editing mode, convert the markdown to html + // and show it + var converter = new showdown.Converter(), + text = textarea.val(), + html = converter.makeHtml(text); + + // Hide the editor and show the preview + codemirror.hide(); + preview.html(html).fadeIn(); + + $(this).data("previewing", "true"); + notification({ + text: "This is how your post looks like.", + type: 'information', + timeout: 2000 + }); + } + + return false; + }); + } + + $("body").on('click', '.delete', function(event) { + event.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(); + + return false; + }); +}); diff --git a/templates/browse.tmpl b/templates/browse.tmpl index bbd550ec..d5a1dc45 100644 --- a/templates/browse.tmpl +++ b/templates/browse.tmpl @@ -1,7 +1,7 @@ {{ define "content" }}{{ $path := .Path }}
-

{{ $path }}

+

Path: {{ $path }}