-
Notifications
You must be signed in to change notification settings - Fork 389
/
PopupSettings.java
291 lines (253 loc) · 7.55 KB
/
PopupSettings.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.markup.html.link;
import org.apache.wicket.util.io.IClusterable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A popup specification can be used as a property of the {@link Link}classes to specify that the
* link should be rendered with an onClick javascript event handler that opens a new window with the
* links' URL.
* <p>
* You can 'or' display flags together like this:
*
* <pre>
* new PopupSettings(PopupSettings.RESIZABLE | PopupSettings.SCROLLBARS);
* </pre>
*
* </p>
*
* @author Jonathan Locke
* @author Eelco Hillenius
*/
public class PopupSettings implements IClusterable
{
/** The log. */
private static final Logger log = LoggerFactory.getLogger(PopupSettings.class);
private static final long serialVersionUID = 1L;
/** Flag to include location bar */
public static final int LOCATION_BAR = 1;
/** Flag to include menu bar */
public static final int MENU_BAR = 2;
/** Flag to make popup resizable */
public static final int RESIZABLE = 4;
/** Flag to include scrollbars */
public static final int SCROLLBARS = 8;
/** Flag to include status bar */
public static final int STATUS_BAR = 16;
/** Flag to include location bar */
public static final int TOOL_BAR = 32;
/** Display flags */
private int displayFlags;
/** Height of popup window. */
private int height = -1;
/** Left position of popup window. */
private int left = -1;
/**
* The target to put in JavaScript. This implementation simply refers to the href attribute, but
* clients may want to override this (e.g. when the HTML element is not an anchor).
*/
private String target = "href";
/** Top position of popup window. */
private int top = -1;
/** Width of popup window. */
private int width = -1;
/**
* The logical name of the window. This can be anything you want, although you should use
* alphanumeric characters only (no spaces or punctuation). If you have a window already open
* and call window.open a second time using the same windowName, the first window will be reused
* rather than opening a second window.
*/
private String windowName = null;
/**
* Constructor.
*/
public PopupSettings()
{
}
/**
* Construct.
*
* @param displayFlags
* Display flags
*/
public PopupSettings(final int displayFlags)
{
this(null, displayFlags);
}
/**
* Construct.
*
* @param windowName
*/
public PopupSettings(String windowName)
{
this(windowName, 0);
}
/**
* Construct.
*
* @param windowName
* The pagemap where this popup must be in. Typically, you should put any popup in a
* separate page map as Wicket holds references to a limited number of pages/
* versions only. If you don't put your popup in a separate page map, the user might
* get page expired exceptions when getting back to the main window again.
* @param displayFlags
* Display flags
*/
public PopupSettings(String windowName, final int displayFlags)
{
this.displayFlags = displayFlags;
this.windowName = windowName;
}
/**
* Get the onClick javascript event handler.
*
* @return the onClick javascript event handler
*/
public String getPopupJavaScript()
{
String windowTitle = windowName;
if (windowTitle == null)
{
windowTitle = "";
}
else
{
// Fix for IE bug.
windowTitle = windowTitle.replaceAll("\\W", "_");
}
StringBuilder script = new StringBuilder("var w = window.open(" + target + ", '").append(
windowTitle).append("', '");
script.append("scrollbars=").append(flagToString(SCROLLBARS));
script.append(",location=").append(flagToString(LOCATION_BAR));
script.append(",menuBar=").append(flagToString(MENU_BAR));
script.append(",resizable=").append(flagToString(RESIZABLE));
script.append(",status=").append(flagToString(STATUS_BAR));
script.append(",toolbar=").append(flagToString(TOOL_BAR));
if (width != -1)
{
script.append(",width=").append(width);
}
if (height != -1)
{
script.append(",height=").append(height);
}
if (left != -1)
{
script.append(",left=").append(left);
}
if (top != -1)
{
script.append(",top=").append(top);
}
script.append("'); try {if (w.blur) w.focus();}catch(ignore){}; return false;");
return script.toString();
}
/**
* Sets the popup window height.
*
* @param popupHeight
* the popup window height.
* @return This
*/
public PopupSettings setHeight(int popupHeight)
{
height = popupHeight;
return this;
}
/**
* Sets the left position of the popup window.
*
* @param popupPositionLeft
* the left position of the popup window.
* @return This
*/
public PopupSettings setLeft(int popupPositionLeft)
{
left = popupPositionLeft;
return this;
}
/**
* Sets the target of the link. The default implementation simply refers to the href attribute of
* the anchor element, but clients may want to override this (e.g. when the HTML element is not an anchor) by
* setting the target explicitly.
*
* <strong>Note</strong>: if the target is an url (relative or absolute) then it should be wrapped in
* quotes, for example: <code>setTarget("'some/url'")</code>. If the url is delivered with an HTML attribute then
* it should be without quotes, for example: <code>setTarget("this.dataset['popup-url']")</code> with markup like:
* <pre><code><a data-popup-url="some/url">Link</a></code></pre>.
*
* @param target
* the target of the link
*/
public void setTarget(String target)
{
this.target = target;
}
/**
* Sets the top position of the popup window.
*
* @param popupPositionTop
* the top position of the popup window.
* @return This
*/
public PopupSettings setTop(int popupPositionTop)
{
top = popupPositionTop;
return this;
}
/**
* Sets the popup window width.
*
* @param popupWidth
* the popup window width.
* @return This
*/
public PopupSettings setWidth(int popupWidth)
{
width = popupWidth;
return this;
}
/**
* Sets the window name. The logical name of the window. This can be anything you want, although
* you should use alphanumeric characters only (no spaces or punctuation). If you have a window
* already open and call window.open a second time using the same windowName, the first window
* will be reused rather than opening a second window
*
* @param popupWindowName
* window name.
* @return This
*/
public PopupSettings setWindowName(String popupWindowName)
{
if (popupWindowName != null)
{
windowName = popupWindowName;
}
return this;
}
/**
* @param flag
* The flag to test
* @return Yes or no depending on whether the flag is set
*/
private String flagToString(final int flag)
{
return (displayFlags & flag) != 0 ? "yes" : "no";
}
}