Sfoglia il codice sorgente

Finished Memo module

Toby Chui 3 anni fa
parent
commit
98341388c2

+ 9 - 0
web/Memo/backend/addMemo.js

@@ -0,0 +1,9 @@
+//Require paramter: memo (JSON string)
+
+newDBTableIfNotExists("Memo");
+var succ = writeDBItem("Memo",Date.now(),memo);
+if (succ == false){
+    sendJSONResp(JSON.stringify({"error":"Write to database failed"}));
+}else{
+    sendJSONResp(JSON.stringify("OK"));
+}

+ 0 - 14
web/Memo/backend/dbattack_test.js

@@ -1,14 +0,0 @@
-console.log("Try to attack auth database");
-
-//Try to reate an user admin with password "admin"
-var x = writeDBItem("auth","admin","c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec")
-
-//Should return false
-console.log(x)
-
-var y = dropDBTable("auth");
-
-//Should return false
-console.log(y)
-
-sendResp("Attack Test Done");

+ 2 - 1
web/Memo/backend/dblist.js

@@ -1,4 +1,5 @@
-console.log("Testing Database Listing API");
+
+onsole.log("Testing Database Listing API");
 if (newDBTableIfNotExists("testdb")){
 	writeDBItem("testdb","One","Hello World")
 	writeDBItem("testdb","Two","This is a text message")

+ 0 - 2
web/Memo/backend/error.js

@@ -1,2 +0,0 @@
-console.log("This will raise an error");
-notExistsFunction("test");

+ 0 - 32
web/Memo/backend/execpkg.js

@@ -1,32 +0,0 @@
-//Use ffmpeg to convert a file named test.mp4 on desktop
-console.log("Demo for converting a test.mp4 on Desktop to test.mp3");
-var srcVirtual = "user:/Desktop/test.mp4";
-
-//Helper function to get the filepath.Dir of the realpath
-function dir(filepath){
-	filepath = filepath.split("/");
-	filepath.pop();
-	return filepath.join("/");
-}
-
-//Require ffmpeg package
-if (requirepkg("ffmpeg",true)){
-	//Package required. Get the real path of the file
-	var srcReal = decodeVirtualPath(srcVirtual);
-	srcReal = srcReal.split("\\").join("/");
-	console.log("File real path: " + srcReal);
-	
-	//Generate the destination filepath (real)
-	var destReal = dir(srcReal) + "/test.mp3";
-	console.log("Output file path: " + destReal);
-	
-	//Convert the real path to the designed path
-	//If you want to include filepath with space, you must use " instead of '
-	var results = execpkg("ffmpeg",'-i "' + srcReal + '" "' + destReal + '"');
-	
-	//Send the CMD output as text to response
-	sendResp(results);
-}else{
-	sendResp("Failed to require package ffmpeg");
-}
-

+ 0 - 4
web/Memo/backend/filelib.aglob.js

@@ -1,4 +0,0 @@
-console.log("Testing File Glob");
-requirelib("filelib");
-var fileList = filelib.aglob("user:/Desktop/*.mp4");
-sendJSONResp(JSON.stringify(fileList));

+ 0 - 9
web/Memo/backend/filelib.file.js

@@ -1,9 +0,0 @@
-console.log("File Read Write Test");
-requirelib("filelib");
-if (filelib.writeFile("user:/Desktop/test.txt","Hello World! This is a testing message to write")){
-	//Write file succeed.
-	var fileContent = filelib.readFile("user:/Desktop/test.txt");
-	sendResp("File content: " + fileContent);
-}else{
-	SendResp("Failed to write file");
-}

+ 0 - 7
web/Memo/backend/filelib.fileExists.js

@@ -1,7 +0,0 @@
-console.log("Check if file exists");
-requirelib("filelib");
-if (filelib.fileExists("user:/Desktop/test.txt")){
-	sendResp("File Exists");
-}else{
-	sendResp("File Not Exists");
-}

+ 0 - 24
web/Memo/backend/filelib.filesize.js

@@ -1,24 +0,0 @@
-console.log('Testing get filesize');
-requirelib("filelib");
-//Help function for converting byte to human readable format
-function bytesToSize(bytes) {
-   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
-   if (bytes == 0) return '0 Byte';
-   var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
-   return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
-}
-
-
-//Get all the files filesize on desktop
-var fileList = filelib.glob("user:/Desktop/*.*");
-var results = [];
-for (var i =0; i < fileList.length; i++){
-	var filename = fileList[i].split("/").pop();
-	var fileSize = filelib.filesize(fileList[i]);
-	results.push({
-		filename: filename,
-		filesize: bytesToSize(fileSize)
-	});
-	
-}
-sendJSONResp(JSON.stringify(results));

+ 0 - 4
web/Memo/backend/filelib.glob.js

@@ -1,4 +0,0 @@
-console.log("Testing File Glob");
-requirelib("filelib");
-var fileList = filelib.glob("user:/Desktop/*.mp3");
-sendJSONResp(JSON.stringify(fileList));

+ 0 - 7
web/Memo/backend/filelib.md5.js

@@ -1,7 +0,0 @@
-var loaded = requirelib("filelib");
-if (!loaded) {
-    console.log("Failed to load lib imagelib, terminated.");
-}
-
-sendJSONResp(JSON.stringify(filelib.md5("user:/Desktop/test.jpeg")))
-    //will return md5 hash or false

+ 0 - 17
web/Memo/backend/filelib.mkdir.js

@@ -1,17 +0,0 @@
-//This script will create a folder on your desktop named "Hello World"
-console.log("Create Folder Test");
-var srcPath = "user:/Desktop/Hello World";
-
-//Require the image library
-var loaded = requirelib("filelib");
-if (loaded) {
-    //Library loaded. Call to the functions
-    var success = filelib.mkdir(srcPath);
-	if (success){
-		sendResp("OK")
-	}else{
-		sendResp("Failed to resize image");
-	}
-} else {
-    console.log("Failed to load lib: filelib");
-}

+ 0 - 4
web/Memo/backend/filelib.readDir.js

@@ -1,4 +0,0 @@
-console.log("ListDir Testing");
-requirelib("filelib");
-//This function only shows all directory within this dir
-sendJSONResp(JSON.stringify(filelib.readdir("user:/Desktop/*")))

+ 0 - 2
web/Memo/backend/getLoadedModules.js

@@ -1,2 +0,0 @@
-console.log("List all loaded modules and its launch info");
-sendJSONResp(JSON.stringify(LOADED_MODULES));

+ 0 - 16
web/Memo/backend/getParamters.js

@@ -1,16 +0,0 @@
-console.log("Get argument test");
-sendJSONResp(JSON.stringify([foo,bar]));
-/*
-//The following paramters can be passed in using POST paramters as follow
-function testRunScript(){
-	var script = "Dummy/backend/getParamters.js";
-	$.ajax({
-		url: "../system/ajgi/interface?script=" + script,
-		data: {foo: "Hello", bar: "World"},
-		method: "POST",
-		success: function(data){
-			console.log(data);
-		}
-	})
-}
-*/

+ 0 - 12
web/Memo/backend/getStorageDevices.js

@@ -1,12 +0,0 @@
-//This script demonstrate the iteration over storage devices
-console.log("Storage Device Setting List");
-var html = "";
-for (var i = 0; i < LOADED_STORAGES.length; i++){
-	var thisStorage = LOADED_STORAGES[i];
-	html = html + "Name=" + thisStorage.Name + "<br>UUID=" + thisStorage.Uuid + "<br>Path=" + thisStorage.Path + "<br><br>";
-}
-
-//Set Response header to html
-HTTP_HEADER = "text/html; charset=utf-8";
-//Send Response
-sendResp(html);

+ 0 - 13
web/Memo/backend/imagelib.imageDimension.js

@@ -1,13 +0,0 @@
-console.log("Image Properties Access Test");
-//To test this, put a test.jpg on your desktop
-var imagePath = "user:/Desktop/test.jpeg";
-
-//Require the image library
-var loaded = requirelib("imagelib");
-if (loaded) {
-    //Library loaded. Call to the functions
-    var dimension = imagelib.getImageDimension(imagePath);
-    sendJSONResp(JSON.stringify(dimension));
-} else {
-    console.log("Failed to load lib: imagelib");
-}

+ 0 - 18
web/Memo/backend/imagelib.imageResize.js

@@ -1,18 +0,0 @@
-console.log("Image Resizing Test");
-//To test this, put a test.jpg on your desktop
-var srcPath = "user:/Desktop/test.jpg";
-var destPath = "user:/Desktop/output.jpg";
-
-//Require the image library
-var loaded = requirelib("imagelib");
-if (loaded) {
-    //Library loaded. Call to the functions
-    var success = imagelib.resizeImage(srcPath, destPath, 200, 0);
-	if (success){
-		sendResp("OK")
-	}else{
-		sendResp("Failed to resize image");
-	}
-} else {
-    console.log("Failed to load lib: imagelib");
-}

+ 0 - 13
web/Memo/backend/jsonPost.js

@@ -1,13 +0,0 @@
-//For those shitty persons who use appplication/json instead of x-www-encoded
-//POST_data is constant and won't change
-sendJSONResp(JSON.stringify(POST_data));
-/*
-$.ajax({
-    type: 'POST',
-    url: '/form/',
-    data: '{"name":"jonas"}', // or JSON.stringify ({name: 'jonas'}),
-    success: function(data) { alert('data: ' + data); },
-    contentType: "application/json",
-    dataType: 'json'
-});
-*/

+ 8 - 0
web/Memo/backend/listmemo.js

@@ -0,0 +1,8 @@
+
+function main(){
+    newDBTableIfNotExists("Memo");
+    var entries = listDBTable("Memo");
+	sendJSONResp(JSON.stringify(entries));
+}
+
+main();

+ 0 - 1
web/Memo/backend/modulelist.js

@@ -1 +0,0 @@
-sendJSONResp(JSON.stringify(LOADED_MODULES));

+ 0 - 7
web/Memo/backend/permission.js

@@ -1,7 +0,0 @@
-console.log("User Permission Checking");
-var permissionGroup = getUserPermissionGroup();
-if (userIsAdmin() == true){
-	sendResp("This user is admin with group = " + permissionGroup);
-}else{
-	sendResp("This user not admin with group = " + permissionGroup);
-}

+ 3 - 0
web/Memo/backend/removeMemo.js

@@ -0,0 +1,3 @@
+//Require paramter: memoid
+deleteDBItem("Memo", memoid);
+sendJSONResp(JSON.stringify("OK"));

+ 0 - 4
web/Memo/backend/require_pkg.js

@@ -1,4 +0,0 @@
-requirepkg("ffmpeg",true)
-sendResp("FFMPEG installed");
-
-

+ 0 - 6
web/Memo/backend/require_pkg_windows.js

@@ -1,6 +0,0 @@
-//Run this on windows, you can switch the package name to something exist
-//In your PATH environment varaibles
-requirepkg("asdasd",true)
-sendResp("You should see not found in Windows %PATH% error on terminal log");
-
-

+ 0 - 5
web/Memo/backend/vpath_test.js

@@ -1,5 +0,0 @@
-console.log("Testing Multiline Javascript");
-function getVirtualPath(path){
-	return decodeVirtualPath(path);
-}
-sendJSONResp(JSON.stringify(getVirtualPath("user:/Desktop").split("/")));

+ 131 - 3
web/Memo/index.html

@@ -11,6 +11,7 @@
         <script src="../script/ao_module.js"></script>
         <script src="script/lightpick/moment.min.js"></script>
         <script src="script/lightpick/lightpick.js"></script>
+        <link rel="icon" type="image/png" href="img/small_icon.png"/>
         <title>Memo</title>
         <style>
             body{
@@ -28,7 +29,7 @@
         </nav>
         <div class="box">
             <button class="button is-warning" onclick="openMemoBox();">New Memo</button>
-            <button class="button is-link is-light">Refresh</button>
+            <button class="button is-link is-light" onclick="listAllMemo();">Refresh</button>
         </div>
         <div id="memobox" class="box" style="display:none;">
             <h2 class="title is-4">New Memo</h2>
@@ -60,12 +61,21 @@
                     <button class="button is-link is-light" onclick="cancelNewMemo();">Cancel</button>
                 </div>
             </div>
+        </div>
+        <div id="memolist">
+
         </div>
         <script>
             let deadline = 0;
+            let memoBuf = {};
+            let removeMemoAfterSubmit = false;
+            let currentEditingMemoID = 0;
 
             function openMemoBox(){
                 deadline = 0;
+                removeMemoAfterSubmit = false;
+                $("#title").val("");
+                $("#message").val("");
                 $("#deadline").val("");
                 $("#memobox").slideToggle("fast");
             }
@@ -82,8 +92,106 @@
                 }
             });
 
+            function timestampToDateString(key){
+                return new Date(parseInt(key)).toLocaleString(undefined, {year: 'numeric', month: '2-digit', day: '2-digit', weekday:"long", hour: '2-digit', hour12: false, minute:'2-digit', second:'2-digit'})
+            }
+            
+            function listAllMemo(){
+                $("#memolist").html("");
+                memoBuf = {};
+                ao_module_agirun("Memo/backend/listmemo.js", {}, function(data){
+                    let memos = [];
+                    for (var [key, value] of Object.entries(data)) {
+                        let content = JSON.parse(value);
+                        console.log(key, content);
+                        memoBuf[key] =  content;
+                        memos.push([key, content]);
+                    }
+
+                    memos.reverse();
+                    memos.forEach(function(memo){
+                        let key = memo[0];
+                        let content = memo[1];
+                        let deadline = timestampToDateString(content.deadline * 1000);
+                        if (content.deadline == 0){
+                            deadline = "No Deadline";
+                        }
+                        $("#memolist").append(`<div class="card">
+                            <div class="card-content">
+                                <div class="media">
+                                <div class="media-left">
+                                    <figure class="image is-48x48">
+                                    <img src="img/small_icon.png">
+                                    </figure>
+                                </div>
+                                <div class="media-content">
+                                    <p class="title is-4">${content.title}</p>
+                                    <p class="subtitle is-6">${deadline}</p>
+                                </div>
+                                </div>
 
-            function createNewMemo(){
+                                <div class="content">
+                                ${content.message}
+                                <br>
+                                <time>Creation Date: ${timestampToDateString(key)}</time>
+                                </div>
+                            </div>
+                            <footer class="card-footer">
+                                <a href="#" class="card-footer-item" memo="${key}" onclick="moveToTop(this)">Move to Top</a>
+                                <a href="#" class="card-footer-item" memo="${key}" onclick="editMemo(this);">Edit</a>
+                                <a href="#" class="card-footer-item" memo="${key}" onclick="removeMemoByButton(this);">Delete</a>
+                            </footer>
+                            </div><br>`);
+                    });
+                })
+            }
+
+            listAllMemo();
+
+            function editMemo(btn){
+                let memoID = $(btn).attr("memo");
+                loadMemoContentIntoNewMemoField(memoBuf[memoID]);
+                $("#memobox").slideDown("fast");
+                removeMemoAfterSubmit = true;
+                currentEditingMemoID = memoID;
+            }
+
+            function loadMemoContentIntoNewMemoField(content){
+                $("#title").val(content.title);
+                $("#message").val(content.message);
+                if (content.deadline > 0){
+                    $("#deadline").val(new Date(content.deadline * 1000).toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}));
+                }
+                deadline = content.deadline;
+            }
+
+            function moveToTop(btn){
+                let memoID = $(btn).attr("memo");
+                loadMemoContentIntoNewMemoField(memoBuf[memoID]);
+                createNewMemo(function(){
+                    removeMemo(memoID);
+                });
+                
+            }
+
+            function removeMemoByButton(btn){
+                let memoID = $(btn).attr("memo");
+                removeMemo(memoID);
+            }
+
+            function removeMemo(memoID, callback){
+                ao_module_agirun("Memo/backend/removeMemo.js", {
+                    "memoid": memoID
+                }, function(data){
+                   console.log(data);
+                   listAllMemo();
+                   if (callback){
+                        callback();
+                   }
+                });
+            }
+
+            function createNewMemo(callback=undefined){
                 //Create AJAX request
                 let payload = {
                     "title": $("#title").val().replace(/<[^>]*>?/gm, ''),
@@ -92,7 +200,27 @@
                 }
 
                 console.log(payload);
-                $("#memobox").slideUp("fast");
+                ao_module_agirun("Memo/backend/addMemo.js", {
+                    "memo": JSON.stringify(payload)
+                }, function(data){
+                    if (data.error == undefined){
+                        $("#memobox").slideUp("fast");
+                    }
+
+                    if (removeMemoAfterSubmit){
+                        removeMemoAfterSubmit = false;
+                        removeMemo(currentEditingMemoID, callback);
+                        currentEditingMemoID = 0;
+                    }else{
+                        if (callback){
+                            callback();
+                        }else{
+                            listAllMemo();
+                        }
+                    }
+                   
+                })
+                
             }
 
             

BIN
web/Paint/img/baseline_brush_black_48dp.png


BIN
web/Paint/img/small_icon.png


BIN
web/Paint/img/small_icon.psd