Attachment 'ImageBrowserContent.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ImageBrowserContent Macro
   4     Version 0.1
   5 
   6     An image browser with a content panel which avoids surge protection.
   7     
   8     [[ImageBrowserContent(index, file0, file1, ...)]]
   9     [[ImageBrowserContent(file0, file1, ...)]]
  10     [[ImageBrowserContent(index)]]
  11     [[ImageBrowserContent]]
  12     
  13     Where index is the 0 based index of the image to show by default, and
  14           file0, file1, ... are the images to display
  15 
  16     If omitted, index defaults to 0 (the first image) and file0, file1, ...
  17     defaults to all image attachments on the current page.
  18 
  19     Comment line 119 and uncomment 120 if you don't want to use the Thumbnail
  20     action (Javascript comment, not Python).
  21 
  22     @copyright: 2007 by Kenneth Bull
  23     @license: GNU GPL, see COPYING for details.
  24 
  25 """
  26 
  27 def parseArgs(args):
  28     if args == None:
  29         return []
  30     params = [""]
  31     quote = ""
  32     i = 0
  33     while i < len(args):
  34         c = args[i]
  35         if quote == "":
  36             if ((c == '"') or (c == "'")) and (params[-1] == ""):
  37                 quote = c
  38                 params[-1] = ""
  39             elif c == ',':
  40                 params.append("")
  41             elif (c != ' ') or (params[-1] != ""):
  42                 params[-1] += c
  43         else:
  44             if c == quote:
  45                 quote = ""
  46             else:
  47                 params[-1] += c
  48         i += 1
  49     return params
  50 
  51 def getImageList(request, pagename):
  52     from MoinMoin.action.AttachFile import _get_files, getFilename
  53     import os, mimetypes
  54     files = _get_files(request, pagename)
  55     imgs = []
  56     for f in files:
  57         if os.path.isfile(getFilename(request, pagename, f)) and (str(mimetypes.guess_type(f)[0])[:5] == "image"):
  58             imgs.append(f)
  59     return imgs
  60 
  61 import StringIO
  62 from MoinMoin.Page import Page
  63 from MoinMoin import wikiutil
  64 
  65 def getPageContent(request, formatter, pagename):
  66     _ = request.getText
  67 
  68     inc_name = wikiutil.AbsPageName(request, formatter.page.page_name, pagename)
  69     this_page = formatter.page
  70 
  71     if not hasattr(this_page, '_macroInclude_pagelist'):
  72         this_page._macroInclude_pagelist = {}
  73 
  74     if this_page._macroInclude_pagelist.has_key(inc_name):
  75         return '<p><strong class="error">Recursive include of "%s" forbidden</strong></p>' % inc_name
  76 
  77     fmt = formatter.__class__(request, is_included=True)
  78     fmt._base_depth = formatter._base_depth
  79     inc_page = Page(request, inc_name, formatter=fmt)
  80 
  81     # set or increment include marker
  82     this_page._macroInclude_pagelist[inc_name] = \
  83         this_page._macroInclude_pagelist.get(inc_name, 0) + 1
  84 
  85     inc_page._macroInclude_pagelist = this_page._macroInclude_pagelist
  86 
  87     if not hasattr(request, "_Include_backto"):
  88         request._Include_backto = this_page.page_name
  89 
  90     # output the included page
  91     strfile = StringIO.StringIO()
  92     request.redirect(strfile)
  93     try:
  94         cid = request.makeUniqueID("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
  95         inc_page.send_page(request, content_only=1, content_id=cid,
  96                            omit_footnotes=True)
  97     finally:
  98         request.redirect()
  99 
 100     # decrement or remove include marker
 101     if this_page._macroInclude_pagelist[inc_name] > 1:
 102         this_page._macroInclude_pagelist[inc_name] = \
 103             this_page._macroInclude_pagelist[inc_name] - 1
 104     else:
 105         del this_page._macroInclude_pagelist[inc_name]
 106 
 107     # return include text
 108     return strfile.getvalue()
 109 
 110 imgbrswrscript = """
 111 <script type="text/javascript">
 112 	function imgbrwsr_content_update()  {
 113 
 114 		link = document.getElementById(this.element.id+"_link");
 115 		link.href = this.images[this.index][0]+"?action=AttachFile&do=get&target="+this.images[this.index][1];
 116 		link.title = this.images[this.index][2];
 117 
 118 		img = document.getElementById(this.element.id+"_image");
 119 		img.src = this.images[this.index][0]+"?action=Thumbnail&w=320&h=240&target="+this.images[this.index][1];
 120 //		img.src = this.images[this.index][0]+"?action=AttachFile&do=get&target="+this.images[this.index][1];
 121 		img.alt = this.images[this.index][2];
 122 
 123 		for (i = 0; i < this.images.length; ++i)  {
 124 			document.getElementById(this.element.id+"_text_"+i).style.display = (i == this.index)? "block": "none";
 125 		}
 126 
 127 		document.getElementById(this.element.id+"_edit").href = this.images[this.index][0]+"/"+this.images[this.index][1]+"?action=edit"
 128 		document.getElementById(this.element.id+"_view").href = this.images[this.index][0]+"/"+this.images[this.index][1]+"?action=show"
 129 		
 130 		if (this.index == 0)	{
 131 			document.getElementById(this.element.id+"_frst").style.display = "none";
 132 			document.getElementById(this.element.id+"_back").style.display = "none";
 133 		} else {
 134 			document.getElementById(this.element.id+"_frst").style.display = "block";
 135 			document.getElementById(this.element.id+"_back").style.display = "block";
 136 		}
 137 		if (this.index == this.images.length - 1)	{
 138 			document.getElementById(this.element.id+"_next").style.display = "none";
 139 			document.getElementById(this.element.id+"_last").style.display = "none";
 140 		} else {
 141 			document.getElementById(this.element.id+"_next").style.display = "block";
 142 			document.getElementById(this.element.id+"_last").style.display = "block";
 143 		}
 144 	}
 145 	function imgbrwsr_content_goto(imgno)	{
 146 		while (imgno < 0)
 147 			imgno += this.images.length;
 148 		if (imgno >= this.images.length)
 149 			imgno %%= this.images.length;
 150 		this.index = imgno;
 151 		this.update();
 152 	}
 153 	function imgbrwsr_content_offset(off)	{
 154 		this.go(this.index+off);
 155 	}
 156 	function imgbrwsr_content_expand()   {
 157 		expand = document.getElementById(this.element.id+"_expand");
 158 		expandimg = document.getElementById(this.element.id+"_expand_img");
 159 		textdiv = document.getElementById(this.element.id+"_text");
 160 		buttondiv = document.getElementById(this.element.id+"_text_buttons");
 161 		if (textdiv.style.display == 'none')    {
 162 			this.element.style.width = '695px';
 163 			this.element.style.background = 'url(%(frameexpandedimg)s)';
 164 			textdiv.style.display = 'block';
 165 			buttondiv.style.display = 'block';
 166 			expand.title = 'Hide the content panel';
 167 			expandimg.src = '%(closeimg)s';
 168                 } else  {
 169 			buttondiv.style.display = 'none';
 170 			textdiv.style.display = 'none';
 171 			this.element.style.background = 'url(%(frameimg)s)';
 172 			this.element.style.width = '358px';
 173 			expand.title = 'Show the content panel';
 174 			expandimg.src = '%(openimg)s';
 175 		}
 176 	}
 177 	var imgbrwsr_contents = new Array();
 178 	function imgbrwsr_content(element, ndx, imgs)   {
 179 		this.id     = imgbrwsr_contents.length;
 180 		this.element= element;
 181 		this.index  = ndx;
 182 		this.images = imgs;
 183 		this.go     = imgbrwsr_content_goto;
 184 		this.offset = imgbrwsr_content_offset;
 185 		this.update = imgbrwsr_content_update;
 186 		this.expand = imgbrwsr_content_expand;
 187 		imgbrwsr_contents[this.id] = this;
 188 		document.getElementById(this.element.id+"_frst").href = "javascript:imgbrwsr_contents["+this.id+"].go(0);";
 189 		document.getElementById(this.element.id+"_back").href = "javascript:imgbrwsr_contents["+this.id+"].offset(-1);";
 190 		document.getElementById(this.element.id+"_next").href = "javascript:imgbrwsr_contents["+this.id+"].offset(1);";
 191 		document.getElementById(this.element.id+"_last").href = "javascript:imgbrwsr_contents["+this.id+"].go(-1);";
 192 		document.getElementById(this.element.id+"_expand").href = "javascript:imgbrwsr_contents["+this.id+"].expand();";
 193 	}
 194 </script>
 195 """
 196 
 197 def execute(macro, args):
 198     from MoinMoin.action import AttachFile
 199     request = macro.request;
 200 
 201     try:
 202         macro.formatter._imgbrwsr_content_id
 203         output = ""
 204     except:
 205         output = imgbrswrscript % {'frameimg':          request.theme.img_url('imgbrwsr/frame.png'),
 206                                    'frameexpandedimg':  request.theme.img_url('imgbrwsr/frame_expanded_buttons.png'),
 207                                    'closeimg':          request.theme.img_url('imgbrwsr/close.png'),
 208                                    'openimg':           request.theme.img_url('imgbrwsr/open.png')}
 209         macro.formatter._imgbrwsr_content_id = 0
 210 
 211     p = parseArgs(args)
 212     try:
 213         ndx = int(p[0])
 214         p.pop(0)
 215     except:
 216         ndx = 0
 217 
 218     if len(p) == 0:
 219         p = getImageList(macro.request, macro.formatter.page.page_name)
 220 
 221     i = 0
 222     script = "<script type='text/javascript'>\n\t(new imgbrwsr_content(document.getElementById('imgbrwsr_content%d'), %d, new Array(" % (macro.formatter._imgbrwsr_content_id, ndx)
 223     output += """<div id='imgbrwsr_content%(id)d' style='float:left; width:358px; height:312px; position:relative; background:url(%(frameimg)s);'>
 224     <a id='imgbrwsr_content%(id)d_link' style='position:absolute; left:19px; top:20px; display:block;'><img id='imgbrwsr_content%(id)d_image' style="width:320px; height:240px; border:none;" /></a>
 225     <div id='imgbrwsr_content%(id)d_text' style='position:absolute; left:354px; top:19px; width:320px; height:240px; overflow:auto; display:none;'>""" % {'id': macro.formatter._imgbrwsr_content_id,
 226                                                                                                                                                           'frameimg': request.theme.img_url('imgbrwsr/frame.png')}
 227     for f in p:
 228         pagename, filename = AttachFile.absoluteName(f, macro.formatter.page.page_name)
 229         if i > 0:
 230             script += ", "
 231         script += "new Array('%s', '%s', '%s')" % (pagename, filename, f)
 232         output += "<div id='imgbrwsr_content%(id)d_text_%(i)d' style='display:none;'>%(text)s</div>" % {'id': macro.formatter._imgbrwsr_content_id, 'i': i, 'text': getPageContent(macro.request, macro.formatter, pagename+'/'+filename)}
 233         i += 1
 234     script += "))).update();\n</script>"
 235     output += """</div>
 236     <a id='imgbrwsr_content%(id)d_frst' title='Go to the first image'    style='position:absolute; left: 11px; top:266px;'><img src='%(firstimg)s' style='border:none;' alt='first' /></a>
 237     <a id='imgbrwsr_content%(id)d_back' title='Go to the previous image' style='position:absolute; left: 55px; top:266px;'><img src='%(backimg)s'  style='border:none;' alt='back'  /></a>
 238     <a id='imgbrwsr_content%(id)d_next' title='Go to the next image'     style='position:absolute; left:271px; top:266px;'><img src='%(nextimg)s' style='border:none;'  alt='next'  /></a>
 239     <a id='imgbrwsr_content%(id)d_last' title='Go to the last image'     style='position:absolute; left:303px; top:266px;'><img src='%(lastimg)s' style='border:none;'  alt='last'  /></a>
 240     <a id='imgbrwsr_content%(id)d_expand' title='Show the content panel' style='position:absolute; left:343px; top:129px;'><img id='imgbrwsr_content%(id)d_expand_img' src="%(openimg)s" style='border:none;' /></a>
 241     <div id='imgbrwsr_content%(id)d_text_buttons' style='display:none;'>
 242         <a id='imgbrwsr_content%(id)d_edit' title='Edit text' style='position:absolute; left:344px; top:270px; display:block;' onmouseover='document.getElementById("imgbrwsr_content%(id)d_view").style.display="none";' onmouseout='document.getElementById("imgbrwsr_content%(id)d_view").style.display="block";'><img src='%(editimg)s' style='border:none;' alt='edit' /></a>
 243         <a id='imgbrwsr_content%(id)d_view' title='View text' style='position:absolute; left:370px; top:270px; display:block;' onmouseover='document.getElementById("imgbrwsr_content%(id)d_edit").style.display="none";' onmouseout='document.getElementById("imgbrwsr_content%(id)d_edit").style.display="block";'><img src='%(viewimg)s' style='border:none;' alt='view' /></a>
 244     </div>
 245 </div>""" % {'id':          macro.formatter._imgbrwsr_content_id,
 246              'firstimg':    request.theme.img_url('imgbrwsr/first.png'),
 247              'backimg':     request.theme.img_url('imgbrwsr/back.png'),
 248              'nextimg':     request.theme.img_url('imgbrwsr/next.png'),
 249              'lastimg':     request.theme.img_url('imgbrwsr/last.png'),
 250              'openimg':     request.theme.img_url('imgbrwsr/open.png'),
 251              'editimg':     request.theme.img_url('imgbrwsr/edit.png'),
 252              'viewimg':     request.theme.img_url('imgbrwsr/view.png')}
 253     macro.formatter._imgbrwsr_content_id += 1
 254     return macro.formatter.rawHTML(output+script)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-08-27 06:16:11, 0.5 KB) [[attachment:Clear.py]]
  • [get | view] (2007-08-28 23:30:49, 6.8 KB) [[attachment:Editor.py]]
  • [get | view] (2007-08-31 16:21:54, 6.5 KB) [[attachment:ImageBrowser.inc]]
  • [get | view] (2007-08-17 18:41:06, 23.6 KB) [[attachment:ImageBrowser.zip]]
  • [get | view] (2007-08-27 06:15:30, 12.1 KB) [[attachment:ImageBrowserContent.py]]
  • [get | view] (2007-08-17 20:07:21, 51.1 KB) [[attachment:ImageBrowserContent.zip]]
  • [get | view] (2007-08-27 22:38:27, 51.5 KB) [[attachment:ImageBrowserContent_0-2.zip]]
  • [get | view] (2007-08-28 19:24:15, 51.5 KB) [[attachment:ImageBrowserContent_0-3.zip]]
  • [get | view] (2007-08-30 21:15:21, 51.5 KB) [[attachment:ImageBrowserContent_0-4.zip]]
  • [get | view] (2007-08-30 22:05:48, 3.5 KB) [[attachment:ImageBrowserGenerator.zip]]
  • [get | view] (2007-08-17 18:41:18, 28.6 KB) [[attachment:ImageBrowserNoLoad.zip]]
  • [get | view] (2007-08-10 22:00:23, 23.1 KB) [[attachment:MacroMarket-ImageBrowser.zip]]
  • [get | view] (2007-08-14 18:43:08, 23.4 KB) [[attachment:MacroMarket-ImageBrowserNoLoad.zip]]
  • [get | view] (2007-08-28 23:30:19, 2.9 KB) [[attachment:SimpleInclude.py]]
  • [get | view] (2007-08-28 23:30:27, 0.2 KB) [[attachment:Test.py]]
  • [get | view] (2007-08-28 23:29:51, 0.9 KB) [[attachment:Thumbnail.py]]
  • [get | view] (2007-08-28 23:29:31, 5.2 KB) [[attachment:Thumbnail_action.py]]
  • [get | view] (2007-08-17 18:26:12, 160.3 KB) [[attachment:imagebrowser.png]]
  • [get | view] (2007-08-17 17:13:09, 39.0 KB) [[attachment:imagebrowsercontent.png]]
  • [get | view] (2007-08-17 17:55:36, 37.2 KB) [[attachment:imagebrowsercontent_closed.png]]
  • [get | view] (2007-08-22 16:42:34, 491.5 KB) [[attachment:imgbrwsr_images.zip]]
  • [get | view] (2007-08-28 23:28:39, 3.8 KB) [[attachment:imgbrwsrgen.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.