Notebook Newpage Code
<html> <head> <title>Notebook Template Newpage Module</title> <meta name="author" content="Timothy Foster" /> <style type="text/css"> @import url('/common--bootstrap/3.0.0/bootstrap.min.css'); @import url('/common--theme/bootstrap-base/css/style.css'); @import url('/local--theme/standard-yellow/style.css'); .hidden{ display: none !important; } </style> <script type="text/javascript"> /** * Notebook Template Newpage Module * By Timothy Foster * Version: 1.00.141004 * Allows the user to create a new notebook subpage of a given category * very easily **************************************************************************/ /** * Grab value of a form given an id * @param __id The id of the element in the document */ function get(__id){ return document.getElementById(__id).value; } /** * Takes a query string and turns it into an associative array * @param query Query string in the form ?key=value&key=value */ function getURLParameters(query){ if(query == "") return {}; var obj = {}; var splitQuery = query.substr(1).split("&"); for(var i = 0; i < splitQuery.length; ++i){ var keyvalue = splitQuery[i].split("="); if(keyvalue.length != 2) continue; obj[keyvalue[0]] = decodeURIComponent(keyvalue[1].replace(/\+/g, " ")); } return obj; } /** * URL Parameters * Wikidot replaces .search, so use hash instead */ var URLParams = getURLParameters(window.location.hash.substr(1)); /** * Initialize the page and variables */ function init(){ setInitialCategory(document.getElementById("notebook-newpage-category")); } /** * Ensures that the inputs are valid * @param category The category name * @param title The title of the page * @return 0 if valid, otherwise return an integer representing the error */ function validate(category, title){ // Category name should begin with a letter // Title must have something in it var category_regex = /^[a-z][a-z0-9\-]*$/; var title_regex = /^.+$/; if(!category_regex.test(category)) return 1; if(!title_regex.test(title)) return 2; return 0; } /** * Function call when the form is submitted */ function submitNNP(){ try{ resetErrors(); var category = get("notebook-newpage-category").toLowerCase(); var title = get("notebook-newpage-title"); var validateValue = validate(category, title); if(validateValue == 0){ // Good input title = encodeURI(title); var redirectURL = "http://" + URLParams["site"] + ".wikidot.com/" + category + ":" + title + "/edit/true/parentPage/" + URLParams["parent"]; redirect(redirectURL); } else if(validateValue == 1){ // Invalid category document.getElementById("notebook-newpage-category-group").classList.add("has-error"); writeError("Invalid category"); } else if(validateValue == 2){ // Invalid title document.getElementById("notebook-newpage-title-group").classList.add("has-error"); writeError("Invalid Title"); } else{ // Unknown error condition writeError("Unknown error"); } return false; } catch(err){ // Terribad writeError(err.toString()); return false; } } /** * Sets the initial category for the category field * @param field The HTML field to populate */ function setInitialCategory(field){ // We search for a tag of the form _c:CATEGORY var category_regex = /_c:[a-z0-9\-]+/; var category = category_regex.exec(URLParams["tags"]); if(category === null) category = URLParams["category"]; else{ // Remove the _c: category = category[0].substr(3); } field.value = category; } /** * Send the user to a different location * @param url The location to send the user to */ function redirect(url){ window.parent.location = url; } /** * Reset all error messages so the form appears normal */ function resetErrors(){ document.getElementById("notebook-newpage-error-group").classList.add("hidden"); document.getElementById("notebook-newpage-category-group").classList.remove("has-error"); document.getElementById("notebook-newpage-title-group").classList.remove("has-error"); } /** * Writes the message to the screen * @param message The error message for debugging purposes */ function writeError(message){ document.getElementById("notebook-newpage-error-group").classList.remove("hidden"); document.getElementById("notebook-newpage-error-label").innerHTML = message; } </script> </head> <body id="notebook-newpage-body" onload="init()"> <form id="notebook-newpage-form" onsubmit="return submitNNP()" role="form"> <div id="notebook-newpage-error-group" class="form-group has-error hidden"> <label id="notebook-newpage-error-label" class="control-label"></label> </div> <div id="notebook-newpage-category-group" class="form-group"> <label for="notebook-newpage-category" class="control-label">Category</label> <input type="text" class="form-control" id="notebook-newpage-category" /> </div> <div id="notebook-newpage-title-group" class="form-group"> <label for="notebook-newpage-title" class="control-label">Page Title</label> <input type="text" class="form-control" id="notebook-newpage-title" placeholder="Page Title" /> </div> <button type="submit" id="notebook-newpage-submit-button" class="btn btn-primary">Create</button> </form> </body> </html>