11548 sujets

JavaScript, DOM et API Web HTML5

Bonjour a tous, j'ai repris l'exemple de jquery photo manager, pour déplacer des images d'un endroit a un autres.

Mon souci c'est que quand je drag and drop un élément ou que je click sur ma balise a pour déplacer mon élément celui ci fais bien l'action voulu, mais il s'efface de son endroit d'origine, alors que j'aimerai que oui il soit déplacer mais qu'il ne s'efface pas.

http://jqueryui.com/demos/droppable/#photo-manager

Voici l'exemple sur leur site vous verrez que si vous déplacer l'image elle s'efface de l'endroit initiale.

Je ne trouve pas l'endroit a effacer pour que ceci ne s'effectue plus, malgré mes recherche sur la doc jquery....


Mon jquery:

 $(function() {
 
	
    	// there's the gallery and the check
    	var $gallery = $( "#gallery" ),
        $check = $( "#contentRightMenuFormResult" );


		// let the gallery items be draggable
    	$( "li", $gallery ).draggable({
       		helper: "clone",
		});


    	// let the check be droppable, accepting the gallery items
    	$check.droppable({
        	accept: "#gallery > li",
        	activeClass: "ui-state-highlight",
        	drop: function( event, ui ) {
            	deleteImage( ui.draggable );
               
        });

                        // resolve the icons behavior with event delegation
                    	$( "ul.gallery2 > li" ).click(function( event ) {
                    		var $item = $( this ),
                    		$target = $( event.target );

                    		if ( $target.is( "a.ui-icon-check" ) ) {
                        		deleteImage( $item );
                    		} else if ( $target.is( "a.ui-icon-zoomin" ) ) {
                        		viewLargerImage( $target );
                    		} else if ( $target.is( "a.ui-icon-closethick" ) ) {
                        		recycleImage( $item );
                    		}

                    		return false;
                    	});
            		}
        		);  
    		});
    
    
    	// image deletion function
    	var recycle_icon = "<a href='link/to/recycle/script/when/we/have/js/off' title='Supprimer de l&rsquo;album' class='ui-icon ui-icon-closethick'>Recycle image</a>";
    	function deleteImage( $item ) {
    		$item.fadeOut(function() {
            	var $list = $( "ul", $check ).length ?
                $( "ul", $check ) :
                $( "<ul class='gallery ui-helper-reset'/>" ).appendTo( $check );

            	$item.find( "a.ui-icon-check" ).remove();
            	$item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
                	$item
                    	/*.animate({ width: "48px" })*/
                    	.find( "img" )
                        	.animate({ height: "72px" });
            	});
        	});
    	}

    	
		// image recycle function
    	var check_icon = "<a href='link/to/check/script/when/we/have/js/off' title='Ajouter a l&apos;album' class='ui-icon ui-icon-check'>Delete image</a>";
    	function recycleImage( $item ) {
        	$item.fadeOut(function() {
            	$item
                	.find( "a.ui-icon-closethick" )
                        .remove()
                	.end()
                	.css( "width", "96px")
                	.append( check_icon )
                	/*.find( "img" )
                    .css( "height", "72px" )*/
                	.end()
                	.appendTo( $gallery )
                	.fadeIn();
        	});
    	}
    

    	// image preview function, demonstrating the ui.dialog used as a modal window
    	function viewLargerImage( $link ) {
        	var src = $link.attr( "href" ),
            	title = $link.siblings( "img" ).attr( "alt" ),
            	$modal = $( "img[src$='" + src + "']" );

        		if ( $modal.length ) {
           			$modal.dialog( "open" );
        		} else {
            		var img = $( "<img alt='" + title + "' width='384' height='288' style='display: none; padding: 8px;' />" )
                		.attr( "src", src ).appendTo( "body" );
            		setTimeout(function() {
                		img.dialog({
                    		title: title,
                    		width: 400,
                    		modal: true,
                    		resizable: false,
                		});
            		}, 1 );
        		}
    		}


    	// resolve the icons behavior with event delegation
        $( "ul.gallery > li" ).click(function( event ) {
            var $item = $( this ),
                $target = $( event.target );

            if ( $target.is( "a.ui-icon-check" ) ) {
                deleteImage( $item );
            } else if ( $target.is( "a.ui-icon-zoomin" ) ) {
                viewLargerImage( $target );
            } else if ( $target.is( "a.ui-icon-closethick" ) ) {
                recycleImage( $item );
            }

            return false;
    	});
    
	
    	// resolve the icons behavior with event delegation
        $( "ul.gallery2 > li" ).click(function( event ) {
            var $item = $( this ),
                $target = $( event.target );

            if ( $target.is( "a.ui-icon-check" ) ) {
                deleteImage( $item );
            } else if ( $target.is( "a.ui-icon-zoomin" ) ) {
                viewLargerImage( $target );
            } else if ( $target.is( "a.ui-icon-closethick" ) ) {
                recycleImage( $item );
            }

            return false;
    	});
	});



Mon HTML :



<div id="contentBlock">
                	<ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">
                    	{% for affiche in  attribu_image %}
                        <li class="ui-widget-content ui-corner-tr">
                        	<h6 class="ui-widget-header">{{ affiche.name|slice:":16" }}</h6>
                            <img src="{% ids_images_prefix %}{% for thumb in  affiche.element_thumb %}{{thumb}}{% endfor %}" alt="{{ affiche.name }}" height="72" />
                            <a href="{% ids_images_prefix %}{% for low in  affiche.element_low %}{{low}}{% endfor %}" title="{{ affiche.price }} {{ affiche.description }} {% for tags in affiche.tags  %} {{ tags }}, {% endfor %}" class="ui-icon ui-icon-zoomin">View larger</a>
                                    
                            <form class="item-forms" action="{% url libertyproject.imagin.ajax.add_image %}" method="get">
                            	{% csrf_token %}
                                <input type="hidden" name="item_id" value="{{affiche.id}}" />
                                {% if selected_album %}
                                	<input type="hidden" name="selected_album_id" value="{{selected_album.id}}" />
                                {% else %}
                                    <input type="hidden" name="selected_album_id" value="{{selected_album}}" />
                                {% endif %}
                                <!--<input class="ui-icon ui-icon-check item-send" type="submit" value="send in album" />-->
                                <a href="link/to/check/script/when/we/have/js/off" title="Ajouter a l&apos;album" class="ui-icon ui-icon-check">Ajouter a l&apos;album</a>
                         	</form>
                     	</li>
                        {% endfor %}
                	</ul>
                <br class="spacer" />
                </div>



<div id="contentRightMenuFormResult">
                            <ul id="gallery2" class="gallery2 ui-helper-reset ui-helper-clearfix">
                            {% for affiche in  album0_items %}
                                <li class="ui-widget-content ui-corner-tr">
                                    <h6 class="ui-widget-header">{{ affiche.name|slice:":16" }}</h6>
                                    <img src="{% ids_images_prefix %}{% for thumb in  affiche.element_thumb %}{{thumb}}{% endfor %}" alt="{{ affiche.name }}" height="72" />
                                    <a href="{% ids_images_prefix %}{% for low in  affiche.element_low %}{{low}}{% endfor %}" title="{{ affiche.price }} {{ affiche.description }} {% for tags in affiche.tags  %} {{ tags }}, {% endfor %}" class="ui-icon ui-icon-zoomin">View larger</a>
                                    <form class="item-forms-remove" action="{% url libertyproject.imagin.ajax.remove_image %}" method="get">
                                        {% csrf_token %}
                                        <input type="hidden" name="item_id" value="{{affiche.id}}" />
                                        {% if selected_album %}
                                        <input type="hidden" name="selected_album_id" value="{{selected_album.id}}" />
                                        {% else %}
                                        <input type="hidden" name="selected_album_id" value="{{selected_album}}" />
                                        {% endif %}
                                        <!--<input class="ui-icon ui-icon-check item-send" type="submit" value="send in album" />-->
                                        <a href='link/to/recycle/script/when/we/have/js/off' title='Supprimer de l&rsquo;album' class='ui-icon ui-icon-closethick'>Recycle image</a>
                                    </form>
                                </li>
                                {% endfor %}
                            </ul>
                            
                        </div>


Après quelque recherche, on ma conseillé d'utiliser le clonage de draggable.

Cella fonctionne, mais j'ai juste encore un souci, il ne me remet pas les attributs CSS attribué a gallery2.

Si j'ai bien saisie, on enlève les attributs de l'élément dropé grâce a removeClass et on attribut les nouveaux grâce a addClass...?

Je vous met mon code modifié :



// let the gallery items be draggable
    	$( "li", $gallery ).draggable({
       		helper: "clone",
			revert: 'invalid',
 
		});
 
 
    	// let the check be droppable, accepting the gallery items
    	$check.droppable({
        	accept: "#gallery > li",
        	activeClass: "ui-state-highlight",
        	drop: function( event, ui ) {
            	$(this).append(ui.helper.clone().removeClass("ui-draggable-dragging").addClass("gallery2 ui-helper-reset ui-helper-clearfix").draggable({helper:'original'}));
                });


Et mon CSS:


    #gallery { float: left; width: 100%; min-height: 12em; padding-left: 0px; } * html #gallery { height: 12em; } /* IE6 */
    .gallery.custom-state-active { background: #eee; }
    .gallery li { float: left; 
                width: 96px;
                height: 120px; 
                padding: 0.4em; 
                margin: 0 12px 15px 0; 
                text-align: center; }
    .gallery li h6 { margin: 0 0 0.4em; cursor: move; }
    .gallery li a { float: right; }
    .gallery li a.ui-icon-zoomin { float: left; }
    .gallery li img { width: 100%; cursor: pointer; }
    
    
    #check { float: right; width: 32%; min-height: 18em; padding: 1%;} * html #check { height: 18em; } /* IE6 */
    #check h4 { line-height: 16px; margin: 0 0 0.4em; }
    #check h4 .ui-icon { float: left; }
    #check .gallery h6 { display: none; }
    input.item-send { background-color: transparent; border: none; cursor: pointer; float: right;}
    
        
    
    #gallery2 { float: left; width: 100%; min-height: 12em; padding-left: 0px; } * html #gallery { height: 12em; } /* IE6 */
    .gallery2.custom-state-active { background: #eee; }
    .gallery2 li { float: left; 
                width: 96px;
                height: 120px; 
                padding: 0.4em; 
                margin: 0 8px 15px 0; 
                text-align: center; }
    .gallery2 li h6 { margin: 0 0 0.4em; cursor: move; }
    .gallery2 li a { float: right; }
    .gallery2 li a.ui-icon-zoomin { float: left; }
    .gallery2 li img { width: 100%; cursor: pointer; }
    
    
    #check { float: right; width: 32%; min-height: 18em; padding: 1%;} * html #check { height: 18em; } /* IE6 */
    #check h4 { line-height: 16px; margin: 0 0 0.4em; }
    #check h4 .ui-icon { float: left; }
    #check .gallery2 h6 { display: none; }
    input.item-send { background-color: transparent; border: none; cursor: pointer; float: right;}


Merci d'avance !!! Smiley cligne
Modifié par Florent V. (02 Nov 2010 - 13:45)
Bonjour,

Peut-être devrais tu changer l'action du drop:

drop: function( event, ui ) { 
                deleteImage( ui.draggable );     
        }); 


Il semblerait que la fonction deleteImage efface l'image Smiley smile
Comme j'ai poster plus bas, j'ai enlever la fonction deleteImage et j'ai mi un clone a la place de l'apel de la fonction.

Ce qui marche bien, a par le faite de attribué une class... Smiley bawling




 // let the check be droppable, accepting the gallery items 
        $check.droppable({ 
            accept: "#gallery > li", 
            activeClass: "ui-state-highlight", 
            drop: function( event, ui ) { 
                $(this).append(ui.helper.clone().removeClass("ui-draggable-dragging").addClass("gallery2 ui-helper-reset ui-helper-clearfix").draggable({helper:'original'})); 
                });