"""Bases HTML components:* the rql input form* the logged user link* pdf view link:organization: Logilab:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"_=unicodefromlogilab.mtconverterimportxml_escapefromrqlimportparsefromcubicweb.selectorsimportyes,two_etypes_rset,match_form_paramsfromcubicweb.schemaimportdisplay_namefromcubicweb.uilibimporttoggle_actionfromcubicweb.webimportcomponentfromcubicweb.web.htmlwidgetsimport(MenuWidget,PopupBoxMenu,BoxSeparator,BoxLink)VISIBLE_PROP_DEF={_('visible'):dict(type='Boolean',default=True,help=_('display the component or not')),}classRQLInputForm(component.Component):"""build the rql input form, usually displayed in the header"""__regid__='rqlinput'cw_property_defs=VISIBLE_PROP_DEFvisible=Falsedefcall(self,view=None):ifhasattr(view,'filter_box_context_info'):rset=view.filter_box_context_info()[0]else:rset=self.cw_rset# display multilines query as one linerql=rsetisnotNoneandrset.printable_rql(encoded=False)orself._cw.form.get('rql','')rql=rql.replace(u"\n",u" ")req=self._cwself.w(u'''<div id="rqlinput" class="%s"> <form action="%s"><fieldset><input type="text" id="rql" name="rql" value="%s" title="%s" tabindex="%s" accesskey="q" class="searchField" /><input type="submit" value="" class="rqlsubmit" tabindex="%s" /></fieldset>'''%(notself.cw_propval('visible')and'hidden'or'',self._cw.build_url('view'),xml_escape(rql),req._('full text or RQL query'),req.next_tabindex(),req.next_tabindex()))ifself._cw.search_state[0]!='normal':self.w(u'<input type="hidden" name="__mode" value="%s"/>'%':'.join(req.search_state[1]))self.w(u'</form></div>')classApplLogo(component.Component):"""build the instance logo, usually displayed in the header"""__regid__='logo'cw_property_defs=VISIBLE_PROP_DEF# don't want user to hide this component using an cwpropertysite_wide=Truedefcall(self):self.w(u'<a href="%s"><img class="logo" src="%s" alt="logo"/></a>'%(self._cw.base_url(),self._cw.external_resource('LOGO')))classApplHelp(component.Component):"""build the help button, usually displayed in the header"""__regid__='help'cw_property_defs=VISIBLE_PROP_DEFdefcall(self):self.w(u'<a href="%s" class="help" title="%s"> </a>'%(self._cw.build_url(_restpath='doc/main'),self._cw._(u'help'),))classUserLink(component.Component):"""if the user is the anonymous user, build a link to login else a link to the connected user object with a loggout link """cw_property_defs=VISIBLE_PROP_DEF# don't want user to hide this component using an cwpropertysite_wide=True__regid__='loggeduserlink'defcall(self):ifnotself._cw.cnx.anonymous_connection:# display useractions and siteactionsactions=self._cw.vreg['actions'].possible_actions(self._cw,rset=self.cw_rset)box=MenuWidget('','userActionsBox',_class='',islist=False)menu=PopupBoxMenu(self._cw.user.login,isitem=False)box.append(menu)foractioninactions.get('useractions',()):menu.append(BoxLink(action.url(),self._cw._(action.title),action.html_class()))ifactions.get('useractions')andactions.get('siteactions'):menu.append(BoxSeparator())foractioninactions.get('siteactions',()):menu.append(BoxLink(action.url(),self._cw._(action.title),action.html_class()))box.render(w=self.w)else:self.anon_user_link()defanon_user_link(self):ifself._cw.vreg.config['auth-mode']=='cookie':self.w(self._cw._('anonymous'))self.w(u''' [<a class="logout" href="javascript: popupLoginBox();">%s</a>]'''%(self._cw._('i18n_login_popup')))else:self.w(self._cw._('anonymous'))self.w(u' [<a class="logout" href="%s">%s</a>]'%(self._cw.build_url('login'),self._cw._('login')))classApplicationMessage(component.Component):"""display messages given using the __message parameter into a special div section """__select__=yes()__regid__='applmessages'# don't want user to hide this component using an cwpropertycw_property_defs={}defcall(self):msgs=[msgformsgin(self._cw.get_shared_data('sources_error',pop=True),self._cw.message)ifmsg]self.w(u'<div id="appMsg" onclick="%s" class="%s">\n'%(toggle_action('appMsg'),(msgsand' 'or'hidden')))formsginmsgs:self.w(u'<div class="message" id="%s">%s</div>'%(self.div_id(),msg))self.w(u'</div>')classApplicationName(component.Component):"""display the instance name"""__regid__='appliname'cw_property_defs=VISIBLE_PROP_DEF# don't want user to hide this component using an cwpropertysite_wide=Truedefcall(self):title=self._cw.property_value('ui.site-title')iftitle:self.w(u'<span id="appliName"><a href="%s">%s</a></span>'%(self._cw.base_url(),xml_escape(title)))classSeeAlsoVComponent(component.RelatedObjectsVComponent):"""display any entity's see also"""__regid__='seealso'context='navcontentbottom'rtype='see_also'role='subject'order=40# register msg not generated since no entity use see_also in cubicweb itselftitle=_('contentnavigation_seealso')help=_('contentnavigation_seealso_description')classEtypeRestrictionComponent(component.Component):"""displays the list of entity types contained in the resultset to be able to filter accordingly. """__regid__='etypenavigation'__select__=two_etypes_rset()|match_form_params('__restrtype','__restrtypes','__restrrql')cw_property_defs=VISIBLE_PROP_DEF# don't want user to hide this component using an cwpropertysite_wide=Truevisible=False# disabled by defaultdefcall(self):_=self._cw._self.w(u'<div id="etyperestriction">')restrtype=self._cw.form.get('__restrtype')restrtypes=self._cw.form.get('__restrtypes','').split(',')restrrql=self._cw.form.get('__restrrql')ifnotrestrrql:rqlst=self.cw_rset.syntax_tree()restrrql=rqlst.as_string(self._cw.encoding,self.cw_rset.args)restrtypes=self.cw_rset.column_types(0)else:rqlst=parse(restrrql)html=[]on_etype=Falseetypes=sorted((display_name(self._cw,etype).capitalize(),etype)foretypeinrestrtypes)forelabel,etypeinetypes:ifetype==restrtype:html.append(u'<span class="selected">%s</span>'%elabel)on_etype=Trueelse:rqlst.save_state()forselectinrqlst.children:select.add_type_restriction(select.selection[0],etype)newrql=rqlst.as_string(self._cw.encoding,self.cw_rset.args)url=self._cw.build_url(rql=newrql,__restrrql=restrrql,__restrtype=etype,__restrtypes=','.join(restrtypes))html.append(u'<span><a href="%s">%s</a></span>'%(xml_escape(url),elabel))rqlst.recover()ifon_etype:url=self._cw.build_url(rql=restrrql)html.insert(0,u'<span><a href="%s">%s</a></span>'%(url,_('Any')))else:html.insert(0,u'<span class="selected">%s</span>'%_('Any'))self.w(u' | '.join(html))self.w(u'</div>')classPdfViewComponent(component.EntityVComponent):__regid__='pdfview'context='ctxtoolbar'defcell_call(self,row,col,view):entity=self.cw_rset.get_entity(row,col)url=entity.absolute_url(vid=view.__regid__,__template='pdf-main-template')iconurl=self._cw.build_url('data/pdf_icon.gif')label=self._cw._('Download page as pdf')self.w(u'<a href="%s" title="%s" class="toolbarButton"><img src="%s" alt="%s"/></a>'%(xml_escape(url),label,iconurl,label))classMetaDataComponent(component.EntityVComponent):__regid__='metadata'context='navbottom'order=1defcell_call(self,row,col,view=None):self.wview('metadata',self.cw_rset,row=row,col=col)defregistration_callback(vreg):vreg.register_all(globals().values(),__name__,(SeeAlsoVComponent,))if'see_also'invreg.schema:vreg.register(SeeAlsoVComponent)