0

我正在开发基于 JQuerUI 的拖放控制系统。这个想法是让“控件”(它们是可拖动的)从工具箱菜单(这些也是可拖动的)添加到“表单”(可放置)中。我设法让代码工作,以便我可以将“控件”放到“表单”上。我现在遇到的问题是,在“表单”的拖放功能中,我需要将信息从“表单”传递到“控件”。我的问题是,在我引用 this 变量时的 drop 函数中,它不是“表单”对象,而是一个 DOM 对象(我认为,对此并不清楚)。这是我到目前为止的代码:

表单对象

$.widget("wdss.form",
{
    gridSize: 10,
    _create: function()
    {
        this.element.droppable({
            accept: ".toolbox-item",
            hoverClass: "ui-state-highlight",
            drop: this.handleDrop
        });
    },
    handleDrop: function( event, ui ) 
    {
        var elemType = $(ui.draggable).data("wdss.type");
            //the this variable here is the wrong one
        var container = $(ui.draggable)[elemType]("GetControl", this); 

        //this.gridSize is undefined here
        var fixedTop = ui.position.top - (ui.position.top % this.gridSize); 
        var fixedLeft = ui.position.left - (ui.position.left % this.gridSize);

        //Set the control's positon and add it to this
        container.css({top: fixedTop, left: fixedLeft});
        container.appendTo(this);
    }
});

控制对象

$.widget("wdss.gauge", $.wdss.control,
{
    form: null,
    canvas: null,
    gauge: null,
    _create: function(form)
    {
        this._super( "_create" );
        this.form = form;
        this.element.on("resizestart", $.proxy(this._ResizeStart, this));
        this.element.on("resizestop", $.proxy(this._ResizeEnd, this));
        this.canvas = $('<canvas></canvas>').appendTo(this.content);
        this.canvas.attr("height", this.content.height());
        this.canvas.attr("width", this.content.width());
        this.gauge = new AquaGauge($(this.canvas), {});
        $('<button style="position: absolute; top: 0; left: 0; width: 20px; height: 20px;">Test</button>').appendTo(this.content
        ).click($.proxy(function() {this.gauge.refresh((Math.random()) - 0.5);}, this));

        //Give us a way to generically call functions. In this case used 
        $(this.element).data("wdss.type", this.widgetName);
    },

    _ResizeStart: function(event, ui)
    {
        this.canvas.hide();
    },

    _ResizeEnd: function(event, ui)
    {
        var maxSize = Math.min(this.content.height(), this.content.width());
        var newHeight = this.content.height() / (this.content.height()/maxSize );
        var newWidth = this.content.width() / (this.content.width()/maxSize );
        var newTop = (this.content.height() - newHeight)/2;
        var newLeft = (this.content.width() - newWidth)/2;

        this.canvas.attr("height", newHeight);
        this.canvas.attr("width", newWidth);
        this.canvas.css({top: newTop, left: newLeft});
        this.canvas.show();
        this.gauge.redraw();
    }
});

工具箱项目

$.widget("wdss.toolboxGauge", $.wdss.toolboxItem,
{
    _create: function()
    {
        this._super( "_create" );
        //Needed to get the control type
        $(this.element).data("wdss.type", this.widgetName);
    },

    GetControl: function(form)
    {
        return $('<div></div>').gauge(form);
    }
});

对此的任何帮助将不胜感激!很抱歉没有评论,这只是一个实验版本,看看我想要的能不能做到

编辑 1

在 ryuutatsuo 发表评论后,我尝试了一些方法并让它发挥作用,但我不确定这是实现我想要的正确方法。这是新代码:

$.widget("wdss.form",
{
    gridSize: 10,
    _create: function()
    {
        $(this.element).data("wdss.form", this); //Added this
        this.element.droppable({
            accept: ".toolbox-item",
            hoverClass: "ui-state-highlight",
            drop: this.handleDrop
        });
    },

    handleDrop: function( event, ui ) 
    {
        var form = $(this).data("wdss.form"); //Added this
        var elemType = $(ui.draggable).data("wdss.type");
        var container = $(ui.draggable)[elemType]("GetControl", form);

        //Fix the coords so they are on the grid
        var fixedTop = ui.position.top - (ui.position.top % form.gridSize);
        var fixedLeft = ui.position.left - (ui.position.left % form.gridSize);

        //Set the control's positon and add it to this
        container.css({top: fixedTop, left: fixedLeft});
        container.appendTo(this);
    }
});
4

1 回答 1

1

我更改了drop处理方式,使其成为匿名函数,从而解决了问题。我相信我的错误是this变量的范围。下面是代码:

 drop: function (event, ui)
         {
            var elemType = $(ui.draggable).data("wdss.type");
            var control = $(ui.draggable)[elemType]("GetControl");

            //Fix the coords so they are on the grid
            var fixedTop = ui.position.top - (ui.position.top % self.gridSize);
            var fixedLeft = ui.position.left - (ui.position.left % self.gridSize);

            fixedLeft = Math.max(0, Math.min(self.element.innerWidth() - control.outerWidth(), fixedLeft));
            fixedTop = Math.max(0, Math.min(self.element.innerHeight() - control.outerHeight(), fixedTop));

            //Set the control's positon and add it to this
            control.css({ top: fixedTop, left: fixedLeft });
            control.appendTo(self.element);
         }
于 2013-09-05T19:06:35.010 回答