Commit fb0ce479 authored by Alexandra Rogova's avatar Alexandra Rogova

getting all attachments ok

parent 7576610c
......@@ -5,16 +5,12 @@
rJS(window)
.ready(function(){
this.feeds = jIO.createJIO({
type : "indexeddb",
database : "mynij-v1.2"
});
this.search_storage = jIO.createJIO({
type : "union",
storage_list : []
});
this.sub_storages = [];
this.info = jIO.createJIO({
......@@ -24,7 +20,7 @@
return this.feeds.put("feeds_doc", {});
})
.declareMethod ("add_attachment", function (title, rss){
var gadget = this;
return this.feeds.putAttachment("feeds_doc", title, new Blob([rss], {type : "text/xml"}))
......@@ -38,21 +34,27 @@
type : "indexeddb",
database : "mynij-v1.2"
}
}
gadget.sub_storages.push(new_sub_storage);
gadget.search_storage = jIO.createJIO({
type : "union",
storage_list : gadget.sub_storages
});
};
var tmp = jIO.createJIO(new_sub_storage);
gadget.sub_storages.push(tmp);
});
})
.declareMethod("allDocs", function (){
var promise_list = [],
i;
for (i=0; i<this.sub_storages.length; i+=1){
promise_list.push(this.sub_storages[i].allDocs());
}
return RSVP.all(promise_list);
})
.declareMethod("get", function () {
return this.search_storage.get.apply(this.search_storage, arguments);
.declareMethod("get", function (id_storage, id_item) {
return this.sub_storages[id_storage].get(id_item);
})
.declareMethod("search", function() {
return this.search_storage.allDocs.apply(this.search_storage, arguments)
return this.search_storage.allDocs.apply(this.search_storage, arguments);
})
.declareMethod("loaded_doc", function(){
......
......@@ -56,51 +56,4 @@
});
});
/*.declareMethod("read_file", function(link) {
var gadget,
xmlhttp,
cur_text,
promise_list = [],
feed_title;
gadget = this;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
cur_text = xmlhttp.responseText,
feed_title = cur_text.slice(cur_text.indexOf("<title>")+7, cur_text.indexOf("</title>"));
return gadget.is_already_parsed(feed_title)
.push (function (resultat){
if (!resultat){
promise_list.push(gadget.state.storage.loaded_doc(feed_title, {"loaded" : "true"}));
cur_text = cur_text.slice(cur_text.indexOf("<item>"), cur_text.length - 1);
while (cur_text.indexOf("<item>") > -1) {
promise_list.push(gadget.parse(cur_text));
cur_text = cur_text.slice(cur_text.indexOf("</item>") + 6, cur_text.length - 1);
}
RSVP.all(promise_list);
}
});
}
};
xmlhttp.open("GET", link, true);
xmlhttp.send();
})
.declareMethod("parse", function(local_cur_text) {
var title_start = local_cur_text.indexOf("<title>") + 7,
title_end = local_cur_text.indexOf("</title>"),
link_start = local_cur_text.indexOf("<link>") + 6,
link_end = local_cur_text.indexOf("</link>"),
body_start = local_cur_text.indexOf("<description>"),
body_end = local_cur_text.indexOf("</description>") + 14;
return this.remove_css(local_cur_text.slice(body_start, body_end))
.push (function(result){
return gadget.state.storage.put({
"title": local_cur_text.slice(title_start, title_end),
"link": local_cur_text.slice(link_start, link_end),
"body": result
});
});
})*/
}(window, RSVP, rJS, jIO));
(function (jIO, DOMParser, Node) {
"use strict";
/////////////////////////////////////////////////////////////
// RSS Parser
/////////////////////////////////////////////////////////////
function RSSParser(txt) {
this._dom_parser = new DOMParser().parseFromString(txt, 'text/xml');
}
RSSParser.prototype.parseElement = function (element) {
var tag_element,
i,
j,
attribute,
result = {};
for (i = element.childNodes.length - 1; i >= 0; i -= 1) {
tag_element = element.childNodes[i];
if ((tag_element.nodeType === Node.ELEMENT_NODE) &&
(tag_element.tagName !== 'item')) {
result[tag_element.tagName] = tag_element.textContent;
for (j = tag_element.attributes.length - 1; j >= 0; j -= 1) {
attribute = tag_element.attributes[j];
if (attribute.value) {
result[tag_element.tagName + '_' + attribute.name] =
attribute.value;
}
}
}
}
return result;
};
RSSParser.prototype.getDocumentList = function (include, id) {
var result_list,
item_list = this._dom_parser.querySelectorAll("rss > channel > item"),
i;
//console.log(item_list);
if ((id === '/0') || (id === undefined)) {
result_list = [{
id: '/0',
value: {}
}];
if (include) {
result_list[0].doc = this.parseElement(
this._dom_parser.querySelector("rss > channel")
);
}
} else {
result_list = [];
}
for (i = 0; i < item_list.length; i += 1) {
if ((id === '/0/' + i) || (id === undefined)) {
result_list.push({
id: '/0/' + i,
value: {}
});
if (include) {
result_list[result_list.length - 1].doc =
this.parseElement(item_list[i]);
}
}
}
return result_list;
};
/////////////////////////////////////////////////////////////
// Helpers
/////////////////////////////////////////////////////////////
function getParser(storage) {
return storage._sub_storage.getAttachment(storage._document_id,
storage._attachment_id,
{format: 'text'})
.push(function (txt) {
return new RSSParser(txt);
});
}
/////////////////////////////////////////////////////////////
// Storage
/////////////////////////////////////////////////////////////
function MyParserStorage(spec) {
this._attachment_id = spec.attachment_id;
this._document_id = spec.document_id;
this._parser_name = spec.parser;
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
MyParserStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === 'include');
};
MyParserStorage.prototype.buildQuery = function (options) {
if (options === undefined) {
options = {};
}
return getParser(this)
.push(function (parser) {
return parser.getDocumentList((options.include_docs || false));
});
};
MyParserStorage.prototype.get = function (id) {
return getParser(this)
.push(function (parser) {
var result_list = parser.getDocumentList(true, id);
//console.log(result_list);
if (result_list.length) {
return result_list[0].doc;
}
throw new jIO.util.jIOError(
"Cannot find parsed document: " + id,
404
);
});
};
MyParserStorage.prototype.put = function(id, metadata){
return this._sub_storage.put(id, metadata);
};
MyParserStorage.prototype.putAttachment = function(id, name, blob){
return this._sub_storage.putAttachment(id, name, blob);
};
jIO.addStorage('my_parser', MyParserStorage);
}(jIO, DOMParser, Node));
\ No newline at end of file
......@@ -66,10 +66,26 @@
})
.declareMethod("search", function (key){
return this.state.model_gadget.search()
.push(function (result){
console.log(result);
})
var gadget = this;
return gadget.state.model_gadget.allDocs()
.push(function (result){
var i, j, promise_list = [];
for (i=0; i<result.length; i+=1){
for (j=1; j<result[i].data.rows.length; j+=1){
promise_list.push(gadget.state.model_gadget.get(i, result[i].data.rows[j].id));
}
}
console.log(promise_list.length);
return RSVP.all(promise_list);
}).push(function(result){
console.log("result :");
console.log(result);
});
/*var options = { query:'title:"% mai %"'};
return gadget.state.model_gadget.search(options)
.push(function(result){
console.log("result : "); console.log(result);
});*/
/*var gadget = this,
options;
......
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