1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-28 02:50:27 +00:00
TiddlyWiki5/core/modules/widgets/fieldmangler.js

129 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-10-21 21:54:58 +00:00
/*\
title: $:/core/modules/widgets/fieldmangler.js
2013-10-21 21:54:58 +00:00
type: application/javascript
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";
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);
this.addEventListeners([
2014-08-28 20:43:44 +00:00
{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
]);
};
/*
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("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 {
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) {
2014-04-28 14:54:32 +00:00
var tiddler = this.wiki.getTiddler(this.mangleTitle);
if(tiddler && typeof event.param === "string") {
var name = event.param.toLowerCase().trim();
if(name !== "" && !$tw.utils.hop(tiddler.fields,name)) {
2014-04-28 14:54:32 +00:00
if(!$tw.utils.isValidFieldName(name)) {
alert($tw.language.getString(
"InvalidFieldName",
{variables:
{fieldName: name}
}
));
return true;
}
var addition = this.wiki.getModificationFields();
addition[name] = "";
this.wiki.addTiddler(new $tw.Tiddler(tiddler,addition));
}
2013-10-21 21:54:58 +00:00
}
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 = this.wiki.getModificationFields();
2013-10-21 21:54:58 +00:00
modification.tags = (tiddler.fields.tags || []).slice(0);
modification.tags.splice(p,1);
if(modification.tags.length === 0) {
modification.tags = undefined;
}
2013-10-21 21:54:58 +00:00
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") {
var tag = event.param.trim();
if(tag !== "") {
var modification = this.wiki.getModificationFields();
modification.tags = (tiddler.fields.tags || []).slice(0);
$tw.utils.pushTop(modification.tags,tag);
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
}
2013-10-21 21:54:58 +00:00
}
return true;
};
exports.fieldmangler = FieldManglerWidget;
})();