Monday, November 08, 2010 at 12:05 AM.

system.verbs.builtins.radio.userInterface.folderToHtml

on folderToHTML (folder, pta) {
	<<Changes:
		<<12/8/01; 7:18:01 PM by DW
			<<Optimize the images to be served from the file system if being browsed locally.
		<<12/4/01; 12:22:19 AM by JES
			<<Don't put the folderView searchArg in the links on icons for files, since it prevents the files from being opened in their native editors.
		<<11/25/01; 3:59:26 PM by JES
			<<Removed the links and extra space in the folder path at the top of the page.
		<<11/25/01; 3:40:35 PM by JES
			<<Added some space between the parent-folder links, and the slash-characters at the top of the page.
		<<11/24/01; 6:17:32 PM by JES
			<<Build html for pta^.title, as a breadcrumb trail. Set pta^.headTitle to the foldername.
		<<11/24/01; 2:47:07 PM by JES
			<<Commented out POST handling for upstream settings.
		<<11/24/01; 11:27:43 AM by DW
			<<Linked it into the new help system, commented the upstreaming prefs form at the bottom of each page.
			<<Moved the blue arrow.
		<<11/14/01; 10:55:31 AM by DW
			<<Localized.
		<<9/5/01; 11:52:10 PM by JES
			<<Use single-quotes to delimit the driver name in the value attribute of the options for the driver popup menu. Using double-quotes caused glossary expansion, which hosed the popup menu's HTML code.
		<<8/11/01; 5:30:58 PM by JES
			<<Don't display invisible files/folders.
		<<8/3/01; 3:12:53 AM by JES
			<<Handle POSTs for the upstreamPrefs command.
		<<8/2/01; 3:17:04 AM by JES
			<<Removed Edit button -- the file icon does this.
		<<8/2/01; 2:46:36 AM by JES
			<<Added a trailing slash character to the up link. This was causing the responder to do a redirect, which discarded the searchArgs. (That is also a bug, now fixed.)
		<<7/25/01; 6:49:07 PM by JES
			<<Added Edit buttons next to files and folders.
		<<5/4/01; 11:09:49 PM by DW
			<<Redesigned the rendering.
			<<Old code is archived here:
				<<fileloop (f in folder)
					<<local (flfolder = file.isfolder (f), name = file.fileFromPath (f), iconname = "unknown", iconlink)
					<<local (mimetype)
					<<bundle //set mimetype
						<<if flfolder
							<<mimeType = "folder"
							<<iconname = "folder"
							<<objectType = '    '
							<<iconlink = folderimg
						<<else
							<<if name endswith ".root"
								<<mimetype = "database"
								<<iconname = "skull"
								<<iconlink =  "<img src=\"/resources/userlandIcons/" + iconname + "\" border=\"0\">"
								<<iconlink = "<a href=\"http://www.scripting.com/frontier5/\">" + iconlink + "</a>"
							<<else
								<<local (objectType = file.type (f))
								<<case sys.os () // decide what the MIME type is
									<<"Win95"
									<<"WinNT"
										<<local (extension = objectType)
										<<extension = string.replaceAll (extension, " ", "") // strip spaces
										<<if defined (user.webserver.prefs.ext2MIME.[extension])
											<<mimeType= user.webserver.prefs.ext2MIME.[extension]
										<<else
											<<mimeType = "text/plain"
									<<"MacOS"
										<<if defined (user.webserver.prefs.type2MIME.[objectType])
											<<mimeType = user.webserver.prefs.type2MIME.[objectType]
										<<else
											<<mimeType = "text/html"
					<<
					<<local (size)
					<<bundle //set size
						<<if flfolder
							<<size = "-"
						<<else
							<<size = string.megabyteString (file.size (f))
					<<
					<<try
						<<iconname = user.webserver.prefs.MIME2icon.[mimetype]
						<<iconlink = radio.images.systemImageRef ("apacheIcons/" + iconname)
					<<
					<<local (itemurl = pta^.path)
					<<if flfolder {name = string.delete (name, sizeof (name), 1)} //pop off trailing path char
					<<if not (itemurl endswith "/")
						<<itemurl = itemurl + "/"
					<<itemurl = itemurl + string.urlEncode (name)
					<<if flfolder == tabletype
						<<itemurl = itemurl + "/"
					<<
					<<add ("<tr>"); indentlevel++
					<<add ("<td align=\"right\">" + string.nthfield (file.created (f), ";", 1) + "</td>")
					<<add ("<td><a href=\"" + itemurl + "\">" + name + "</a></td>")
					<<add ("<td>" + size + "</td>")
					<<add ("<td>" + mimetype + "</td>")
					<<add ("</tr>"); indentlevel--
				<<bundle //add a comment if one is present
					<<local (f = folder + "#comment")
					<<if file.exists (f)
						<<add ("Comment: " + string (file.readwholefile (f)))
		<<5/1/01; 7:48:23 PM by DW
			<<Converted from Frontier's mainResponder.folderToHtml.
	html.setpagetableaddress (pta); //imageref needs it set
	
	local (flsamemachine = pta^.radioResponder.flSameMachine);
	local (folderimg = radio.images.systemImageRef ("apacheIcons/folder2", flFileUrl:flSameMachine));
	local (fileimg = radio.images.systemImageRef ("apacheIcons/file2", flFileUrl:flSameMachine));
	<<bundle //JES: we could use a separate icon for image files, but we need a good icon
		<<local (imgimg = radio.images.systemImageRef ("apacheIcons/image2"))
	local (globeimg = radio.images.systemImageRef ("qbullet/remote", flFileUrl:flSameMachine));
	local (args);
	new (tabletype, @args); //args for urls
	webserver.parseArgs (pta^.searchArgs, @args);
	local (htmltext = "", indentlevel = 0);
	on add (s) {
		htmltext = htmltext + string.filledString ("\t", indentlevel) + s + "\r"};
	
	add ("<p>" + radio.string.getlocalizedstring ("folderView.intro") + " " + radio.userInterface.helpLink ("The Folder command") + "</p>");
	
	<<bundle //look in postargs for an upstreamSettings command
		<<if pta^.method == "POST"
			<<if defined (pta^.radioResponder.postArgs.command)
				<<case pta^.radioResponder.postArgs.command
					<<"postUpstreamSettings"
					<<"upstreamSettings"
						<<local (flError)
						<<add (radio.userInterface.upstreamSettings (folder, @flError, pta))
						<<if not flError
							<<return (htmltext)
	
	local (bluearrow);
	bundle { //set bluearrow
		if string.lower (folder) != string.lower (user.radio.prefs.wwwfolder) {
			bluearrow = "<a href=\"../?" + webserver.encodeArgs (@args) + "\" title=\"" + radio.string.getlocalizedstring ("folderView.blueArrowToolTip") + "\">" + radio.images.systemImageRef ("qbullet/scrollup", flFileUrl:flSameMachine) + "</a>"}
		else {
			bluearrow = " "}};
	
	add ("<table>"); indentlevel++;
	add ("<tr>"); indentlevel++;
	
	local (ctfiles = 0);
	bundle { //add the directory listing
		add ("<td>"); indentlevel++;
		add ("<table cellspacing=\"9\" cellpadding=\"0\">"); indentlevel++;
		add ("<tr><td>" + bluearrow + "</td><td><b>Title</b></td><td align=\"right\"><b>Date</b></td><td align=\"right\"><b>Size</b></td><td align=\"right\"><b>Hits</b></td><td align=\"right\"><b>Link</b></td></tr>");
		local (f, flfolder, fname, adrcache);
		fileloop (f in folder) {
			if not file.isVisible (f) { //don't display invisible files
				continue};
			radio.file.getfileattributes (f, @adrcache);
			flfolder = adrcache^.flFolder;
			fname = file.fileFromPath (f);
			
			local (sizestring);
			bundle { //set sizestring
				if flfolder {
					sizestring = adrcache^.ctFiles + " items"}
				else {
					sizestring = string.megabyteString (adrcache^.size)}};
			
			local (itemurl = pta^.path);
			if not (itemurl endswith "/") {
				itemurl = itemurl + "/"};
			if flfolder { //pop off trailing path char
				 fname = string.delete (fname, sizeof (fname), 1)};
			itemurl = itemurl + string.urlEncode (fname);
			if flfolder {
				args.folderView = 1;
				itemurl = itemurl + "/"};
				<<itemurl = itemurl + "?folderView=1"
			
			local (title = fname);
			<<if defined (adrcache^)
				<<if defined (adrcache^.title)
					<<title = adrcache^.title
			
			local (iconimg);
			bundle { //set iconimg
				if flfolder {
					iconimg = folderimg}
				else {
					<<bundle //JES: we could use a separate icon for image files
						<<local (lowername = string.lower (fname))
						<<case true
							<<lowername endsWith ".jpg"
							<<lowername endsWith ".jpeg"
							<<lowername endsWith ".png"
							<<lowername endsWith ".gif"
								<<iconimg = imgimg
						<<else
							<<iconimg = fileimg
					iconimg = fileimg};
				if flsamemachine {
					local (localargs = args);
					if defined (localargs.folderView) {
						delete (@localargs.folderView)};
					localargs.command = "edit";
					iconimg = "<a href=\"" + itemurl + "?" + webserver.encodeArgs (@localargs) + "\">" + iconimg + "</a>"}};
					<<delete (@args.command)
			
			add ("<tr>"); indentlevel++;
			add ("<td align=\"middle\">" + iconimg + "</td>");
			bundle { //add the url
				local (url = itemurl);
				if sizeof (args) > 0 {
					url = url + "?" + webserver.encodeArgs (@args)};
				add ("<td><a href=\"" + url + "\">" + title + "</a></td>")};
			add ("<td align=\"right\">" + string.nthfield (file.created (f), ";", 1) + "</td>");
			add ("<td align=\"right\" nowrap>" + sizestring + "</td>");
			add ("<td align=\"right\">" + adrcache^.hits + "</td>");
			bundle { //add link
				local (s = "<td> </td>");
				if defined (adrcache^.upstream.url) {
					if adrcache^.upstream.url != "" {
						s = "<td align=\"right\" valign=\"middle\"><a href=\"" + adrcache^.upstream.url + "\">" + globeimg + "</a></td>"}};
				add (s)};
			<<bundle //add Edit button
				<<add ("<td align=\"right\" valign=\"middle\" width=\"35\">" + radio.userInterface.editButton (f, systemImagePath: "icons/editbutton") + "</td>")
			add ("</tr>"); indentlevel--;
			ctfiles++};
		add ("</table>"); indentlevel--;
		add ("</td>")};
	<<bundle //add parent folder link (blue arrow)
		<<if string.lower (folder) != string.lower (user.radio.prefs.wwwfolder)
			<<add ("<td valign=\"top\"><table cellspacing=\"9\" cellpadding=\"0\"><tr><td>" + bluearrow + "</td></tr></table></td>")
	add ("</tr>"); indentlevel--;
	add ("</table>"); indentlevel--;
	
	<<bundle //add the upstreaming prefs form
		<<if folder beginsWith user.radio.prefs.upstream.folder
			<<local (action = "", buttontext = "Upstream This Folder")
			<<if defined (pta^.searchArgs)
				<<action = "?" + pta^.searchArgs
			<<add ("<form method=\"POST\" action=\"" + action + "\">"); indentlevel++
			<<add ("<input name=\"command\" type=\"hidden\" value=\"upstreamSettings\">")
			<<bundle //add the popup menu for the driver
				<<local (selectedDriver = "xmlStorageSystem")
				<<local (adrfolder)
				<<radio.file.getFileAttributes (folder, @adrfolder)
				<<local (adrspec)
				<<if file.exists (folder + system.verbs.builtins.radio.data.fileNames.upstreamFileName)
					<<if radio.upstream.getUpstreamSpec (adrfolder, @adrspec) //set selectedDriver and buttontext
						<<selectedDriver = adrspec^.type
						<<buttontext = "Edit Upstream Settings"
				<<add ("<select name=\"upstreamDriver\">")
				<<local (driverlist = {}, nomad)
				<<bundle //add all the drivers to the list
					<<for nomad in @radio.upstream.builtinDrivers
						<<local (lowername = string.lower (nameOf (nomad^)))
						<<driverlist = driverlist + lowername
					<<for nomad in @user.radio.drivers.upstream
						<<local (lowername = string.lower (nameOf (nomad^)))
						<<if not (driverlist contains lowername)
							<<driverlist = driverlist + lowername
				<<for drivername in driverlist //add each driver to the popup menu
					<<local (adrdriver)
					<<radio.upstream.findDriver (drivername, @adrdriver)
					<<local (driverName = nameOf (adrdriver^))
					<<local (selectedtext = "")
					<<if nameOf (adrdriver^) == selectedDriver
						<<selectedtext = " selected"
					<<add ("<option value='" + driverName + "\'" + selectedtext + ">" + driverName + "</option>")
				<<add ("</select> ")
			<<add ("<input type=\"submit\" value=\"" + buttontext + "\">")
			<<add ("</form>"); indentlevel--
	
	local (parentfolder = file.folderfrompath (user.radio.prefs.wwwfolder));
	local (foldername = string.replaceall (string.delete (folder, 1, sizeof (parentfolder)), file.getpathchar (), "/"));
	pta^.headTitle = foldername;
	pta^.radioResponder.ctFilesInFolder = ctfiles;
	<<bundle //build pta^.title, as a breadcrumb trail
		<<local (titlehtml, ixfolder = 1, ctfolders = string.countFields (foldername, "/"))
		<<loop
			<<local (pathpart = string.nthField (foldername, "/", ixfolder))
			<<if ixfolder < ctfolders
				<<local (url = string.filledString ("../", ctfolders - ixfolder) + "?" + webserver.encodeArgs (@args))
				<<titlehtml = titlehtml + "<a href=\"" + url + "\">" + pathpart + "</a> / "
			<<else //link to the folder
				<<titlehtml = titlehtml + pathpart + " / "
			<<ixfolder++
			<<if ixfolder > ctfolders
				<<break
		<<pta^.title = titlehtml
	pta^.title = foldername;
	return (htmltext)}
<<bundle //testing
	<<local (params = scratchpad.params)
	<<folderToHtml ("Backup:Radio UserLand:www:system:upstream:", @params)



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.