Hi Danielo

It looks like you're trying to use the variable "resultPlugin" to expose
the title of the tiddler generated by the widget. There's an immediate
problem with that: you've got state retained in the widget that isn't part
of the tiddler store. That means that your widget wouldn't be able to
retain the resultPlugin value if it were refreshed by a parent widget.

I think a better approach is for your widget to have an attribute that is
used to pass the name of a tiddler that should be assigned the title of the
newly generated tiddler.

Best wishes

Jeremy





On Thu, Apr 17, 2014 at 1:59 PM, Danielo Rodríguez <[email protected]>wrote:

> Hello Jeremy,
>
> I'm facing more problems than I though with this one. I have another
> problem with the params. The widget is not getting the params correctly.
> Sometimes it gets empty strings and in other cases it took the previous
> value. I don't know where the problem is.
>
> If you don't mind, I will paste the code here. Meanwhile I will try to set
> a git repository on my computer. The code is a bit dirty but I have plans
> to change it.
>
> Thanks in advance.
>
> /*\
> title: $:/plugins/danielo/packAsPlugin/packAsPlugin.js
> type: application/javascript
> module-type: widget
>
> encrypttiddler widget
>
> ```
>
> ```
>
>
> \*/
> (function (){
>
> /*jslint node: true, browser: true */
> /*global $tw: false */
> "use strict";
>
> var Widget = require("$:/core/modules/widgets/widget.js").widget;
>
> var packAsPlugin = function (parseTreeNode,options) {
>     this.initialise(parseTreeNode,options);
>     this.addEventListeners([
>             {type: "tw-pack-plugin", handler: "handlePackevent"},
>             {type: "tw-unpack-plugin", handler: "handleUnpackevent"}
>             ]);
> };
>
> /*
> Inherit from the base widget class
> */
> packAsPlugin.prototype = new Widget();
>
> /*
> Render this widget into the DOM
> */
> packAsPlugin.prototype.render = function (parent,nextSibling) {
>     this.parentDomNode = parent;
>     this.computeAttributes();
>     this.execute();
>     this.renderChildren(parent,nextSibling);
> };
>
> /*
> Compute the internal state of the widget
> */
> packAsPlugin.prototype.execute = function () {
>     // Get attributes
>      this.pluginDependencies=this.getAttribute("dependencies");
>        this.pluginDescription=this.getAttribute("description","plugin
> packed with Danielo's pack plugin");
>      this.pluginType=this.getAttribute("type","plugin");
>       this.pluginVersion=this.getAttribute("version");
>
>
> this.pluginAuthor=this.getAttribute("author",$tw.wiki.getTiddlerText("$:/status/UserName")
> || "Danielo" );
>       this.pluginName=this.getAttribute("name","yourPlugin") || "aPlugin" ;
>
>
>  
> this.tiddlersFilter=this.getAttribute("filter","[!is[system]!is[shadow]!has[draft.of]]");
>     // Construct the child widgets
>
> console.log(this.pluginAuthor+"-Name:"+this.pluginName+"-filter:"+this.tiddlersFilter);
>         this.makeChildWidgets();
> };
>
> /*
> Selectively refreshes the widget if needed. Returns true if the widget or
> any of its children needed re-rendering
> */
> packAsPlugin.prototype.refresh = function (changedTiddlers) {
>     var changedAttributes = this.computeAttributes();
>     console.log(this.computeAttributes());
>     if(changedAttributes.tiddler) {
>         this.refreshSelf();
>         return true;
>     } else {
>         return this.refreshChildren(changedTiddlers);
>     }
> };
>
> packAsPlugin.prototype.handlePackevent = function (event){
>     var pluginFields=this.createPluginTiddler();
>     var sourceTiddlers=this.wiki.filterTiddlers(this.tiddlersFilter);
>     var components=[];
>     console.log("Pack of plugins to include ["+sourceTiddlers+"]");
>     for(var i=0;i<sourceTiddlers.length;i++){
>         components.push(this.createComponentTiddler(sourceTiddlers[i]));
>         console.log("Packing: "+sourceTiddlers[i]);
>         }
>
>     this.saveTiddler("",pluginFields);
>     console.log("Created plugin tiddler "+pluginFields.title + " Version
> "+pluginFields.version);
>     $tw.utils.repackPlugin(pluginFields.title,components);
>
>     this.setVariable("resultPlugin",pluginFields.title);
>     this.makeChildWidgets();
>
> };
>
> packAsPlugin.prototype.handleUnpackevent = function (event){
> //WIP
> };
>
> packAsPlugin.prototype.getBasicTitle = function (){ return
> "$:/plugins/"+this.pluginAuthor+"/"+this.pluginName };
>
> packAsPlugin.prototype.createComponentTitle = function (componentName){
> return this.getBasicTitle()+"/"+this.basename(componentName)
> };
>
> packAsPlugin.prototype.basename = function (path) {
>     return path.replace(/\\/g,'/').replace( /.*\//, '' );
> };
>
> packAsPlugin.prototype.createComponentTiddler = function (originalTitle){
>     var tiddler=this.wiki.getTiddler(originalTitle);
>     if(tiddler){
>         var componentTitle = this.createComponentTitle(originalTitle);
>         this.saveTiddler(tiddler,{title:componentTitle});
>         console.log("Created "+componentTitle);
>         return componentTitle;
>     }
> };
>
>
> packAsPlugin.prototype.createPluginTiddler = function (){
>     var previousVersion = this.wiki.getTiddler(this.getBasicTitle());
>     this.pluginVersion = previousVersion ? previousVersion.fields.version
> : this.pluginVersion;
>     var fields={
>                     title:this.getBasicTitle(),
>                     dependencies: this.pluginDependencies,
>                     description: this.pluginDescription,
>                     version:this.pluginVersion,
>                     "plugin-type":this.pluginType,
>                     type:"application/json",
>                     text: JSON.stringify({tiddlers:{}})
>                 };
>         return fields;
> };
>
> packAsPlugin.prototype.saveTiddler=function (tiddler,fields){
>     this.wiki.addTiddler(  new
> $tw.Tiddler(this.wiki.getModificationFields(),tiddler,fields ) );
> };
>
> exports.packPlugin = packAsPlugin;
>
> })();
>



-- 
Jeremy Ruston
mailto:[email protected]

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/d/optout.

Reply via email to