Fix datepicker arrows style on hover
[cds-indico.git] / indico / MaKaC / webinterface / pages / conf_calendar.py
blob37fe4dd7dd6070902a89290a781ee8161d9225df
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from copy import copy
18 from datetime import datetime
19 import random
21 from MaKaC import conference
22 from MaKaC.common import timezoneUtils
23 from MaKaC.common.Counter import Counter
24 from MaKaC.webinterface.pages.main import WPMainBase
25 from MaKaC.webinterface import urlHandlers
26 from MaKaC.webinterface import wcomponents
28 from indico.core.config import Config
29 from indico.util.i18n import _, get_current_locale
30 from indico.util.date_time import format_date
33 class WPCalendarBase( WPMainBase ):
35 def __init__( self, rh, calendar, categ=None ):
36 WPMainBase.__init__( self, rh )
37 self._cal = calendar
38 self._categ = categ
39 self._locTZ = timezoneUtils.DisplayTZ(self._getAW(),None,useServerTZ=1).getDisplayTZ()
41 def _getTitle(self):
42 return WPMainBase._getTitle(self) + " - " + _("Calendar Overview")
45 class CategColorList:
46 _colorList = ["#f4dabe", "#bcdff2", "#f7f4c0", "#CCFF99", "#e0baef", "#ecf409", "#FFFFCC" ]
48 def __init__( self, categList ):
49 self._colorDict = {}
50 self._colorIdx = 0
51 for categ in categList:
52 self._colorDict[categ.getId()] = self.getNewColor()
54 def getNewColor( self ):
55 if self._colorIdx >= len( self._colorList ):
56 r = random.randint(130,255)
57 g = random.randint(130,255)
58 b = random.randint(130,255)
59 return "#%X%X%X"%(r,g,b)
60 #return "#CCCCCC"
61 else:
62 color = self._colorList[self._colorIdx]
63 self._colorIdx += 1
64 return color
66 def getColor( self, categ ):
67 if not self._colorDict.has_key( categ.getId() ):
68 return None
69 return self._colorDict[ categ.getId() ]
72 class WCalendarMonthItem:
74 def __init__( self, month, multipleColor, categColors, cal ):
75 self._month = month
76 self._cal = cal
77 self._multipleColor = multipleColor
78 self._categColors = categColors
80 def _getDiv( self, day ):
81 fulldate = "%s%02d%02d" % (self._month.getYear(),self._month.getMonthNumber(),day.getDayNumber())
82 res = [ """
83 <div id="d%s" class="calendarList">
84 <table cellSpacing=0 cellPadding=1 bgcolor="#A0A0A0" border=0>
85 <tr>
86 <td valign=top>
87 <table cellSpacing=0 cellPadding=4 bgcolor="#FFFFFF" border=0>"""%fulldate ]
88 for conf in day.getConferences():
89 categs = self._cal.getConferenceCategories( conf )
90 if len(categs) > 1:
91 colors = ["""
92 <table cellspacing="0" cellpadding="0" border="0" align="left">
93 <tr>"""]
94 for categ in categs:
95 colors.append("""<td bgcolor="%s">&nbsp;</td>"""%self._categColors.getColor( categ ))
96 colors.append("""
97 </tr>
98 </table>""")
99 maincolor = self._multipleColor
100 else:
101 colors = []
102 maincolor = self._categColors.getColor( categs[0] )
103 title = conf.getTitle()
104 res.append("""
105 <tr bgcolor="#ECECEC" %s>
106 <td valign=top>%s<a href="%s">%s</a></td>
107 </tr>""" % (maincolor, "\n".join(colors), urlHandlers.UHConferenceDisplay.getURL(conf), title))
108 res.append("""
109 </table>
110 </td>
111 </tr>
112 </table>
113 </div>""")
114 res.append("""
115 <script type="text/javascript">
116 <!--
117 menudivs["d%s"] = new Object();
118 menudivs["d%s"].id = "d%s";
119 menudivs["d%s"].anchorId = "a%s";
120 menudivs["d%s"].spanId = "s%s";
121 menudivs["d%s"].menuOn = false;
122 menudivs["d%s"].onAnchor = false;
123 //-->
124 </script>
125 """%(fulldate,fulldate,fulldate,fulldate,fulldate,fulldate,fulldate,fulldate,fulldate))
126 return "\n".join(res)
129 def getHTML( self ):
130 res = []
131 divs = []
133 for day in self._month.getDayList():
134 fulldate = "%s%02d%02d" % (self._month.getYear(),self._month.getMonthNumber(),day.getDayNumber())
135 if day.getDayNumber() == 1:
136 for i in range(day.getWeekDay()):
137 res.append("<td></td>")
138 categs = day.getCategories()
139 if len(categs)>1:
140 colors = ["""
141 <table cellspacing="0" cellpadding="0" border="0" align="left">
142 <tr>"""]
143 for categ in categs:
144 colors.append("""<td bgcolor="%s">&nbsp;</td>"""%self._categColors.getColor( categ ))
145 colors.append("""
146 </tr>
147 </table>""")
148 res.append( """<td align="right" bgcolor="%s" onMouseOver="setOnAnchor('d%s')" onMouseOut="clearOnAnchor('d%s')">%s<span style="cursor: default;" id="a%s">%s</span></td>"""%( self._multipleColor, fulldate, fulldate, "\n".join(colors), fulldate, day.getDayNumber() ) )
149 divs.append(self._getDiv(day))
150 elif len(categs) == 1:
151 res.append( """<td align="right" bgcolor="%s" onMouseOver="setOnAnchor('d%s')" onMouseOut="clearOnAnchor('d%s')"><span style="cursor: default;" id="a%s">%s</span></td>"""%( self._categColors.getColor( categs[0] ), fulldate, fulldate, fulldate, day.getDayNumber() ) )
152 divs.append(self._getDiv(day))
153 else:
154 res.append( """<td align="right">%s</td>"""%day.getDayNumber() )
155 if day.getWeekDay() == 6:
156 res.append("""
157 </tr>
158 <tr>\n""")
159 str = """
160 <table cellspacing="1" cellpadding="5">
161 <tr>
162 <td colspan="7" align="center" style="font-size: 1.2em;">%s</b></td>
163 </tr>
164 <tr>
166 </tr>
167 <tr>
169 </tr>
170 </table>
172 """ % (format_date(self._month._date, 'MMM YYYY'),
173 ''.join(list('<td align="right" bgcolor="#CCCCCC">{}</td>'.format(
174 get_current_locale().weekday(wd)[:2]) for wd in xrange(0, 7))),
175 "\n".join(res), "\n".join(divs))
177 return str
179 class WCalendar(wcomponents.WTemplated):
180 _multipleColor = "#DDDDDD"
182 def __init__( self, aw, cal):
183 self._aw = aw
184 self._cal = cal
186 def _getLegendItem( self, color, caption ):
187 str = """<div style="font-size: 15px; margin-bottom: 5px;">
188 <div style="border: 1px solid #AAAAAA; float: left; width: 15px; height: 15px; background-color: %s"></div>
189 <span style="padding-left: 10px">%s</span>
190 </div>
191 """%( color, caption )
192 return str
194 def _getLegend( self, categDisplayURLGen ):
195 cl = CategColorList( self._cal.getCategoryList() )
196 l = []
197 for categ in self._cal.getCategoryList():
198 url = categDisplayURLGen( categ )
199 caption = """<a href="%s">%s</a>"""%( url, categ.getName() )
200 l.append( """%s"""%self._getLegendItem(cl.getColor(categ), caption) )
201 l.append( """%s"""%self._getLegendItem( self._multipleColor, _("Multiple") ) )
203 return """%s """%("".join(l))
205 def _getCalendar( self ):
206 cl = CategColorList( self._cal.getCategoryList() )
207 res = ["<tr>"]
208 idx = 0
209 for month in self._cal.getMonthList():
210 if idx == self._cal.getNrColumns():
211 res.append("</tr>\n<tr>")
212 idx = 0
213 m = WCalendarMonthItem( month, self._multipleColor, cl, self._cal)
214 res.append(""" <td valign="top">%s</td>"""%m.getHTML() )
215 idx += 1
216 res.append("</tr>")
217 return "".join( res )
219 def getVars( self ):
220 vars = wcomponents.WTemplated.getVars( self )
221 locNM = self._cal.getLocator()
222 del locNM["months"]
223 vars["locatorNoMonth"]= locNM.getWebForm()
224 vars["locator"]= self._cal.getLocator().getWebForm()
225 for i in range(6, 13):
226 if self._cal.getNrMonths() == i:
227 vars["def%s"%i] = "selected"
228 else:
229 vars["def%s"%i] = ""
230 vars["legend"] = self._getLegend( urlHandlers.UHCategoryDisplay.getURL )
231 vars["calendar"] = self._getCalendar()
232 vars["selectedmonths"] = int(self._cal.getNrMonths())
233 vars["selectedcolumns"] = int(self._cal.getNrColumns())
234 vars["selectedmonth"] = int(self._cal.getStartDate().strftime("%m"))
235 vars["selectedyear"] = int(self._cal.getStartDate().strftime("%Y"))
236 return vars
239 class WPCalendar(WPCalendarBase):
241 def _getBody(self, params):
242 wc = WCalendar(self._getAW(), self._cal)
243 cat = self._categ
244 catman = conference.CategoryManager()
245 catList = []
246 for cat in self._cal.getCategoryList():
247 while cat.getId() != "0":
248 cat = cat.getOwner()
249 catList.append(cat.getId())
250 params = {"changeMonthsURL": urlHandlers.UHCalendar.getURL(),
251 "categDisplayURL": urlHandlers.UHCategoryDisplay.getURL(self._categ),
252 "selCategsURL": str(urlHandlers.UHCalendarSelectCategories.getURL()),
253 "categoryTitle": self._categ.getTitle(),
254 "currentYear": datetime.now().year,
256 params["selCategsURL"] += "?xs=" + catman.getRoot().getId()
257 for id in catList:
258 params["selCategsURL"] += "&xs=" + id
259 return wc.getHTML(params)
261 def _getNavigationDrawer(self):
262 #link = [{"url": urlHandlers.UHCalendar.getURL([self._categ]), "title": _("Calendar overview")}]
263 pars = {"target": self._categ, "isModif": False}
264 return wcomponents.WNavigationDrawer( pars, type = "Calendar" )
267 class MulSelectCategTree:
269 def __init__( self, aw ):
270 self._aw = aw
271 self._number = Counter()
272 def _getItem( self, categ, level=0 ):
273 if not categ.canView( self._aw ):
274 return ""
275 html = ""
276 number=self._number.newCount()
277 for i in range(level):
278 html = "%s&nbsp;&nbsp;&nbsp;"%html
279 cfg = Config.getInstance()
280 checked = ""
282 if categ in self._selCategs:
283 checked = "checked"
284 title = """<input type="checkbox" name="selCateg" id="%s" onClick="alerta(%s);" value="%s" %s> %s"""%\
285 (number,number,categ.getId(), checked, categ.getName() )
286 if categ in self._expandedCategs:
287 temp = copy( self._expandedCategs )
288 temp.remove( categ )
289 html = """%s<a href="%s"><img src="%s" border="0" alt="fold"></a> %s"""%\
290 ( html, \
291 self._expandURLGen( temp ), \
292 cfg.getSystemIconURL("exploded.png"), \
293 title )
294 for subcat in categ.getSubCategoryList():
295 html = "%s<br>%s"%(html, self._getItem(subcat, level+1) )
296 else:
297 html = """%s<a href="%s"><img src="%s" border="0" alt="unfold"></a> %s"""%\
298 ( html, \
299 self._expandURLGen( self._expandedCategs+[categ] ), \
300 cfg.getSystemIconURL("collapsd.png"), \
301 title )
302 return html
305 def getHTML( self, selected, expanded, expandURLGen ):
306 self._selCategs = selected
307 self._expandedCategs = expanded
308 self._expandURLGen = expandURLGen
309 r = conference.CategoryManager().getRoot()
310 return self._getItem( r )
313 class WCalendarSelectCategs( wcomponents.WTemplated ):
315 def __init__( self, aw, cal ):
316 self._aw = aw
317 self._cal = cal
319 def getHTML( self, expanded, expandURLGen, calendarURL ):
320 self._exp = expanded
321 self._expURLGen = expandURLGen
322 self._calURL = calendarURL
323 return wcomponents.WTemplated.getHTML( self, {} )
325 def getVars( self ):
326 vars = wcomponents.WTemplated.getVars( self )
327 l = self._cal.getLocator()
328 vars["locator"] = l.getWebForm()
329 del l["selCateg"]
330 vars["locatorNoCategs"] = l.getWebForm()
331 vars["categs"] = MulSelectCategTree( self._aw ).getHTML( \
332 self._cal.getCategoryList(), \
333 self._exp, self._expURLGen )
334 vars["calendarURL"] = self._calURL
335 return vars
338 class WPCalendarSelectCategories( WPCalendarBase ):
340 def _getExpandedCategURL( self, exList ):
341 url = urlHandlers.UHCalendarSelectCategories.getURL( self._cal )
342 ex = []
343 for categ in exList:
344 ex.append( categ.getId() )
345 url.addParam( "xs", ex )
346 return url
348 def _getBody( self, params ):
349 wc = WCalendarSelectCategs( self._getAW(), self._cal )
350 return wc.getHTML( params["expanded"], \
351 self._getExpandedCategURL, \
352 urlHandlers.UHCalendar.getURL() )