Commit f6d0d95d authored by Ivan Tyagov's avatar Ivan Tyagov

Initial implementation of GadgetCatalog which can handle:

- multiple gadget repositories
- multiple gadgets per repository
- API to select proper gadget based on what it provides.
parent df79a4fd
...@@ -556,11 +556,29 @@ var RenderJs = (function () { ...@@ -556,11 +556,29 @@ var RenderJs = (function () {
/* /*
* Gadget catalog provides API to get list of gadgets from a repository * Gadget catalog provides API to get list of gadgets from a repository
*/ */
var cache_id = "setGadgetIndexUrlList";
function updateGadgetIndexFromURL(url) {
$.ajax({url:url,
async: false, // To simplify update
dataType: "json",
success: function(data) {
// Store remote JSON as it is in local html5 cache
// as we don't need to reinvent another structure yet!
RenderJs.Cache.set(url, data)
}
});
};
return { return {
updateGadgetIndexFromURL: function (url) { updateGadgetIndex: function () {
/* /*
* Update gadget index from a remote repository. * Update gadget index from all configured remote repositories.
*/ */
$.each(RenderJs.GadgetCatalog.getGadgetIndexUrlList(),
function(index, value) {
updateGadgetIndexFromURL(value);
});
}, },
setGadgetIndexUrlList: function (url_list) { setGadgetIndexUrlList: function (url_list) {
...@@ -568,7 +586,6 @@ var RenderJs = (function () { ...@@ -568,7 +586,6 @@ var RenderJs = (function () {
* Set list of Gadget Index repositories. * Set list of Gadget Index repositories.
*/ */
// store in Cache (html5 storage) // store in Cache (html5 storage)
var cache_id = "setGadgetIndexUrlList";
RenderJs.Cache.set(cache_id, url_list) RenderJs.Cache.set(cache_id, url_list)
}, },
...@@ -577,11 +594,10 @@ var RenderJs = (function () { ...@@ -577,11 +594,10 @@ var RenderJs = (function () {
* Get list of Gadget Index repositories. * Get list of Gadget Index repositories.
*/ */
// get from Cache (html5 storage) // get from Cache (html5 storage)
var cache_id = "setGadgetIndexUrlList";
return RenderJs.Cache.get(cache_id, undefined) return RenderJs.Cache.get(cache_id, undefined)
}, },
getGadgetListThatProvide: function (service_list) { getGadgetListThatProvide: function (service) {
/* /*
* Return list of all gadgets that providen a given service. * Return list of all gadgets that providen a given service.
* Read this list from data structure created in HTML5 local * Read this list from data structure created in HTML5 local
...@@ -589,6 +605,21 @@ var RenderJs = (function () { ...@@ -589,6 +605,21 @@ var RenderJs = (function () {
*/ */
// XXX: get from Cache stored index and itterate over it // XXX: get from Cache stored index and itterate over it
// to find matching ones // to find matching ones
var gadget_list = new Array();
$.each(RenderJs.GadgetCatalog.getGadgetIndexUrlList(),
function(index, url) {
// get repos from cache
var cached_repo = RenderJs.Cache.get(url)
$.each(cached_repo['gadget_list'],
function(index, gadget) {
if (jQuery.inArray(service, gadget["service_list"]) > -1) {
// gadget provides a service, add to list
gadget_list.push(gadget);
}
}
)
});
return gadget_list;
}, },
registerServiceList: function (gadget, service_list) { registerServiceList: function (gadget, service_list) {
......
{
"gadget_list":[ {"title": "HTML WYSIWYG",
"description": "A simple HTML editor",
"url": "http://example.com/html-editor.html",
"service_list": ["edit_html", "view_html"]},
{"title": "SVG WYSIWYG",
"description": "A simple SVG editor",
"url": "http://example.com/svg-editor.html",
"service_list": ["edit_svg", "view_svg"]}
]
}
\ No newline at end of file
{"first_name": "John",
"last_name": "Doh"}
\ No newline at end of file
...@@ -24,6 +24,14 @@ function parseJSONAndUpdateNameSpace(result) { ...@@ -24,6 +24,14 @@ function parseJSONAndUpdateNameSpace(result) {
last_name=result['last_name']; last_name=result['last_name'];
} }
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function setupRenderJSTest(){ function setupRenderJSTest(){
/* /*
* Main RenderJS test entry point * Main RenderJS test entry point
...@@ -167,10 +175,31 @@ function setupRenderJSTest(){ ...@@ -167,10 +175,31 @@ function setupRenderJSTest(){
module("GadgetCatalog"); module("GadgetCatalog");
test('GadgetCatalog', function () { test('GadgetCatalog', function () {
cleanUp(); cleanUp();
var url_list = new Array('gadget_index/gagdet_index.json'); // generate random argument to test always with new cache id
var url_list = new Array('gadget_index/gadget_index.json?t='+makeid());
RenderJs.GadgetCatalog.setGadgetIndexUrlList(url_list) RenderJs.GadgetCatalog.setGadgetIndexUrlList(url_list)
deepEqual(url_list, RenderJs.GadgetCatalog.getGadgetIndexUrlList()); deepEqual(url_list, RenderJs.GadgetCatalog.getGadgetIndexUrlList());
RenderJs.GadgetCatalog.updateGadgetIndex();
cached = RenderJs.Cache.get(url_list[0]);
equal("HTML WYSIWYG", cached["gadget_list"][0]["title"]);
deepEqual(["edit_html", "view_html"], cached["gadget_list"][0]["service_list"]);
// check that we can find gadgets that provide some service_list
gadget_list = RenderJs.GadgetCatalog.getGadgetListThatProvide("edit_html");
equal("HTML WYSIWYG", gadget_list[0]["title"]);
deepEqual(["edit_html", "view_html"], gadget_list[0]["service_list"]);
gadget_list = RenderJs.GadgetCatalog.getGadgetListThatProvide("view_html");
equal("HTML WYSIWYG", gadget_list[0]["title"]);
deepEqual(["edit_html", "view_html"], gadget_list[0]["service_list"]);
gadget_list = RenderJs.GadgetCatalog.getGadgetListThatProvide("edit_svg");
equal("SVG WYSIWYG", gadget_list[0]["title"]);
deepEqual(["edit_svg", "view_svg"], gadget_list[0]["service_list"]);
// no such service is provided by gadget repos
equal(0, RenderJs.GadgetCatalog.getGadgetListThatProvide("edit_html1"));
}); });
}; };
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