1 /**
  2  * BasicFilter (Inherited from Filter)
  3  */
  4 wso2vis.f.BasicFilter = function(dataField, dataLabel, filterArray) {
  5 	wso2vis.f.Filter.call(this);
  6 
  7     this.dataField(dataField)
  8         .dataLabel(dataLabel)
  9 	    .filterArray(filterArray);
 10 
 11     /* @private */
 12     this.remainingArray = [];
 13 };
 14 
 15 wso2vis.extend(wso2vis.f.BasicFilter, wso2vis.f.Filter);
 16 
 17 wso2vis.f.BasicFilter.prototype
 18     .property("dataField")
 19     .property("dataLabel")
 20     .property("filterArray");
 21 
 22 wso2vis.f.BasicFilter.prototype.filterData = function(data) {	
 23     function getLbl(obj, dataLabel, x, that) {
 24         var r;
 25         if (obj instanceof Array) {
 26             r = obj[x];
 27         }
 28         else {
 29             r = obj;
 30         }
 31         return that.traverseToDataField(r, dataLabel);
 32     }
 33 	    
 34 	function filter(object, dataLabel, filterArray, that) {	    
 35 	    var dcount = 1;
 36 	    if (object instanceof Array)
 37 	        dcount = object.length;
 38 	    
 39 	    if ((filterArray === undefined) || (filterArray == null)) {
 40 	        var arr = [];
 41 	         
 42 	        for (var i = dcount - 1; i >= 0; i--) {
 43 				arr.push(getLbl(object, dataLabel, i, that));
 44 			}
 45 			return {rem:[], fil:arr, isObj:false};					    
 46 	    }
 47 	    else {
 48 	        remainingArray = [];
 49 			var isObj = false;
 50 		    for (var i = dcount - 1; i >= 0; i--) {
 51 			    var found = false;
 52 			    var label = getLbl(object, dataLabel, i, that);
 53 			    for (var j = 0; j < filterArray.length; j++) {
 54 				    if (label == filterArray[j]) {
 55 					    found  = true;
 56 					    break;
 57 				    }							
 58 			    }
 59 			    if (!found) {				
 60 				    remainingArray.push(label);
 61 				    if (object instanceof Array)
 62 				        object.splice(i, 1); // not found! remove from the object				
 63 				    else {
 64 				        isObj = true;
 65 				    }
 66 			    }			
 67 		    }
 68 		    return {rem:remainingArray, fil:filterArray, isObj:isObj};
 69 	    }
 70 	}
 71 	
 72 	function sortthem(object, dataLabel, filterArray, that) {
 73 	    if ((filterArray === undefined) || (filterArray == null)) {
 74 	        return;
 75 	    }
 76 	    var dcount = 1;
 77 	    if (object instanceof Array)
 78 	        dcount = object.length;
 79 	        
 80 		var index = 0;
 81 		for (var i = 0; i < filterArray.length; i++) {
 82 			for (var j = 0; j < dcount; j++) {
 83 			    var label = getLbl(object, dataLabel, j, that);
 84 				if (label == filterArray[i]) {
 85 					if (index != j) {
 86 						var temp = object[index];
 87 						object[index] = object[j];
 88 						object[j] = temp;
 89 					}
 90 					index++;
 91 					break;
 92 				}														
 93 			}
 94 		}
 95 	}
 96 
 97     
 98 	var cloned = JSON.parse(JSON.stringify(data)); //eval (data.toSource());				
 99 	var filtered = wso2vis.fn.traverseToDataField(cloned, this.dataField());
100 	var result = filter(filtered, this.dataLabel(), this.filterArray(), this);
101 	this.remainingArray = result.rem;
102 	this.filterArray(result.fil);
103 	if (result.isObj) {
104 		wso2vis.fn.traverseNKillLeaf(cloned, this.dataField());
105 	}
106 	else {
107 		sortthem(filtered, this.dataLabel(), this.filterArray(), this);
108 	}
109 
110 	return cloned;
111 };
112