get widget name via js

100 views
Skip to first unread message

Tobias Beer

unread,
Oct 24, 2016, 2:26:48 AM10/24/16
to tiddly...@googlegroups.com
Hi everyone,

If I do

console.log(widget)

the console shows me the name of the widget as it was assigned at the bottom of each widget as NameOfWidget:

exports.widgetname = NameOfWidget;

How can I get either NameOfWidget or widgetname via js?

Best wishes,

Tobias.

BJ

unread,
Oct 24, 2016, 7:10:40 AM10/24/16
to TiddlyWikiDev

Hi Tobias,

The widgets are gather in a list in the initialization of widgets, ie (see widget.js)

    // Hashmap of the widget classes
    if(!this.widgetClasses) {
        Widget.prototype.widgetClasses = $tw.modules.applyMethods("widget");
    }


here is a macro to list them


(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro

*/

exports.name = "widgets";

exports.params = [

];
/*
Run the macro
*/


exports.run = function() {



var returns = "----\n";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
var widget = new Widget();
for  (var widgets in widget.widgetClasses) {
returns = returns + "widget "  +widgets +"\n\n";
}


return returns;
}
})();


all the best BJ

Tobias Beer

unread,
Oct 24, 2016, 3:35:33 PM10/24/16
to TiddlyWikiDev
Hi BJ,

I think you misunderstood me. I am not looking for a list of all widget class names.

What I am looking for is the (prototype?) name of a widget instance, e.g.: widget.type or something.

I looked at the widget as the console logs it and the console does show its name, but if I open the widget properties, I have no clue how to programatically determine what this widget is called.

Best wishes,

Tobias.

BJ

unread,
Oct 24, 2016, 4:47:46 PM10/24/16
to TiddlyWikiDev
these are the "widgetname" that you refer to.

The "NameOfWidget" are only local symbols, and not available globally.

Tobias Beer

unread,
Oct 25, 2016, 12:35:12 AM10/25/16
to TiddlyWikiDev
Hi BJ,

I know these hold all the widget names, but if I have a reference to a single widget object / instance, how do I know its name / type / class  / prototype, whichever term best suits what I'm trying to read via js?

Maybe I'm just not seeing it. I want to read some property or call some function which will return "LinkWidget" or just  "link" if I do someWidgetFoo.type or someWidgetFoo.getType() or the likes.

Best wishes,

Tobias.

BJ

unread,
Oct 25, 2016, 4:32:20 AM10/25/16
to tiddly...@googlegroups.com
we could add a 'type' or name to the object, but at present you would need to do this:

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro

*/

exports.name = "widgets";

exports.params = [

];
/*
Run the macro
*/


exports.run = function() {



var returns = "";
var Widget = require("$:/core/modules/
widgets/link.js").link;

var widget = new Widget();
for  (var widgets in widget.widgetClasses) {
if (widget instanceof  widget.widgetClasses[widgets] && widgets!="widget") returns = widgets;
}

return returns;
}
})();

Tobias Beer

unread,
Oct 25, 2016, 7:44:12 AM10/25/16
to tiddly...@googlegroups.com
Hi BJ,
 
we could add a 'type' or name to the object, but at present you would need to do this:

Ok, I see. That doesn't look too pretty.

So, a new "helper" function on the base Widget class would be great,
so the prototype class would have it,
but not every single widget would need to define its name.

Any idea how to implement that? e.g.

exports.type = function() {
   
return ?!?;
}

Thanks,

Tobias.

BJ

unread,
Oct 25, 2016, 8:56:16 AM10/25/16
to tiddly...@googlegroups.com
LinkWidget.prototype.name = "link";

then we can access as

alert(widget.name)

Jeremy Ruston

unread,
Oct 25, 2016, 9:27:57 AM10/25/16
to tiddly...@googlegroups.com
Hi Tobias,

For widgets that have been created in the usual way, `widget.parseTreeNode.type` will return “browse”, “edit-text”, etc. It’s the technique that is used in the core, for example:

--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywikidev.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywikidev/0770341b-6b5f-4790-972a-616b5c40d4b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Ruston

unread,
Oct 25, 2016, 9:40:08 AM10/25/16
to tiddly...@googlegroups.com
For widgets that have been created in the usual way, `widget.parseTreeNode.type` will return “browse”, “edit-text”, etc. It’s the technique that is used in the core, for example:


I realised afterwards that that is a misleading example; the code there is walking a parse tree, not the widget tree. Here’s a better example:


Best wishes

Jeremy

Tobias Beer

unread,
Oct 25, 2016, 12:10:54 PM10/25/16
to TiddlyWikiDev
Hi Jeremy,
 

Thanks, appreciated. Makes total sense that it would be there.

Vielen Dank für Ihre Anfrage.
Reply all
Reply to author
Forward
0 new messages