/
gallery.dialog.js
207 lines (187 loc) · 7.01 KB
/
gallery.dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(function($) {
$.widget("ui.gallery_dialog", {
_init: function() {
var self = this;
if (!self.options.immediate) {
this.element.click(function(event) {
event.preventDefault();
self._show($(event.currentTarget).attr("href"));
return false;
});
} else {
self._show(this.element.attr("href"));
}
},
_show: function(sHref) {
var self = this;
var eDialog = '<div id="g-dialog"></div>';
$("body").append(eDialog);
if (!self.options.close) {
self.options.close = self.close_dialog;
}
$("#g-dialog").dialog(self.options);
$("#g-dialog").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.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();
}
}
});
$("#g-dialog").dialog("option", "self", self);
},
_layout: function() {
var dialogWidth;
var dialogHeight = $("#g-dialog").height();
var cssWidth = new String($("#g-dialog form").css("width"));
var childWidth = cssWidth.replace(/[^0-9]/g,"");
var size = $.gallery_get_viewport_size();
if ($("#g-dialog iframe").length) {
dialogWidth = size.width() - 100;
// Set the iframe width and height
$("#g-dialog iframe").width("100%").height(size.height() - 100);
} else if ($("#g-dialog .g-dialog-panel").length) {
dialogWidth = size.width() - 100;
$("#g-dialog").dialog("option", "height", size.height() - 100);
} else if (childWidth == "" || childWidth > 300) {
dialogWidth = 500;
}
$("#g-dialog").dialog('option', 'width', dialogWidth);
},
form_loaded: function(event, ui) {
// Should be defined (and localized) in the theme
MSG_CANCEL = MSG_CANCEL || 'Cancel';
var eCancel = '<a href="#" class="g-cancel g-left">' + MSG_CANCEL + '</a>';
if ($("#g-dialog .submit").length) {
$("#g-dialog .submit").addClass("ui-state-default ui-corner-all");
$.fn.gallery_hover_init();
$("#g-dialog .submit").parent().append(eCancel);
$("#g-dialog .g-cancel").click(function(event) {
$("#g-dialog").dialog("close");
event.preventDefault();
});
}
$("#g-dialog .ui-state-default").hover(
function() {
$(this).addClass("ui-state-hover");
},
function() {
$(this).removeClass("ui-state-hover");
}
);
},
close_dialog: function(event, ui) {
var self = $("#g-dialog").dialog("option", "self");
if ($("#g-dialog form").length) {
self._trigger("form_closing", null, $("#g-dialog form"));
}
self._trigger("dialog_closing", null, $("#g-dialog"));
$("#g-dialog").dialog("destroy").remove();
},
_ajaxify_dialog: function() {
var self = this;
$("#g-dialog form").ajaxForm({
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) {
// 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') {
eval(data.reset + '()');
}
}
if (data.result == "success") {
if (data.location) {
window.location = data.location;
} else {
window.location.reload();
}
}
}
});
},
_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) {}
});
$.extend($.ui.gallery_dialog, {
defaults: {
autoOpen: false,
autoResize: true,
modal: true,
resizable: false,
position: "center"
}
});
})(jQuery);