Click here to Skip to main content
15,915,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a tool box which have some objects which are draggable. So far the code works fine but when i drag an object, it removes from the tool box. But what i want is to not to remove it from there.
Any help would be highly appreciated. And the code is as follows

C#
<script>
        $(document).ready(function(id) {


            var count_dropped_hits = 0;
            var data = {
                "images": [
                    {"id" : "obj_0" ,"src" : "background1.png"  ,"width" : "500", "height" : "400"}
                ]
            };


            // Array Remove

            Array.prototype.remove = function(from, to) {
              var rest = this.slice((to || from) + 1 || this.length);
              this.length = from < 0 ? this.length + from : from;
              return this.push.apply(this, rest);
            };



            /*  remove an object from data */
            $('.remove',$('#tools')).live('click',function(){
                var $this = $(this);

                /* the element next to this is the input that stores the obj id */
                var objid = $this.next().val();
                /* remove the object from the sidebar */
                $this.parent().remove();
                /* ,from the picture */
                var divwrapper = $('#'+objid).parent().parent();
                $('#'+objid).remove();
                /* add again to the objects list */
                var image_elem      = $this.parent().find('img');
                var thumb_width     = image_elem.attr('width');
                var thumb_height    = image_elem.attr('height');
                var thumb_src       = image_elem.attr('src');
                $('<img />',{
                    id          :   objid,
                    src         :   thumb_src,
                    width       :   thumb_width,
                    //height        :   thumb_height,
                    className   :   'ui-widget-content'
                }).appendTo(divwrapper).resizable({
                    handles : 'se',
                    stop    : resizestop
                }).parent('.ui-wrapper').draggable({
                    revert: 'invalid',
                    helper:'clone'
                });
                /* and unregister it - delete from object data */
                var index = exist_object(objid);
                data.images.remove(index);
            });



            /*  checks if an object was already registered */

            function exist_object(id){
                for(var i = 0;i<data.images.length;++i){
                    if(data.images[i].id == id)
                        return i;
                }
                return -1;
            }



            /* triggered when stop resizing an object */

            function resizestop(event, ui) {
                //calculate and change values to obj (width and height)
                var $this       = $(this);
                var objid       = $this.find('.ui-widget-content').attr('id');
                var objwidth    = ui.size.width;
                var objheight   = ui.size.height;

                var index       = exist_object(objid);

                if(index!=-1) { //if exists (it should!) update width and height
                    data.images[index].width    = objwidth;
                    data.images[index].height   = objheight;
                }
            }


            /* objects are resizable and draggable */

            $('#objects img').resizable({
                /* only diagonal (south east)*/
                handles : 'se',
                stop    : resizestop,
            }).parent('.ui-wrapper').draggable({
                revert  : 'invalid',
                helper  : 'clone'
            });




            $('#background').droppable({
                accept  : '#objects div', // accept only draggables from #objects
                drop    : function(event, ui) {

                    //reference to the current element
                    var $this       = $(this);
                    ++count_dropped_hits;

                    var draggable_elem = ui.draggable.clone();
                    draggable_elem.css('z-index',count_dropped_hits);

                    /* object was dropped : register it */
                    var objsrc      = draggable_elem.find('.ui-widget-content').attr('src');

                    //get width and height as decimal numbers
                    var objwidth    = parseFloat(draggable_elem.css('width'),10);
                    var objheight   = parseFloat(draggable_elem.css('height'),10);

                    /* for top and left we decrease the top and left of the droppable element */
                    var objtop      = ui.offset.top - $this.offset().top;
                    var objleft     = ui.offset.left - $this.offset().left;

                    var objid       = draggable_elem.find('.ui-widget-content').attr('id');

                    var index       = exist_object(objid);

                    if(index!=-1) { //if exists update top and left
                        data.images[index].top  = objtop;
                        data.images[index].left = objleft;
                    }
                    else{
                        /* register new one */
                        var newObject = {
                            'id'        : objid,
                            'src'       : objsrc,
                            'width'     : objwidth,
                            'height'    : objheight,
                            'top'       : objtop,
                            'left'      : objleft,
                            'rotation'  : '0'

                        };
                        data.images.push(newObject);
                        /* add object to sidebar*/

                        $('<div/>',{
                            className   :   'item'
                        }).append(
                            $('<div/>',{
                                className   :   'thumb',
                                html        :   '<img width="50" class="ui-widget-content" src="'+objsrc+'"></img>'
                            })
                        ).append(
                            $('<div/>',{
                                className   :   'slider',
                                html        :   '<span>Rotate</span><span class="degrees">0</span>'
                            })
                        ).append(
                            $('<a/>',{
                                className   :   'remove'
                            })
                        ).append(
                            $('<input/>',{
                                type        :   'hidden',
                                value       :   objid       // keeps track of which object is associated
                            })
                        ).appendTo($('#tools'));
                        $('.slider').slider({
                            orientation : 'horizontal',
                            max         : 180,
                            min         : -180,
                            value       : 0,
                            slide       : function(event, ui) {
                                var $this = $(this);
                                /* Change the rotation and register that value in data object when it stops */
                                draggable_elem.css({'-moz-transform':'rotate('+ui.value+'deg)','-webkit-transform':'rotate('+ui.value+'deg)'});
                                $('.degrees',$this).html(ui.value);
                            },
                            stop        : function(event, ui) {
                                newObject.rotation = ui.value;
                            }
                        });
                    }
                }
            });


            /* User presses the download button */

            $('#submit').bind('click',function(){
                var dataString  = JSON.stringify(data);
                $('#jsondata').val(dataString);
                $('#jsonform').submit();
            });
        });
    </script>
Posted
Comments
ZurdoDev 15-Nov-15 10:21am    
Your code has a comment that reads,
/* remove the object from the sidebar */

That sounds like a good place to start.
Member 10408581 15-Nov-15 10:41am    
In detail, the above code works as follows.
1. There is a tool set (on left side) which contain some objects which are draggable
2. When dragging an object, it removes from that tool set (- which is my issue )
3. Dragged image can be successfully dropped into a canvas area
4. At the same time the dropped object can be seen in a panel (on right side) with a delete button (x) . This is to remove the dropped image if necessary.
5. After deleting, the object is replaced back to the tool set (on left side).


all these steps are okay except in Step 2 issue.
The comment which you are referring to (/* remove the object from the sidebar */) is about removing the dropped image from right panel when user click on the delete button on it. Which means; that is a needed functionality :)
ZurdoDev 15-Nov-15 20:54pm    
If that code is not relevant to the question then please do not post it, for future reference.

Just do as Solution 1 says and copy the object and put the copy in the original place.

1 solution

This is a very quick and very general answer: you can deep-close any thinkable JavaScript object using this totally universal operation:
JavaScript
var deepClone = Object.create(sourceObject);

I'm not saying that this is what you have to do in your particular situation. Maybe you just need to change your whole drag-and-drop design. I've just shown something which always works and directly answers your "how to copy" question.

—SA
 
Share this answer
 
v3
Comments
Member 10408581 15-Nov-15 12:49pm    
@SA- Thank you for your help!! :) I'll try to use that in such situations.
But still i'm not a lucky one to get through with my problem :( Thanks again!!!
Sergey Alexandrovich Kryukov 15-Nov-15 13:37pm    
You are welcome. If this is one of the things which works for you (this is a good to know thing anyway), consider accepting the answer formally. It won't prevent others from sending alternative suggestions.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900