diff options
author | Nathan Kinkade <nkinkade@nkinka.de> | 2010-08-06 21:36:32 +0000 |
---|---|---|
committer | Nathan Kinkade <nkinkade@nkinka.de> | 2010-08-06 21:36:32 +0000 |
commit | 691ce806dc9aefac596a692ff2ba927a81a65440 (patch) | |
tree | 410f64288ef1d8bbc8455509af74d0e7582dc48e /lib/gallery.dialog.js | |
parent | c83650d83ad8b1f4bda30cac2ae8efa6e1c97287 (diff) | |
parent | 8559cdb5b6bfa87864941f726521660023779fa7 (diff) |
Merge branch 'master' of git://github.com/gallery/gallery3
Diffstat (limited to 'lib/gallery.dialog.js')
-rw-r--r-- | lib/gallery.dialog.js | 101 |
1 files changed, 78 insertions, 23 deletions
diff --git a/lib/gallery.dialog.js b/lib/gallery.dialog.js index 6ec8c634..450f4c88 100644 --- a/lib/gallery.dialog.js +++ b/lib/gallery.dialog.js @@ -1,3 +1,4 @@ + (function($) { $.widget("ui.gallery_dialog", { _init: function() { @@ -26,27 +27,42 @@ $("#g-dialog").gallery_show_loading(); - $.get(sHref, function(data) { - $("#g-dialog").html(data).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")); - if ($("#g-dialog form").length) { - self.form_loaded(null, $("#g-dialog form")); - } - self._layout(); - - $("#g-dialog").dialog("open"); - // Remove titlebar for progress dialogs or set title - if ($("#g-dialog #g-progress").length) { - $(".ui-dialog-titlebar").remove(); - } else if ($("#g-dialog h1").length) { - $("#g-dialog").dialog('option', 'title', $("#g-dialog h1:eq(0)").html()); - $("#g-dialog h1:eq(0)").hide(); - } else if ($("#g-dialog fieldset legend").length) { - $("#g-dialog").dialog('option', 'title', $("#g-dialog fieldset legend:eq(0)").html()); - } + var content = ""; + if (mimeType[1] == "application/json") { + data = JSON.parse(data); + content = unescape(data.html); + } else { + content = data; + } + + $("#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(); - if ($("#g-dialog form").length) { - self._ajaxify_dialog(); + if ($("#g-dialog form").length) { + self._ajaxify_dialog(); + } } }); $("#g-dialog").dialog("option", "self", self); @@ -106,19 +122,46 @@ _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); + self._set_title(); self._ajaxify_dialog(); self.form_loaded(null, $("#g-dialog form")); if (typeof data.reset == 'function') { @@ -136,6 +179,18 @@ }); }, + _set_title: function() { + // Remove titlebar for progress dialogs or set title + if ($("#g-dialog #g-progress").length) { + $(".ui-dialog-titlebar").remove(); + } else if ($("#g-dialog h1").length) { + $("#g-dialog").dialog('option', 'title', $("#g-dialog h1:eq(0)").html()); + $("#g-dialog h1:eq(0)").hide(); + } else if ($("#g-dialog fieldset legend").length) { + $("#g-dialog").dialog('option', 'title', $("#g-dialog fieldset legend:eq(0)").html()); + } + }, + form_closing: function(event, ui) {}, dialog_closing: function(event, ui) {} }); |