Commit 143031ba authored by Alexandra Rogova's avatar Alexandra Rogova

new database model, server client separation

parent 4f5d5b13
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
.ready(function(){ .ready(function(){
this.index = elasticlunr(function () { this.index = elasticlunr(function () {
this.use(elasticlunr.fr); this.use(elasticlunr.fr);
this.addField('title'); this.addField('title');
......
...@@ -4,59 +4,26 @@ ...@@ -4,59 +4,26 @@
"use strict"; "use strict";
rJS(window) rJS(window)
.declareAcquiredMethod("get_model", "get_model") .declareMethod("parse", function(file_link){
.declareMethod("read_file_new", function(link){
var gadget = this, var gadget = this,
storage; title;
return gadget.get_model() return new RSVP.Queue().push(function(){
.push(function(result){ return jIO.util.ajax({url : file_link});
storage = result; })
.push(function(file){
var rss;
rss = file.currentTarget.responseText;
title = rss.slice(rss.indexOf("<title>")+7, rss.indexOf("</title>"));
return gadget.remove_css(rss);
}) })
.push (function(){ .push(function(rss_without_css){
var xmlhttp = new XMLHttpRequest(); return {title : title, content : rss_without_css};
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4){
var rss,
title;
rss = xmlhttp.responseText;
title = rss.slice(rss.indexOf("<title>")+7, rss.indexOf("</title>"));
return gadget.is_already_loaded(title)
.push(function(is_loaded){
if (!is_loaded){
return gadget.remove_css(rss)
.push(function(rss_without_css){
return storage.add_attachment(title, rss_without_css);
})
.push(function(){
return storage.loaded_doc(title, {"loaded" : "true"});
});
}
});
}
};
xmlhttp.open("GET", link, true);
xmlhttp.send();
}); });
}) })
.declareMethod("remove_css", function(body){ .declareMethod("remove_css", function(body){
var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi'); var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi');
return body.replace(regEx, ""); return body.replace(regEx, "");
})
.declareMethod("is_already_loaded", function(title){
return this.get_model()
.push(function(storage){
return storage.all_loaded_docs();
})
.push(function (result){
var i;
for (i = 0; i<result.data.rows.length; i+=1){
if (result.data.rows[i].id === title) return true;
}
return false;
});
}); });
}(window, RSVP, rJS, jIO)); }(window, RSVP, rJS, jIO));
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
} else { } else {
return this.cut_description(item.body, key) return this.cut_description(item.body, key)
.push(function (result){ .push(function (result){
console.log(result);
var array = [...result.matchAll(key)], var array = [...result.matchAll(key)],
i; i;
for (i=0; i<array.length; i+=1){ for (i=0; i<array.length; i+=1){
...@@ -55,7 +54,6 @@ ...@@ -55,7 +54,6 @@
.declareMethod("clear", function(){ .declareMethod("clear", function(){
var list = document.getElementById("list"); var list = document.getElementById("list");
console.log(list);
while (list.firstChild) { while (list.firstChild) {
list.removeChild(list.firstChild); list.removeChild(list.firstChild);
} }
......
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, jIO*/
(function (window, RSVP, rJS, jIO) {
"use strict";
rJS(window)
.ready(function(){
var gadget = this;
this.my_indexes = [];
var index_storage = jIO.createJIO({
type :"indexeddb",
database :"my_indexes"
});
return index_storage.allDocs()
.push(function(result){
var i,
promise_list = [];
for (i=0; i<result.length; i+=1){ //CHECK THIS
promise_list.push(index_storage.get(result[i].id));
}
return RSVP.all(promise_list);
})
.push(function(result){
var i,
promise_list = [];
for (i=0; i<result.length; i+=1){
promise_list.push(gadget._load_index(result[i]));
}
return RSVP.all(promise_list);
});
})
.declareMethod("_load_index", function(to_load){ /*!!! DOES NOT WORK CHECK WHAT index_storage.get(index_id) RETURNS !!!*/
/*var raw_index,
index,
index_storage,
gadget = this;*/
/*index_storage = jIO.createJIO({
type :"indexeddb",
database :"my_indexes"
});*/
/*return index_storage.get(index_id)
.push(function(result){*/
var raw_index,
index,
key,
doc;
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 = substorage id, "/a/b" = item id in x
});
raw_index = JSON.parse(to_load);
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);
}
this.my_indexes.push({
name : "" , // get name,
index : index
});
//});
})
.declareMethod("add_rss", function(index_name, link){
//WIP needs proxy
})
.declareMethod("remove_rss", function(index_name, something){
//WIP needs working brain
})
.declareMethod("_get_index", function(index_name){
var i;
for (i=0; i<this.my_indexes; i+=1){
if (this.my_indexes[i].name === index_name) return this.my_indexes[i];
}
return null;
})
.declareMethod("search", function(index_name, key){
return this._get_index(index_name)
.push(function(result){
if (result === null) return [];
else return result.search(key);
});
})
.declareMethod("my_indexes", function(){
index_storage = jIO.createJIO({
type :"indexeddb",
database :"my_indexes"
});
return index_storage.allDocs()
.push(function(result){
var i,
promise_list = [];
for (i = 0; i<result.data.rows.length; i+=1){
promise_list.push(index_storage.get(result.data.rows[i].id));
}
return promise_list;
})
.push(function(result){
var i,
info = [];
for (i=0; i< result.data.rows.length; i+=1){ //CHECK
info.push(result.data.rows[i]);
}
return info;
});
})
.declareMethod("download_index", function(index_id){
var gadget = this;
return this.getDeclaredGadget("server")
.push(function(server){
return server.get(index_id);
})
.push(function(result){
return gadget._load_index(result);
});
});
}(window, RSVP, rJS, jIO));
\ No newline at end of file
...@@ -17,34 +17,18 @@ ...@@ -17,34 +17,18 @@
<form id = "search_bar"> <form id = "search_bar">
<input type="search" required> <input type="search" required>
</form> </form>
<nav id="navbar">
<ul>
<li id="options"><a>Mes index</a></li>
<li><a class="active">Scolaire</a></li>
<li><a>Index 1</a></li>
<li><a>Index 2</a></li>
<li><a>Index 3</a></li>
<li><a>Index 4</a></li>
<li><a>Index 5</a></li>
<li><a>Index 6</a></li>
<li><a>Index 7</a></li>
<li><a>Index 8</a></li>
<li><a>Index 9</a></li>
<li><a>Index 10</a></li>
</ul>
</nav>
<dialog id="myDialog">
This is an open dialog window
</dialog>
<div data-gadget-url="gadget_result.html" <div data-gadget-url="gadget_result.html"
data-gadget-scope="result" data-gadget-scope="result"
data-gadget-sandbox="public"> data-gadget-sandbox="public">
<div data-gadget-url="gadget_model.html"
data-gadget-scope="model"
data-gadget-sandbox="public">
<div data-gadget-url="gadget_parser.html" <div data-gadget-url="gadget_parser.html"
data-gadget-scope="parser" data-gadget-scope="parser"
data-gadget-sandbox="public"> data-gadget-sandbox="public">
<div data-gadget-url="gadget_server_model.html"
data-gadget-scope="server_model"
data-gadget-sandbox="public">
<div data-gadget-url="gadget_client_model.html"
data-gadget-scope="client_model"
data-gadget-sandbox="public">
</div> </div>
</body> </body>
</html> </html>
(function (window, document, rJS, RSVP) { (function (window, document, rJS, RSVP) {
var gadget; var gadget;
var preventDefault = function (e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
};
rJS(window) rJS(window)
.allowPublicAcquisition("get_server_model", function(){
return this.state.server_model_gadget;
})
.setState({ .setState({
model_gadget : null, server_model_gadget : null,
client_model_gadget : null,
parser_gadget : null, parser_gadget : null,
result_gadget : null, result_gadget : null,
to_load: [ to_load: [
...@@ -31,15 +30,20 @@ ...@@ -31,15 +30,20 @@
}) })
.ready(function(){ .ready(function(){
var model_gadget, var server_model_gadget,
client_model_gadget,
result_gadget, result_gadget,
gadget; gadget = this;
console.log("ready !"); return gadget.getDeclaredGadget("server_model")
gadget = this; .push(function(server_model){
return gadget.getDeclaredGadget("model") server_model_gadget = server_model;
.push(function(model){ })
model_gadget = model; .push(function(){
return gadget.getDeclaredGadget("client_model");
})
.push(function(client_model){
client_model_gadget = client_model;
}) })
.push(function(){ .push(function(){
return gadget.getDeclaredGadget("result"); return gadget.getDeclaredGadget("result");
...@@ -52,44 +56,61 @@ ...@@ -52,44 +56,61 @@
}) })
.push(function(result){ .push(function(result){
return gadget.changeState({ return gadget.changeState({
model_gadget : model_gadget, server_model_gadget : server_model_gadget,
client_model_gadget : client_model_gadget,
parser_gadget : result, parser_gadget : result,
result_gadget : result_gadget result_gadget : result_gadget
}); });
})
.push(function(){
return gadget.set_on_click_options();
})
.push(function(){
document.addEventListener("keydown", function(event){
if (event.key === "Escape" && document.getElementById("myDialog").open){
document.getElementById("myDialog").close();
return gadget.enableScroll();
}
}, true);
}); });
}) })
.onStateChange(function (modification_dict){
var gadget = this,
promise_list = [],
i;
for (i = 0; i < gadget.state.to_load.length; i++){
promise_list.push(gadget.state.parser_gadget.read_file_new("./test-files/" + gadget.state.to_load[i]));
}
promise_list.push(gadget.state.model_gadget.load_index());
RSVP.all(promise_list);
})
.allowPublicAcquisition("get_model", function(){ .onStateChange(function (modification_dict){
return this.getDeclaredGadget("model"); var gadget = this;
return gadget.state.server_model_gadget.loaded()
.push(function(loaded){
if (!loaded){
return gadget.state.server_model_gadget.add_doc("scolaire")
.push(function(){
return gadget.state.server_model_gadget.add_index("scolaire");
})
.push(function(){
var promise_list = [],
i;
for (i = 0; i < gadget.state.to_load.length; i++){
promise_list.push(gadget.state.parser_gadget.parse("./test-files/" + gadget.state.to_load[i]));
}
return RSVP.all(promise_list);
})
.push(function(parsed_file_items){
var i,
promise_list = [],
queue = new RSVP.Queue(),
add_to_feed;
add_to_feed = function(doc_name, feed){
queue.push(function(){
return gadget.state.server_model_gadget.add_feed_to_doc(doc_name, feed);
});
};
for (i=0; i<parsed_file_items.length; i+=1){
add_to_feed("scolaire", {title : parsed_file_items[i].title, content : parsed_file_items[i].content});
}
return queue;
})
.push(function(){
return gadget.state.server_model_gadget.add_doc_to_index("scolaire", "scolaire");
})
.push(function(){
return gadget.state.client_model_gadget.download_new_index("scolaire");
});
}
});
}) })
.declareMethod("search", function (key){ .declareMethod("search", function (key){
var gadget = this; var gadget = this;
return gadget.state.result_gadget.clear() return gadget.state.result_gadget.clear()
.push(function(){ .push(function(){
return gadget.state.model_gadget.search(key); return gadget.state.client_model_gadget.search_in_index("scolaire", key);
}) })
.push(function(result){ .push(function(result){
if (result.length === 0) { if (result.length === 0) {
...@@ -109,31 +130,6 @@ ...@@ -109,31 +130,6 @@
.onEvent("submit", function(event){ .onEvent("submit", function(event){
this.search(event.target.elements[0].value); this.search(event.target.elements[0].value);
})
.declareMethod("set_on_click_options", function(){
var options = document.getElementById("options"),
gadget = this;
options.onclick = function(){
document.getElementById("myDialog").showModal();
return gadget.disableScroll();
};
})
.declareMethod("disableScroll", function(){
if (window.addEventListener) window.addEventListener('DOMMouseScroll', preventDefault, false);
document.addEventListener('wheel', preventDefault, {passive: false}); // Disable scrolling in Chrome
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
})
.declareMethod("enableScroll", function(){
if (window.removeEventListener) window.removeEventListener('DOMMouseScroll', preventDefault, false);
document.removeEventListener('wheel', preventDefault, {passive: false}); // Enable scrolling in Chrome
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
}); });
}(window, document, rJS, RSVP)); }(window, document, rJS, RSVP));
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, jIO*/
(function (window, RSVP, rJS, jIO) {
"use strict";
rJS(window)
.delcareMethod("get_all_available_indexes", function(){
var all_indexes = jIO.createJIO({
type : "indexeddb",
database : "all_indexes"
});
return all_indexes.allDocs()
.push(function(result){
var i,
promise_list = [];
for (i = 0; i<result.data.rows.length; i+=1){
promise_list.push(all_indexes.get(result.data.rows[i].id));
}
return promise_list;
})
.push(function(result){
var i,
info = [];
for (i=0; i< result.data.rows.length; i+=1){ //CHECK
info.push(result.data.rows[i]);
}
return info;
});
})
.declareMethod("get", function(index_id){
var all_indexes = jIO.createJIO({
type : "indexeddb",
database : "all_indexes"
});
return all_indexes.get(index_id);
});
}(window, RSVP, rJS, jIO));
\ No newline at end of file
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