2013-10-21 21:54:58 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/fieldmangler.js
|
2013-10-21 21:54:58 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-21 21:54:58 +00:00
|
|
|
|
|
|
|
Field mangler widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-21 21:54:58 +00:00
|
|
|
|
|
|
|
var FieldManglerWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
FieldManglerWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
FieldManglerWidget.prototype.render = function(parent,nextSibling) {
|
2021-01-09 13:25:48 +00:00
|
|
|
this.addEventListeners([
|
|
|
|
{type: "tm-remove-field", handler: "handleRemoveFieldEvent"},
|
|
|
|
{type: "tm-add-field", handler: "handleAddFieldEvent"},
|
|
|
|
{type: "tm-remove-tag", handler: "handleRemoveTagEvent"},
|
|
|
|
{type: "tm-add-tag", handler: "handleAddTagEvent"}
|
|
|
|
]);
|
2013-10-21 21:54:58 +00:00
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
FieldManglerWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
2013-10-30 13:36:44 +00:00
|
|
|
this.mangleTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
2013-10-21 21:54:58 +00:00
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
FieldManglerWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if(changedAttributes.tiddler) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-05-30 18:20:17 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2013-10-21 21:54:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleRemoveFieldEvent = function(event) {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
|
|
deletion = {};
|
|
|
|
deletion[event.param] = undefined;
|
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,deletion));
|
2021-04-08 15:56:36 +00:00
|
|
|
return false;
|
2013-10-21 21:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleAddFieldEvent = function(event) {
|
2014-12-04 09:39:14 +00:00
|
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
|
|
addition = this.wiki.getModificationFields(),
|
|
|
|
addField = function(name,value) {
|
2021-07-14 08:15:30 +00:00
|
|
|
var trimmedName = name.trim();
|
|
|
|
if(!value && tiddler) {
|
|
|
|
value = tiddler.fields[trimmedName];
|
2014-03-26 13:38:43 +00:00
|
|
|
}
|
2021-07-14 08:15:30 +00:00
|
|
|
addition[trimmedName] = value || "";
|
2014-12-04 09:39:14 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
addition.title = this.mangleTitle;
|
|
|
|
if(typeof event.param === "string") {
|
|
|
|
addField(event.param,"");
|
|
|
|
}
|
|
|
|
if(typeof event.paramObject === "object") {
|
|
|
|
for(var name in event.paramObject) {
|
|
|
|
addField(name,event.paramObject[name]);
|
2013-11-28 10:35:16 +00:00
|
|
|
}
|
2013-10-21 21:54:58 +00:00
|
|
|
}
|
2014-12-04 09:39:14 +00:00
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,addition));
|
2021-04-08 15:56:36 +00:00
|
|
|
return false;
|
2013-10-21 21:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleRemoveTagEvent = function(event) {
|
2017-09-20 13:09:18 +00:00
|
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
|
|
modification = this.wiki.getModificationFields();
|
2013-10-21 21:54:58 +00:00
|
|
|
if(tiddler && tiddler.fields.tags) {
|
|
|
|
var p = tiddler.fields.tags.indexOf(event.param);
|
|
|
|
if(p !== -1) {
|
|
|
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
|
|
|
modification.tags.splice(p,1);
|
2014-01-20 11:53:26 +00:00
|
|
|
if(modification.tags.length === 0) {
|
|
|
|
modification.tags = undefined;
|
|
|
|
}
|
2017-09-20 13:09:18 +00:00
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
2013-10-21 21:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 15:56:36 +00:00
|
|
|
return false;
|
2013-10-21 21:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FieldManglerWidget.prototype.handleAddTagEvent = function(event) {
|
2017-09-20 13:09:18 +00:00
|
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
|
|
modification = this.wiki.getModificationFields();
|
2014-03-31 16:59:28 +00:00
|
|
|
if(tiddler && typeof event.param === "string") {
|
|
|
|
var tag = event.param.trim();
|
|
|
|
if(tag !== "") {
|
|
|
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
|
|
|
$tw.utils.pushTop(modification.tags,tag);
|
2021-05-30 18:20:17 +00:00
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
2014-03-31 16:59:28 +00:00
|
|
|
}
|
2015-10-08 18:39:19 +00:00
|
|
|
} else if(typeof event.param === "string" && event.param.trim() !== "" && this.mangleTitle.trim() !== "") {
|
|
|
|
var tag = [];
|
|
|
|
tag.push(event.param.trim());
|
2017-09-20 13:09:18 +00:00
|
|
|
this.wiki.addTiddler(new $tw.Tiddler({title: this.mangleTitle, tags: tag},modification));
|
2013-10-21 21:54:58 +00:00
|
|
|
}
|
2021-04-08 15:56:36 +00:00
|
|
|
return false;
|
2013-10-21 21:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fieldmangler = FieldManglerWidget;
|
|
|
|
|
|
|
|
})();
|