[PATCH 4 of 5 saem_ref v3] Use a jQuery widget to make some lists sortable

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 5 17:41:04 CEST 2018


Frank Bessou a écrit :
> # HG changeset patch
> # User Frank Bessou <frank.bessou at logilab.fr>
> # Date 1537867860 -7200
> #      Tue Sep 25 11:31:00 2018 +0200
> # Node ID c628bbf06784ede4676bd5652a4995db2ced31cb
> # Parent  82bc0a62a379c8677c7863b320b81198e443144a
> # Available At http://hg.logilab.org/review/cubes/saem_ref
> #              hg pull http://hg.logilab.org/review/cubes/saem_ref -r c628bbf06784
> # EXP-Topic CONSEJIRA-578-584
> Use a jQuery widget to make some lists sortable
> 
> This jQuery widget calls a new ajax controller which uses ISortable
> properties to sort elements in database.
> 
> We add a 'collection' property to Sortable adapter which returns a list
> the list of entities which are in the same collection (related to a
> common parent through the same relation).
> 
> We make RelatedEntitiesListView use a listvid property to select which
> view must be used to display a given list.
> The SortableListView adds js corresponding to sorting and adds the
> needed attributes on the generated markup.
> 

> diff --git a/cubicweb_saem_ref/views/__init__.py b/cubicweb_saem_ref/views/__init__.py
> --- a/cubicweb_saem_ref/views/__init__.py
> +++ b/cubicweb_saem_ref/views/__init__.py
> @@ -17,6 +17,8 @@
>  
>  from functools import wraps
>  
> +from six import text_type
> +
>  from logilab.common.decorators import monkeypatch
>  from logilab.mtconverter import xml_escape
>  from logilab.common.registry import yes
> @@ -26,6 +28,7 @@ from cubicweb import NoSelectableObject,
>  from cubicweb.utils import json_dumps, js_href
>  from cubicweb.uilib import domid, js
>  from cubicweb.view import EntityView
> +from cubicweb.web.views import ajaxcontroller
>  from cubicweb.predicates import (has_permission, is_instance, multi_lines_rset, match_kwargs,
>                                   partial_has_related_entities)
>  from cubicweb.web import component, formwidgets as fw
> @@ -300,6 +303,24 @@ class SAEMHTMLPageFooter(basetemplates.H
>                 logo_mairie=self._cw.data_url('mairie_bx.jpg')))
>  
>  
> +class SortableListView(baseviews.ListView):
> +    __regid__ = 'sortable-list'
> +    __select__ = baseviews.ListView.__select__ & match_kwargs('listid')
> +
> +    def call(self, **kwargs):
> +        self._cw.add_js('cubes.saem_ref.js')
> +        listid = kwargs['listid']
> +        self._cw.add_onload(
> +            js.saem.makeSortable(listid, kwargs['rtype'])
> +        )
> +        super(SortableListView, self).call(**kwargs)
> +
> +    def cell_call(self, row, col=0, vid=None, klass=None, **kwargs):
> +        self.w(u'<li data-eid={} >'.format(self.cw_rset[row][0]))
> +        self.wview(self.item_vid, self.cw_rset, row=row, col=col, vid=vid, **kwargs)
> +        self.w(u'</li>')
> +
> +
>  class RelatedEntitiesListView(EntityView):
>      """Abstract entity view for displaying an related entities in a list.
>  
> @@ -312,6 +333,7 @@ class RelatedEntitiesListView(EntityView
>      rtype = None
>      role = 'object'
>      subvid = 'saem.listitem'
> +    listvid = 'list'
>      target_etype = None
>      subvid_kwargs = None
>  
> @@ -340,7 +362,10 @@ class RelatedEntitiesListView(EntityView
>              self.divid = u'{}{}'.format(domid(self.__regid__), entity.eid)
>              self.w(u'<div id="{}">'.format(self.divid))
>              self.do_paginate(rset=rset)
> -            self._cw.view('list', rset=rset, w=self.w, subvid=self.subvid, **kwargs)
> +            listid = self.divid + "_list"
> +            kwargs['rtype'] = self.rtype
> +            self._cw.view(self.listvid, rset=rset, w=self.w,
> +                          subvid=self.subvid, listid=listid, **kwargs)
>              self.w(u'</div>')
>  
>      def page_navigation_url(self, navcomp, _path, params):
> @@ -478,6 +503,15 @@ class RestPathEvaluator(urlpublishing.Re
>          return super(RestPathEvaluator, self).set_vid_for_rset(req, cls, rset)
>  
>  
> + at ajaxcontroller.ajaxfunc()

Can you add output_type='json'? Otherwise, we get an error in browser
console. (I suspect the default output type is xhtml.)

I tested in the UI and it works, so modulo remarks on the previous
patch, it's ok by me.

> +def sort_relation_targets(self, rtype, eids):
> +    first_item = self._cw.entity_from_eid(eids[0]).cw_adapt_to('ISortable')
> +    collection = first_item.collection
> +    for item in collection:
> +        index = next(idx for idx, v in enumerate(eids) if v == text_type(item.eid))
> +        item.cw_set(index=index)
> +
> +
>  def registration_callback(vreg):
>      from cubicweb.web.views import actions, cwuser, tableview, undohistory
>      vreg.register_all(globals().values(), __name__, (



More information about the saem-devel mailing list