(function() {

// MAXB.NET LABS

})();

javascript, jquery

jQuery.lazy().draggable().resizable();

Posted on by Massimiliano Balestrieri | Comments

Questa è la continuazione del post precedente

Scrivendo il primo post mi è venuta in mente una piccola modifica.
Creare una sorta di “manager” (ah che brutto termine per un programmatore!) che abiliti le funzionalità ui grazie ad un evento jQuery.hover();

Non so se ci sia un risparmio di risorse…
ma mi sembrava una puona applicazione del pattern lazy… che in questi giorni mi frullava nella testa.

JLazy = {
    build : function(options)
    {
        return this.each(
            function(nr)
            {
                jQuery(this)
                .draggable({ stop : function(){this.recorder.on_resize()}})
                .draggable("disable")
                .resizable({ stop : function(){this.recorder.on_resize()}, transparent: true })
                .resizable("disable")
                .JResizeRecorder()
                .hover(
                    function(){
                        jQuery(this)
                        .resizable("enable")
                        .draggable("enable");

                        console.log("Abilitato il dragging e il resize " + jQuery(this));
                    },
                    function(){
                        jQuery(this)
                        .resizable("disable")
                        .draggable("disable");

                        console.log("Disabilitato il dragging e il resize " + jQuery(this));
                    }
                );
            }
        );
    }
};
jQuery.fn.JLazy = JLazy.build;

Quindi invece di:

elem.resizable({ transparent: true, stop : function(){this.recorder.on_resize()} }).JResizeRecorder();

scriverò:

elem.JLazy();

Ecco il document.ready della demo:

$(document).ready(function(){
    var add = jQuery('<a href="#">ADD BOX</a>').click(function(){
        var count = jQuery(".box").length + 1;
        var id_div = "div" + count;
        jQuery("#playground").append("<div id=\""+id_div+"\" class=\"box\"></div>");//<h1>"+ id_div +"</h1>
        var elem = jQuery("#" + id_div);
        elem.JLazy();
    });
    var add5 = jQuery('<a href="#">ADD 5 BOX</a>').click(function(){
        var count = jQuery(".box").length + 1;
        for(var x = 0;x < 5;x++){
            var id_div = "div" + (count + x);
            jQuery("#playground").append("<div id=\""+ id_div +"\" class=\"box\"></div>");//<h1>"+ id_div +"</h1>
            var elem = jQuery("#" + id_div);
            elem.JLazy();
        }
    });
    jQuery("#top").append(add , "  | ");
    jQuery("#top").append(add5 , "  | ");
});

DEMO

This entry was posted in javascript, jquery and tagged , . Bookmark the permalink.

2 Comments

  1. samba
    Posted 8 May 2008 at 16:30 | Permalink

    excellent work! i think there is something wrong with prev&next buttons. if you drag it from positon0 to position1 then click prev then drag it position2 than press prev this will carry box to positon1.

  2. Massimiliano Balestrieri
    Posted 8 May 2008 at 17:00 | Permalink

    @samba

    Sorry!
    Fixed update method of recorder.

    if(_position < _stack.length)
    _stack = _stack.slice(0,_position);

    thanks for comment. IS THE FIRST!

2 Trackbacks

  1. By (function() { : Samba! Primo commento on 8 May 2008 at 18:28

    [...] Grazie a Samba ho trovato un “piccolo” bug sui primi post. [...]

  2. By (function() { : jQuery Style Observer on 9 May 2008 at 08:10

    [...] Continua da [...]