2 * Copyright 2000-2018 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.client.ui.calendar.schedule.dd;
18 import com.google.gwt.dom.client.Element;
19 import com.google.gwt.user.client.DOM;
20 import com.vaadin.client.WidgetUtil;
21 import com.vaadin.client.ui.dd.VAcceptCallback;
22 import com.vaadin.client.ui.dd.VDragEvent;
23 import com.vaadin.v7.client.ui.calendar.CalendarConnector;
24 import com.vaadin.v7.client.ui.calendar.schedule.DateCell;
25 import com.vaadin.v7.client.ui.calendar.schedule.DateCellDayEvent;
28 * Handles DD when the weekly view is showing in the Calendar. In the weekly
29 * view, drops are only allowed in the the time slots for each day. The slot
30 * index and the day index are included in the drop details sent to the server.
35 public class CalendarWeekDropHandler extends CalendarDropHandler {
37 private Element currentTargetElement;
38 private DateCell currentTargetDay;
40 public CalendarWeekDropHandler(CalendarConnector connector) {
48 * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragAccepted
49 * (com.vaadin.terminal.gwt.client.ui.dd.VDragEvent)
52 protected void dragAccepted(VDragEvent drag) {
54 currentTargetElement = drag.getElementOver();
55 currentTargetDay = WidgetUtil.findWidget(currentTargetElement,
61 * Removes the CSS style name from the emphasized element
63 private void deEmphasis() {
64 if (currentTargetElement != null) {
65 currentTargetDay.removeEmphasisStyle(currentTargetElement);
66 currentTargetElement = null;
71 * Add a CSS stylen name to current target element
73 private void emphasis() {
74 currentTargetDay.addEmphasisStyle(currentTargetElement);
81 * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragOver(com
82 * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
85 public void dragOver(final VDragEvent drag) {
86 if (isLocationValid(drag.getElementOver())) {
87 validate(new VAcceptCallback() {
89 public void accepted(VDragEvent event) {
97 * Checks if the location is a valid drop location
100 * The element to check
103 private boolean isLocationValid(Element elementOver) {
104 Element weekGridElement = calendarConnector.getWidget().getWeekGrid()
106 Element timeBarElement = calendarConnector.getWidget().getWeekGrid()
107 .getTimeBar().getElement();
109 Element todayBarElement = null;
110 if (calendarConnector.getWidget().getWeekGrid().hasToday()) {
111 todayBarElement = calendarConnector.getWidget().getWeekGrid()
112 .getDateCellOfToday().getTodaybarElement();
115 // drops are not allowed in:
117 // - allday event list
121 return DOM.isOrHasChild(weekGridElement, elementOver)
122 && !DOM.isOrHasChild(timeBarElement, elementOver)
123 && todayBarElement != elementOver
124 && (WidgetUtil.findWidget(elementOver,
125 DateCellDayEvent.class) == null);
132 * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragEnter(com
133 * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
136 public void dragEnter(VDragEvent drag) {
137 // NOOP, we determine drag acceptance in dragOver
144 * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#drop(com.vaadin
145 * .terminal.gwt.client.ui.dd.VDragEvent)
148 public boolean drop(VDragEvent drag) {
149 if (isLocationValid(drag.getElementOver())) {
150 updateDropDetails(drag);
152 return super.drop(drag);
161 * Update the drop details sent to the server
166 private void updateDropDetails(VDragEvent drag) {
167 int slotIndex = currentTargetDay.getSlotIndex(currentTargetElement);
168 int dayIndex = calendarConnector.getWidget().getWeekGrid()
169 .getDateCellIndex(currentTargetDay);
171 drag.getDropDetails().put("dropDayIndex", dayIndex);
172 drag.getDropDetails().put("dropSlotIndex", slotIndex);
179 * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragLeave(com
180 * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
183 public void dragLeave(VDragEvent drag) {
185 super.dragLeave(drag);