mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-30 13:29:56 +00:00
Add a fieldmangler widget
This commit is contained in:
parent
06af26b1c8
commit
7393a296dd
110
core/modules/new_widgets/fieldmangler.js
Normal file
110
core/modules/new_widgets/fieldmangler.js
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/new_widgets/fieldmangler.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: new_widget
|
||||||
|
|
||||||
|
Field mangler widget
|
||||||
|
|
||||||
|
\*/
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
/*jslint node: true, browser: true */
|
||||||
|
/*global $tw: false */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var Widget = require("$:/core/modules/new_widgets/widget.js").widget;
|
||||||
|
|
||||||
|
var FieldManglerWidget = function(parseTreeNode,options) {
|
||||||
|
this.initialise(parseTreeNode,options);
|
||||||
|
this.addEventListeners([
|
||||||
|
{type: "tw-remove-field", handler: "handleRemoveFieldEvent"},
|
||||||
|
{type: "tw-add-field", handler: "handleAddFieldEvent"},
|
||||||
|
{type: "tw-remove-tag", handler: "handleRemoveTagEvent"},
|
||||||
|
{type: "tw-add-tag", handler: "handleAddTagEvent"}
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Inherit from the base widget class
|
||||||
|
*/
|
||||||
|
FieldManglerWidget.prototype = new Widget();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Render this widget into the DOM
|
||||||
|
*/
|
||||||
|
FieldManglerWidget.prototype.render = function(parent,nextSibling) {
|
||||||
|
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
|
||||||
|
this.mangleTitle = this.getAttribute("title",this.getVariable("tiddlerTitle"));
|
||||||
|
// 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 {
|
||||||
|
return this.refreshChildren(changedTiddlers);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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));
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
FieldManglerWidget.prototype.handleAddFieldEvent = function(event) {
|
||||||
|
var tiddler = this.wiki.getTiddler(this.mangleTitle);
|
||||||
|
if(tiddler && typeof event.param === "string" && event.param !== "" && !$tw.utils.hop(tiddler.fields,event.param)) {
|
||||||
|
var addition = {};
|
||||||
|
addition[event.param] = "";
|
||||||
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,addition));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
FieldManglerWidget.prototype.handleRemoveTagEvent = function(event) {
|
||||||
|
var tiddler = this.wiki.getTiddler(this.mangleTitle);
|
||||||
|
if(tiddler && tiddler.fields.tags) {
|
||||||
|
var p = tiddler.fields.tags.indexOf(event.param);
|
||||||
|
if(p !== -1) {
|
||||||
|
var modification = {};
|
||||||
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
||||||
|
modification.tags.splice(p,1);
|
||||||
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
FieldManglerWidget.prototype.handleAddTagEvent = function(event) {
|
||||||
|
var tiddler = this.wiki.getTiddler(this.mangleTitle);
|
||||||
|
if(tiddler && typeof event.param === "string" && event.param !== "") {
|
||||||
|
var modification = {};
|
||||||
|
modification.tags = (tiddler.fields.tags || []).slice(0);
|
||||||
|
$tw.utils.pushTop(modification.tags,event.param);
|
||||||
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.fieldmangler = FieldManglerWidget;
|
||||||
|
|
||||||
|
})();
|
Loading…
Reference in New Issue
Block a user