<commit_before>/*
 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (http://www.leroymerlin.fr/) - initial implementation
 */

package org.nuxeo.opensocial.container.client.view;

import java.util.ArrayList;

import org.nuxeo.opensocial.container.client.ContainerEntryPoint;
import org.nuxeo.opensocial.container.client.JsLibrary;
import org.nuxeo.opensocial.container.client.bean.GadgetBean;
import org.nuxeo.opensocial.container.client.bean.GadgetPosition;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.ExtElement;
import com.gwtext.client.dd.DragData;
import com.gwtext.client.dd.DragSource;
import com.gwtext.client.dd.DropTargetConfig;
import com.gwtext.client.dd.ScrollManager;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.portal.PanelProxy;
import com.gwtext.client.widgets.portal.PortalColumn;
import com.gwtext.client.widgets.portal.PortalDropZone;

/**
 * DropZone serve for catch drag and drop event and call container service for
 * save
 * 
 * @author <NAME>
 * 
 */
public class DropZone extends PortalDropZone {

    private static final String DEFAULT = "default";

    private static final String ZONE_CLASS = "x-column-possible";

    private static final int GHOST_HEIGHT = 100;

    private static final int GHOST_WIDTH = 140;

    private static ContainerPortal portal;

    private int lastCW = -1;

    private PosGrid[] grid;

    private Container lastPosC;

    private int col;

    public DropZone(ContainerPortal portal, DropTargetConfig config) {
        super(portal, config);
        ScrollManager.register(portal.getBody()
                .getDOM());
        DropZone.portal = portal;
        overrideDragDrop(GHOST_WIDTH, GHOST_HEIGHT, ZONE_CLASS);
    }

    private static native void overrideDragDrop(int defaultX, int defaultY,
            String className)
    /*-{
      $wnd.Ext.override($wnd.Ext.Panel.DD, {
        startDrag : function(x,y){
          var g = this.proxy.getGhost();
          g.setX(x - 70);
          g.setWidth(140);

          if(g.getHeight() > 50)
            g.setHeight(100);

          $wnd.Ext.select("div.x-column").addClass("x-column-possible");
          g.select("div.x-tool").setStyle("display","none");
          g.select("div.x-panel-tl").setStyle("background-color","#a29f9a");
        },

        alignElWithMouse: function(el, iPageX, iPageY) {
          var fly = el.dom ? el : $wnd.Ext.fly(el, '_dd');
          var X = iPageX - 70;
          var Y = iPageY - 10;
          fly.setLeftTop(X, Y);
          this.cachePosition(X, Y);
          this.autoScroll(X, Y, el.offsetHeight, el.offsetWidth);
          return this.getTargetCoord(X, Y);
        },

        endDrag : function(e){
          this.proxy.hide();
          this.panel.saveState();
          $wnd.Ext.select("div.x-column").removeClass("x-column-possible");
          $wnd.Ext.select("div.x-tool").setStyle("display","block");
          @org.nuxeo.opensocial.container.client.view.DropZone::endDragDrop(Ljava/lang/String;)(this.id);
        }
      });

      $wnd.Ext.override($wnd.Ext.dd.PanelProxy, {
        hide : function(){
          if(this.ghost) {
            var el = this.panel.el;
            el.dom.style.display = '';
            if(this.proxy) {
              this.proxy.remove();
              delete this.proxy;
            }
            this.ghost.remove();
            delete this.ghost;
          }
        }
      });

      $wnd.Ext.override($wnd.Ext.dd.DragSource, {
        onDragDrop : function(e, id){
          var target = this.cachedTarget || $wnd.Ext.dd.DragDropMgr.getDDById(id);
          if(this.beforeDragDrop(target, e, id) !== false){
            if(target.isNotifyTarget){
              if(target.notifyDrop(this, e, this.dragData))
                this.onValidDrop(target, e, id);
              else
                this.onValidDrop(target, e, id);
              }else
                this.onValidDrop(target, e, id);
              if(this.afterDragDrop)
                this.afterDragDrop(target, e, id);
            }
          delete this.cachedTarget;
        },

        onDragOut : function(e, id){
          var target = this.cachedTarget || $wnd.Ext.dd.DragDropMgr.getDDById(id);
          target.notifyOut(this, e, this.dragData);
        },

        onInvalidDrop : function(target, e, id){
          this.onDragDrop(e,id);
        }

      });
    }-*/;

    /**
     * This method is called by the code JSNI in overrideDragDrop, overriding
     * the method of endDrag PortalDropZone not working
     * 
     * @param id
     */
    public static void endDragDrop(final String id) {
        GadgetPortlet gp = portal.getGadgetPortlet(id);
        if (gp.getGadgetBean()
                .isCollapsed()) {
            GadgetPortlet.collapse(id, "");
            gp.addListener(new PortletListener(gp));
        }
        JsLibrary.hideGwtContainerMask();
    };

    Timer t;

    @Override
    public String notifyOver(DragSource source, final EventObject e,
            DragData data) {

        if (t != null)
            t.cancel();

        final JavaScriptObject jsObj = source.getProxy()
                .getJsObj();
<commit_msg>NXP-4433 Drag and Drop : set auto width to gadget after drop<commit_after>/*
 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (http://www.leroymerlin.fr/) - initial implementation
 */

package org.nuxeo.opensocial.container.client.view;

import java.util.ArrayList;

import org.nuxeo.opensocial.container.client.ContainerEntryPoint;
import org.nuxeo.opensocial.container.client.JsLibrary;
import org.nuxeo.opensocial.container.client.bean.GadgetBean;
import org.nuxeo.opensocial.container.client.bean.GadgetPosition;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.ExtElement;
import com.gwtext.client.dd.DragData;
import com.gwtext.client.dd.DragSource;
import com.gwtext.client.dd.DropTargetConfig;
import com.gwtext.client.dd.ScrollManager;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.portal.PanelProxy;
import com.gwtext.client.widgets.portal.PortalColumn;
import com.gwtext.client.widgets.portal.PortalDropZone;

/**
 * DropZone serve for catch drag and drop event and call container service for
 * save
 * 
 * @author <NAME>
 * 
 */
public class DropZone extends PortalDropZone {

    private static final String DEFAULT = "default";

    private static final String ZONE_CLASS = "x-column-possible";

    private static final int GHOST_HEIGHT = 100;

    private static final int GHOST_WIDTH = 140;

    private static ContainerPortal portal;

    private int lastCW = -1;

    private PosGrid[] grid;

    private Container lastPosC;

    private int col;

    public DropZone(ContainerPortal portal, DropTargetConfig config) {
        super(portal, config);
        ScrollManager.register(portal.getBody()
                .getDOM());
        DropZone.portal = portal;
        overrideDragDrop(GHOST_WIDTH, GHOST_HEIGHT, ZONE_CLASS);
    }

    private static native void overrideDragDrop(int defaultX, int defaultY,
            String className)
    /*-{
      $wnd.Ext.override($wnd.Ext.Panel.DD, {
        startDrag : function(x,y){
          var g = this.proxy.getGhost();
          g.setX(x - 70);
          g.setWidth(140);

          if(g.getHeight() > 50)
            g.setHeight(100);

          $wnd.Ext.select("div.x-column").addClass("x-column-possible");
          g.select("div.x-tool").setStyle("display","none");
          g.select("div.x-panel-tl").setStyle("background-color","#a29f9a");
        },

        alignElWithMouse: function(el, iPageX, iPageY) {
          var fly = el.dom ? el : $wnd.Ext.fly(el, '_dd');
          var X = iPageX - 70;
          var Y = iPageY - 10;
          fly.setLeftTop(X, Y);
          this.cachePosition(X, Y);
          this.autoScroll(X, Y, el.offsetHeight, el.offsetWidth);
          return this.getTargetCoord(X, Y);
        },

        endDrag : function(e){
          this.proxy.hide();
          this.panel.saveState();
          $wnd.Ext.select("div.x-column").removeClass("x-column-possible");
          $wnd.Ext.select("div.x-tool").setStyle("display","block");
          @org.nuxeo.opensocial.container.client.view.DropZone::endDragDrop(Ljava/lang/String;)(this.id);
        }
      });

      $wnd.Ext.override($wnd.Ext.dd.PanelProxy, {
        hide : function(){
          if(this.ghost) {
            var el = this.panel.el;
            el.dom.style.display = '';
            if(this.proxy) {
              this.proxy.remove();
              delete this.proxy;
            }
            this.ghost.remove();
            delete this.ghost;
          }
        }
      });

      $wnd.Ext.override($wnd.Ext.dd.DragSource, {
        onDragDrop : function(e, id){
          var target = this.cachedTarget || $wnd.Ext.dd.DragDropMgr.getDDById(id);
          if(this.beforeDragDrop(target, e, id) !== false){
            if(target.isNotifyTarget){
              if(target.notifyDrop(this, e, this.dragData))
                this.onValidDrop(target, e, id);
              else
                this.onValidDrop(target, e, id);
              }else
                this.onValidDrop(target, e, id);
              if(this.afterDragDrop)
                this.afterDragDrop(target, e, id);
            }
          delete this.cachedTarget;
        },

        onDragOut : function(e, id){
          var target = this.cachedTarget || $wnd.Ext.dd.DragDropMgr.getDDById(id);
          target.notifyOut(this, e, this.dragData);
        },

        onInvalidDrop : function(target, e, id){
          this.onDragDrop(e,id);
        }

      });
    }-*/;

    /**
     * This method is called by the code JSNI in overrideDragDrop, overriding
     * the method of endDrag PortalDropZone not working
     * 
     * @param id
     */
    public static void endDragDrop(final String id) {
        GadgetPortlet gp = portal.getGadgetPortlet(id);
        if (gp.getGadgetBean()
                .isCollapsed()) {
            GadgetPortlet.collapse(id, "");
            gp.addListener(new PortletListener(gp));
        }
        JsLibrary.hideGwtContainerMask();
        // -1 for auto size
        gp.setWidth(-1);
    };

    Timer t;

    @Override
    public String notifyOver(DragSource source, final EventObject e,
            DragData data) {

        if (t != null)
            t.cancel();

        final JavaScriptObject jsObj = source.getProxy()
                .getJsObj();

{'fraction_non_alphanumeric': 0.12005212575338003, 'fraction_numerical': 0.00602704023456589, 'mean_word_length': 2.4222408026755855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4561396', 'n_tokens_mistral': 3842, 'n_tokens_neox': 3693, 'n_words': 795}