From cfd948bd5bc4145a25ffc6190c296993d7ad2d73 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 11 Jun 2017 00:19:59 -0400 Subject: [PATCH] creating new note, moving note from tree and into tree --- app.py | 64 ++++++++++++++++++++++++++++++- app.pyc | Bin 5041 -> 7460 bytes demo.ncdb | Bin 1015808 -> 1015808 bytes frontend/index.html | 91 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 136 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index 59e5f218d..2768406d5 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,10 @@ import base64 from flask import Flask, request, send_from_directory from flask_restful import Resource, Api from flask_cors import CORS +import time +import math +import random +import string def dict_factory(cursor, row): d = {} @@ -31,7 +35,8 @@ def insert(tablename, rec): keys = ','.join(rec.keys()) question_marks = ','.join(list('?'*len(rec))) values = tuple(rec.values()) - execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values) + cursor = execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values) + return cursor.lastrowid def delete(tablename, note_id): execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id]) @@ -39,6 +44,7 @@ def delete(tablename, note_id): def execute(sql, params=[]): cursor = conn.cursor() cursor.execute(sql, params) + return cursor def getResults(sql, params=[]): cursor = conn.cursor() @@ -95,6 +101,62 @@ class Notes(Resource): api.add_resource(Notes, '/notes/') +class NotesChildren(Resource): + def post(self, parent_note_id): + note = request.get_json(force=True) + + noteId = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(22)) + + now = math.floor(time.time()); + + insert("notes", { + 'note_id': noteId, + 'note_title': note['note_title'], + 'note_text': '', + 'note_clone_id': '', + 'date_created': now, + 'date_modified': now, + 'icon_info': 'pencil', + 'is_finished': 0 + }) + + if parent_note_id == "root": + parent_note_id = "" + + insert("notes_tree", { + 'note_id': noteId, + 'note_pid': parent_note_id, + 'note_pos': 0, + 'is_expanded': 0 + }) + + conn.commit() + + return { + 'note_id': noteId + } + +api.add_resource(NotesChildren, '/notes//children') + +class MoveAfterNote(Resource): + def put(self, note_id, after_note_id): + after_note = getSingleResult("select * from notes_tree where note_id = ?", [after_note_id]) + + if after_note <> None: + execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [after_note['note_pid'], after_note['note_pos'] + 1, note_id]) + + conn.commit() + +api.add_resource(MoveAfterNote, '/notes//moveAfter/') + +class MoveToNote(Resource): + def put(self, note_id, parent_id): + execute("update notes_tree set note_pid = ? where note_id = ?", [parent_id, note_id]) + + conn.commit() + +api.add_resource(MoveToNote, '/notes//moveTo/') + class Tree(Resource): def get(self): notes = getResults("select notes_tree.*, notes.note_title from notes_tree join notes on notes.note_id = notes_tree.note_id order by note_pid, note_pos") diff --git a/app.pyc b/app.pyc index 0eb1fc716ac5161f9471654ca2a048ea824bf035..a62df2ebc6c630c442626240c0f95e259144e3c1 100644 GIT binary patch delta 2961 zcmai0TW=dh6h5=-yPd>#t}P*PleBSD=mkQ6HceaFQf>{fL8vtYSb{tNaaY4@mQd1jgw6CL}pk8Z<`?1%k)KRlO(f7 z_Y}#T862Q3Sd^!?A$rh^3ub%|$$64PW;{)Dm{giJp?AdSCAwa_m((P+W=P#jvPicq z5)vjxQssfvETmFKY*t?t)4fW(t3q-j+BF&Pni}jn6iXc$=iG$|o775`QfHtZNcRmqc3X#{>?C)l# z8A}U?>0iB4uONxMmErS)ft$3#Z|lDJX~vsi97g0Sn~bY(TAA4#rQ zPC@rF%lhMFt~|%Pb08$f#jGfcMg3LsHBr&WQcsDa`i<18wQK1wQr+SESdtGVQ4mFK zXPy>E^s|}Ckw&{+O@QqzGO6m@>F32M{d@YFS+5i$Hm?k=CN_iPFQdy2v| zz{du|Oi-BNG5iBPxc_q@96ZTc=2D9nSnL4DKp++Z8x?8>h1hbHcg74@*I z6p-tmy@O--Cw>rXz+K=eC9AB_;mbN_nXECH0x|c3d$$_OoYHB8+lIH<_WiD5g>FYh zX?28mod>D1H%#J$!x-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u+FihCDrvJUtPSBo- diff --git a/frontend/index.html b/frontend/index.html index 529cebe76..637ecdd4d 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -105,7 +105,7 @@ let curTag = curContent.substr(0, endOfTag + 1); - console.log(contents); + //console.log(contents); for (tagId in tags) { let tag = tags[tagId]; @@ -134,21 +134,21 @@ } if (curTag.substr(0, 4) == " { function copyTitle(notes) { - for (key in notes) { - var note = notes[key]; - + for (let note of notes) { note.title = note.note_title; note.key = note.note_id; @@ -258,30 +256,52 @@ hotkeys: { keydown: { "insert": function(node) { - node.appendSibling({ - "title": "New!" - }).setActive(true); + let parentKey = (node.getParent() == null || node.getParent().key == "root_1") ? "root" : node.getParent().key; + + createNote(parentKey); }, "shift+insert": function(node) { - node.addChildren({ - "title": "New!" - }).setActive(true); + createNote(node.key); }, "del": function(node) { node.remove(); }, + "shift+up": function(node) { + if (node.getPrevSibling() != null) { + node.moveTo(node.getPrevSibling(), 'before'); + } + }, + "shift+down": function(node) { + if (node.getNextSibling() != null) { + node.moveTo(node.getNextSibling(), 'after'); + } + }, "shift+left": function(node) { if (node.getParent() != null) { - node.moveTo(node.getParent(), 'after'); + $.ajax({ + url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getParent().key, + type: 'PUT', + contentType: "application/json", + success: function(result) { + node.moveTo(node.getParent(), 'after'); + } + }); } }, "shift+right": function(node) { let prevSibling = node.getPrevSibling(); if (prevSibling != null) { - node.moveTo(prevSibling); + $.ajax({ + url: baseUrl + 'notes/' + node.key + '/moveTo/' + prevSibling.key, + type: 'PUT', + contentType: "application/json", + success: function(result) { + node.moveTo(prevSibling); - prevSibling.setExpanded(true); + prevSibling.setExpanded(true); + } + }); } } } @@ -292,6 +312,41 @@ var globalNote; + function setParent(noteId, newParentKey, successCallback) { + let newNoteName = "new note"; + + $.ajax({ + url: baseUrl + 'notes/' + nodeId + '/setParent/' + newParentKey, + type: 'PUT', + contentType: "application/json", + success: function(result) { + successCallback(); + } + }); + } + + function createNote(parentKey) { + let newNoteName = "new note"; + + $.ajax({ + url: baseUrl + 'notes/' + parentKey + '/children' , + type: 'POST', + data: JSON.stringify({ + note_title: newNoteName + }), + contentType: "application/json", + success: function(result) { + node.appendSibling({ + "title": newNoteName, + "key": result.note_id, + "note_id": result.note_id + }).setActive(true); + + message("Created!"); + } + }); + } + function loadNote(noteId) { $.get(baseUrl + 'notes/' + noteId).then(function(note) { globalNote = note;