Il faut écouter l'événement « readystatechange » plutôt que « load » pour récupérer...
[Portfolio.git] / skins / selectioncontext.py
1 ##parameters=
2 from Products.CMFCore.utils import getToolByName
3 from Products.Portfolio.utils import translate
4 def _(message) : return translate(message, context).encode('utf-8')
5 bsize = 20
6 options = {}
7
8 uidh = getToolByName(context, 'portal_uidhandler')
9 utool = getToolByName(context, 'portal_url')
10 atool = getToolByName(context, 'portal_actions')
11 pptool = getToolByName(context, 'portal_photo_print', None)
12 portal_url = utool()
13 selectionUrl = atool.getActionInfo('user/selection')['url']
14 portal = utool.getPortalObject()
15 portalDepth = len(portal.getPhysicalPath())
16 req = context.REQUEST
17 toUrl = req.physicalPathToURL
18 resp = req.RESPONSE
19
20 if traverse_subpath[-1] == 'photo_view_ajax' :
21 photoSubPath = traverse_subpath[:-1]
22 ajax = True
23 else :
24 photoSubPath = traverse_subpath
25 ajax = False
26
27 photo = portal.restrictedTraverse(photoSubPath)
28 photouid = uidh.register(photo)
29
30 lightboxpath = req.SESSION.get('lightboxpath', None)
31 selectionIsLightbox = False
32 if lightboxpath is None :
33 selection = req.SESSION.get('objects_selection', [])
34 else :
35 try :
36 lightbox = portal.restrictedTraverse(lightboxpath)
37 selection = lightbox.getUidList()
38 selectionIsLightbox = True
39 except:
40 req.SESSION.set('lightboxpath', None)
41 selection = req.SESSION.get('objects_selection', [])
42
43 if pptool :
44 buyable = bool(pptool.getPrintingOptionsFor(photo))
45 sd = context.session_data_manager.getSessionData(create=1)
46 cart = sd.get('cart', None)
47 if cart and cart.locked :
48 buyable = False
49 else :
50 buyable = False
51 options['buyable'] = buyable
52
53 infos = []
54 posOfPhoto = 0
55
56 if ajax == True :
57 try : posOfPhoto = selection.index(photouid)
58 except ValueError : pass
59 options['backToContextUrl'] = '%s?b_start:int=%s' % (selectionUrl, posOfPhoto/bsize*bsize)
60 photoPath = list(photo.getPhysicalPath())
61 photoPath.insert(portalDepth, 'selectioncontext')
62 options['lastBcUrl'] = toUrl(photoPath)
63 meth = getattr(photo, 'photo_view_ajax_template')
64 return meth(req, resp, **options)
65
66
67 for i, uid in enumerate(selection) :
68 b = uidh.getBrain(uid)
69 size = b.getThumbnailSize
70 size = {'width':int(size['width']/2.0), 'height':int(size['height']/2.0)}
71
72 if uid == photouid :
73 className = 'selected displayed'
74 posOfPhoto = i
75 else :
76 className = 'selected'
77
78 path = b.getPath().split('/')
79 path.insert(portalDepth, 'selectioncontext')
80
81 d = {'src': '%s/getThumbnail' % b.getURL()
82 ,'href': toUrl(path)
83 ,'thumbSize': size
84 ,'title' : b.Title
85 ,'className': className
86 , 'index': i
87 }
88 infos.append(d)
89
90 if posOfPhoto > 0 :
91 previous = infos[posOfPhoto - 1]['href']
92 else :
93 previous = infos[0]['href']
94
95 if posOfPhoto < len(infos) -1 :
96 next = infos[posOfPhoto + 1]['href']
97 else :
98 next = infos[-1]['href']
99
100
101 contextInfos = {'infos':infos,
102 'isSelected':True,
103 'backUrl' : '%s?b_start:int=%s' % (selectionUrl, posOfPhoto/bsize*bsize),
104 'index' : posOfPhoto,
105 'previous' : previous,
106 'next' : next,
107 'reBaseCtxUrl':'/^%s/' % (portal_url+'/selectioncontext/').replace('/', '\/'),
108 'canonicalUrl': "'%s/'" % portal_url}
109
110 options['contextInfos'] = contextInfos
111
112 # breadcrumbs customization
113 if selectionIsLightbox :
114 lastBcTitle = '%s (%s)' % (_('My selection'), lightbox.title_or_id())
115 else :
116 lastBcTitle = _('My selection')
117
118 breadcrumbs = [
119 { 'id' : 'root'
120 , 'title' : portal.title
121 , 'url' : portal_url},
122
123 {'id' : 'selection_view'
124 ,'title' : lastBcTitle
125 , 'url' : selectionUrl},
126
127 {'id' : photo.getId()
128 ,'title' : photo.title_or_id()
129 , 'url' : req.ACTUAL_URL}
130 ]
131
132 options['breadcrumbs'] = breadcrumbs
133
134
135 ti = photo.getTypeInfo()
136 method_id = ti.queryMethodID('view', context=photo)
137 meth = getattr(photo, method_id)
138 return meth(req, resp, **options)