allow re-ordering of favorites by drag-and-drop
This commit is contained in:
parent
841eb204eb
commit
1d7f01dccb
3 changed files with 56 additions and 19 deletions
|
@ -58,14 +58,21 @@ class FavoriteView(NodeView):
|
|||
def listFavorites(self):
|
||||
if self.favorites is None:
|
||||
return
|
||||
for uid in self.favorites.list(self.person):
|
||||
self.registerDojoDnd()
|
||||
form = self.request.form
|
||||
if 'favorites_change_order' in form:
|
||||
uids = form.get('favorite_uids')
|
||||
if uids:
|
||||
self.favorites.reorder(uids)
|
||||
for trackUid, uid in self.favorites.listWithTracks(self.person):
|
||||
obj = util.getObjectForUid(uid)
|
||||
if obj is not None:
|
||||
yield dict(url=self.getUrlForTarget(obj),
|
||||
uid=uid,
|
||||
title=obj.title,
|
||||
description=obj.description,
|
||||
object=obj)
|
||||
object=obj,
|
||||
trackUid=trackUid)
|
||||
|
||||
def add(self):
|
||||
if self.favorites is None:
|
||||
|
|
|
@ -1,24 +1,42 @@
|
|||
<metal:actions define-macro="favorites_portlet"
|
||||
tal:define="view nocall:context/@@favorites_view;
|
||||
targetUid view/targetUid">
|
||||
<div tal:repeat="item view/listFavorites">
|
||||
<span style="float:right" class="delete-item"> <a href="removeFavorite.html"
|
||||
tal:attributes="href
|
||||
string:${view/virtualTargetUrl}/removeFavorite.html?id=${item/uid};
|
||||
title string:Remove from favorites"
|
||||
i18n:attributes="title">X</a> </span>
|
||||
<a tal:attributes="href item/url;
|
||||
title item/description"
|
||||
tal:content="item/title">Some object</a>
|
||||
<form method="post">
|
||||
<div dojoType="dojo.dnd.Source" withHandles="true" id="favorites_list">
|
||||
<div class="dojoDndItem dojoDndHandle" style="padding: 0"
|
||||
tal:repeat="item view/listFavorites">
|
||||
<span style="float:right" class="delete-item"> <a
|
||||
tal:attributes="href
|
||||
string:${view/virtualTargetUrl}/removeFavorite.html?id=${item/uid};
|
||||
title string:Remove from favorites"
|
||||
i18n:attributes="title">X</a> </span>
|
||||
<a tal:attributes="href item/url;
|
||||
title item/description"
|
||||
tal:content="item/title">Some object</a>
|
||||
<input type="hidden" name="favorite_uids:list"
|
||||
tal:attributes="value item/trackUid" />
|
||||
</div>
|
||||
</div>
|
||||
<div id="addFavorite" class="action"
|
||||
tal:condition="targetUid">
|
||||
<a i18n:translate=""
|
||||
tal:attributes="href
|
||||
string:${view/virtualTargetUrl}/addFavorite.html?id=$targetUid;
|
||||
title string:Add current object to favorites"
|
||||
i18n:attributes="title">Add to Favorites</a>
|
||||
<div>
|
||||
<input type="submit" style="display: none"
|
||||
name="favorites_change_order" id="favorites_change_order"
|
||||
value="Save Changes"
|
||||
i18n:attributes="value" />
|
||||
<script language="javascript">
|
||||
dojo.subscribe('/dnd/drop', function(data) {
|
||||
if (data.node.id == 'favorites_list') {
|
||||
dojo.byId('favorites_change_order').style.display = ''}});
|
||||
</script>
|
||||
</div>
|
||||
</form>
|
||||
<div id="addFavorite" class="action"
|
||||
tal:condition="targetUid">
|
||||
<a i18n:translate=""
|
||||
tal:attributes="href
|
||||
string:${view/virtualTargetUrl}/addFavorite.html?id=$targetUid;
|
||||
title string:Add current object to favorites"
|
||||
i18n:attributes="title">Add to Favorites</a>
|
||||
</div>
|
||||
</metal:actions>
|
||||
|
||||
|
||||
|
|
|
@ -41,12 +41,16 @@ class Favorites(object):
|
|||
for item in self.listTracks(person, sortKey, type):
|
||||
yield item.taskId
|
||||
|
||||
def listWithTracks(self, person, sortKey=None, type='favorite'):
|
||||
for item in self.listTracks(person, sortKey, type):
|
||||
yield util.getUidForObject(item), item.taskId
|
||||
|
||||
def listTracks(self, person, sortKey=None, type='favorite'):
|
||||
if person is None:
|
||||
return
|
||||
personUid = util.getUidForObject(person)
|
||||
if sortKey is None:
|
||||
sortKey = lambda x: -x.timeStamp
|
||||
sortKey = lambda x: (x.data.get('order', 0), -x.timeStamp)
|
||||
for item in sorted(self.context.query(userName=personUid), key=sortKey):
|
||||
if type is not None:
|
||||
if item.type != type:
|
||||
|
@ -77,6 +81,14 @@ class Favorites(object):
|
|||
self.context.removeTrack(track)
|
||||
return changed
|
||||
|
||||
def reorder(self, uids):
|
||||
for idx, uid in enumerate(uids):
|
||||
track = util.getObjectForUid(uid)
|
||||
if track is not None:
|
||||
data = track.data
|
||||
data['order'] = idx
|
||||
track.data = data
|
||||
|
||||
|
||||
class Favorite(Track):
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue