Commit e35862e0 authored by Ivan Tyagov's avatar Ivan Tyagov

When remove a gadget make sure its siblings gadgets get remove as well

so they don't become orphans.
TabbulkarGadget keeps track of most recent gadget being used and cleans
it up when new one is added, thus reducing trash in Gadgetindex and DOM.
parent a8f1951c
......@@ -325,7 +325,7 @@ var RenderJs = (function () {
].join('\n');
tab_container.append(html_string);
tab_gadget = tab_container.find(".gadget");
tab_gadget = tab_container.find('#' + gadget_id);
// render new gadget
RenderJs.bootstrap(tab_container);
......@@ -462,9 +462,16 @@ var RenderJs = (function () {
/*
* Remove gadget (including its DOM element).
*/
// unregister from GadgetIndex
var gadget;
// unregister root from GadgetIndex
RenderJs.GadgetIndex.unregisterGadget(this);
// remove its DOM element
// gadget might contain sub gadgets so before remove entire
// DOM we must unregister them from GadgetIndex
this.getDom().find("[data-gadget]").each( function () {
gadget = RenderJs.GadgetIndex.getGadgetById($(this).attr("id"));
RenderJs.GadgetIndex.unregisterGadget(gadget);
});
// remove root's entire DOM element
$(this.getDom()).remove();
};
},
......@@ -473,6 +480,7 @@ var RenderJs = (function () {
/*
* Generic tabular gadget
*/
var gadget_list = [];
return {
toggleVisibility: function (visible_dom) {
/*
......@@ -493,7 +501,18 @@ var RenderJs = (function () {
tab_gadget = RenderJs.addGadget(
dom_id, gadget_id, gadget, gadget_data_handler, gadget_data_source
);
// XXX: we should unregister all gadgets (if any we replace now in DOM)
// we should unregister all gadgets part of this TabbularGadget
$.each(gadget_list,
function (index, gadget_id) {
var gadget = RenderJs.GadgetIndex.getGadgetById(gadget_id);
gadget.remove();
// update list of root gadgets inside TabbularGadget
gadget_list.splice($.inArray(gadget_id, gadget_list), 1);
}
);
// add it as root gadget
gadget_list.push(tab_gadget.attr("id"));
}
};
}()),
......
......@@ -93,6 +93,7 @@ function setupRenderJSTest(){
root_gadget.remove();
equal(RenderJs.GadgetIndex.getGadgetList().length, 0);
equal(0, $("#new-gadget").length);
// XXX: test removing a root gadget also removes its sibling gadgets and their DOM
});
});
......@@ -123,6 +124,8 @@ function setupRenderJSTest(){
equal($("#qunit-fixture").children("#new_added").length, 1);
equal(RenderJs.GadgetIndex.getGadgetList().length, 1);
equal(RenderJs.GadgetIndex.getRootGadget().getDom().attr("id"), "new_added");
// XXX: test adding a new tab gadget and that old one was removed from
// both DOM and GadgetIndex
});
});
......
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