From c45588b7748e2c8aad025673712a4e32eab6504d Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Sat, 24 Feb 2018 23:00:18 -0500 Subject: [PATCH] Server fixes, single phoneme edit --- SmearcarDB/server.py | 32 ++++++++++----------- SmearcarDB/static/index.css | 1 + SmearcarDB/static/index.js | 55 +++++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/SmearcarDB/server.py b/SmearcarDB/server.py index c932d46..8a532d7 100644 --- a/SmearcarDB/server.py +++ b/SmearcarDB/server.py @@ -63,16 +63,15 @@ def database(): final['languages'] = [f.name for f in Language.query.all()] final['phonemes'] = [f.name for f in Phoneme.query.all()] for language in Language.query.all(): - languageobject = {'name': language.name, + languageobject = {'id': language.id, + 'name': language.name, 'source': language.source, 'phonemes': {}} - languageobject['name'] = language.name for frequency in language.phonemes: languageobject['phonemes'][frequency.phoneme.name] = frequency.value final['values'].append(languageobject) return final - def phoneme_add(info): """Add or edit value associated with phoneme.""" # info = { @@ -164,11 +163,11 @@ def backend(): # POST method appends input to database['values'] if request.method == "POST": - recieved = request.get_json() - language = Language(name=recieved['name'], source=recieved['source']) + received = request.get_json() + language = Language(name=received['name'], source=received['source']) db.session.add(language) - for phoneme, value in recieved['phonemes'].items(): + for phoneme, value in received['phonemes'].items(): with db.session.no_autoflush: search = Phoneme.query.filter_by(name=phoneme).first() if not search: @@ -182,8 +181,8 @@ def backend(): # PATCH method inputs edited language and returns updated database elif request.method == "PATCH": - recieved = request.get_json() - patch_functions[recieved['action']](recieved['data']) + received = request.get_json() + patch_functions[received['action']](received['data']) db.session.commit() return jsonify(database()) @@ -194,17 +193,18 @@ def backend(): def updates(): if request.method == "POST": - recieved = request.get_json() - update = Update(author=recieved['author'], - title=recieved['title'], - content=recieved['content']) + received = request.get_json() + update = Update(author=received['author'], + title=received['title'], + content=received['content']) db.session.add(update) elif request.method == "PATCH": - update = Update.query.filter_by(id=recieved['id']).first() - update.name = recieved['author'] - update.title = recieved['title'] - update.content = recieved['content'] + received = request.get_json() + update = Update.query.filter_by(id=received['id']).first() + update.name = received['author'] + update.title = received['title'] + update.content = received['content'] db.session.commit() return jsonify([{"author": update.name, diff --git a/SmearcarDB/static/index.css b/SmearcarDB/static/index.css index 1663949..a4d205d 100644 --- a/SmearcarDB/static/index.css +++ b/SmearcarDB/static/index.css @@ -325,6 +325,7 @@ a { } #dataTableCont input { + border: 1px solid rgba(0,0,0,0.3); font-size: 100%; width: 60%; padding: 0; diff --git a/SmearcarDB/static/index.js b/SmearcarDB/static/index.js index e508e7e..7791890 100644 --- a/SmearcarDB/static/index.js +++ b/SmearcarDB/static/index.js @@ -146,13 +146,13 @@ function generateDropOp() { // For options that change based on data. var a = document.createElement("a"); p.appendChild(document.createTextNode("Type: " + (langInfo.type || "N/A"))); p2.appendChild(document.createTextNode("Source: ")); - if(langInfo.source.length > 0) { + if(langInfo.source === null) { + p2.appendChild(document.createTextNode("N/A")); + } else if(langInfo.source.length > 0) { a.href = langInfo.source; srcText = (langInfo.source.length > 60) ? langInfo.source.substring(0, 57) + "..." : langInfo.source; a.appendChild(document.createTextNode(srcText)); p2.appendChild(a); - } else { - p2.appendChild(document.createTextNode("N/A")); } info.appendChild(p); info.appendChild(p2); @@ -189,20 +189,24 @@ function generateDropOp() { // For options that change based on data. } p1.appendChild(document.createTextNode(phonemes[i])); p2.appendChild(document.createTextNode(langInfo.phonemes[phonemes[i]])); - p2.onclick = function() { - if(dataOpen) closeEditInput(); + p2.className = "dataEdit"; + p2.onclick = function(event) { + if(this === event.target) return; + closeEditInput(); dataOpen = true; var input = document.createElement("input"); var value = this.childNodes[0].nodeValue; this.removeChild(this.childNodes[0]); this.appendChild(input); input.value = value; + input.id = "dataOpen"; + input.focus(); } dataBox.children[tableNum].appendChild(p1); dataBox.children[tableNum].appendChild(p2); } var graphData = Object.entries(langInfo.phonemes).sort(function(a,b) { - return b[1] - a[1]; + return b[1] - a[1]; }); graphData = [graphData.map(function(a,b) { return a[0]; @@ -275,36 +279,39 @@ function generateDropOp() { // For options that change based on data. } function closeEditInput() { - var input = document.querySelectorAll("#dataTableCont input")[0]; - var p = input.parentNode; - /*$.ajax({ + try { + var input = document.getElementById("dataOpen"); + var p = input.parentNode; + var patchData = { + action: 'phoneme_add', + data: { + language_id: language(dropOpStore["langSelect"]).id, + phoneme: p.previousSibling.innerText, + value: input.value + } + }; + $.ajax({ url: serverURL + '/server', type: 'PATCH', - data: { - action: 'phoneme' - } + dataType: "json", + contentType: 'application/json;charset=UTF-8', + data: JSON.stringify(patchData) }) .then( function success(incoming) { - data = incoming; - generateDropOp(); - createDrop(); + p.appendChild(document.createTextNode(input.value)); + p.removeChild(input); }, function error(e) { console.log(e); } - );*/ - p.appendChild(document.createTextNode(input.value)); - p.removeChild(input); - dataOpen = false; - + ); + dataOpen = false; + } catch(err) {} } document.addEventListener("click", function(event) { - console.log(event.target); - var input = document.querySelectorAll("#dataTableCont input")[0]; - - if(dataOpen && !(event.target === input || event.target === input.parentNode)) { + if(event.target.className !== "dataEdit") { closeEditInput(); } });