You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VAbstractPopupCalendar.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.Date;
  18. import com.google.gwt.aria.client.Id;
  19. import com.google.gwt.aria.client.LiveValue;
  20. import com.google.gwt.aria.client.Roles;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.event.dom.client.ClickEvent;
  23. import com.google.gwt.event.dom.client.ClickHandler;
  24. import com.google.gwt.event.dom.client.DomEvent;
  25. import com.google.gwt.event.dom.client.KeyCodes;
  26. import com.google.gwt.event.dom.client.MouseOutEvent;
  27. import com.google.gwt.event.dom.client.MouseOutHandler;
  28. import com.google.gwt.event.dom.client.MouseOverEvent;
  29. import com.google.gwt.event.dom.client.MouseOverHandler;
  30. import com.google.gwt.event.logical.shared.CloseEvent;
  31. import com.google.gwt.event.logical.shared.CloseHandler;
  32. import com.google.gwt.i18n.client.DateTimeFormat;
  33. import com.google.gwt.user.client.DOM;
  34. import com.google.gwt.user.client.Event;
  35. import com.google.gwt.user.client.Timer;
  36. import com.google.gwt.user.client.Window;
  37. import com.google.gwt.user.client.ui.Button;
  38. import com.google.gwt.user.client.ui.FlowPanel;
  39. import com.google.gwt.user.client.ui.Label;
  40. import com.google.gwt.user.client.ui.PopupPanel;
  41. import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
  42. import com.google.gwt.user.client.ui.RootPanel;
  43. import com.google.gwt.user.client.ui.Widget;
  44. import com.vaadin.client.BrowserInfo;
  45. import com.vaadin.client.ComputedStyle;
  46. import com.vaadin.client.VConsole;
  47. import com.vaadin.client.ui.VAbstractCalendarPanel.FocusOutListener;
  48. import com.vaadin.client.ui.VAbstractCalendarPanel.SubmitListener;
  49. import com.vaadin.client.ui.aria.AriaHelper;
  50. import com.vaadin.shared.ui.datefield.TextualDateFieldState;
  51. /**
  52. * Represents a date selection component with a text field and a popup date/time
  53. * selector.
  54. *
  55. * <b>Note:</b> To change the keyboard assignments used in the popup dialog you
  56. * should extend <code>com.vaadin.client.ui.VAbstractCalendarPanel</code> and
  57. * then pass set it by calling the
  58. * <code>setCalendarPanel(VAbstractCalendarPanel panel)</code> method.
  59. *
  60. * @since 8.0
  61. */
  62. public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPanel<R>, R extends Enum<R>>
  63. extends VAbstractTextualDate<R>
  64. implements Field, ClickHandler, CloseHandler<PopupPanel>, SubPartAware {
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public final Button calendarToggle = new Button();
  67. /** For internal use only. May be removed or replaced in the future. */
  68. public PANEL calendar;
  69. /** For internal use only. May be removed or replaced in the future. */
  70. public final VOverlay popup;
  71. /** For internal use only. May be removed or replaced in the future. */
  72. public boolean parsable = true;
  73. private boolean open;
  74. /*
  75. * #14857: If calendarToggle button is clicked when calendar popup is
  76. * already open we should prevent calling openCalendarPanel() in onClick,
  77. * since we don't want to reopen it again right after it closes.
  78. */
  79. private boolean preventOpenPopupCalendar;
  80. private boolean cursorOverCalendarToggleButton;
  81. private boolean toggleButtonClosesWithGuarantee;
  82. private boolean textFieldEnabled = true;
  83. private String captionId;
  84. private Label selectedDate;
  85. private Element descriptionForAssistiveDevicesElement;
  86. private final String CALENDAR_TOGGLE_ID = "popupButton";
  87. public VAbstractPopupCalendar(PANEL calendarPanel, R resolution) {
  88. super(resolution);
  89. calendarToggle.setText("");
  90. calendarToggle.addClickHandler(this);
  91. calendarToggle.addDomHandler(new MouseOverHandler() {
  92. @Override
  93. public void onMouseOver(MouseOverEvent event) {
  94. cursorOverCalendarToggleButton = true;
  95. }
  96. }, MouseOverEvent.getType());
  97. calendarToggle.addDomHandler(new MouseOutHandler() {
  98. @Override
  99. public void onMouseOut(MouseOutEvent event) {
  100. cursorOverCalendarToggleButton = false;
  101. }
  102. }, MouseOutEvent.getType());
  103. // -2 instead of -1 to avoid FocusWidget.onAttach to reset it
  104. calendarToggle.getElement().setTabIndex(-2);
  105. Roles.getButtonRole().set(calendarToggle.getElement());
  106. Roles.getButtonRole().setAriaHiddenState(calendarToggle.getElement(),
  107. true);
  108. add(calendarToggle);
  109. // Description of the usage of the widget for assistive device users
  110. descriptionForAssistiveDevicesElement = DOM.createDiv();
  111. descriptionForAssistiveDevicesElement.setInnerText(
  112. TextualDateFieldState.DESCRIPTION_FOR_ASSISTIVE_DEVICES);
  113. AriaHelper.ensureHasId(descriptionForAssistiveDevicesElement);
  114. Roles.getTextboxRole().setAriaDescribedbyProperty(text.getElement(),
  115. Id.of(descriptionForAssistiveDevicesElement));
  116. AriaHelper.setVisibleForAssistiveDevicesOnly(
  117. descriptionForAssistiveDevicesElement, true);
  118. calendar = calendarPanel;
  119. calendar.setParentField(this);
  120. calendar.setFocusOutListener(new FocusOutListener() {
  121. @Override
  122. public boolean onFocusOut(DomEvent<?> event) {
  123. event.preventDefault();
  124. closeCalendarPanel();
  125. return true;
  126. }
  127. });
  128. // FIXME: Problem is, that the element with the provided id does not
  129. // exist yet in html. This is the same problem as with the context menu.
  130. // Apply here the same fix (#11795)
  131. Roles.getTextboxRole().setAriaControlsProperty(text.getElement(),
  132. Id.of(calendar.getElement()));
  133. Roles.getButtonRole().setAriaControlsProperty(
  134. calendarToggle.getElement(), Id.of(calendar.getElement()));
  135. calendar.setSubmitListener(new SubmitListener() {
  136. @Override
  137. public void onSubmit() {
  138. // Update internal value and send valuechange event if immediate
  139. updateValue(calendar.getDate());
  140. // Update text field (a must when not immediate).
  141. buildDate(true);
  142. closeCalendarPanel();
  143. }
  144. @Override
  145. public void onCancel() {
  146. closeCalendarPanel();
  147. }
  148. });
  149. popup = new VOverlay(true, false);
  150. popup.setOwner(this);
  151. FlowPanel wrapper = new FlowPanel();
  152. selectedDate = new Label();
  153. selectedDate.setStyleName(getStylePrimaryName() + "-selecteddate");
  154. AriaHelper.setVisibleForAssistiveDevicesOnly(selectedDate.getElement(),
  155. true);
  156. Roles.getTextboxRole().setAriaLiveProperty(selectedDate.getElement(),
  157. LiveValue.ASSERTIVE);
  158. Roles.getTextboxRole().setAriaAtomicProperty(selectedDate.getElement(),
  159. true);
  160. wrapper.add(selectedDate);
  161. wrapper.add(calendar);
  162. popup.setWidget(wrapper);
  163. popup.addCloseHandler(this);
  164. DOM.setElementProperty(calendar.getElement(), "id",
  165. "PID_VAADIN_POPUPCAL");
  166. sinkEvents(Event.ONKEYDOWN);
  167. updateStyleNames();
  168. }
  169. @Override
  170. protected void onAttach() {
  171. super.onAttach();
  172. DOM.appendChild(RootPanel.get().getElement(),
  173. descriptionForAssistiveDevicesElement);
  174. }
  175. @Override
  176. protected void onDetach() {
  177. super.onDetach();
  178. descriptionForAssistiveDevicesElement.removeFromParent();
  179. closeCalendarPanel();
  180. }
  181. /**
  182. * Changes the current date, and updates the
  183. * {@link VDateField#bufferedResolutions}, possibly
  184. * {@link VDateField#sendBufferedValues()} to the server if needed
  185. *
  186. * @param newDate
  187. * the new {@code Date} to update
  188. */
  189. @SuppressWarnings("deprecation")
  190. public void updateValue(Date newDate) {
  191. Date currentDate = getCurrentDate();
  192. R resolution = getCurrentResolution();
  193. if (currentDate == null || newDate.getTime() != currentDate.getTime()) {
  194. setCurrentDate((Date) newDate.clone());
  195. bufferedResolutions.put(calendar.getResolution(calendar::isYear),
  196. newDate.getYear() + 1900);
  197. if (!calendar.isYear(resolution)) {
  198. bufferedResolutions.put(
  199. calendar.getResolution(calendar::isMonth),
  200. newDate.getMonth() + 1);
  201. if (!calendar.isMonth(resolution)) {
  202. bufferedResolutions.put(
  203. calendar.getResolution(calendar::isDay),
  204. newDate.getDate());
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * Checks whether the text field is enabled.
  211. *
  212. * @see VAbstractPopupCalendar#setTextFieldEnabled(boolean)
  213. * @return The current state of the text field.
  214. */
  215. public boolean isTextFieldEnabled() {
  216. return textFieldEnabled;
  217. }
  218. /**
  219. * Sets the state of the text field of this component. By default the text
  220. * field is enabled. Disabling it causes only the button for date selection
  221. * to be active, thus preventing the user from entering invalid dates. See
  222. * <a href="http://dev.vaadin.com/ticket/6790>#6790</a>.
  223. *
  224. * @param textFieldEnabled
  225. */
  226. public void setTextFieldEnabled(boolean textFieldEnabled) {
  227. this.textFieldEnabled = textFieldEnabled;
  228. updateTextFieldEnabled();
  229. }
  230. protected void updateTextFieldEnabled() {
  231. boolean reallyEnabled = isEnabled() && isTextFieldEnabled();
  232. // IE has a non input disabled themeing that can not be overridden so we
  233. // must fake the functionality using readonly and unselectable
  234. if (BrowserInfo.get().isIE()) {
  235. if (!reallyEnabled) {
  236. text.getElement().setAttribute("unselectable", "on");
  237. text.getElement().setAttribute("readonly", "");
  238. text.setTabIndex(-2);
  239. } else if (reallyEnabled
  240. && text.getElement().hasAttribute("unselectable")) {
  241. text.getElement().removeAttribute("unselectable");
  242. text.getElement().removeAttribute("readonly");
  243. text.setTabIndex(0);
  244. }
  245. } else {
  246. text.setEnabled(reallyEnabled);
  247. }
  248. if (reallyEnabled) {
  249. calendarToggle.setTabIndex(-1);
  250. Roles.getButtonRole()
  251. .setAriaHiddenState(calendarToggle.getElement(), true);
  252. } else {
  253. calendarToggle.setTabIndex(0);
  254. Roles.getButtonRole()
  255. .setAriaHiddenState(calendarToggle.getElement(), false);
  256. }
  257. handleAriaAttributes();
  258. }
  259. /**
  260. * Set correct tab index for disabled text field in IE as the value set in
  261. * setTextFieldEnabled(...) gets overridden in
  262. * TextualDateConnection.updateFromUIDL(...).
  263. *
  264. * @since 7.3.1
  265. */
  266. public void setTextFieldTabIndex() {
  267. if (BrowserInfo.get().isIE() && !textFieldEnabled) {
  268. // index needs to be -2 because FocusWidget updates -1 to 0 onAttach
  269. text.setTabIndex(-2);
  270. }
  271. }
  272. @Override
  273. public void bindAriaCaption(
  274. com.google.gwt.user.client.Element captionElement) {
  275. if (captionElement == null) {
  276. captionId = null;
  277. } else {
  278. captionId = captionElement.getId();
  279. }
  280. if (isTextFieldEnabled()) {
  281. super.bindAriaCaption(captionElement);
  282. } else {
  283. AriaHelper.bindCaption(calendarToggle, captionElement);
  284. }
  285. handleAriaAttributes();
  286. }
  287. private void handleAriaAttributes() {
  288. Widget removeFromWidget;
  289. Widget setForWidget;
  290. if (isTextFieldEnabled()) {
  291. setForWidget = text;
  292. removeFromWidget = calendarToggle;
  293. } else {
  294. setForWidget = calendarToggle;
  295. removeFromWidget = text;
  296. }
  297. Roles.getFormRole()
  298. .removeAriaLabelledbyProperty(removeFromWidget.getElement());
  299. if (captionId == null) {
  300. Roles.getFormRole()
  301. .removeAriaLabelledbyProperty(setForWidget.getElement());
  302. } else {
  303. Roles.getFormRole().setAriaLabelledbyProperty(
  304. setForWidget.getElement(), Id.of(captionId));
  305. }
  306. }
  307. /*
  308. * (non-Javadoc)
  309. *
  310. * @see
  311. * com.google.gwt.user.client.ui.UIObject#setStyleName(java.lang.String)
  312. */
  313. @Override
  314. public void setStyleName(String style) {
  315. super.setStyleName(style);
  316. updateStyleNames();
  317. }
  318. @Override
  319. public void setStylePrimaryName(String style) {
  320. removeStyleName(getStylePrimaryName() + "-popupcalendar");
  321. super.setStylePrimaryName(style);
  322. updateStyleNames();
  323. }
  324. @Override
  325. protected void updateStyleNames() {
  326. super.updateStyleNames();
  327. if (getStylePrimaryName() != null && calendarToggle != null) {
  328. addStyleName(getStylePrimaryName() + "-popupcalendar");
  329. calendarToggle.setStyleName(getStylePrimaryName() + "-button");
  330. popup.setStyleName(getStylePrimaryName() + "-popup");
  331. calendar.setStyleName(getStylePrimaryName() + "-calendarpanel");
  332. }
  333. }
  334. /**
  335. * Opens the calendar panel popup.
  336. */
  337. public void openCalendarPanel() {
  338. if (!open && !readonly && isEnabled()) {
  339. open = true;
  340. if (getCurrentDate() != null) {
  341. calendar.setDate((Date) getCurrentDate().clone());
  342. } else if (getDefaultDate() != null) {
  343. calendar.setDate(getDefaultDate());
  344. } else {
  345. calendar.setDate(new Date());
  346. }
  347. // clear previous values
  348. popup.setWidth("");
  349. popup.setHeight("");
  350. popup.setPopupPositionAndShow(new PopupPositionCallback());
  351. } else {
  352. VConsole.error("Cannot reopen popup, it is already open!");
  353. }
  354. }
  355. /*
  356. * (non-Javadoc)
  357. *
  358. * @see
  359. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  360. * .dom.client.ClickEvent)
  361. */
  362. @Override
  363. public void onClick(ClickEvent event) {
  364. if (event.getSource() == calendarToggle && isEnabled()) {
  365. if (open) {
  366. closeCalendarPanel();
  367. } else if (!preventOpenPopupCalendar) {
  368. openCalendarPanel();
  369. }
  370. preventOpenPopupCalendar = false;
  371. }
  372. }
  373. /*
  374. * (non-Javadoc)
  375. *
  376. * @see
  377. * com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google.gwt
  378. * .event.logical.shared.CloseEvent)
  379. */
  380. @Override
  381. public void onClose(CloseEvent<PopupPanel> event) {
  382. if (event.getSource() == popup) {
  383. buildDate();
  384. if (!BrowserInfo.get().isTouchDevice() && textFieldEnabled) {
  385. /*
  386. * Move focus to textbox, unless on touch device (avoids opening
  387. * virtual keyboard) or if textField is disabled.
  388. */
  389. focus();
  390. }
  391. open = false;
  392. if (cursorOverCalendarToggleButton
  393. && !toggleButtonClosesWithGuarantee) {
  394. preventOpenPopupCalendar = true;
  395. }
  396. toggleButtonClosesWithGuarantee = false;
  397. }
  398. }
  399. /**
  400. * Sets focus to Calendar panel.
  401. *
  402. * @param focus
  403. */
  404. public void setFocus(boolean focus) {
  405. calendar.setFocus(focus);
  406. }
  407. @Override
  408. public void setEnabled(boolean enabled) {
  409. super.setEnabled(enabled);
  410. updateTextFieldEnabled();
  411. calendarToggle.setEnabled(enabled);
  412. Roles.getButtonRole().setAriaDisabledState(calendarToggle.getElement(),
  413. !enabled);
  414. }
  415. /**
  416. * Sets the content of a special field for assistive devices, so that they
  417. * can recognize the change and inform the user (reading out in case of
  418. * screen reader).
  419. *
  420. * @param selectedDate
  421. * Date that is currently selected
  422. */
  423. public void setFocusedDate(Date selectedDate) {
  424. this.selectedDate.setText(DateTimeFormat.getFormat("dd, MMMM, yyyy")
  425. .format(selectedDate));
  426. }
  427. /**
  428. * For internal use only. May be removed or replaced in the future.
  429. *
  430. * @see com.vaadin.client.ui.VAbstractTextualDate#buildDate()
  431. */
  432. @Override
  433. public void buildDate() {
  434. // Save previous value
  435. String previousValue = getText();
  436. super.buildDate();
  437. // Restore previous value if the input could not be parsed
  438. if (!parsable) {
  439. setText(previousValue);
  440. }
  441. updateTextFieldEnabled();
  442. }
  443. /**
  444. * Update the text field contents from the date. See {@link #buildDate()}.
  445. *
  446. * @param forceValid
  447. * true to force the text field to be updated, false to only
  448. * update if the parsable flag is true.
  449. */
  450. protected void buildDate(boolean forceValid) {
  451. if (forceValid) {
  452. parsable = true;
  453. }
  454. buildDate();
  455. }
  456. /*
  457. * (non-Javadoc)
  458. *
  459. * @see com.vaadin.client.ui.VDateField#onBrowserEvent(com.google
  460. * .gwt.user.client.Event)
  461. */
  462. @Override
  463. public void onBrowserEvent(com.google.gwt.user.client.Event event) {
  464. super.onBrowserEvent(event);
  465. if (DOM.eventGetType(event) == Event.ONKEYDOWN
  466. && event.getKeyCode() == getOpenCalenderPanelKey()) {
  467. openCalendarPanel();
  468. event.preventDefault();
  469. }
  470. }
  471. /**
  472. * Get the key code that opens the calendar panel. By default it is the down
  473. * key but you can override this to be whatever you like
  474. *
  475. * @return
  476. */
  477. protected int getOpenCalenderPanelKey() {
  478. return KeyCodes.KEY_DOWN;
  479. }
  480. /**
  481. * Closes the open popup panel.
  482. */
  483. public void closeCalendarPanel() {
  484. if (open) {
  485. toggleButtonClosesWithGuarantee = true;
  486. popup.hide(true);
  487. }
  488. }
  489. @Override
  490. public com.google.gwt.user.client.Element getSubPartElement(
  491. String subPart) {
  492. if (subPart.equals(CALENDAR_TOGGLE_ID)) {
  493. return calendarToggle.getElement();
  494. }
  495. return super.getSubPartElement(subPart);
  496. }
  497. @Override
  498. public String getSubPartName(
  499. com.google.gwt.user.client.Element subElement) {
  500. if (calendarToggle.getElement().isOrHasChild(subElement)) {
  501. return CALENDAR_TOGGLE_ID;
  502. }
  503. return super.getSubPartName(subElement);
  504. }
  505. /**
  506. * Set a description that explains the usage of the Widget for users of
  507. * assistive devices.
  508. *
  509. * @param descriptionForAssistiveDevices
  510. * String with the description
  511. */
  512. public void setDescriptionForAssistiveDevices(
  513. String descriptionForAssistiveDevices) {
  514. descriptionForAssistiveDevicesElement
  515. .setInnerText(descriptionForAssistiveDevices);
  516. }
  517. /**
  518. * Get the description that explains the usage of the Widget for users of
  519. * assistive devices.
  520. *
  521. * @return String with the description
  522. */
  523. public String getDescriptionForAssistiveDevices() {
  524. return descriptionForAssistiveDevicesElement.getInnerText();
  525. }
  526. /**
  527. * Sets the start range for this component. The start range is inclusive,
  528. * and it depends on the current resolution, what is considered inside the
  529. * range.
  530. *
  531. * @param rangeStart
  532. * - the allowed range's start date
  533. */
  534. public void setRangeStart(Date rangeStart) {
  535. calendar.setRangeStart(rangeStart);
  536. }
  537. /**
  538. * Sets the end range for this component. The end range is inclusive, and it
  539. * depends on the current resolution, what is considered inside the range.
  540. *
  541. * @param rangeEnd
  542. * - the allowed range's end date
  543. */
  544. public void setRangeEnd(Date rangeEnd) {
  545. calendar.setRangeEnd(rangeEnd);
  546. }
  547. private class PopupPositionCallback implements PositionCallback {
  548. @Override
  549. public void setPosition(int offsetWidth, int offsetHeight) {
  550. final int width = offsetWidth;
  551. final int height = offsetHeight;
  552. final int browserWindowWidth = Window.getClientWidth()
  553. + Window.getScrollLeft();
  554. final int windowHeight = Window.getClientHeight()
  555. + Window.getScrollTop();
  556. int left = calendarToggle.getAbsoluteLeft();
  557. // Add a little extra space to the right to avoid
  558. // problems with IE7 scrollbars and to make it look
  559. // nicer.
  560. int extraSpace = 30;
  561. boolean overflow = left + width + extraSpace > browserWindowWidth;
  562. if (overflow) {
  563. // Part of the popup is outside the browser window
  564. // (to the right)
  565. left = browserWindowWidth - width - extraSpace;
  566. }
  567. int top = calendarToggle.getAbsoluteTop();
  568. int extraHeight = 2;
  569. boolean verticallyRepositioned = false;
  570. ComputedStyle style = new ComputedStyle(popup.getElement());
  571. int[] margins = style.getMargin();
  572. int desiredPopupBottom = top + height
  573. + calendarToggle.getOffsetHeight() + margins[0]
  574. + margins[2];
  575. if (desiredPopupBottom > windowHeight) {
  576. int updatedLeft = left;
  577. left = getLeftPosition(left, width, style, overflow);
  578. // if position has not been changed then it means there is no
  579. // space to make popup fully visible
  580. if (updatedLeft == left) {
  581. // let's try to show popup on the top of the field
  582. int updatedTop = top - extraHeight - height - margins[0]
  583. - margins[2];
  584. verticallyRepositioned = updatedTop >= 0;
  585. if (verticallyRepositioned) {
  586. top = updatedTop;
  587. }
  588. }
  589. // Part of the popup is outside the browser window
  590. // (below)
  591. if (!verticallyRepositioned) {
  592. verticallyRepositioned = true;
  593. top = windowHeight - height - extraSpace + extraHeight;
  594. }
  595. }
  596. if (verticallyRepositioned) {
  597. popup.setPopupPosition(left, top);
  598. } else {
  599. popup.setPopupPosition(left,
  600. top + calendarToggle.getOffsetHeight() + extraHeight);
  601. }
  602. doSetFocus();
  603. }
  604. private int getLeftPosition(int left, int width, ComputedStyle style,
  605. boolean overflow) {
  606. if (positionRightSide()) {
  607. // Show to the right of the popup button unless we
  608. // are in the lower right corner of the screen
  609. if (overflow) {
  610. return left;
  611. } else {
  612. return left + calendarToggle.getOffsetWidth();
  613. }
  614. } else {
  615. int[] margins = style.getMargin();
  616. int desiredLeftPosition = calendarToggle.getAbsoluteLeft()
  617. - width - margins[1] - margins[3];
  618. if (desiredLeftPosition >= 0) {
  619. return desiredLeftPosition;
  620. } else {
  621. return left;
  622. }
  623. }
  624. }
  625. private boolean positionRightSide() {
  626. int buttonRightSide = calendarToggle.getAbsoluteLeft()
  627. + calendarToggle.getOffsetWidth();
  628. int textRightSide = text.getAbsoluteLeft() + text.getOffsetWidth();
  629. return buttonRightSide >= textRightSide;
  630. }
  631. private void doSetFocus() {
  632. /*
  633. * We have to wait a while before focusing since the popup needs to
  634. * be opened before we can focus
  635. */
  636. Timer focusTimer = new Timer() {
  637. @Override
  638. public void run() {
  639. setFocus(true);
  640. }
  641. };
  642. focusTimer.schedule(100);
  643. }
  644. }
  645. }