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. --- modules/gallery/libraries/InPlaceEdit.php | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 modules/gallery/libraries/InPlaceEdit.php (limited to 'modules/gallery/libraries/InPlaceEdit.php') 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 -- cgit v1.2.3 From 8ac7a5c0d32320c74f5f0c84cf43165265786c93 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 5 Nov 2009 13:21:40 -0800 Subject: Remove debugging statement --- modules/gallery/libraries/InPlaceEdit.php | 1 - 1 file changed, 1 deletion(-) (limited to 'modules/gallery/libraries/InPlaceEdit.php') diff --git a/modules/gallery/libraries/InPlaceEdit.php b/modules/gallery/libraries/InPlaceEdit.php index eb685b65..057874e3 100644 --- a/modules/gallery/libraries/InPlaceEdit.php +++ b/modules/gallery/libraries/InPlaceEdit.php @@ -74,7 +74,6 @@ class InPlaceEdit_Core { $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]; -- cgit v1.2.3 From beb63a83804e57050a23b3a90ebed93be41b6769 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 6 Nov 2009 23:05:20 -0800 Subject: Clean up the In place edit api: 1) Only allow 1 in place edit to be active at a time (gets around the issue of using an id to identify the form 2) remove the add_ prefix from some of the api methods 3) clean up inconsistent naming --- lib/gallery.in_place_edit.js | 7 +++++++ modules/gallery/libraries/InPlaceEdit.php | 8 ++++---- modules/tag/controllers/admin_tags.php | 18 +++++++++--------- modules/tag/css/tag.css | 4 ++-- 4 files changed, 22 insertions(+), 15 deletions(-) (limited to 'modules/gallery/libraries/InPlaceEdit.php') diff --git a/lib/gallery.in_place_edit.js b/lib/gallery.in_place_edit.js index 38c4efb1..4a90581b 100644 --- a/lib/gallery.in_place_edit.js +++ b/lib/gallery.in_place_edit.js @@ -15,6 +15,13 @@ var tag_width = $(target).width(); $(self).data("tag_width", tag_width); + var form = $("#g-inplace-edit-form"); + if (form.length > 0) { + var parent = form.parent(); + form.remove(); + parent.children().show(); + } + $.get(self.options.form_url.replace("__ID__", $(target).attr('rel')), function(data) { var parent = $(target).parent(); parent.children().hide(); diff --git a/modules/gallery/libraries/InPlaceEdit.php b/modules/gallery/libraries/InPlaceEdit.php index 057874e3..67ab3805 100644 --- a/modules/gallery/libraries/InPlaceEdit.php +++ b/modules/gallery/libraries/InPlaceEdit.php @@ -35,22 +35,22 @@ class InPlaceEdit_Core { return $instance; } - public function add_action($action) { + public function action($action) { $this->action = $action; return $this; } - public function add_rules($rules) { + public function rules($rules) { $this->rules += $rules; return $this; } - public function add_messages($messages) { + public function messages($messages) { $this->messages += $messages; return $this; } - public function add_callback($callback) { + public function callback($callback) { $this->callback = $callback; return $this; } diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index d79f697c..67587c2e 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -70,7 +70,7 @@ class Admin_Tags_Controller extends Admin_Controller { $tag = ORM::factory("tag", $id); if ($tag->loaded) { print InPlaceEdit::factory($tag->name) - ->add_action("admin/tags/rename/$id") + ->action("admin/tags/rename/$id") ->render(); } } @@ -83,15 +83,15 @@ class Admin_Tags_Controller extends Admin_Controller { kohana::show_404(); } - $inplaceedit = InPlaceEdit::factory($tag->name) - ->add_action("admin/tags/rename/$tag->id") - ->add_rules(array("required", "length[1,64]")) - ->add_messages(array("in_use" => t("There is already a tag with that name"))) - ->add_callback(array($this, "check_for_duplicate")); + $in_place_edit = InPlaceEdit::factory($tag->name) + ->action("admin/tags/rename/$tag->id") + ->rules(array("required", "length[1,64]")) + ->messages(array("in_use" => t("There is already a tag with that name"))) + ->callback(array($this, "check_for_duplicate")); - if ($inplaceedit->validate()) { + if ($in_place_edit->validate()) { $old_name = $tag->name; - $tag->name = $inplaceedit->value(); + $tag->name = $in_place_edit->value(); $tag->save(); $message = t("Renamed tag %old_name to %new_name", @@ -101,7 +101,7 @@ class Admin_Tags_Controller extends Admin_Controller { print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", "form" => $inplaceedit->render())); + print json_encode(array("result" => "error", "form" => $in_place_edit->render())); } } diff --git a/modules/tag/css/tag.css b/modules/tag/css/tag.css index 03ed444b..6d6438e3 100644 --- a/modules/tag/css/tag.css +++ b/modules/tag/css/tag.css @@ -91,6 +91,6 @@ padding: .1em 0 .2em 0; } -#g-rename-tag-form ul { +#g-tag-admin form ul { margin-bottom: 0; -} \ No newline at end of file +} -- cgit v1.2.3