Commit feb459b4 authored by Alexandra Rogova's avatar Alexandra Rogova

new model implemented + searx results in 2nd column + logo

parent 143031ba
<!doctype html>
<html>
<head>
<title>Model Gadget</title>
<script src="../jio/external/rsvp-2.0.4.js"></script>
<script src="../jio/dist/jio-latest.js"></script>
<script src="../renderjs/dist/renderjs-latest.js"></script>
<script src="./elasticlunr/elasticlunr.js"></script>
<script src="./lunr-languages/lunr.stemmer.support.js"></script>
<script src="./lunr-languages/lunr.fr.js"></script>
<script src="gadget_client_model.js"></script>
</head>
<body>
</body>
</html>
\ No newline at end of file
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, jIO*/
(function (window, document, RSVP, rJS, jIO) {
"use strict";
rJS(window)
.declareAcquiredMethod("get_server_model", "get_server_model")
.setState({
my_docs : null,
my_indexes : null,
indexes : null
})
.ready(function(){
var my_docs = jIO.createJIO({
type : "indexeddb",
database : "mynij.my_docs"
}),
my_indexes = jIO.createJIO({
type : "indexeddb",
database : "mynij.my_indexes"
});
this.changeState({
my_docs : my_docs,
my_indexes : my_indexes,
indexes : []
});
})
.onStateChange(function (modification_dict){
if (this.state.indexes.length === 0) return this._load_my_indexes();
})
.declareMethod("search_in_index", function(index_name, key){
if (this.state.indexes.length === 0) return null;
return this.get_right_index(index_name)
.push(function(result_index){
if (result_index === null) return null;
return result_index.search(key);
});
})
.declareMethod("get_right_index", function(index_name){
var i;
for (i=0; i<this.state.indexes.length; i+=1){
if (this.state.indexes[i].name === index_name) return this.state.indexes[i].index;
}
return null;
})
.declareMethod("search_in_all", function(key){
// TODO later
})
.declareMethod("_load_my_indexes", function(){
var gadget = this,
index_names;
return gadget.state.my_indexes.allDocs()
.push(function(index_ids){
var i,
promise_list = [];
index_names = index_ids;
for (i=0; i<index_ids.data.rows.length; i+=1){
promise_list.push(gadget.state.my_indexes.get(index_ids.data.rows[i].id));
}
return RSVP.all(promise_list);
})
.push(function(indexes){
var i,
promise_list = [];
for (i=0; i<indexes.length; i+=1){
promise_list.push(gadget._build_index(index_names.data.rows[i].id, indexes[i]));
}
return RSVP.all(promise_list);
});
})
.declareMethod("download_new_index", function(index_name){
var gadget = this,
server,
index_received;
return gadget.get_server_model()
.push(function(server_model){
server = server_model;
})
.push(function(){
return server.get_index(index_name);
})
.push(function(index_result){
index_received = index_result;
var promise_list = [];
promise_list.push(gadget._download_all_index_docs(index_result.docs, server));
promise_list.push(gadget._download_actual_index(index_name, index_received.index));
return RSVP.all(promise_list);
});
})
.declareMethod("_download_all_index_docs", function(doc_list, server){
var i,
gadget = this,
promise_list = [],
doc_info;
for (i=0; i<doc_list.length; i+=1){
promise_list.push(server.get_doc(doc_list[i]));
}
return new RSVP.Queue()
.push(function(){
return RSVP.all(promise_list);
})
.push(function(doc_info){
var i,
promise_list = [];
for (i=0; i<doc_info.length; i+=1){
promise_list.push(gadget.state.my_docs.put(doc_list[i], {links : doc_info[i]}));
promise_list.push(gadget._download_doc_attachments(doc_list[i], doc_info[i], server));
}
return RSVP.all(promise_list);
});
})
.declareMethod("_download_doc_attachments", function(doc_name, attachment_list, server){
var i,
gadget = this,
promise_list = [];
for (i=0; i<attachment_list.links.length; i+=1){
promise_list.push(server.get_attachment(doc_name, attachment_list.links[i], {"format": "text"}));
}
return new RSVP.Queue()
.push(function(){
return RSVP.all(promise_list);
})
.push(function(attachments){
var i,
promise_list = [];
for (i=0; i<attachments.length; i+=1){
promise_list.push(gadget.state.my_docs.putAttachment(doc_name, attachment_list.links[i], new Blob([attachments[i], {type : "text/xml"}])));
}
return RSVP.all(promise_list);
});
})
.declareMethod("_download_actual_index", function(index_name, index){
this.state.indexes.push({name : index_name, index : index});
return this.state.my_indexes.put(index_name, JSON.stringify(index));
})
.declareMethod("_build_index", function(index_name, index){
var raw_index,
new_index,
key,
doc;
new_index = elasticlunr(function () {
this.use(elasticlunr.fr);
this.addField('title');
this.addField('body');
this.addField('link');
this.setRef('id'); //id = index.x."/a/b" where index = index name, x = doc name, "/a/b" = item id in x
});
raw_index = JSON.parse(index);
for (key in raw_index.documentStore.docs){
doc = {
"id" : raw_index.documentStore.docs[key].id,
"title" : raw_index.documentStore.docs[key].title,
"body" : raw_index.documentStore.docs[key].body,
"link" : raw_index.documentStore.docs[key].link
};
new_index.addDoc(doc);
}
this.state.indexes.push({name: index_name, index : new_index});
});
}(window, document, RSVP, rJS, jIO));
\ No newline at end of file
......@@ -9,7 +9,9 @@
<link rel="stylesheet" type="text/css" href="result.css">
</head>
<body>
<ul id="list">
<ul id="mynij-results">
</ul>
<ul id="searx-results">
</ul>
</body>
</html>
\ No newline at end of file
......@@ -9,12 +9,10 @@
list_item,
title,
link,
body,
body_text,
parser,
xmlDoc;
link_par,
body;
list = document.getElementById("list");
list = document.getElementById("mynij-results");
list_item = document.createElement("LI");
title = document.createElement("a");
......@@ -27,7 +25,7 @@
link.appendChild(document.createTextNode(item.link));
link.href = item.link;
link.className = "link";
var link_par = document.createElement('p');
link_par = document.createElement('p');
link_par.appendChild(link);
list_item.appendChild(link_par);
......@@ -52,8 +50,46 @@
}
})
.declareMethod("show_searx_result", function(item, key){
var list,
list_item,
title,
link,
link_par,
body;
list = document.getElementById("searx-results");
list_item = document.createElement("li");
title = document.createElement("a");
title.appendChild(document.createTextNode(item.querySelector('h4.result_header').textContent));
title.className="title";
title.href =item.querySelector('div.external-link').textContent;
list_item.appendChild(title);
link = document.createElement("a");
link.appendChild(document.createTextNode(item.querySelector('div.external-link').textContent));
link.href = item.querySelector('div.external-link').textContent;
link.className = "link";
link_par = document.createElement("p");
link_par.appendChild(link);
list_item.appendChild(link_par);
body = document.createElement("p");
body.className = "body";
if (item.querySelector('p.result-content') !== null) body.innerHTML = item.querySelector('p.result-content').textContent;
else body.innerHTML = "";
list_item.appendChild(body);
list.appendChild(list_item);
})
.declareMethod("clear", function(){
var list = document.getElementById("list");
var list = document.getElementById("mynij-results");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
list = document.getElementById("searx-results");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
......
<!doctype html>
<html>
<head>
<title>Model Gadget</title>
<script src="../jio/external/rsvp-2.0.4.js"></script>
<script src="../jio/dist/jio-latest.js"></script>
<script src="../renderjs/dist/renderjs-latest.js"></script>
<script src="./elasticlunr/elasticlunr.js"></script>
<script src="./lunr-languages/lunr.stemmer.support.js"></script>
<script src="./lunr-languages/lunr.fr.js"></script>
<script src="gadget_server_model.js"></script>
</head>
<body>
</body>
</html>
\ No newline at end of file
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, jIO*/
(function (window, RSVP, rJS, jIO) {
"use strict";
rJS(window)
.setState({
docs : null,
indexes : null
})
.ready(function(){
var docs_db = jIO.createJIO({
type : "indexeddb",
database : "mynij.all_docs"
});
var indexes_db = jIO.createJIO({
type : "indexeddb",
database : "mynij.all_indexes"
});
return this.changeState({
docs : docs_db,
indexes : indexes_db
});
})
.declareMethod("get_all_available_indexes", function(){
//TODO later
})
.declareMethod("get_doc", function(doc_name){ //tested OK returns {links:[]}
return this.state.docs.get(doc_name);
})
.declareMethod("get_index", function(index_name){ //tested OK
return this.state.indexes.get(index_name)
.push(function(result_index){
var raw_index,
index,
key,
doc,
doc_list;
doc_list = result_index.docs;
index = elasticlunr(function () {
this.use(elasticlunr.fr);
this.addField('title');
this.addField('body');
this.addField('link');
this.setRef('id'); //id = index.x."/a/b" where index = index name, x = doc name, "/a/b" = item id in x
});
raw_index = JSON.parse(result_index.index);
for (key in raw_index.documentStore.docs){
doc = {
"id" : raw_index.documentStore.docs[key].id,
"title" : raw_index.documentStore.docs[key].title,
"body" : raw_index.documentStore.docs[key].body,
"link" : raw_index.documentStore.docs[key].link
};
index.addDoc(doc);
}
return {index : index, docs : doc_list};
});
})
.declareMethod("get_attachment", function(doc, att, format){
return this.state.docs.getAttachment(doc, att, format);
})
.declareMethod("add_doc", function(new_doc){ //tested OK
return this.state.docs.put(new_doc, {links : []});
})
.declareMethod("add_feed_to_doc", function(doc_name, feed){ //tested OK
var gadget = this;
return gadget.state.docs.putAttachment(doc_name, feed.title, new Blob([feed.content], {type : "text/xml"}))
.push(function(){
return gadget.state.docs.get(doc_name);
})
.push(function(doc){
var link_list = doc.links;
link_list.push(feed.title);
return gadget.state.docs.put(doc_name, {links : link_list});
});
})
.declareMethod("add_index", function(name){ //tested OK
var new_index = elasticlunr(function () {
this.use(elasticlunr.fr);
this.addField('title');
this.addField('body');
this.addField('link');
this.setRef('id'); //id = index.x."/a/b" where index = index name, x = doc name, "/a/b" = item id in x
});
return this.state.indexes.put(name, {index : JSON.stringify(new_index), docs : []});
})
.declareMethod("add_doc_to_index", function(index_name, doc_name){ //tested OK - to split & clean
var index,
parser_list = [],
gadget = this;
return gadget.get_index(index_name)
.push(function(result_index){
index = result_index;
})
.push(function(){
return gadget.get_doc(doc_name);
})
.push(function(result_doc){
var attachment_list = result_doc.links,
i,
parser,
promise_list = [];
for (i=0; i<attachment_list.length; i+=1){
var parser_def = {
type : "my_parser",
document_id : doc_name,
attachment_id : attachment_list[i],
parser : "rss",
sub_storage : {
type : "indexeddb",
database : "mynij.all_docs"
}
};
parser = jIO.createJIO(parser_def);
parser_list.push(parser);
promise_list.push(parser.allDocs());
}
return RSVP.all(promise_list); //returns [{data : {rows : [id : _], total_rows : _}]
})
.push(function(all_item_ids){
var i,
j,
promise_list = [];
for (i=0; i<all_item_ids.length; i+=1){
for (j=0; j<all_item_ids[i].data.total_rows; j+=1){
promise_list.push(parser_list[i].get(all_item_ids[i].data.rows[j].id));
}
}
return RSVP.all(promise_list); //returns [{link : _, title : _, description : _, pubDate : _, ...}] ! ignore [0], it's the feed info !
})
.push(function(all_items){
var i,
id,
doc;
for (i=1; i<all_items.length; i+=1){
id = index_name+"."+doc_name+"./0/"+i;
doc = {
"id" : id,
"title" : all_items[i].title,
"body" : all_items[i].description,
"link" : all_items[i].link
};
index.index.addDoc(doc);
}
})
.push(function(){
var doc_list = index.docs;
doc_list.push(doc_name);
return gadget.state.indexes.put(index_name, {index : JSON.stringify(index.index), docs : doc_list});
});
})
.declareMethod("loaded", function(){
return this.state.docs.allDocs()
.push(function(result){
return (result.data.rows.length !== 0);
});
});
}(window, RSVP, rJS, jIO));
\ No newline at end of file
logo.png

5.87 KB

......@@ -96,6 +96,7 @@ input{
padding-bottom:0.2%;
}
/*
#mynij{
position: fixed;
left: 1.5%;
......@@ -107,3 +108,15 @@ input{
line-height: 38px;
letter-spacing: -1px;
}
*/
#mynij{
position: fixed;
left: 1.5%;
top: 1.5%;
}
#mynij img {
height: 5em;
}
#list{
#gadget_result{
display : flex;
}
#mynij-results, #searx-results{
margin-top : 4%;
text-align: left;
}
#list > li {
#mynij-results > li, #searx-results > li {
margin: auto;
list-style-type: none;
position: relative;
display: table;
margin-left: 8.3%;
margin-left: 10%;
margin-top: 1%;
padding-top: 1%;
padding-bottom: 1%;
padding-left : 1%;
background: #fff;
color: #2b2b5d;
width: 98%;
max-width: 48em;
width: 90%;
max-width: 40em;
text-align : left;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
border-radius: 8px;
......@@ -26,25 +30,25 @@
color: #1122CC;
text-decoration: none;
font-size: x-large;
max-width: 48em;
max-width: 40em;
}
.link, .link:visited {
text-decoration: none;
color: #009933;
max-width: 48em;
max-width: 40em;
}
.body {
color : #444444;
max-width: 48em;
max-width: 40em;
}
#list > a {
#mynij-results > a, #searx-results > a {
position: relative;
}
#list > a:after {
#mynij-results > a:after , #searx-results > a:after{
content: '';
position: absolute;
bottom: -.4em;
......@@ -55,13 +59,13 @@
transition: all ease .2s;
}
#list > a:hover:after {
#mynij-results > a:hover:after , #searx-results >a:hover:after{
left: 0;
right: 0;
height: 2px;
}
#list > a:before {
#mynij-results > a:before , #searx-results > a:before{
position: absolute;
transform: translateX(-100%);
left: -10px;
......
......@@ -13,11 +13,13 @@
<link rel="stylesheet" type="text/css" href="mynij.css">
</head>
<body>
<div id ="mynij">Mynij</div>
<!-- <div id ="mynij">Mynij</div> -->
<div id ="mynij"><img src="./logo.png" height="3em"></div>
<form id = "search_bar">
<input type="search" required>
</form>
<div data-gadget-url="gadget_result.html"
<div id = "gadget_result"
data-gadget-url="gadget_result.html"
data-gadget-scope="result"
data-gadget-sandbox="public">
<div data-gadget-url="gadget_parser.html"
......
......@@ -128,8 +128,31 @@
});
})
.declareMethod("add_searx_results", function(key){
var gadget = this;
return new RSVP.Queue()
.push(function(){
return jIO.util.ajax({url : "https://cors-anywhere.herokuapp.com/search.disroot.org/?q=" + key + "&category_general=on&theme=oscar&language=fr"});
})
.push(function(resultHTML){
var i,
promise_list = [],
elements,
doc = new DOMParser().parseFromString(resultHTML.currentTarget.response, "text/html");
elements = doc.body.querySelectorAll('div.result.result-default');
for (i=0; i<elements.length; i+=1){
promise_list.push(gadget.state.result_gadget.show_searx_result(elements[i], key));
}
return RSVP.all(promise_list);
});
})
.onEvent("submit", function(event){
this.search(event.target.elements[0].value);
var gadget = this;
return gadget.search(event.target.elements[0].value)
.push(function(){
return gadget.add_searx_results(event.target.elements[0].value);
});
});
}(window, document, rJS, RSVP));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment