[gen] Static param SomeClass.resultModes replaces 'resultMode' and allows to define the way instances of this class are shown on query results: as a 'list' (the default), a 'grid' or using a custom px that must be defined on SomeClass.

This commit is contained in:
Gaetan Delannay 2015-03-19 18:56:36 +01:00
parent 790f3d4495
commit 588fa9b54f
11 changed files with 176 additions and 74 deletions

View file

@ -162,6 +162,7 @@ class UiSearch:
# Default values for request parameters defining query sort and filter # Default values for request parameters defining query sort and filter
sortFilterDefaults = {'sortKey': '', 'sortOrder': 'asc', sortFilterDefaults = {'sortKey': '', 'sortOrder': 'asc',
'filterKey': '', 'filterValue': ''} 'filterKey': '', 'filterValue': ''}
pxByMode = {'list': 'pxResultList', 'grid': 'pxResultGrid'}
# Rendering a search # Rendering a search
pxView = Px(''' pxView = Px('''
@ -175,55 +176,46 @@ class UiSearch:
# Search results, as a list (used by pxResult below) # Search results, as a list (used by pxResult below)
pxResultList = Px(''' pxResultList = Px('''
<x var="showHeaders=showHeaders|True; <table class="list" width="100%">
checkboxes=uiSearch.search.checkboxes; <!-- Headers, with filters and sort arrows -->
checkboxesId=rootHookId + '_objs'; <tr if="showHeaders">
cbShown=uiSearch.showCheckboxes(); <th if="checkboxes" class="cbCell" style=":'display:%s' % cbDisplay">
cbDisplay=cbShown and 'table-cell' or 'none'"> <img src=":url('checkall')" class="clickable"
<script>:uiSearch.getAjaxData(ajaxHookId, ztool, popup=inPopup, \ title=":_('check_uncheck')"
checkboxes=checkboxes, checkboxesId=checkboxesId, \ onclick=":'toggleAllCbs(%s)' % q(checkboxesId)"/>
cbDisplay=cbDisplay, startNumber=startNumber, \ </th>
totalNumber=totalNumber)</script> <th for="column in columns"
<table class="list" width="100%"> var2="field=column.field;
<!-- Headers, with filters and sort arrows --> sortable=field.isSortable(usage='search');
<tr if="showHeaders"> filterable=field.filterable"
<th if="checkboxes" class="cbCell" style=":'display:%s' % cbDisplay"> width=":column.width" align=":column.align">
<img src=":url('checkall')" class="clickable" <x>::ztool.truncateText(_(field.labelId))</x>
title=":_('check_uncheck')" <x if="(totalNumber &gt; 1) or filterValue">:tool.pxSortAndFilter</x>
onclick=":'toggleAllCbs(%s)' % q(checkboxesId)"/> <x>:tool.pxShowDetails</x>
</th> </th>
<th for="column in columns" </tr>
var2="field=column.field;
sortable=field.isSortable(usage='search');
filterable=field.filterable"
width=":column.width" align=":column.align">
<x>::ztool.truncateText(_(field.labelId))</x>
<x if="(totalNumber &gt; 1) or filterValue">:tool.pxSortAndFilter</x>
<x>:tool.pxShowDetails</x>
</th>
</tr>
<!-- Results --> <!-- Results -->
<tr if="not zobjects"> <tr if="not zobjects">
<td colspan=":len(columns)+1">:_('query_no_result')</td> <td colspan=":len(columns)+1">:_('query_no_result')</td>
</tr> </tr>
<x for="zobj in zobjects" <x for="zobj in zobjects"
var2="rowCss=loop.zobj.odd and 'even' or 'odd'; var2="rowCss=loop.zobj.odd and 'even' or 'odd';
@currentNumber=currentNumber + 1">:zobj.appy().pxViewAsResult</x> @currentNumber=currentNumber + 1">:zobj.appy().pxViewAsResult</x>
</table> </table>
<!-- The button for selecting objects and closing the popup --> <!-- The button for selecting objects and closing the popup -->
<div if="inPopup and cbShown" align=":dleft"> <div if="inPopup and cbShown" align=":dleft">
<input type="button" <input type="button"
var="label=_('object_link_many'); css=ztool.getButtonCss(label)" var="label=_('object_link_many'); css=ztool.getButtonCss(label)"
value=":label" class=":css" style=":url('linkMany', bg=True)" value=":label" class=":css" style=":url('linkMany', bg=True)"
onclick=":'onSelectObjects(%s,%s,%s,%s,%s,%s,%s)' % \ onclick=":'onSelectObjects(%s,%s,%s,%s,%s,%s,%s)' % \
(q(rootHookId), q(uiSearch.initiator.url), \ (q(rootHookId), q(uiSearch.initiator.url), \
q(uiSearch.initiatorMode), q(sortKey), q(sortOrder), \ q(uiSearch.initiatorMode), q(sortKey), q(sortOrder), \
q(filterKey), q(filterValue))"/> q(filterKey), q(filterValue))"/>
</div> </div>
<!-- Init checkboxes if present --> <!-- Init checkboxes if present -->
<script if="checkboxes">:'initCbs(%s)' % q(checkboxesId)</script> <script if="checkboxes">:'initCbs(%s)' % q(checkboxesId)</script>
<script>:'initFocus(%s)' % q(ajaxHookId)</script></x>''') <script>:'initFocus(%s)' % q(ajaxHookId)</script>''')
# Search results, as a grid (used by pxResult below) # Search results, as a grid (used by pxResult below)
pxResultGrid = Px(''' pxResultGrid = Px('''
@ -246,8 +238,12 @@ class UiSearch:
pxResult = Px(''' pxResult = Px('''
<div var="ajaxHookId='queryResult'; <div var="ajaxHookId='queryResult';
className=className|req['className']; className=className|req['className'];
klass=ztool.getAppyClass(className);
searchName=field.name|req.get('search', ''); searchName=field.name|req.get('search', '');
uiSearch=field|ztool.getSearch(className, searchName, ui=True); uiSearch=field|ztool.getSearch(className, searchName, ui=True);
resultMode=uiSearch.getResultMode(klass, req);
customPx=resultMode not in uiSearch.pxByMode;
maxResults=customPx and 'NO_LIMIT' or None;
rootHookId=uiSearch.getRootHookId(); rootHookId=uiSearch.getRootHookId();
refInfo=ztool.getRefInfo(); refInfo=ztool.getRefInfo();
refObject=refInfo[0]; refObject=refInfo[0];
@ -261,11 +257,13 @@ class UiSearch:
filterKey=req.get('filterKey', ''); filterKey=req.get('filterKey', '');
filterValue=req.get('filterValue', ''); filterValue=req.get('filterValue', '');
queryResult=ztool.executeQuery(className, \ queryResult=ztool.executeQuery(className, \
search=uiSearch.search, startNumber=startNumber, \ search=uiSearch.search, startNumber=startNumber, \
remember=True, sortBy=sortKey, sortOrder=sortOrder, \ maxResults=maxResults, remember=True, sortBy=sortKey, \
filterKey=filterKey, filterValue=filterValue, \ sortOrder=sortOrder, filterKey=filterKey, \
refObject=refObject, refField=refField); filterValue=filterValue, refObject=refObject, \
refField=refField);
zobjects=queryResult.objects; zobjects=queryResult.objects;
objects=maxResults and [z.appy() for z in zobjects];
totalNumber=queryResult.totalNumber; totalNumber=queryResult.totalNumber;
batchSize=queryResult.batchSize; batchSize=queryResult.batchSize;
batchNumber=len(zobjects); batchNumber=len(zobjects);
@ -273,10 +271,17 @@ class UiSearch:
newSearchUrl='%s/search?className=%s%s' % \ newSearchUrl='%s/search?className=%s%s' % \
(ztool.absolute_url(), className, refUrlPart); (ztool.absolute_url(), className, refUrlPart);
showSubTitles=req.get('showSubTitles', 'true') == 'true'; showSubTitles=req.get('showSubTitles', 'true') == 'true';
klass=ztool.getAppyClass(className); target=ztool.getLinksTargetInfo(klass);
resultMode=uiSearch.getResultMode(klass); showHeaders=showHeaders|True;
target=ztool.getLinksTargetInfo(klass)" checkboxes=uiSearch.search.checkboxes;
checkboxesId=rootHookId + '_objs';
cbShown=uiSearch.showCheckboxes();
cbDisplay=cbShown and 'table-cell' or 'none'"
id=":ajaxHookId"> id=":ajaxHookId">
<script>:uiSearch.getAjaxData(ajaxHookId, ztool, popup=inPopup, \
checkboxes=checkboxes, checkboxesId=checkboxesId, \
cbDisplay=cbDisplay, startNumber=startNumber, \
totalNumber=totalNumber)</script>
<x if="zobjects or filterValue"> <!-- Pod templates --> <x if="zobjects or filterValue"> <!-- Pod templates -->
<table var="fields=ztool.getResultPodFields(className); <table var="fields=ztool.getResultPodFields(className);
@ -303,20 +308,18 @@ class UiSearch:
<span class="discreet">:uiSearch.translatedDescr</span><br/> <span class="discreet">:uiSearch.translatedDescr</span><br/>
</td> </td>
<!-- (Top) navigation --> <!-- (Top) navigation -->
<td align=":dright" width="200px">:tool.pxNavigate</td> <td if="not customPx"
align=":dright" width="200px">:tool.pxNavigate</td>
</tr> </tr>
</table> </table>
<!-- Results, as a list or grid --> <!-- Results -->
<x var="columnLayouts=ztool.getResultColumnsLayouts(className, refInfo); <x var="columnLayouts=ztool.getResultColumnsLayouts(className, refInfo);
columns=ztool.getColumnsSpecifiers(className,columnLayouts,dir); columns=ztool.getColumnsSpecifiers(className,columnLayouts,dir);
currentNumber=0"> currentNumber=0"><x>:uiSearch.getPx(resultMode, klass)</x></x>
<x if="resultMode == 'list'">:uiSearch.pxResultList</x>
<x if="resultMode != 'list'">:uiSearch.pxResultGrid</x>
</x>
<!-- (Bottom) navigation --> <!-- (Bottom) navigation -->
<x>:tool.pxNavigate</x> <x if="not customPx">:tool.pxNavigate</x>
</x> </x>
<x if="not zobjects and not filterValue"> <x if="not zobjects and not filterValue">
@ -373,10 +376,22 @@ class UiSearch:
Else, simply return the name of the search.''' Else, simply return the name of the search.'''
return getattr(self, 'initiatorHook', self.name) return getattr(self, 'initiatorHook', self.name)
def getResultMode(self, klass): def getAllResultModes(self, klass):
'''Must we show, on pxResult, instances of p_klass as a list or '''How must we show the result? As a list, grid, or a custom px?'''
as a grid?''' return getattr(klass, 'resultModes', ('list',))
return getattr(klass, 'resultMode', 'list')
def getResultMode(self, klass, req):
'''Get the current result mode'''
res = req.get('resultMode')
if not res: res = self.getAllResultModes(klass)[0]
return res
def getPx(self, mode, klass):
'''What is the PX to show, according to the current result p_mode?'''
if mode in UiSearch.pxByMode:
return getattr(UiSearch, UiSearch.pxByMode[mode])
# It must be a custom PX on p_klass
return getattr(klass, mode)
def showCheckboxes(self): def showCheckboxes(self):
'''If checkboxes are enabled for this search (and if an initiator field '''If checkboxes are enabled for this search (and if an initiator field
@ -420,4 +435,9 @@ class UiSearch:
return "new AjaxData('%s', 'pxViewAsResultFromAjax', %s, '%s', '%s')"% \ return "new AjaxData('%s', 'pxViewAsResultFromAjax', %s, '%s', '%s')"% \
(hook, sutils.getStringDict(params), parentHook, (hook, sutils.getStringDict(params), parentHook,
zobj.absolute_url()) zobj.absolute_url())
def getModeText(self, mode, _):
'''Gets the i18n text corresponding to p_mode'''
if mode in UiSearch.pxByMode: return _('result_mode_%s' % mode)
return _('custom_%s' % mode)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -795,3 +795,11 @@ msgstr "${site} - ${title} - ${template}"
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." msgstr "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
#. Default: "List"
msgid "result_mode_list"
msgstr "List"
#. Default: "Grid"
msgid "result_mode_grid"
msgstr "Grid"

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -795,3 +795,11 @@ msgstr "${site} - ${title} - ${template}"
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "Bonjour, cet email vous est envoyé depuis ${site}. Veuillez consulter le(s) fichier(s) joint(s)." msgstr "Bonjour, cet email vous est envoyé depuis ${site}. Veuillez consulter le(s) fichier(s) joint(s)."
#. Default: "List"
msgid "result_mode_list"
msgstr "Liste"
#. Default: "Grid"
msgid "result_mode_grid"
msgstr "Grille"

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -794,3 +794,11 @@ msgstr ""
#. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)." #. Default: "Hello, this email that was sent to you via ${site}. Please consult the attached file(s)."
msgid "podmail_body" msgid "podmail_body"
msgstr "" msgstr ""
#. Default: "List"
msgid "result_mode_list"
msgstr ""
#. Default: "Grid"
msgid "result_mode_grid"
msgstr ""

View file

@ -56,11 +56,16 @@ function len(dict) {
return res; return res;
} }
function switchLanguage(selectWidget, siteUrl) { function switchLanguage(select, siteUrl) {
var language = selectWidget.options[selectWidget.selectedIndex].value; var language = select.options[select.selectedIndex].value;
goto(siteUrl + '/config/changeLanguage?language=' + language); goto(siteUrl + '/config/changeLanguage?language=' + language);
} }
function switchResultMode(select, hook) {
var mode = select.options[select.selectedIndex].value;
askAjax(hook, null, {'resultMode': mode});
}
var isIe = (navigator.appName == "Microsoft Internet Explorer"); var isIe = (navigator.appName == "Microsoft Internet Explorer");
function getElementsHavingName(tag, name) { function getElementsHavingName(tag, name) {
@ -308,11 +313,14 @@ function askAjax(hook, form, params, waiting) {
} }
else var mode = d.mode; else var mode = d.mode;
// Get p_params if given. Note that they override anything else. // Get p_params if given. Note that they override anything else.
if (params && ('mode' in params)) { var px = d.px;
mode = params['mode']; delete params['mode'] } if (params) {
if (params) { for (var key in params) d.params[key] = params[key]; } if ('mode' in params) { mode = params['mode']; delete params['mode'] };
askAjaxChunk(hook, mode, d.url, d.px, d.params, d.beforeSend, if ('px' in params) { px = params['px']; delete params['px'] };
evalInnerScripts, waiting); for (var key in params) d.params[key] = params[key];
}
askAjaxChunk(hook, mode, d.url, px, d.params, d.beforeSend, evalInnerScripts,
waiting);
} }
function askBunch(hookId, startNumber) { function askBunch(hookId, startNumber) {

View file

@ -344,11 +344,21 @@ class ToolWrapper(AbstractWrapper):
<div var="className=req['className']; <div var="className=req['className'];
searchName=req.get('search', ''); searchName=req.get('search', '');
uiSearch=ztool.getSearch(className, searchName, ui=True); uiSearch=ztool.getSearch(className, searchName, ui=True);
klass=ztool.getAppyClass(className);
resultModes=uiSearch.getAllResultModes(klass);
rootHookId=uiSearch.getRootHookId(); rootHookId=uiSearch.getRootHookId();
cssJs=None" cssJs=None"
id=":rootHookId"> id=":rootHookId">
<script>:uiSearch.getCbJsInit(rootHookId)</script> <script>:uiSearch.getCbJsInit(rootHookId)</script>
<x>:tool.pxPagePrologue</x><x>:uiSearch.pxResult</x> <x>:tool.pxPagePrologue</x>
<div align=":dright" if="len(resultModes) &gt; 1">
<select name="px"
onchange=":'switchResultMode(this, %s)' % q('queryResult')">
<option for="mode in resultModes"
value=":mode">:uiSearch.getModeText(mode, _)</option>
</select>
</div>
<x>:uiSearch.pxResult</x>
</div>''', template=AbstractWrapper.pxTemplate, hook='content') </div>''', template=AbstractWrapper.pxTemplate, hook='content')
pxSearch = Px(''' pxSearch = Px('''