Fixed some typos but basically your "all/none" checkbox is a modifier for the source object onto which call the changeGrep function.
Ok you have a lot of material now, so it's up to you now.
//Our Pseudo Event Class
var EventManager = function()
{
//Our internal key object
var o = {
};
//return the Event Manager API
return {
//Add a listener to the class for a specific event
addEventListener:function(name, handler) {
//Return if the supplied handler already belongs to the listened event
if ( this.hasEventListener (name, handler) ) return;
//Adding the handler for the event
o[name] = o[name] || [];
o[name][o[name].length]=handler;
},
//Executes every handler attached to the event
//passing data as argument
dispatchEvent:function(name, data) {
var n, i=0;
//Return if no handler is set
if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return;
n = o[name].length;
//executes every handler
for ( i = 0; i<n; i++ ) {
o[name](data);
}
},
//Remove a specific handler for the event
removeEventListener:function(name, handler) {
//We will redefine the internal chain of handlers
//passing the content of the "new" newArray array
var newArray = [], n;
//Return if no handler is set
if ( !this.hasEventListener ( name, handler ) ) return;
n = o[name].length;
//Storing handlers into newArray if different from specific handler
while ( n-- ) {
if ( (o[name] !== handler) ) {
newArray [ newArray.length ] = o[name];
}
}
//Redefining handlers array
o[name] = newArray ;
},
//Checking if some event host a specific handler
hasEventListener:function(name, handler) {
var n;
if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return false;
n = o[name].length;
//If handle is found, return true. Otherwise return false
while ( n-- ) if ( o[name] === handler ) return true;
return false;
},
//Some utility to get access to the handler array for some specific event or all the eventManager class
expose:function(name){
if ( name ) {
if ( !o[name] || !(o[name] instanceof Array) || o[name.length] ) return "no events set";
else return o[name].toSource();
}
else {
return o.toSource();
}
}
}
};
var main = function() {
var ev = new EventManager(),
u, actions = {};
var CBX = function(p, label, v, callback) {
var cbx = p.add('checkbox', undefined, label);
cbx.onClick = function() {
ev.dispatchEvent ( (cbx.value? "ADD_ACTION":"REMOVE_ACTION"), {label:label, callback:callback} );
}
return cbx;
};
var w = new Window ('dialog','About CBX & EVENTS');
var p1 = w.add('panel', u,'MAIN CBX');
new CBX ( p1, 'F/C 1', false, cb1CallBack );
//adding main checkbox that once click will dispatch the "DEFINE_TARGET" event
var mainCbx = w.add('checkbox',undefined, 'Apply to all documents');
mainCbx.onClick = function() {
ev.dispatchEvent ( "DEFINE_TARGET", mainCbx.value );
}
var btn = w.add('button',u, 'Run' );
btn.onClick = function () {
w.close(1);
}
ev.addEventListener ( "ADD_ACTION", function(data) {
ev.actions = ev.actions || {};
ev.actions[data.label] = data.callback;
});
ev.addEventListener ( "REMOVE_ACTION", function(data) {
if ( !ev.actions) return;
delete ev.actions[data.label];
});
ev.target = app.properties.activeDocument;
//add a listener so any event of type "DEFINE_TARGET" dispatched set ev.target value;
ev.addEventListener ( "DEFINE_TARGET", function(data) {
ev.target = data? app : app.properties.activeDocument;
});
if ( w.show() == 1 && ev.actions) {
//looping through all properties of the ev.actions object
//which are functions
for ( prop in ev.actions ) {
ev.actions[prop](ev.target);
}
}
}
function grep(target, findProps, changeProps) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.properties = findProps;
app.changeGrepPreferences.properties = changeProps;
target.changeGrep();
}
function cb1CallBack(target) {
var findProps = {
findWhat:"\\s{2, }",
},
changeProps = {
changeTo:" ",
};
grep (target, findProps, changeProps );
}
if ( !app.properties.activeDocument ) {
alert('you need an open document at least');
}
else {
main();
}