~divmod-dev/divmod.org/remove-third-level-2038

« back to all changes in this revision

Viewing changes to Nevow/nevow/js/Nevow/Athena/__init__.js

  • Committer: glyph
  • Date: 2007-04-01 05:03:51 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:11948
Add a public API to remove athena widgets from a page.

This change adds the Athena.Widget.detach method, among others, to allow
widgets to be cleanly removed from a page.

I am merging this myself since exarkun is gone for a week.

Author: exarkun

Reviewer: glyph

Fixes #2016

Show diffs side-by-side

added added

removed removed

Lines of Context:
628
628
Nevow.Athena.NoSuchMethod = Divmod.Error.subclass("Nevow.Athena.NoSuchMethod");
629
629
 
630
630
 
 
631
/**
 
632
 * Error class thrown when an attempt is made to remove a child from a widget
 
633
 * which is not its parent.
 
634
 */
 
635
Nevow.Athena.NoSuchChild = Divmod.Error.subclass("Nevow.Athena.NoSuchChild");
 
636
 
631
637
Nevow.Athena.Widget = Nevow.Athena.RemoteReference.subclass('Nevow.Athena.Widget');
632
638
Nevow.Athena.Widget.methods(
633
639
    function __init__(self, widgetNode) {
788
794
    },
789
795
 
790
796
    /**
 
797
     * Remove the given child widget from this widget.
 
798
     *
 
799
     * @param child: A widget which is currently a child of this widget.
 
800
     */
 
801
    function removeChildWidget(self, child) {
 
802
        for (var i = 0; i < self.childWidgets.length; ++i) {
 
803
            if (self.childWidgets[i] === child) {
 
804
                self.childWidgets.splice(i, 1);
 
805
                child.setWidgetParent(null);
 
806
                return;
 
807
            }
 
808
        }
 
809
        throw Nevow.Athena.NoSuchChild();
 
810
    },
 
811
 
 
812
    /**
791
813
     * Remove all the child widgets from this widget.
792
814
     */
793
815
    function removeAllChildWidgets(self) {
795
817
            self.childWidgets[i].setWidgetParent(null);
796
818
        }
797
819
        self.childWidgets = [];
 
820
    },
 
821
 
 
822
    /**
 
823
     * Locally remove this widget from its parent and from the general widget
 
824
     * tracking system.
 
825
     */
 
826
    function _athenaDetachClient(self) {
 
827
        for (var i = 0; i < self.childWidgets.length; ++i) {
 
828
            self.childWidgets[i]._athenaDetachClient();
 
829
        }
 
830
        if (self.widgetParent !== null) {
 
831
            self.widgetParent.removeChildWidget(self);
 
832
        }
 
833
        delete Nevow.Athena.Widget._athenaWidgets[self.objectID];
 
834
        self.detached();
 
835
    },
 
836
 
 
837
    /**
 
838
     * Disconnect this widget from its server-side component and remove it from
 
839
     * the tracking collection.
 
840
     *
 
841
     * This function will *not* work correctly if the parent/child
 
842
     * relationships of this widget do not exactly match the parent/child
 
843
     * relationships of the corresponding fragments or elements on the server.
 
844
     */
 
845
    function detach(self) {
 
846
        var result = self.callRemote('_athenaDetachServer');
 
847
        result.addCallback(
 
848
            function(ignored) {
 
849
                self._athenaDetachClient();
 
850
            });
 
851
        return result;
 
852
    },
 
853
 
 
854
    /**
 
855
     * Application-level callback invoked when L{detach} succeeds or when the
 
856
     * server invokes the detach logic from its side.
 
857
     *
 
858
     * This is invoked after this widget has been disassociated from its parent
 
859
     * and from the page.
 
860
     *
 
861
     * Override this.
 
862
     */
 
863
    function detached(self) {
798
864
    });
799
865
 
800
866