Parcourir la source

tab functions

master
RENOX il y a 5 mois
Parent
révision
2cfe109ffb
5 fichiers modifiés avec 98 ajouts et 46 suppressions
  1. +10
    -4
      src/main/java/planetrenox/controller/FormController.java
  2. +13
    -7
      src/main/java/planetrenox/controller/RequestValidation.java
  3. +2
    -4
      src/main/resources/static/index.html
  4. +1
    -1
      src/main/resources/static/js/filmgrain.js
  5. +72
    -30
      src/main/resources/static/js/main.js

+ 10
- 4
src/main/java/planetrenox/controller/FormController.java Voir le fichier

@@ -56,10 +56,10 @@ public class FormController
* "DOCUMENT_TYPE": "SAVED"
* "DOCUMENT_TYPE": "DECRYPT_FAILURE"
*/
@PostMapping(path = "/", params = {"name", "password", "text"})
public Map<String, Object> save(@RequestParam("name") String name, @RequestParam("password") String password, @RequestParam("text") String text)
@PostMapping(path = "/", params = {"name", "password", "text", "tabs"})
public Map<String, Object> save(@RequestParam("name") String name, @RequestParam("password") String password, @RequestParam("text") String text, @RequestParam("tabs") int tabs)
{
Map<String, Object> saveDocument = RequestValidation.validateSave(name, password, text);
Map<String, Object> saveDocument = RequestValidation.validateSave(name, password, text, tabs);
if ((boolean) saveDocument.get("INVALID")) return saveDocument; // Validation Failed
else // Validation Successful
{
@@ -70,7 +70,10 @@ public class FormController
if (dungeon == null) // New User
{
saveDocument.put("DOCUMENT_TYPE", "SAVED");
dungeon = new Dungeon(name, AES256_GCM.encrypt(text, password), null);
dungeon = new Dungeon(name, AES256_GCM.encrypt(text, password), new java.util.HashMap<>(Map.of(
"TAB_COUNT", Integer.toString(tabs),
"TYPE", "FREE"
)));
repository.insert(dungeon);
return saveDocument;
} else // Existing Dungeon
@@ -82,6 +85,9 @@ public class FormController
{
saveDocument.put("DOCUMENT_TYPE", "SAVED");
dungeon.setEncryptionData(AES256_GCM.encrypt(text, password));
Map<String, String> meta = dungeon.getMeta();
meta.put("TAB_COUNT", Integer.toString(tabs));
dungeon.setMeta(meta);
repository.save(dungeon);
}
return saveDocument;


+ 13
- 7
src/main/java/planetrenox/controller/RequestValidation.java Voir le fichier

@@ -30,17 +30,17 @@ public class RequestValidation
loginDocument.put("INVALID", true);
loginDocument.put("NAME_LENGTH_OVER60", true);
}
if(password.length()>128)
else if(password.length()>128)
{
loginDocument.put("INVALID", true);
loginDocument.put("PASS_LENGTH_OVER128", true);
}
if (name.equals("") || password.equals(""))
else if (name.equals("") || password.equals(""))
{
loginDocument.put("INVALID", true);
loginDocument.put("EMPTY", true);
}
if (ILLEGAL_NAME_PATTERN.matcher(name).find())
else if (ILLEGAL_NAME_PATTERN.matcher(name).find())
{
loginDocument.put("INVALID", true);
loginDocument.put("ILLEGAL_CHAR", true);
@@ -49,7 +49,7 @@ public class RequestValidation
return loginDocument;
}

public static Map<String, Object> validateSave(String name, String password, String text)
public static Map<String, Object> validateSave(String name, String password, String text, int tabs)
{
Map<String, Object> saveDocument = new java.util.HashMap<>(Map.of(
"DOCUMENT_TYPE", "VALIDATION_FAILED",
@@ -57,6 +57,7 @@ public class RequestValidation
"EMPTY", false,
"NAME_LENGTH_OVER60", false,
"PASS_LENGTH_OVER128", false,
"ILLEGAL_TABS", false,
"ILLEGAL_CHAR", false,
"CLEAN_NAME", "",
"CLEAN_TEXT", ""
@@ -70,21 +71,26 @@ public class RequestValidation
saveDocument.put("INVALID", true);
saveDocument.put("NAME_LENGTH_OVER60", true);
}
if(password.length()>128)
else if(password.length()>128)
{
saveDocument.put("INVALID", true);
saveDocument.put("PASS_LENGTH_OVER128", true);
}
if (name.equals("") || password.equals("") || text.equals(""))
else if (name.equals("") || password.equals("") || text.equals(""))
{
saveDocument.put("INVALID", true);
saveDocument.put("EMPTY", true);
}
if (ILLEGAL_NAME_PATTERN.matcher(name).find())
else if (ILLEGAL_NAME_PATTERN.matcher(name).find())
{
saveDocument.put("INVALID", true);
saveDocument.put("ILLEGAL_CHAR", true);
}
else if (tabs < 1 || tabs > 50)
{
saveDocument.put("INVALID", true);
saveDocument.put("ILLEGAL_TABS", true);
}

return saveDocument;
}

+ 2
- 4
src/main/resources/static/index.html Voir le fichier

@@ -110,7 +110,7 @@
<!-- form segment -->
<div class="ui form">
<div class="field">
<textarea id="textarea" style="background-color: whitesmoke; color: black;border: none;"></textarea>
<textarea spellcheck="false" id="textarea" style="background-color: whitesmoke; color: black;border: none;"></textarea>
</div>
</div>
</div>
@@ -118,9 +118,7 @@

</div>

<input type="hidden" id="tabs" value="1">

<script src="js/post.js"></script>
<script src="js/main.js"></script>
<script src="js/filmgrain.js"></script>
</body>


+ 1
- 1
src/main/resources/static/js/filmgrain.js Voir le fichier

@@ -9,7 +9,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
*/

var viewWidth,
viewHeight,


src/main/resources/static/js/post.js → src/main/resources/static/js/main.js Voir le fichier

@@ -1,7 +1,13 @@
"use strict";


/* BEGIN KEYBOARD EVENTS */
var currentTabID;
var tabContent;
var onScreenTabCount;


/* BEGIN EVENTS */

document.getElementById("name").addEventListener("keypress", function (e) {
if (e.key === 'Enter') {
initValidateLogin();
@@ -13,10 +19,16 @@ document.getElementById("password").addEventListener("keypress", function (e) {
initValidateLogin();
}
});

document.getElementById("tab1").addEventListener("click", function (e) {
initSelectTab(e);
});

/* END KEYBOARD EVENTS */


/* BEGIN INIT METHODS ACCESSIBLE BY DOCUMENT */

function initValidateLogin() {
let name = document.getElementById("name").value;
let password = document.getElementById("password").value;
@@ -44,11 +56,22 @@ function initValidateLogin() {
}
}

function initValidateSave() {
function initValidateSave() { //TODO what if name is taken
let name = document.getElementById("name-form").value;
let text = document.getElementById("textarea").value;
let password;
if (document.getElementById("password-form").value === "") // existing user
let tabCount = 0;
let text = "";
let empty = true;
tabContent.set(currentTabID, document.getElementById("textarea").value);
tabContent.forEach(combineTabsLocalEvent);
function combineTabsLocalEvent(values){
if (values !== "" && values != null){
tabCount++;
empty = false;
text += "|textdungeon-tab" + tabCount + "|" + values + "|textdungeon-end|";
}
}
if (document.getElementById("password-form").value === "") // logged in user
password = sessionStorage.getItem(name);
else
password = document.getElementById("password-form").value;
@@ -56,8 +79,8 @@ function initValidateSave() {
blinkUtil(document.getElementById("name-form"), .1, 6, "whitesmoke", "#ff1a1a");
} else if (password === "" || password === null) {
blinkUtil(document.getElementById("password-form"), .1, 6, "whitesmoke", "#ff1a1a");
} else if (text === "" || text === null) {
document.getElementById("textarea").placeholder = "Cannot save an empty form.";
} else if (empty) {
document.getElementById("textarea").placeholder = "Cannot save an empty dungeon.";
} else if (name.length > 60) {
blinkUtil(document.getElementById("name-form"), .1, 6, "whitesmoke", "#ff1a1a");
document.getElementById("name-form").value = "";
@@ -72,7 +95,7 @@ function initValidateSave() {
document.getElementById("name-form").placeholder = "Avoid special characters.";
} else {
sessionStorage.setItem(name, password);
organizeSaveLocalEvent(name, password, text);
saveLocalEvent(name, password, text, tabCount);
}
}

@@ -91,6 +114,10 @@ function initEmptyForm() {
document.getElementById("textarea").rows = window.innerHeight / 24;
document.getElementById("page-form-menu").style.display = "inherit";
document.getElementById("page-form").style.display = "inherit";
onScreenTabCount = 1;
currentTabID = "tab1";
tabContent = new Map();
tabContent.set("tab1", "");
}

function initLogout() {
@@ -101,6 +128,7 @@ function initLogout() {
document.getElementById("page-form").style.display = "none";
document.getElementById("div-logo").style.display = "inherit";
document.getElementById("div-login-parent").style.display = "inherit";
document.querySelectorAll('.generated-tab').forEach(e => e.remove());
}

function initAboutPage() {
@@ -120,20 +148,31 @@ function initDelete() {
}

function initAddTab() {
// TODO 10 charlength - 2 free
document.getElementById("tabs").value = parseInt(document.getElementById("tabs").value) + 1;
let tab = "<a id=\"tab" + document.getElementById("tabs").value + "\" class=\"item\" style=\"border: none;background-color: #252628;display: inherit;\">Tab</a>";
onScreenTabCount++;
tabContent.set("tab" + onScreenTabCount, "");
let tab = "<a id=\"tab" + onScreenTabCount + "\" class=\"item generated-tab\" style=\"border: none;background-color: #252628;display: inherit;\">Tab</a>";
document.getElementById('div-helper-tabs').insertAdjacentHTML('beforebegin', tab);
var newTab = document.getElementById("tab" + document.getElementById("tabs").value);
var newTab = document.getElementById("tab" + onScreenTabCount);
newTab.addEventListener("click", function (e) {
selectTabLocalEvent(e);
initSelectTab(e);
});
}

function initSelectTab(e) {
tabContent.set(currentTabID, document.getElementById("textarea").value);
document.getElementById(currentTabID).classList.remove("active");
document.getElementById(currentTabID).style.backgroundColor = "#252628";
document.getElementById(e.target.id).classList.add("active");
document.getElementById(e.target.id).style.backgroundColor = "#3d3e3f";
document.getElementById("textarea").value = tabContent.get(e.target.id);
currentTabID = e.target.id;
}

/* END INIT METHODS ACCESSIBLE BY DOCUMENT */


/* BEGIN LOCAL EVENTS */

function loginLocalEvent(name, password) {
let xmlrequest = new XMLHttpRequest();
xmlrequest.open("POST", "/");
@@ -145,14 +184,30 @@ function loginLocalEvent(name, password) {
}
}

function saveLocalEvent(name, password, text) {
function saveLocalEvent(name, password, text, tabCount) {
let xmlrequest = new XMLHttpRequest();
xmlrequest.open("POST", "/");
xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlrequest.send("name=" + name + "&password=" + password + "&text=" + text); // not prod
xmlrequest.onload = function () // not prod
xmlrequest.send("name=" + name + "&password=" + password + "&text=" + text + "&tabs=" + tabCount);
xmlrequest.onload = function ()
{
console.log(xmlrequest.response);// not prod
saveResponseLocalEvent(xmlrequest.response);
}
}

function saveResponseLocalEvent(response) {
// TODO NOTIFY
let responseJson = JSON.parse(response);
if (responseJson.DOCUMENT_TYPE==="DECRYPT_FAILURE"){
blinkUtil(document.getElementById("name-form"), .1, 6, "whitesmoke", "#ff1a1a");
document.getElementById("name-form").value = "";
document.getElementById("name-form").placeholder = "Existing name.";
}else {
document.getElementById("name-form").disabled = true;
document.getElementById("password-form").style.display = "none";
document.getElementById("btn-logout").style.display = "inherit";
document.getElementById("btn-settings").style.display = "inherit";
}
}

@@ -191,7 +246,7 @@ function loginResponseLocalEvent(response) {
}

function populateFormLocalEvent(responseJson) {
// TODO 10 charlength title
document.getElementById("name-form").value = responseJson.DUNGEON.name;
document.getElementById("name-form").disabled = "true";
document.getElementById("password-form").style.display = "none";
@@ -201,24 +256,11 @@ function populateFormLocalEvent(responseJson) {
// TODO Tab Information
}

function organizeSaveLocalEvent(name, password, text) {
// TODO Organize Tabs
saveLocalEvent(name, password, text);
document.getElementById("name-form").disabled = true;
document.getElementById("password-form").style.display = "none";
document.getElementById("btn-logout").style.display = "inherit";
document.getElementById("btn-settings").style.display = "inherit";
// TODO Notify
}

function selectTabLocalEvent(e) { //TODO also pass curr selected tab

}

/* END LOCAL EVENTS */


/* BEGIN UTILITIES */

function blinkUtil(el, freq, rounds, color1, color2) {
let state = false;
let currRound = 0;

Chargement…
Annuler
Enregistrer