• One
  • Two
  • Javascript: mySort = new Fx.Sort($$('ul#list>li')); I can add" />
  • One
  • Two
  • Javascript: mySort = new Fx.Sort($$('ul#list>li')); I can add" />
  • One
  • Two
  • Javascript: mySort = new Fx.Sort($$('ul#list>li')); I can add"/>

    Mootools: How to add items to an existing Fx.Sort instance

    435 Views Asked by At

    I've got a problem with Mootools Fx.Sort.

    <ul id="list">
       <li>One</li>
       <li>Two</li>
    </ul>
    

    Javascript:

    mySort = new Fx.Sort($$('ul#list>li'));
    

    I can add more elements to the list:

    $('list').adopt(new Element('li', { text: 'Three' }));
    

    But the run-time created list elements, obviously, are not considered by Fx.Sort instance and they cannot be sorted with the others.

    Is there a way to add them to the existing Fx.Sort? Or the only thing is to replace mySort with a new instance every time I add an element at run-time?

    2

    There are 2 best solutions below

    0
    Dimitar Christoff On BEST ANSWER

    right - it's trickier to keep it compatible with implementations of the Fx.Sort and keep it sorting as it should etc, here's a working example whereby any of the items being clicked goes to the top and then expands:

    http://jsfiddle.net/dimitar/FcN32/

    specific to you:

    Fx.Sort.implement({
    
        adopt: function(el, pos) {
            if (!this.element)
                this.element = this.elements[0] && this.elements[0].getParent();
    
            var len = this.currentOrder.length;
            pos = (pos !== null && typeof pos === 'number')
                ? this.currentOrder.contains(pos) ? pos : len
                : len;
    
    
            this.elements.include(el);
            if (pos === len) {
                // don't care, attach to bottom.
                el.inject(this.element);
                this.currentOrder.push(this.elements.indexOf(el));
            }
            else {
                // we are injecting at a particular place in the order
                el.inject(this.elements[pos], "before");
                var newOrder = this.currentOrder.slice(0, pos) || [];
                newOrder.push(this.elements.indexOf(el));
                this.currentOrder = newOrder.combine(this.currentOrder.slice(pos));
            }
            if (el.getStyle('position') == 'static') el.setStyle('position', 'relative');                              
            this.rearrangeDOM();
        }
    
    });
    

    this is being called like instance.adopt(someel, <optional pos>) where pos is a numeric position in the list. if omitted, it will append to tail. hope it helps...

    6
    ldiqual On

    Taking a look at the Fx.Sort instance, there are some attributes that you can modify according to your needs:

    console.log(new Fx.Sort($$('ul#list>li'));
    

    This is how I would do it (not tested):

    var mySort = new Fx.Sort($$('ul#list>li'));
    var newElement = new Element('li', { text: 'Three' });
    $('list').adopt(newElement);
    mySort.elements.include(newElement);
    mySort.subjects.include(newElement);
    
    // Order of elements
    var orderSize = mySort.currentOrder.length;
    mySort.currentOrder[orderSize] = orderSize;
    

    However, it's modifying the internal mechanism of Mootools More FX.Sort, so it may not work.