summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-11-05 12:55:06 -0800
committerTim Almdal <tnalmdal@shaw.ca>2009-11-05 13:00:41 -0800
commit03408f3e39bbb0b407b2949721090dd6b07913ac (patch)
treeca54f73d366cb3050cc3f6d5aa86d716bb7bc643
parent88852c45eac5981a4cd82f3207df1fad0cf51fcc (diff)
Refactor the in place editting in tags admin out into a separate widget as part ofthe gallery module. Create the jQuery widget, form template and library to support generalized in place editting. Part of the fix for ticket #750.
-rw-r--r--lib/gallery.in_place_edit.js62
-rw-r--r--modules/gallery/libraries/InPlaceEdit.php89
-rw-r--r--modules/gallery/views/in_place_edit.html.php24
3 files changed, 175 insertions, 0 deletions
diff --git a/lib/gallery.in_place_edit.js b/lib/gallery.in_place_edit.js
new file mode 100644
index 00000000..38c4efb1
--- /dev/null
+++ b/lib/gallery.in_place_edit.js
@@ -0,0 +1,62 @@
+(function($) {
+ $.widget("ui.gallery_in_place_edit", {
+ _init: function() {
+ var self = this;
+ $(self).data("parent", self.element.parent());
+ this.element.click(function(event) {
+ event.preventDefault();
+ self._show(event.currentTarget);
+ return false;
+ });
+ },
+
+ _show: function(target) {
+ var self = this;
+ var tag_width = $(target).width();
+ $(self).data("tag_width", tag_width);
+
+ $.get(self.options.form_url.replace("__ID__", $(target).attr('rel')), function(data) {
+ var parent = $(target).parent();
+ parent.children().hide();
+ parent.append(data);
+ parent.find("form :text")
+ .width(tag_width)
+ .focus();
+ $(".g-short-form").gallery_short_form();
+ parent.find("form .g-cancel").click(function(event) {
+ parent.find("form").remove();
+ parent.children().show();
+ event.preventDefault();
+ return false;
+ });
+ self._ajaxify_edit();
+ });
+
+ },
+
+ _ajaxify_edit: function() {
+ var self = this;
+ var form = $($(self).data("parent")).find("form");
+ $(form).ajaxForm({
+ dataType: "json",
+ success: function(data) {
+ if (data.result == "success") {
+ window.location.reload();
+ } else {
+ $(form).replaceWith(data.form);
+ var width = $(self).data("tag_width");
+ $($(self).data("parent")).find("form :text")
+ .width(width)
+ .focus();
+ $(".g-short-form").gallery_short_form();
+ self._ajaxify_edit();
+ }
+ }
+ });
+ }
+ });
+
+ $.extend($.ui.gallery_in_place_edit, {
+ defaults: {}
+ });
+})(jQuery);
diff --git a/modules/gallery/libraries/InPlaceEdit.php b/modules/gallery/libraries/InPlaceEdit.php
new file mode 100644
index 00000000..eb685b65
--- /dev/null
+++ b/modules/gallery/libraries/InPlaceEdit.php
@@ -0,0 +1,89 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class InPlaceEdit_Core {
+ private $rules = array();
+ private $messages = array();
+ private $callback = array();
+ private $initial_value;
+ private $action = "";
+ private $errors;
+ private $form;
+
+ static function factory($initial_value) {
+ $instance = new InPlaceEdit();
+ $instance->initial_value = $initial_value;
+ $instance->form = array("input" => $initial_value);
+ $instance->errors = array("input" => "");
+
+ return $instance;
+ }
+
+ public function add_action($action) {
+ $this->action = $action;
+ return $this;
+ }
+
+ public function add_rules($rules) {
+ $this->rules += $rules;
+ return $this;
+ }
+
+ public function add_messages($messages) {
+ $this->messages += $messages;
+ return $this;
+ }
+
+ public function add_callback($callback) {
+ $this->callback = $callback;
+ return $this;
+ }
+
+ public function validate() {
+ $post = Validation::factory($_POST)
+ ->add_callbacks("input", $this->callback);
+ foreach ($this->rules as $rule) {
+ $post->add_rules("input", $rule);
+ }
+
+ $valid = $post->validate();
+ $this->form = array_merge($this->form, $post->as_array());
+ $this->errors = array_merge($this->errors, $post->errors());
+ return $valid;
+ }
+
+ public function render() {
+ $v = new View("in_place_edit.html");
+ $v->hidden = array("csrf" => access::csrf_token());
+ $v->action = url::site($this->action);
+ $v->form = $this->form;
+ $v->errors = $this->errors;
+ Kohana::log("alert", Kohana::debug($this->errors));
+ foreach ($v->errors as $key => $error) {
+ if (!empty($error)) {
+ $v->errors[$key] = $this->messages[$error];
+ }
+ }
+ return $v->render();
+ }
+
+ public function value() {
+ return $this->form["input"];
+ }
+} \ No newline at end of file
diff --git a/modules/gallery/views/in_place_edit.html.php b/modules/gallery/views/in_place_edit.html.php
new file mode 100644
index 00000000..5cc42cd6
--- /dev/null
+++ b/modules/gallery/views/in_place_edit.html.php
@@ -0,0 +1,24 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<style>
+ #g-inplace-edit-form ul {
+ margin: 0;
+ }
+ #g-inplace-edit-message {
+background-color: #FFF;
+ }
+</style>
+<?= form::open($action, array("method" => "post", "id" => "g-inplace-edit-form", "class" => "g-short-form"), $hidden) ?>
+ <ul>
+ <li <? if (!empty($errors["input"])): ?> class="g-error"<? endif ?>>
+ <?= form::input("input", $form["input"], " class='textbox'") ?>
+ </li>
+ <li>
+ <?= form::submit(array("class" => "submit ui-state-default"), t("Save")) ?>
+ </li>
+ <li><a href="#" class="g-cancel"><?= t("Cancel") ?></a></li>
+ </ul>
+<?= form::close() ?>
+<? if (!empty($errors["input"])): ?>
+<div id="g-inplace-edit-message" class="g-error"><?= $errors["input"] ?></div>
+<? endif ?>
+