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
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
var RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING = true; var RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING = true;
// by default RenderJs will examine and bind all interaction gadgets // by default RenderJs will examine and bind all interaction gadgets
// available // available
var RENDERJS_ENABLE_IMPLICIT_INTERACTION_BIND = true; var RENDERJS_ENABLE_IMPLICIT_INTERACTION_BIND = true;
// by default RenderJs will examine and create all routes // by default RenderJs will examine and create all routes
...@@ -276,7 +276,7 @@ var RenderJs = (function () { ...@@ -276,7 +276,7 @@ var RenderJs = (function () {
updateGadgetData: function (gadget) { updateGadgetData: function (gadget) {
/* /*
* Gadget can be updated from "data-gadget-source" (i.e. a json) * Gadget can be updated from "data-gadget-source" (i.e. a json)
* and "data-gadget-handler" attributes (i.e. a namespace Javascript) * and "data-gadget-handler" attributes (i.e. a namespace Javascript)
*/ */
var data_source, data_handler; var data_source, data_handler;
data_source = gadget.attr("data-gadget-source"); data_source = gadget.attr("data-gadget-source");
...@@ -325,7 +325,7 @@ var RenderJs = (function () { ...@@ -325,7 +325,7 @@ var RenderJs = (function () {
].join('\n'); ].join('\n');
tab_container.append(html_string); tab_container.append(html_string);
tab_gadget = tab_container.find(".gadget"); tab_gadget = tab_container.find('#' + gadget_id);
// render new gadget // render new gadget
RenderJs.bootstrap(tab_container); RenderJs.bootstrap(tab_container);
...@@ -462,9 +462,16 @@ var RenderJs = (function () { ...@@ -462,9 +462,16 @@ var RenderJs = (function () {
/* /*
* Remove gadget (including its DOM element). * Remove gadget (including its DOM element).
*/ */
// unregister from GadgetIndex var gadget;
// unregister root from GadgetIndex
RenderJs.GadgetIndex.unregisterGadget(this); 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(); $(this.getDom()).remove();
}; };
}, },
...@@ -473,6 +480,7 @@ var RenderJs = (function () { ...@@ -473,6 +480,7 @@ var RenderJs = (function () {
/* /*
* Generic tabular gadget * Generic tabular gadget
*/ */
var gadget_list = [];
return { return {
toggleVisibility: function (visible_dom) { toggleVisibility: function (visible_dom) {
/* /*
...@@ -493,7 +501,18 @@ var RenderJs = (function () { ...@@ -493,7 +501,18 @@ var RenderJs = (function () {
tab_gadget = RenderJs.addGadget( tab_gadget = RenderJs.addGadget(
dom_id, gadget_id, gadget, gadget_data_handler, gadget_data_source 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"));
} }
}; };
}()), }()),
...@@ -706,7 +725,7 @@ var RenderJs = (function () { ...@@ -706,7 +725,7 @@ var RenderJs = (function () {
/* /*
* Bind event between gadgets. * Bind event between gadgets.
*/ */
var gadget_id, gadget_connection_list, var gadget_id, gadget_connection_list,
createMethodInteraction = function ( createMethodInteraction = function (
original_source_method_id, source_gadget_id, original_source_method_id, source_gadget_id,
source_method_id, destination_gadget_id, source_method_id, destination_gadget_id,
......
...@@ -93,6 +93,7 @@ function setupRenderJSTest(){ ...@@ -93,6 +93,7 @@ function setupRenderJSTest(){
root_gadget.remove(); root_gadget.remove();
equal(RenderJs.GadgetIndex.getGadgetList().length, 0); equal(RenderJs.GadgetIndex.getGadgetList().length, 0);
equal(0, $("#new-gadget").length); 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(){ ...@@ -123,6 +124,8 @@ function setupRenderJSTest(){
equal($("#qunit-fixture").children("#new_added").length, 1); equal($("#qunit-fixture").children("#new_added").length, 1);
equal(RenderJs.GadgetIndex.getGadgetList().length, 1); equal(RenderJs.GadgetIndex.getGadgetList().length, 1);
equal(RenderJs.GadgetIndex.getRootGadget().getDom().attr("id"), "new_added"); 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