mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-24 02:27:19 +00:00
4858b24cfe
* add EventListeners in the render() method instead of the constructor * scrollable widget: add EventListeners in render() method instead of constructor + ... move logic from constructor to render() * linkcatcher: add EventListeners in render() instead of constructor * fieldmangler: add EventListeners in render() instead of constructor * edit-bitmap: initialise editorOperations in render() instead of constructor * list-widget: initialise storyviews in render() instead of constructor * vars widget: execute Widget.call(this) in render() instead of constructor ... not shure what this should do * Update fieldmangler.js * Update edit-bitmap.js * Update linkcatcher.js * Update navigator.js * Update scrollable.js * Update list.js * Update vars.js
149 lines
4.1 KiB
JavaScript
149 lines
4.1 KiB
JavaScript
/*\
|
|
title: $:/core/modules/widgets/fieldmangler.js
|
|
type: application/javascript
|
|
module-type: widget
|
|
|
|
Field mangler widget
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
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) {
|
|
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"}
|
|
]);
|
|
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"));
|
|
// 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),
|
|
addition = this.wiki.getModificationFields(),
|
|
hadInvalidFieldName = false,
|
|
addField = function(name,value) {
|
|
var trimmedName = name.toLowerCase().trim();
|
|
if(!$tw.utils.isValidFieldName(trimmedName)) {
|
|
if(!hadInvalidFieldName) {
|
|
alert($tw.language.getString(
|
|
"InvalidFieldName",
|
|
{variables:
|
|
{fieldName: trimmedName}
|
|
}
|
|
));
|
|
hadInvalidFieldName = true;
|
|
return;
|
|
}
|
|
} else {
|
|
if(!value && tiddler) {
|
|
value = tiddler.fields[trimmedName];
|
|
}
|
|
addition[trimmedName] = value || "";
|
|
}
|
|
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]);
|
|
}
|
|
}
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,addition));
|
|
return true;
|
|
};
|
|
|
|
FieldManglerWidget.prototype.handleRemoveTagEvent = function(event) {
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
modification = this.wiki.getModificationFields();
|
|
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);
|
|
if(modification.tags.length === 0) {
|
|
modification.tags = undefined;
|
|
}
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
FieldManglerWidget.prototype.handleAddTagEvent = function(event) {
|
|
var tiddler = this.wiki.getTiddler(this.mangleTitle),
|
|
modification = this.wiki.getModificationFields();
|
|
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);
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,modification));
|
|
}
|
|
} else if(typeof event.param === "string" && event.param.trim() !== "" && this.mangleTitle.trim() !== "") {
|
|
var tag = [];
|
|
tag.push(event.param.trim());
|
|
this.wiki.addTiddler(new $tw.Tiddler({title: this.mangleTitle, tags: tag},modification));
|
|
}
|
|
return true;
|
|
};
|
|
|
|
exports.fieldmangler = FieldManglerWidget;
|
|
|
|
})();
|