Skip to content

Commit

Permalink
Merge branch 'dialog'
Browse files Browse the repository at this point in the history
  • Loading branch information
bharat committed Aug 1, 2010
2 parents 563afbe + bf7115c commit 3b187d7
Show file tree
Hide file tree
Showing 33 changed files with 276 additions and 179 deletions.
76 changes: 62 additions & 14 deletions lib/gallery.dialog.js
Expand Up @@ -27,19 +27,42 @@

$("#g-dialog").gallery_show_loading();

$.getJSON(sHref, function(data) {
$("#g-dialog").html(unescape(data.form)).gallery_show_loading();
$.ajax({
url: sHref,
type: "GET",
beforeSend: function(xhr) {
// Until we convert to jquery 1.4, we need to save the XMLHttpRequest object so that we
// can detect the mime type of the reply
this.xhrData = xhr;
},
success: function(data, textStatus, xhr) {
// Pre jquery 1.4, get the saved XMLHttpRequest object
if (xhr == undefined) {
xhr = this.xhrData;
}
var mimeType = /^(\w+\/\w+)\;?/.exec(xhr.getResponseHeader("Content-Type"));

var content = "";
if (mimeType[1] == "application/json") {
data = JSON.parse(data);
content = unescape(data.form);
} else {
content = data;
}

if ($("#g-dialog form").length) {
self.form_loaded(null, $("#g-dialog form"));
}
self._layout();
$("#g-dialog").html(content).gallery_show_loading();

if ($("#g-dialog form").length) {
self.form_loaded(null, $("#g-dialog form"));
}
self._layout();

$("#g-dialog").dialog("open");
self._set_title();
$("#g-dialog").dialog("open");
self._set_title();

if ($("#g-dialog form").length) {
self._ajaxify_dialog();
if ($("#g-dialog form").length) {
self._ajaxify_dialog();
}
}
});
$("#g-dialog").dialog("option", "self", self);
Expand Down Expand Up @@ -99,17 +122,42 @@
_ajaxify_dialog: function() {
var self = this;
$("#g-dialog form").ajaxForm({
dataType: "json",
beforeSubmit: function(formData, form, options) {
form.find(":submit")
.addClass("ui-state-disabled")
.attr("disabled", "disabled");
return true;
},
beforeSend: function(xhr) {
// Until we convert to jquery 1.4, we need to save the XMLHttpRequest object so that we
// can detect the mime type of the reply
this.xhrData = xhr;
},
success: function(data) {
if (data.form) {
var formData = unescape(data.form);
$("#g-dialog form").replaceWith(formData);
// Pre jquery 1.4, get the saved XMLHttpRequest object
xhr = this.xhrData;
if (xhr) {
var mimeType = /^(\w+\/\w+)\;?/.exec(xhr.getResponseHeader("Content-Type"));

var content = "";
if (mimeType[1] == "application/json") {
data = JSON.parse(data);
} else {
data = {"html": escape(data)};
}
} else {
// Uploading files (eg: watermark) uses a fake xhr in jquery.form.js so
// all we have is in the data field, which should be some very simple JSON.
// Weirdly enough in Chrome the result gets wrapped in a <pre> element and
// looks like this:
// <pre style="word-wrap: break-word; white-space: pre-wrap;">{"result":"success",
// "location":"\/~bharat\/gallery3\/index.php\/admin\/watermarks"}</pre>
// bizarre. Strip that off before parsing.
data = JSON.parse(data.match("({.*})")[0]);
}

if (data.html) {
$("#g-dialog").html(unescape(data.html));
$("#g-dialog").dialog("option", "position", "center");
$("#g-dialog form :submit").removeClass("ui-state-disabled")
.attr("disabled", null);
Expand Down
42 changes: 32 additions & 10 deletions lib/gallery.panel.js
Expand Up @@ -31,15 +31,37 @@
if (should_open) {
$(parent).after(ePanel);
$("#g-panel td").html(sHref);
$.getJSON(sHref, function(data) {
$("#g-panel td").html(unescape(data.form));
self._ajaxify_panel();
if ($(element).attr("open_text")) {
$(element).attr("orig_text", $(element).children(".g-button-text").text());
$(element).children(".g-button-text").text($(element).attr("open_text"));
$.ajax({
url: sHref,
type: "GET",
beforeSend: function(xhr) {
// Until we convert to jquery 1.4, we need to save the
// XMLHttpRequest object
this.xhrData = xhr;
},
success: function(data, textStatus, xhr) {
// Pre jquery 1.4, get the saved XMLHttpRequest object
if (xhr == undefined) {
xhr = this.xhrData;
}
var mimeType = /^(\w+\/\w+)\;?/.exec(xhr.getResponseHeader("Content-Type"));
var content = "";
if (mimeType[1] == "application/json") {
data = JSON.parse(data);
content = unescape(data.html);
} else {
content = data;
}

$("#g-panel td").html(content);
self._ajaxify_panel();
if ($(element).attr("open_text")) {
$(element).attr("orig_text", $(element).children(".g-button-text").text());
$(element).children(".g-button-text").text($(element).attr("open_text"));
}
$("#g-panel").addClass(parentClass).show().slideDown("slow");
}
$("#g-panel").addClass(parentClass).show().slideDown("slow");
});
});
}

return false;
Expand All @@ -57,8 +79,8 @@
return true;
},
success: function(data) {
if (data.form) {
$("#g-panel td form").replaceWith(data.form);
if (data.html) {
$("#g-panel td").html(data.html);
self._ajaxify_panel();
}
if (data.result == "success") {
Expand Down
8 changes: 4 additions & 4 deletions modules/comment/controllers/admin_manage_comments.php
Expand Up @@ -34,10 +34,10 @@ public function index() {

public function menu_labels() {
$menu = $this->_menu($this->_counts());
print json_encode(array((string) $menu->get("unpublished")->label,
(string) $menu->get("published")->label,
(string) $menu->get("spam")->label,
(string) $menu->get("deleted")->label));
json::reply(array((string) $menu->get("unpublished")->label,
(string) $menu->get("published")->label,
(string) $menu->get("spam")->label,
(string) $menu->get("deleted")->label));
}

public function queue($state) {
Expand Down
9 changes: 4 additions & 5 deletions modules/comment/controllers/comments.php
Expand Up @@ -56,13 +56,12 @@ public function create($id) {
$view = new Theme_View("comment.html", "other", "comment-fragment");
$view->comment = $comment;

print json_encode(
array("result" => "success",
"view" => (string) $view,
"form" => (string) comment::get_add_form($item)));
json::reply(array("result" => "success",
"view" => (string)$view,
"form" => (string)comment::get_add_form($item)));
} else {
$form = comment::prefill_add_form($form);
print json_encode(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "form" => (string)$form));
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/exif/controllers/exif.php
Expand Up @@ -28,6 +28,6 @@ public function show($item_id) {
$view = new View("exif_dialog.html");
$view->details = exif::get($item);

print json_encode(array("form" => (string) $view));
print $view;
}
}
5 changes: 3 additions & 2 deletions modules/gallery/controllers/admin.php
Expand Up @@ -78,15 +78,16 @@ private static function _reauth_check() {
$result->location = url::abs_site("");
}

print json_encode($result);
json::reply($result);
}

private static function _prompt_for_reauth($controller_name, $args) {
if (request::method() == "get") {
// Avoid anti-phishing protection by passing the url as session variable.
Session::instance()->set("continue_url", url::abs_current(true));
}

// Save the is_ajax value as we lose it, if set, when we redirect
Session::instance()->set("is_ajax_request", request::is_ajax());
url::redirect("reauthenticate");
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/gallery/controllers/admin_advanced_settings.php
Expand Up @@ -39,7 +39,7 @@ public function edit($module_name, $var_name) {
$group->input("var_name")->label(t("Setting"))->value($var_name)->disabled(1);
$group->textarea("value")->label(t("Value"))->value($value);
$group->submit("")->value(t("Save"));
print json_encode(array("form" => (string) $form));
print $form;
}

public function save($module_name, $var_name) {
Expand All @@ -50,6 +50,6 @@ public function save($module_name, $var_name) {
t("Saved value for %var (%module_name)",
array("var" => $var_name, "module_name" => $module_name)));

print json_encode(array("result" => "success"));
json::reply(array("result" => "success"));
}
}
2 changes: 1 addition & 1 deletion modules/gallery/controllers/admin_languages.php
Expand Up @@ -51,7 +51,7 @@ public function save() {
}
module::set_var("gallery", "default_locale", $new_default_locale);

print json_encode(array("result" => "success"));
json::reply(array("result" => "success"));
}

public function share() {
Expand Down
28 changes: 14 additions & 14 deletions modules/gallery/controllers/admin_maintenance.php
Expand Up @@ -64,7 +64,7 @@ public function start($task_callback) {
log::info("tasks", t("Task %task_name started (task id %task_id)",
array("task_name" => $task->name, "task_id" => $task->id)),
html::anchor("admin/maintenance", t("maintenance")));
print json_encode(array("form" => (string) $view));
print $view;
}

/**
Expand All @@ -86,7 +86,7 @@ public function resume($task_id) {
log::info("tasks", t("Task %task_name resumed (task id %task_id)",
array("task_name" => $task->name, "task_id" => $task->id)),
html::anchor("admin/maintenance", t("maintenance")));
print json_encode(array("form" => (string) $view));
print $view;
}

/**
Expand All @@ -103,7 +103,7 @@ public function show_log($task_id) {
$view = new View("admin_maintenance_show_log.html");
$view->task = $task;

print json_encode(array("form" => (string) $view));
print $view;
}

/**
Expand Down Expand Up @@ -211,19 +211,19 @@ public function run($task_id) {
break;
}
// Using sprintf("%F") to avoid comma as decimal separator.
print json_encode(array("result" => "success",
"task" => array(
"percent_complete" => sprintf("%F", $task->percent_complete),
"status" => (string) $task->status,
"done" => (bool) $task->done),
"location" => url::site("admin/maintenance")));
json::reply(array("result" => "success",
"task" => array(
"percent_complete" => sprintf("%F", $task->percent_complete),
"status" => (string) $task->status,
"done" => (bool) $task->done),
"location" => url::site("admin/maintenance")));

} else {
print json_encode(array("result" => "in_progress",
"task" => array(
"percent_complete" => sprintf("%F", $task->percent_complete),
"status" => (string) $task->status,
"done" => (bool) $task->done)));
json::reply(array("result" => "in_progress",
"task" => array(
"percent_complete" => sprintf("%F", $task->percent_complete),
"status" => (string) $task->status,
"done" => (bool) $task->done)));
}
}
}
2 changes: 1 addition & 1 deletion modules/gallery/controllers/admin_modules.php
Expand Up @@ -57,7 +57,7 @@ public function confirm() {
$result["dialog"] = (string)$v;
$result["allow_continue"] = empty($messages["error"]);
}
print json_encode($result);
json::reply($result);
}

public function save() {
Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/controllers/admin_sidebar.php
Expand Up @@ -50,7 +50,7 @@ public function update() {
$result["active"] = $v->render();
$message = t("Updated sidebar blocks");
$result["message"] = (string) $message;
print json_encode($result);
json::reply($result);
}

private function _get_blocks() {
Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/controllers/admin_themes.php
Expand Up @@ -52,7 +52,7 @@ public function preview($type, $theme_name) {
} else {
$view->url = item::root()->url("theme=$theme_name");
}
print json_encode(array("form" => (string) $view));
print $view;
}

public function choose($type, $theme_name) {
Expand Down
14 changes: 7 additions & 7 deletions modules/gallery/controllers/albums.php
Expand Up @@ -113,9 +113,9 @@ public function create($parent_id) {
message::success(t("Created album %album_title",
array("album_title" => html::purify($album->title))));

print json_encode(array("result" => "success", "location" => $album->url()));
json::reply(array("result" => "success", "location" => $album->url()));
} else {
print json_encode(array("result" => "error", "form" => (string) $form));
print $form;
}
}

Expand Down Expand Up @@ -153,13 +153,13 @@ public function update($album_id) {

if ($form->from_id->value == $album->id) {
// Use the new url; it might have changed.
print json_encode(array("result" => "success", "location" => $album->url()));
json::reply(array("result" => "success", "location" => $album->url()));
} else {
// Stay on the same page
print json_encode(array("result" => "success"));
json::reply(array("result" => "success"));
}
} else {
print json_encode(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
}

Expand All @@ -168,14 +168,14 @@ public function form_add($album_id) {
access::required("view", $album);
access::required("add", $album);

print json_encode(array("form" => (string) album::get_add_form($album)));
print album::get_add_form($album);
}

public function form_edit($album_id) {
$album = ORM::factory("item", $album_id);
access::required("view", $album);
access::required("edit", $album);

print json_encode(array("form" => (string) album::get_edit_form($album)));
print album::get_edit_form($album);
}
}
2 changes: 1 addition & 1 deletion modules/gallery/controllers/l10n_client.php
Expand Up @@ -91,7 +91,7 @@ public function save() {

Gallery_I18n::clear_cache($locale);

print json_encode(new stdClass());
json::reply(new stdClass());
}

public function toggle_l10n_mode() {
Expand Down

0 comments on commit 3b187d7

Please sign in to comment.