Loading of examples from my API.
This commit is contained in:
parent
ad1c0a6a28
commit
ee94ab8383
|
@ -333,6 +333,16 @@
|
|||
background-color: @black;
|
||||
color: @silver;
|
||||
}
|
||||
|
||||
.no-gf2-info {
|
||||
color: @silver;
|
||||
}
|
||||
.bad-gf2-info {
|
||||
color: @orange;
|
||||
}
|
||||
.good-gf2-info {
|
||||
color: @black;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from message.simple_messages import ClickMessage, DataChgClickMessage
|
||||
from message.simple_messages import ClickMessage, DataChgClickMessage, NoAction
|
||||
from message.message import msg, Message
|
||||
from view import modals
|
||||
from browser import window, document
|
||||
|
@ -14,30 +14,44 @@ from model.example_clusters import ExampleClusters
|
|||
|
||||
|
||||
class SkeExample:
|
||||
def __init__(self, line):
|
||||
COLOR_CLASS = {
|
||||
None: "no-gf2-info",
|
||||
True: "goof-gf2-info",
|
||||
False: "bad-gf2-info"
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.left = ""
|
||||
self.right = ""
|
||||
self.mid = ""
|
||||
self.s_id = ""
|
||||
self.gf2_good = None
|
||||
|
||||
@staticmethod
|
||||
def fromLine(line):
|
||||
result = SkeExample()
|
||||
|
||||
for left_el in line.Left:
|
||||
self.left += left_el.str
|
||||
result.left += left_el.str
|
||||
|
||||
for right_el in line.Right:
|
||||
self.right += right_el.str
|
||||
result.right += right_el.str
|
||||
|
||||
for mid_el in line.Kwic:
|
||||
self.mid += mid_el.str
|
||||
result.mid += mid_el.str
|
||||
|
||||
for ref in line.Refs:
|
||||
if ref.startswith("s.id"):
|
||||
self.s_id = ref.split("=")[1]
|
||||
result.s_id = ref.split("=")[1]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def view(self):
|
||||
return [h("span", {}, self.left + " "),
|
||||
return [h("span.{}".format(self.COLOR_CLASS[self.gf2_good]), {}, [
|
||||
h("span", {}, self.left + " "),
|
||||
h("span", {"style": {"font-weight": "bold"}}, self.mid),
|
||||
h("span", {}, self.right)]
|
||||
h("span", {}, self.right)])]
|
||||
|
||||
|
||||
class SkeCollocation:
|
||||
|
@ -61,13 +75,32 @@ class SkeCollocation:
|
|||
|
||||
def get_parser(ske_index_type):
|
||||
if ske_index_type == 0:
|
||||
return lambda data: [SkeExample(line) for line in data.Lines]
|
||||
return lambda data: [SkeExample.fromLine(line) for line in data.Lines]
|
||||
elif ske_index_type == 1:
|
||||
return lambda data: [SkeCollocation(item) for item in data.Items]
|
||||
else:
|
||||
raise "unknown ske index type"
|
||||
|
||||
|
||||
def match_gf2_examples(data, *args):
|
||||
if type(data) is not list:
|
||||
return
|
||||
|
||||
url = "https://gf2examples.cjvt.si/get_examples"
|
||||
xhr = __new__(XMLHttpRequest())
|
||||
xhr.open("POST", url, True)
|
||||
|
||||
request_dict = {}
|
||||
for example in data:
|
||||
request_dict[example.s_id] = example.mid
|
||||
|
||||
xhr.setRequestHeader("Content-type", "application/json");
|
||||
xhr.addEventListener("load", msg(SkeModalGf2Update, *args, data))
|
||||
|
||||
to_send = window.JSON.stringify(request_dict)
|
||||
xhr.send(to_send)
|
||||
|
||||
|
||||
class SkeModal(ClickMessage):
|
||||
def on_event(self, event):
|
||||
# event could be data if this is the return from external library
|
||||
|
@ -84,12 +117,15 @@ class SkeModal(ClickMessage):
|
|||
ske_index = self.get_arg(2, int)
|
||||
ske_lookup = model.ske.url_for_kind_index(ske_index)
|
||||
|
||||
next_message = msg(SkeModal, page_num, search_term, ske_index)
|
||||
|
||||
# could be none if empty
|
||||
data = self.get_arg(3)
|
||||
|
||||
if data is None:
|
||||
params = {"additional_refs": "s.id,p.id",
|
||||
"page_num": page_num,
|
||||
"error_callback": msg(SkeModal, page_num, search_term, ske_index),
|
||||
"error_callback": next_message,
|
||||
"data_parser": get_parser(ske_index)}
|
||||
|
||||
gdex = get_preference("ske_gdex")
|
||||
|
@ -99,10 +135,14 @@ class SkeModal(ClickMessage):
|
|||
return
|
||||
params["gdex"] = gdex
|
||||
|
||||
model.ske.request(search_term,
|
||||
msg(SkeModal, page_num, search_term, ske_index),
|
||||
ske_lookup,
|
||||
params)
|
||||
model.ske.request(search_term, next_message, ske_lookup, params)
|
||||
|
||||
elif type(data) is list:
|
||||
window.console.log(data)
|
||||
# check if gf2 examples are loaded or not
|
||||
if data[0].gf2_good is None:
|
||||
# we get the data, we have to match it with available data on our gf2 examples API
|
||||
match_gf2_examples(data, page_num, search_term, ske_index)
|
||||
|
||||
model.modal_set(lambda: modals.ske_list(
|
||||
search_term, data, page_num, model.entry.senses, model.ske.request_kinds))
|
||||
|
@ -111,6 +151,27 @@ class SkeModal(ClickMessage):
|
|||
return False
|
||||
|
||||
|
||||
class SkeModalGf2Update(SkeModal):
|
||||
def on_event(self, event):
|
||||
response_data = window.JSON.parse(event.target.response)
|
||||
data = self.get_arg(3)
|
||||
|
||||
data_dict = {}
|
||||
for example in data:
|
||||
example.gf_good = False
|
||||
data_dict[example.s_id] = example
|
||||
|
||||
good_response = dict(response_data["good"])
|
||||
for gf_sid, gf_data in good_response.items():
|
||||
data_dict[gf_sid].left = gf_data.left
|
||||
data_dict[gf_sid].mid = gf_data.mid
|
||||
data_dict[gf_sid].right = gf_data.right
|
||||
data_dict[gf_sid].gf2_good = True
|
||||
|
||||
# changed data_dict, now we can redraw!
|
||||
# just let it do its thing in update_model
|
||||
|
||||
|
||||
class ShowSkeModal(SkeModal):
|
||||
def on_event(self, event):
|
||||
# if just showing first time, than show empty results and let user click search
|
||||
|
|
Loading…
Reference in New Issue
Block a user