001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.addins.client.dnd.js;
021
022import gwt.material.design.addins.client.dnd.constants.Restriction;
023import gwt.material.design.client.constants.Axis;
024import jsinterop.annotations.JsOverlay;
025import jsinterop.annotations.JsPackage;
026import jsinterop.annotations.JsProperty;
027import jsinterop.annotations.JsType;
028
029/**
030 * Options for dnd component.
031 *
032 * @author kevzlou7979
033 */
034@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
035public class JsDragOptions {
036
037    @JsProperty
038    public boolean inertia;
039
040    @JsProperty
041    public String axis;
042
043    @JsProperty
044    public JsDragRestrictions restrict;
045
046    @JsOverlay
047    public static JsDragOptions create() {
048        return create(true, null, new Restriction());
049    }
050
051    @JsOverlay
052    public static JsDragOptions create(Restriction restriction) {
053        return create(true, null, restriction);
054    }
055
056    @JsOverlay
057    public static JsDragOptions create(boolean inertia) {
058        return create(inertia, null, new Restriction());
059    }
060
061    @JsOverlay
062    public static JsDragOptions create(Axis axis) {
063        return create(true, axis, new Restriction());
064    }
065
066    @JsOverlay
067    public static JsDragOptions create(boolean inertia, Axis axis, Restriction restriction) {
068        JsDragOptions options = new JsDragOptions();
069        options.inertia = inertia;
070        if (axis != null) {
071            if (axis.equals(Axis.HORIZONTAL)) {
072                options.axis = "x";
073            } else {
074                options.axis = "y";
075            }
076        }
077
078        // Restrict Options
079        JsDragRestrictions restrict = new JsDragRestrictions();
080        if (restriction != null) {
081            restrict.restriction = restriction.getRestriction();
082            restrict.endOnly = restriction.isEndOnly();
083
084            // Element Rec Options
085            JsDragElementRect elementRect = new JsDragElementRect();
086            elementRect.top = restriction.getTop();
087            elementRect.left = restriction.getLeft();
088            elementRect.bottom = restriction.getBottom();
089            elementRect.right = restriction.getRight();
090            restrict.elementRect = elementRect;
091        }
092        options.restrict = restrict;
093
094        return options;
095    }
096}