Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Several TreeWidget improvements
Browse files Browse the repository at this point in the history
- Flare update disallows dropping items onto leafs
- Improving TreeWidget node/leaf fetching

Latter one fixes a race condition causing duplicate directory listing and also simplifies the code. Code was copied to flare with this commit
  • Loading branch information
phorward committed Apr 13, 2022
1 parent 8f6781a commit 02ac57e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 55 deletions.
2 changes: 1 addition & 1 deletion vi/flare
99 changes: 45 additions & 54 deletions vi/widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import logging


from flare.viur.widgets.tree import TreeItemWidget,TreeLeafWidget, TreeNodeWidget
from flare.viur.widgets.tree import TreeItemWidget, TreeLeafWidget, TreeNodeWidget


class TreeWidget(html5.Div):
Expand Down Expand Up @@ -105,7 +105,7 @@ def __init__(self, module, rootNode=None, node=None, context=None, *args, **kwar
"listRootNodes",
self.context or {},
successHandler=self.onSetDefaultRootNode,
failureHandler=self.showErrorMsg
failureHandler=self._showErrorMsg
)

def requestStructure( self ):
Expand Down Expand Up @@ -175,9 +175,6 @@ def onKeyUp(self, event):
self._isShiftPressed = False
self._ctlStartRow = None




def getActions(self):
"""
Returns a list of actions that are being set for the ActionBar.
Expand Down Expand Up @@ -239,7 +236,10 @@ def extendSelection(self, element):
element.addClass("is-focused")

if self.selectionMulti:
self._currentRow = self.entryFrame._children.index(element)
try:
self._currentRow = self.entryFrame.children().index(element)
except ValueError:
self._currentRow = None

self.selectionChangedEvent.fire(self, self.selection)

Expand All @@ -259,7 +259,7 @@ def activateSelection(self, element):
def requestChildren(self, element):
self.loadNode(element.data["key"])

def showErrorMsg(self, req=None, code=None):
def _showErrorMsg(self, req=None, code=None):
"""
Removes all currently visible elements and displayes an error message
"""
Expand Down Expand Up @@ -372,7 +372,7 @@ def collectExpandedNodes(currNode):

self.loadNode(self.rootNode)

def loadNode(self, node, cursor=None, reqType=None, overrideParams=None):
def loadNode(self, node, skelType=None, cursor=None, overrideParams=None):
"""
Fetch the (direct) children of the given node.
Once the list is received, append them to their parent node.
Expand All @@ -381,92 +381,83 @@ def loadNode(self, node, cursor=None, reqType=None, overrideParams=None):
"""
self.node = node

# In case no skelType was provided, reload both.
if skelType is None:
self._currentRequests.clear()

self.loadNode(node, "node")
if self.leafWidget:
self.loadNode(node, "leaf")
return

params = {
"parententry": node,
"skelType": skelType,
"parententry": self.node,
"orderby": "sortindex",
"amount": 99
"limit": 99
}

def nodeReq():
if cursor:
params.update({"cursor": cursor})
if cursor:
params["cursor"] = cursor

if overrideParams:
params.update(overrideParams)
if overrideParams:
params.update(overrideParams)

if self.context:
params.update(self.context)
if self.context:
params.update(self.context)

r = NetworkService.request(self.module, "list/node",
params,
successHandler=self.onRequestSucceded,
failureHandler=self.showErrorMsg)
r.reqType = "node"
r.node = node
self._currentRequests.append(r)

def leafReq():
if self.leafWidget:
if cursor:
params.update({"cursor": cursor})

r = NetworkService.request(self.module, "list/leaf", params,
successHandler=self.onRequestSucceded,
failureHandler=self.showErrorMsg)
r.reqType = "leaf"
r.node = node
self._currentRequests.append(r)

if reqType == 'node':
nodeReq()
elif reqType == 'leaf':
leafReq()
else:
nodeReq()
leafReq()
req = NetworkService.request(
self.module, "list", params,
successHandler=self._onRequestSucceded,
failureHandler=self._showErrorMsg
)
req.skelType = skelType
self._currentRequests.append(req)

def onRequestSucceded(self, req):
def _onRequestSucceded(self, req):
"""
The NetworkRequest for a (sub)node finished.
Create a new HierarchyItem for each entry received and add them to our view
"""
if not req in self._currentRequests:
self.actionBar.resetLoadingState()

if req not in self._currentRequests:
# Prevent inserting old (stale) data
self.actionBar.resetLoadingState()
return

self._currentRequests.remove(req)
data = NetworkService.decode(req)

if req.node == self.rootNode:
if self.node == self.rootNode:
ol = self.entryFrame
else:
tmp = self.itemForKey(req.node)
tmp = self.itemForKey(self.node)
if not tmp:
ol = self.entryFrame
else:
ol = tmp.ol

for skel in data["skellist"]:
if req.reqType == "leaf":
if req.skelType == "leaf":
hi = self.leafWidget(self.module, skel, self.viewLeafStructure, self)
else:
hi = self.nodeWidget(self.module, skel, self.viewNodeStructure, self)

ol.appendChild(hi)

if hi.data["key"] in self._expandedNodes:
hi.toggleExpand()
if not hi.childrenLoaded:
hi.childrenLoaded = True
self.loadNode(hi.data["key"])

ol.sortChildren(self.getChildKey)

if not ol._children and ol != self.entryFrame:
if not ol.children() and ol != self.entryFrame:
ol.parent().addClass("has-no-child")

if data["skellist"] and data["cursor"]:
self.loadNode(req.node, data["cursor"], req.reqType)

self.actionBar.resetLoadingState()
self.loadNode(self.node, req.skelType, data["cursor"])

def onDrop(self, event):
"""
Expand Down

0 comments on commit 02ac57e

Please sign in to comment.