2013-06-04 11:02:37 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/fieldmangler.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
The fieldmangler widget modifies the fields of the current tiddler in response to messages.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var FieldManglerWidget = function(renderer) {
|
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
|
|
|
this.generate();
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.generate = function() {
|
|
|
|
var self = this;
|
|
|
|
// Get parameters from our attributes
|
|
|
|
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.tiddlerTitle);
|
|
|
|
// Set the element
|
|
|
|
this.tag = "div";
|
|
|
|
this.attributes = {
|
|
|
|
"class": "tw-fieldmangler"
|
|
|
|
};
|
|
|
|
// Set event handlers
|
|
|
|
this.events = [
|
|
|
|
{name: "tw-remove-field", handlerObject: this, handlerMethod: "handleRemoveFieldEvent"},
|
2013-06-09 18:26:05 +00:00
|
|
|
{name: "tw-add-field", handlerObject: this, handlerMethod: "handleAddFieldEvent"},
|
|
|
|
{name: "tw-remove-tag", handlerObject: this, handlerMethod: "handleRemoveTagEvent"},
|
|
|
|
{name: "tw-add-tag", handlerObject: this, handlerMethod: "handleAddTagEvent"}
|
2013-06-04 11:02:37 +00:00
|
|
|
];
|
|
|
|
// Render the children
|
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.renderer.parseTreeNode.children);
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleRemoveFieldEvent = function(event) {
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle),
|
|
|
|
deletion = {};
|
2013-08-15 17:17:11 +00:00
|
|
|
deletion[event.param] = undefined;
|
2013-06-04 11:02:37 +00:00
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,deletion));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleAddFieldEvent = function(event) {
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle);
|
2013-06-09 18:26:05 +00:00
|
|
|
if(tiddler && typeof event.param === "string" && event.param !== "" && !$tw.utils.hop(tiddler.fields,event.param)) {
|
2013-06-04 11:02:37 +00:00
|
|
|
var addition = {};
|
|
|
|
addition[event.param] = "";
|
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,addition));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-06-09 18:26:05 +00:00
|
|
|
FieldManglerWidget.prototype.handleRemoveTagEvent = function(event) {
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle);
|
|
|
|
if(tiddler && tiddler.fields.tags) {
|
2013-08-15 17:17:11 +00:00
|
|
|
var p = tiddler.fields.tags.indexOf(event.param);
|
2013-06-09 18:26:05 +00:00
|
|
|
if(p !== -1) {
|
|
|
|
var modification = {};
|
2013-06-11 13:52:01 +00:00
|
|
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
2013-06-09 18:26:05 +00:00
|
|
|
modification.tags.splice(p,1);
|
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleAddTagEvent = function(event) {
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle);
|
|
|
|
if(tiddler && typeof event.param === "string" && event.param !== "") {
|
|
|
|
var modification = {};
|
2013-06-11 13:52:01 +00:00
|
|
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
2013-06-09 18:26:05 +00:00
|
|
|
$tw.utils.pushTop(modification.tags,event.param);
|
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-06-04 11:02:37 +00:00
|
|
|
exports.fieldmangler = FieldManglerWidget;
|
|
|
|
|
|
|
|
})();
|