From ad19ca520090593bd111bc17752868be56fcb80d Mon Sep 17 00:00:00 2001 From: Erik Forkalsrud Date: Sat, 21 Aug 2010 21:06:24 -0700 Subject: [PATCH] Adding hidden URL (*.edit) to edit captions through web interface. --- .../forkalsrud/album/web/AlbumServlet.java | 44 ++ src/main/webapp/WEB-INF/velocity/edit.vm | 81 +++ .../assets/jeditable/jquery.autogrow.js | 132 +++++ .../jeditable/jquery.jeditable.autogrow.js | 38 ++ .../assets/jeditable/jquery.jeditable.js | 543 ++++++++++++++++++ .../assets/jeditable/jquery.jeditable.mini.js | 38 ++ 6 files changed, 876 insertions(+) create mode 100644 src/main/webapp/WEB-INF/velocity/edit.vm create mode 100644 src/main/webapp/assets/jeditable/jquery.autogrow.js create mode 100644 src/main/webapp/assets/jeditable/jquery.jeditable.autogrow.js create mode 100644 src/main/webapp/assets/jeditable/jquery.jeditable.js create mode 100644 src/main/webapp/assets/jeditable/jquery.jeditable.mini.js diff --git a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java index 56c859d..65fc90d 100644 --- a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java @@ -1,6 +1,8 @@ package org.forkalsrud.album.web; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.Calendar; @@ -26,6 +28,7 @@ import org.forkalsrud.album.exif.DirectoryEntry; import org.forkalsrud.album.exif.Entry; import org.forkalsrud.album.exif.FileEntry; import org.forkalsrud.album.exif.Thumbnail; +import org.springframework.web.util.HtmlUtils; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; @@ -163,6 +166,13 @@ public class AlbumServlet environment.close(); } + @Override + public void doPost(HttpServletRequest req, HttpServletResponse res) + throws ServletException, IOException + { + doGet(req, res); + } + @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException @@ -192,6 +202,12 @@ public class AlbumServlet return; } + if (pathInfo.endsWith(".edit")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".edit".length()); + handleEdit(req, res, (FileEntry)resolveEntry(pathInfo)); + return; + } + File file = new File(base, pathInfo); if (!file.canRead()) { res.setStatus(HttpServletResponse.SC_FORBIDDEN); @@ -237,6 +253,34 @@ public class AlbumServlet } } + void handleEdit(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { + try { + String value = req.getParameter("value"); + if (value != null) { + File propertyFile = new File(entry.getPath().getParent(), "album.properties"); + Properties props = new Properties(); + if (propertyFile.exists()) { + FileInputStream fis = new FileInputStream(propertyFile); + props.load(fis); + fis.close(); + } + props.setProperty("file." + entry.getName() + ".caption", value); + FileOutputStream fos = new FileOutputStream(propertyFile); + props.store(fos, "online editor"); + fos.close(); + res.setContentType("text/html"); + res.getWriter().println(HtmlUtils.htmlEscape(value)); + return; + } + res.setContentType("text/html"); + req.setAttribute("entry", entry); + req.setAttribute("thmb", new Integer(640)); + RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/edit.vm"); + rd.forward(req, res); + } catch (Exception ex) { + throw new RuntimeException("sadness", ex); + } + } boolean etagMatches(HttpServletRequest req, String fileEtag) { diff --git a/src/main/webapp/WEB-INF/velocity/edit.vm b/src/main/webapp/WEB-INF/velocity/edit.vm new file mode 100644 index 0000000..fba41e6 --- /dev/null +++ b/src/main/webapp/WEB-INF/velocity/edit.vm @@ -0,0 +1,81 @@ + + + + + + $entry.name + + + + + + + + + + + +

$entry.name

+
+#set($dim = $entry.thumbnail.size.scaled($thmb)) +#set($thpath = $mapper.map(${entry.thumbnail.getPath()})) +
+

$!entry.caption

+ + diff --git a/src/main/webapp/assets/jeditable/jquery.autogrow.js b/src/main/webapp/assets/jeditable/jquery.autogrow.js new file mode 100644 index 0000000..dcfb39a --- /dev/null +++ b/src/main/webapp/assets/jeditable/jquery.autogrow.js @@ -0,0 +1,132 @@ +/* + * Auto Expanding Text Area (1.2.2) + * by Chrys Bader (www.chrysbader.com) + * chrysb@gmail.com + * + * Special thanks to: + * Jake Chapa - jake@hybridstudio.com + * John Resig - jeresig@gmail.com + * + * Copyright (c) 2008 Chrys Bader (www.chrysbader.com) + * Licensed under the GPL (GPL-LICENSE.txt) license. + * + * + * NOTE: This script requires jQuery to work. Download jQuery at www.jquery.com + * + */ + +(function(jQuery) { + + var self = null; + + jQuery.fn.autogrow = function(o) + { + return this.each(function() { + new jQuery.autogrow(this, o); + }); + }; + + + /** + * The autogrow object. + * + * @constructor + * @name jQuery.autogrow + * @param Object e The textarea to create the autogrow for. + * @param Hash o A set of key/value pairs to set as configuration properties. + * @cat Plugins/autogrow + */ + + jQuery.autogrow = function (e, o) + { + this.options = o || {}; + this.dummy = null; + this.interval = null; + this.line_height = this.options.lineHeight || parseInt(jQuery(e).css('line-height')); + this.min_height = this.options.minHeight || parseInt(jQuery(e).css('min-height')); + this.max_height = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));; + this.textarea = jQuery(e); + + if(this.line_height == NaN) + this.line_height = 0; + + // Only one textarea activated at a time, the one being used + this.init(); + }; + + jQuery.autogrow.fn = jQuery.autogrow.prototype = { + autogrow: '1.2.2' + }; + + jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend; + + jQuery.autogrow.fn.extend({ + + init: function() { + var self = this; + this.textarea.css({overflow: 'hidden', display: 'block'}); + this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() }); + this.checkExpand(); + }, + + startExpand: function() { + var self = this; + this.interval = window.setInterval(function() {self.checkExpand()}, 400); + }, + + stopExpand: function() { + clearInterval(this.interval); + }, + + checkExpand: function() { + + if (this.dummy == null) + { + this.dummy = jQuery('
'); + this.dummy.css({ + 'font-size' : this.textarea.css('font-size'), + 'font-family': this.textarea.css('font-family'), + 'width' : this.textarea.css('width'), + 'padding' : this.textarea.css('padding'), + 'line-height': this.line_height + 'px', + 'overflow-x' : 'hidden', + 'position' : 'absolute', + 'top' : 0, + 'left' : -9999 + }).appendTo('body'); + } + + // Strip HTML tags + var html = this.textarea.val().replace(/(<|>)/g, ''); + + // IE is different, as per usual + if ($.browser.msie) + { + html = html.replace(/\n/g, '
new'); + } + else + { + html = html.replace(/\n/g, '
new'); + } + + if (this.dummy.html() != html) + { + this.dummy.html(html); + + if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)) + { + this.textarea.css('overflow-y', 'auto'); + } + else + { + this.textarea.css('overflow-y', 'hidden'); + if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) + { + this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100); + } + } + } + } + + }); +})(jQuery); \ No newline at end of file diff --git a/src/main/webapp/assets/jeditable/jquery.jeditable.autogrow.js b/src/main/webapp/assets/jeditable/jquery.jeditable.autogrow.js new file mode 100644 index 0000000..2057670 --- /dev/null +++ b/src/main/webapp/assets/jeditable/jquery.jeditable.autogrow.js @@ -0,0 +1,38 @@ +/* + * Autogrow textarea for Jeditable + * + * Copyright (c) 2008 Mika Tuupola + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/mit-license.php + * + * Depends on Autogrow jQuery plugin by Chrys Bader: + * http://www.aclevercookie.com/facebook-like-auto-growing-textarea/ + * + * Project home: + * http://www.appelsiini.net/projects/jeditable + * + * Revision: $Id$ + * + */ + +$.editable.addInputType('autogrow', { + element : function(settings, original) { + var textarea = $('