1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Refactored widgets not to use a base class

This commit is contained in:
Jeremy Ruston 2013-01-01 17:51:02 +00:00
parent f8340bc4dc
commit d96e0073ac
10 changed files with 100 additions and 79 deletions

View File

@ -22,7 +22,7 @@ var WidgetRenderer = function(renderTree,renderContext,parseTreeNode) {
this.parseTreeNode = parseTreeNode;
// Widget classes
if(!this.widgetClasses) {
WidgetRenderer.prototype.widgetClasses = $tw.modules.createClassesFromModules("widget",null,$tw.WidgetBase);
WidgetRenderer.prototype.widgetClasses = $tw.modules.applyMethods("widget");
}
// Compute our attributes
this.attributes = {};
@ -30,8 +30,7 @@ var WidgetRenderer = function(renderTree,renderContext,parseTreeNode) {
// Create the widget object
var WidgetClass = this.widgetClasses[this.parseTreeNode.tag];
if(WidgetClass) {
this.widget = new WidgetClass();
this.widget.init(this);
this.widget = new WidgetClass(this);
} else {
// Error if we couldn't find the widget
this.children = this.renderTree.createRenderers(this.renderContext,[
@ -74,26 +73,46 @@ WidgetRenderer.prototype.getAttribute = function(name,defaultValue) {
WidgetRenderer.prototype.render = function(type) {
// Render the widget if we've got one
if(this.widget && this.widget.render) {
return this.widget.render(type);
if(this.widget) {
if(this.widget.render) {
return this.widget.render(type);
} else if(this.widget.children) {
var output = [];
$tw.utils.each(this.widget.children,function(node) {
if(node.render) {
output.push(node.render(type));
}
});
return output.join("");
}
}
};
WidgetRenderer.prototype.renderInDom = function() {
var self = this;
// Create the wrapper element
this.domNode = document.createElement(this.parseTreeNode.isBlock ? "div" : "span");
this.domNode.setAttribute("data-widget-type",this.parseTreeNode.tag);
this.domNode.setAttribute("data-widget-attr",JSON.stringify(this.attributes));
// Render the widget if we've got one
if(this.widget) {
this.widget.renderInDom(this.domNode);
if(this.widget.renderInDom) {
this.widget.renderInDom(this.domNode);
} else if(this.widget.children) {
// Render any child nodes
$tw.utils.each(this.widget.children,function(node) {
if(node.renderInDom) {
self.domNode.appendChild(node.renderInDom());
}
});
}
// Attach any event handlers
if(this.widget.getEventListeners) {
$tw.utils.addEventListeners(this.domNode,this.widget.getEventListeners());
}
}
// Call the postRenderInDom hook if the widget has one
if(this.widget.postRenderInDom) {
if(this.widget && this.widget.postRenderInDom) {
this.widget.postRenderInDom();
}
// Return the dom node

View File

@ -12,16 +12,14 @@ Implements the button widget.
/*global $tw: false */
"use strict";
exports.name = "button";
exports.init = function(renderer) {
var ButtonWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
ButtonWidget.prototype.generateChildNodes = function() {
// Get the parameters from the attributes
this.message = this.renderer.getAttribute("message");
this.param = this.renderer.getAttribute("param");
@ -52,14 +50,14 @@ exports.generateChildNodes = function() {
}]);
};
exports.dispatchMessage = function(event) {
ButtonWidget.prototype.dispatchMessage = function(event) {
$tw.utils.dispatchCustomEvent(event.target,this.message,{
param: this.param,
tiddlerTitle: this.renderer.getContextTiddlerTitle()
});
};
exports.triggerPopup = function(event) {
ButtonWidget.prototype.triggerPopup = function(event) {
var title = this.popup;
if(this.qualifyTiddlerTitles) {
title = title + "-" + this.renderer.getContextScopeId();
@ -71,12 +69,12 @@ exports.triggerPopup = function(event) {
});
};
exports.setTiddler = function() {
ButtonWidget.prototype.setTiddler = function() {
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.set);
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.set, text: this.setTo}));
};
exports.handleClickEvent = function(event) {
ButtonWidget.prototype.handleClickEvent = function(event) {
if(this.message) {
this.dispatchMessage(event);
}
@ -90,7 +88,7 @@ exports.handleClickEvent = function(event) {
return false;
};
exports.handleMouseOverOrOutEvent = function(event) {
ButtonWidget.prototype.handleMouseOverOrOutEvent = function(event) {
if(this.popup) {
this.triggerPopup(event);
}
@ -98,7 +96,7 @@ exports.handleMouseOverOrOutEvent = function(event) {
return false;
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
ButtonWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
if(changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes.qualifyTiddlerTitles || changedAttributes["class"] || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup])) {
// Remove old child nodes
@ -121,4 +119,6 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
}
};
exports.button = ButtonWidget;
})();

View File

@ -12,16 +12,14 @@ The view widget displays the fields of a tiddler through a text substitution tem
/*global $tw: false */
"use strict";
exports.name = "fields";
exports.init = function(renderer) {
var FieldsWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
FieldsWidget.prototype.generateChildNodes = function() {
// Get parameters from our attributes
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.getContextTiddlerTitle());
this.template = this.renderer.getAttribute("template");
@ -75,4 +73,6 @@ exports.generateChildNodes = function() {
this.children = this.renderer.renderTree.createRenderers(this.renderer.renderContext,[node]);
};
exports.fields = FieldsWidget;
})();

View File

@ -12,21 +12,19 @@ Implements the link widget.
/*global $tw: false */
"use strict";
exports.name = "link";
var isLinkExternal = function(to) {
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
return externalRegExp.test(to);
};
exports.init = function(renderer) {
var LinkWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
LinkWidget.prototype.generateChildNodes = function() {
// Get the parameters from the attributes
this.to = this.renderer.getAttribute("to");
this.hover = this.renderer.getAttribute("hover");
@ -65,7 +63,7 @@ exports.generateChildNodes = function() {
}]);
};
exports.handleClickEvent = function(event) {
LinkWidget.prototype.handleClickEvent = function(event) {
if(isLinkExternal(this.to)) {
event.target.setAttribute("target","_blank");
return true;
@ -80,7 +78,7 @@ exports.handleClickEvent = function(event) {
}
};
exports.handleMouseOverOrOutEvent = function(event) {
LinkWidget.prototype.handleMouseOverOrOutEvent = function(event) {
if(this.hover) {
$tw.popup.triggerPopup({
textRef: this.hover,
@ -94,7 +92,7 @@ exports.handleMouseOverOrOutEvent = function(event) {
return false;
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
LinkWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Set the class for missing tiddlers
if(this.targetTitle && changedTiddlers[this.targetTitle]) {
$tw.utils.toggleClass(this.children[0].domNode,"tw-tiddler-missing",!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle));
@ -121,4 +119,6 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
}
};
exports.link = LinkWidget;
})();

View File

@ -12,7 +12,12 @@ The list widget
/*global $tw: false */
"use strict";
exports.name = "list";
var ListWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
/*
These types are shorthands for particular filters
@ -25,14 +30,7 @@ var typeMappings = {
shadows: "[is[shadow]sort[title]]"
};
exports.init = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
ListWidget.prototype.generateChildNodes = function() {
// Get our attributes
this.itemClass = this.renderer.getAttribute("itemClass");
this.template = this.renderer.getAttribute("template");
@ -63,7 +61,7 @@ exports.generateChildNodes = function() {
}]);
};
exports.getTiddlerList = function() {
ListWidget.prototype.getTiddlerList = function() {
var filter;
if(this.renderer.hasAttribute("type")) {
filter = typeMappings[this.renderer.getAttribute("type")];
@ -79,7 +77,7 @@ exports.getTiddlerList = function() {
/*
Create and execute the nodes representing the empty message
*/
exports.getEmptyMessage = function() {
ListWidget.prototype.getEmptyMessage = function() {
return {
type: "element",
tag: "span",
@ -90,7 +88,7 @@ exports.getEmptyMessage = function() {
/*
Create a list element representing a given tiddler
*/
exports.createListElement = function(title) {
ListWidget.prototype.createListElement = function(title) {
// Define an event handler that adds navigation information to the event
var handleEvent = function(event) {
event.navigateFromTitle = title;
@ -122,7 +120,7 @@ exports.createListElement = function(title) {
/*
Create the tiddler macro needed to represent a given tiddler
*/
exports.createListElementMacro = function(title) {
ListWidget.prototype.createListElementMacro = function(title) {
// Check if the tiddler is a draft
var tiddler = this.renderer.renderTree.wiki.getTiddler(title),
isDraft = tiddler ? tiddler.hasField("draft.of") : false;
@ -164,7 +162,7 @@ exports.createListElementMacro = function(title) {
/*
Remove a list element from the list, along with the attendant DOM nodes
*/
exports.removeListElement = function(index) {
ListWidget.prototype.removeListElement = function(index) {
// Get the list element
var listElement = this.children[0].children[index];
// Remove the DOM node
@ -178,7 +176,7 @@ Return the index of the list element that corresponds to a particular title
startIndex: index to start search (use zero to search from the top)
title: tiddler title to seach for
*/
exports.findListElementByTitle = function(startIndex,title) {
ListWidget.prototype.findListElementByTitle = function(startIndex,title) {
while(startIndex < this.children[0].children.length) {
if(this.children[0].children[startIndex].children[0].attributes.target === title) {
return startIndex;
@ -188,7 +186,7 @@ exports.findListElementByTitle = function(startIndex,title) {
return undefined;
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
ListWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Reexecute the widget if any of our attributes have changed
if(changedAttributes.itemClass || changedAttributes.template || changedAttributes.editTemplate || changedAttributes.emptyMessage || changedAttributes.type || changedAttributes.filter || changedAttributes.template) {
// Remove old child nodes
@ -207,7 +205,7 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
}
};
exports.handleListChanges = function(changedTiddlers) {
ListWidget.prototype.handleListChanges = function(changedTiddlers) {
var t,
prevListLength = this.list.length,
frame = this.children[0];
@ -267,4 +265,6 @@ exports.handleListChanges = function(changedTiddlers) {
}
};
exports.list = ListWidget;
})();

View File

@ -12,16 +12,14 @@ Implements the navigator widget.
/*global $tw: false */
"use strict";
exports.name = "navigator";
exports.init = function(renderer) {
var NavigatorWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
NavigatorWidget.prototype.generateChildNodes = function() {
// Get our parameters
this.storyTitle = this.renderer.getAttribute("story");
this.historyTitle = this.renderer.getAttribute("history");
@ -29,7 +27,7 @@ exports.generateChildNodes = function() {
this.children = this.renderer.renderTree.createRenderers(this.renderer.renderContext,this.renderer.parseTreeNode.children);
};
exports.getEventListeners = function() {
NavigatorWidget.prototype.getEventListeners = function() {
return [
{name: "tw-navigate", handlerObject: this, handlerMethod: "handleNavigateEvent"},
{name: "tw-EditTiddler", handlerObject: this, handlerMethod: "handleEditTiddlerEvent"},
@ -39,7 +37,7 @@ exports.getEventListeners = function() {
];
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
NavigatorWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// We don't need to refresh ourselves, so just refresh any child nodes
$tw.utils.each(this.children,function(node) {
if(node.refreshInDom) {
@ -48,7 +46,7 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
});
};
exports.getStoryList = function() {
NavigatorWidget.prototype.getStoryList = function() {
var text = this.renderer.renderTree.wiki.getTextReference(this.storyTitle,"");
if(text && text.length > 0) {
this.storyList = text.split("\n");
@ -57,14 +55,14 @@ exports.getStoryList = function() {
}
};
exports.saveStoryList = function() {
NavigatorWidget.prototype.saveStoryList = function() {
var storyTiddler = this.renderer.renderTree.wiki.getTiddler(this.storyTitle);
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler({
title: this.storyTitle
},storyTiddler,{text: this.storyList.join("\n")}));
};
exports.findTitleInStory = function(title,defaultIndex) {
NavigatorWidget.prototype.findTitleInStory = function(title,defaultIndex) {
for(var t=0; t<this.storyList.length; t++) {
if(this.storyList[t] === title) {
return t;
@ -74,7 +72,7 @@ exports.findTitleInStory = function(title,defaultIndex) {
};
// Navigate to a specified tiddler
exports.handleNavigateEvent = function(event) {
NavigatorWidget.prototype.handleNavigateEvent = function(event) {
if(this.storyTitle) {
// Update the story tiddler if specified
this.getStoryList();
@ -101,7 +99,7 @@ exports.handleNavigateEvent = function(event) {
};
// Close a specified tiddler
exports.handleCloseTiddlerEvent = function(event) {
NavigatorWidget.prototype.handleCloseTiddlerEvent = function(event) {
this.getStoryList();
// Look for tiddlers with this title to close
var slot = this.findTitleInStory(event.tiddlerTitle,-1);
@ -114,7 +112,7 @@ exports.handleCloseTiddlerEvent = function(event) {
};
// Place a tiddler in edit mode
exports.handleEditTiddlerEvent = function(event) {
NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) {
this.getStoryList();
// Replace the specified tiddler with a draft in edit mode
for(var t=0; t<this.storyList.length; t++) {
@ -143,7 +141,7 @@ exports.handleEditTiddlerEvent = function(event) {
};
// Take a tiddler out of edit mode, saving the changes
exports.handleSaveTiddlerEvent = function(event) {
NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) {
this.getStoryList();
var storyTiddlerModified = false;
for(var t=0; t<this.storyList.length; t++) {
@ -180,7 +178,7 @@ exports.handleSaveTiddlerEvent = function(event) {
};
// Create a new draft tiddler
exports.handleNewTiddlerEvent = function(event) {
NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) {
// Get the story details
this.getStoryList();
// Create the new tiddler
@ -218,4 +216,6 @@ exports.handleNewTiddlerEvent = function(event) {
return false;
};
exports.navigator = NavigatorWidget;
})();

View File

@ -12,16 +12,14 @@ Implements the reveal widget.
/*global $tw: false */
"use strict";
exports.name = "reveal";
exports.init = function(renderer) {
var RevealWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
RevealWidget.prototype.generateChildNodes = function() {
// Get the parameters from the attributes
this.state = this.renderer.getAttribute("state");
this.type = this.renderer.getAttribute("type");
@ -61,7 +59,7 @@ exports.generateChildNodes = function() {
/*
Read the state tiddler
*/
exports.readState = function() {
RevealWidget.prototype.readState = function() {
// Start with the default value for being open or closed
if(this["default"]) {
this.isOpen = this["default"] === "open";
@ -84,11 +82,11 @@ exports.readState = function() {
}
};
exports.readMatchState = function(state) {
RevealWidget.prototype.readMatchState = function(state) {
this.isOpen = state === this.text;
};
exports.readPopupState = function(state) {
RevealWidget.prototype.readPopupState = function(state) {
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/,
match = popupLocationRegExp.exec(state);
// Check if the state matches the location regexp
@ -108,7 +106,7 @@ exports.readPopupState = function(state) {
}
};
exports.handleClickEvent = function(event) {
RevealWidget.prototype.handleClickEvent = function(event) {
if(event.type === "click" && this.type === "popup") {
// Cancel the popup if we get a click on it
if(this.stateTitle) {
@ -120,7 +118,7 @@ exports.handleClickEvent = function(event) {
return true;
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
RevealWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
if(changedAttributes.state || changedAttributes.type || changedAttributes.text || changedAttributes.position || changedAttributes["default"] || changedAttributes.qualifyTiddlerTitles || changedAttributes["class"]) {
// Remove old child nodes
@ -161,7 +159,7 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
this.postRenderInDom();
};
exports.postRenderInDom = function() {
RevealWidget.prototype.postRenderInDom = function() {
switch(this.type) {
case "popup":
if(this.isOpen) {
@ -198,4 +196,6 @@ exports.postRenderInDom = function() {
}
};
exports.reveal = RevealWidget;
})();

View File

@ -46,16 +46,14 @@ of the tiddler `Foo`.
/*global $tw: false */
"use strict";
exports.name = "transclude";
exports.init = function(renderer) {
var TranscludeWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Generate child nodes
this.generateChildNodes();
};
exports.generateChildNodes = function() {
TranscludeWidget.prototype.generateChildNodes = function() {
var tr, templateParseTree, templateTiddler;
// Get the render target details
this.targetTitle = this.renderer.getAttribute("target",this.renderer.getContextTiddlerTitle());
@ -109,7 +107,7 @@ exports.generateChildNodes = function() {
this.children = this.renderer.renderTree.createRenderers(newRenderContext,[node]);
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
TranscludeWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Set the class for missing tiddlers
if(this.targetTitle && changedTiddlers[this.targetTitle]) {
$tw.utils.toggleClass(this.children[0].domNode,"tw-tiddler-missing",!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle));
@ -136,4 +134,6 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
}
};
exports.transclude = TranscludeWidget;
})();

View File

@ -12,8 +12,6 @@ The view widget displays a tiddler field.
/*global $tw: false */
"use strict";
exports.name = "view";
/*
Define the "text" viewer here so that it is always available
*/
@ -42,7 +40,7 @@ TextViewer.prototype.render = function() {
// We'll cache the available field viewers here
var fieldViewers = undefined;
exports.init = function(renderer) {
var ViewWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Initialise the field viewers if they've not been done already
@ -54,7 +52,7 @@ exports.init = function(renderer) {
this.generateChildNodes();
};
exports.generateChildNodes = function() {
ViewWidget.prototype.generateChildNodes = function() {
// Get parameters from our attributes
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.getContextTiddlerTitle());
this.fieldName = this.renderer.getAttribute("field","text");
@ -93,7 +91,7 @@ exports.generateChildNodes = function() {
this.children = this.viewer.render();
};
exports.refreshInDom = function(changedAttributes,changedTiddlers) {
ViewWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.format || (this.tiddlerTitle && changedTiddlers[this.tiddlerTitle])) {
// Remove old child nodes
@ -116,4 +114,6 @@ exports.refreshInDom = function(changedAttributes,changedTiddlers) {
}
};
exports.view = ViewWidget;
})();

View File

@ -1,5 +1,7 @@
title: $:/templates/PageTemplate
<$nosuchwidget />
<!-- The navigator catches navigation events and updates the story and history tiddlers -->
<$navigator story="$:/StoryList" history="$:/HistoryList">