jeromeetienne / tquery

extension system for three.js
http://jeromeetienne.github.io/tquery/
MIT License
651 stars 120 forks source link

Add the .not('selector') functionnality to tQuery.Object3D #191

Open leconcepteur opened 12 years ago

leconcepteur commented 12 years ago

Functional mimic of the jQuery .not() functionality. It's ugly and not efficient, but you get the idea.

/**
 * Exlcude specified Object3D
 *
 * @param {} object
 * @param {THREE.Object3D} rootnode
 * @returns {tQuery.Object3D} chained API
*/
tQuery.Object3D.prototype.not = function(object, root){
    // handle the case of selector
    if( typeof object === "string" ){
        object  = tQuery.Object3D._select(object, root);
    }

    if( typeof object === "string" )        object = tQuery.Object3D._select(object, root);
    else if( !object )                      object = [];
    else if ( !(object instanceof Array) )  object = [object];

    var lists   = [];
    this._lists.forEach(function(child){
        object.forEach(function(objectChild){
            if (child != objectChild) {
                lists = lists.concat(child);
            }
        });
    });

    // call parent ctor
    tQuery.Object3D.parent.constructor.call(this, lists);

    return this;
}
leconcepteur commented 12 years ago

Examples :

tQuery('cube').not('.excludeClass').scale(2).translate(1,0,0); // Do not affect objects with class 'excludeClass'
tQuery('cube').not('#excludeId').scale(2).translate(1,0,0); // Do not affect object with id 'excludeId'
tQuery('*').not('cube').scale(2).translate(1,0,0); // Do not affect cubes
jeromeetienne commented 12 years ago

what is this ?

tQuery.Object3D.parent.constructor.call(this, lists);
leconcepteur commented 12 years ago

Sorry I'm just being lazy there. I didn't quite understood this part of the code, all I know is that it doesn't work and this._list is not populated if I don't use this line. I took it from the constructor of tQuery.Object3D.

That's kind of the reason I didn't do a Pull Request, as I'm not sure myself of what I was doing ;)

jeromeetienne commented 12 years ago

what about

this._lists = lists;
leconcepteur commented 12 years ago

Hahaha! It works perfect :P Sorry, laziness is a big flaw when coding.

leconcepteur commented 12 years ago

I have corrected the code. My loop was buggy.

/**
 * Exclude specified Object3D
 *
 * @param {} object
 * @param {THREE.Object3D} rootnode
 * @returns {tQuery.Object3D} chained API
*/
tQuery.Object3D.prototype.not = function(object, root){
    if( typeof object === "string" )        object = tQuery.Object3D._select(object, root);
    else if( !object )                      object = [];
    else if ( !(object instanceof Array) )  object = [object];

    var lists   = [];
    object.forEach(function(objectChild){
        if (this._lists.indexOf(objectChild) !== -1) {
            this._lists.splice(this._lists.indexOf(objectChild), 1);
        }
    }.bind(this));

    return this;
}
jeromeetienne commented 12 years ago

.not() seems a good idea. what are the other call() similar in jQuery ? while at it, we may do that too.

i know of .filter(). any other ?