From 03408f3e39bbb0b407b2949721090dd6b07913ac Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 5 Nov 2009 12:55:06 -0800 Subject: 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. --- lib/gallery.in_place_edit.js | 62 +++++++++++++++++++ modules/gallery/libraries/InPlaceEdit.php | 89 ++++++++++++++++++++++++++++ modules/gallery/views/in_place_edit.html.php | 24 ++++++++ 3 files changed, 175 insertions(+) create mode 100644 lib/gallery.in_place_edit.js create mode 100644 modules/gallery/libraries/InPlaceEdit.php create mode 100644 modules/gallery/views/in_place_edit.html.php 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 @@ +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 @@ + + + "post", "id" => "g-inplace-edit-form", "class" => "g-short-form"), $hidden) ?> + + + +
+ + -- cgit v1.2.3