getWidgetsByClassName method

3 views
Skip to first unread message

thomas

unread,
Jul 11, 2008, 6:23:39 AM7/11/08
to porcupine-users
Hi,

I needed a way to access a widget based on its css class name.
So here it is:

Widget.prototype.getWidgetsByClassName = function(cssName, deep) {
var w;
var ws = [];
var deep = (typeof(deep) == 'undefined')?false:deep;
for (var i=0; i<this.widgets.length; i++) {
w = this.widgets[i];
if (w.div.className == cssName) ws.push(w);
if(deep)
ws = ws.concat(w.getWidgetsByClassName(cssName, deep));
}
return ws;
}

It accepts a string with the name of the class to match and an
optional Boolean indicating whether the search
will be deep or not. By default the method searches the current
widget's direct children.

Hope you like it.
Thanks

tkouts

unread,
Jul 11, 2008, 6:35:52 AM7/11/08
to porcupine-users
Your need sounds reasonable. Just a minor correction (I just removed
the "var deep..." line):

Widget.prototype.getWidgetsByClassName = function(cssName, deep) {
var w;
var ws = [];
for (var i=0; i<this.widgets.length; i++) {
w = this.widgets[i];
if (w.div.className == cssName) ws.push(w);
if (deep)
ws =
ws.concat(w.getWidgetsByClassName(cssName, deep));
}
return ws;
}

I really like the "deep" option. I guess I have to add the "deep"
argument to other similar methods such as "getWidgetById" and
"getWidgetsByType" ;).

Thanks.

tkouts

unread,
Jul 11, 2008, 6:50:53 AM7/11/08
to porcupine-users
Giving it a second thought in order to retain backwards compatibility
the method becomes:

Widget.prototype.getWidgetsByClassName = function(cssName, shallow) {
var w;
var ws = [];
for (var i=0; i<this.widgets.length; i++) {
w = this.widgets[i];
if (w.div.className == cssName) ws.push(w);
if(!shallow)
ws =
ws.concat(w.getWidgetsByClassName(cssName, deep));
}
return ws;
}

On Jul 11, 1:23 pm, thomas <tbel...@velti.com> wrote:
Reply all
Reply to author
Forward
0 new messages