1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-31 23:26:18 +00:00

Merge pull request #1118 from welford/hook

Add hooks mechanism and th-opening-default-tiddlers-list hook
This commit is contained in:
Jeremy Ruston 2014-11-25 19:25:00 +00:00
commit 92b957f6f8
5 changed files with 69 additions and 2 deletions

View File

@ -1899,6 +1899,35 @@ $tw.boot.isStartupTaskEligible = function(taskModule) {
return true;
};
/*
Global Hooks mechanism which allows plugins to modify default functionality
*/
$tw.hooks = $tw.hooks || { names: {}};
/*
Add hooks to the hashmap
*/
$tw.hooks.addHook = function(hookName,definition) {
if($tw.utils.hop($tw.hooks.names,hookName)) {
$tw.hooks.names[hookName].push(definition);
}
else {
$tw.hooks.names[hookName] = [definition];
}
};
/*
Invoke the hook by key
*/
$tw.hooks.invokeHook = function(hookName, value) {
if($tw.utils.hop($tw.hooks.names,hookName)) {
for (var i = 0; i < $tw.hooks.names[hookName].length; i++) {
value = $tw.hooks.names[hookName][i](value);
}
}
return value;
};
/////////////////////////// Main boot function to decrypt tiddlers and then startup
$tw.boot.boot = function() {

View File

@ -58,6 +58,8 @@ exports.startup = function() {
window.location.hash = "";
var storyFilter = $tw.wiki.getTiddlerText(DEFAULT_TIDDLERS_TITLE),
storyList = $tw.wiki.filterTiddlers(storyFilter);
//invoke any hooks that might change the default story list
storyList = $tw.hooks.invokeHook("th-opening-default-tiddlers-list",storyList);
$tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields());
if(storyList[0]) {
$tw.wiki.addToHistory(storyList[0]);
@ -116,6 +118,8 @@ function openStartupTiddlers(options) {
}
// Process the story filter to get the story list
var storyList = $tw.wiki.filterTiddlers(storyFilter);
//invoke any hooks that might change the default story list
storyList = $tw.hooks.invokeHook("th-opening-default-tiddlers-list",storyList);
// If the target tiddler isn't included then splice it in at the top
if(target && storyList.indexOf(target) === -1) {
storyList.unshift(target);

View File

@ -1,8 +1,8 @@
modified: 20140920124011558
modified: 20141122200310516
tags: TableOfContents
title: HelloThere
Welcome to the developer documentation for TiddlyWiki (http://tiddlywiki.com/). It is currently a work in progress as material from two different sources is adapted and merged:
Welcome to the developer documentation for TiddlyWiki (http://tiddlywiki.com/). It is currently a work in progress as material from two different sources is adapted and merged in addition to original content being added:
* An assignment by Christian Jurke and Christian Heigele, two students working on their Master's degree in Information Technology at the Gießen University of Applied Sciences (Technische Hochschule Mittelhessen). Their work can be seen in the [[Introduction]] and the tiddlers that link from it.
* The original developer documentation from http://tiddlywiki.com:
@ -22,3 +22,5 @@ Welcome to the developer documentation for TiddlyWiki (http://tiddlywiki.com/).
** SyncAdaptorModules
** WidgetModules
** WikiRuleModules
*Original developer documentation
** [[Hook Mechanism]]

View File

@ -0,0 +1,24 @@
created: 20141122200310516
modified: 20141122200310516
title: Hook Mechanism
type: text/vnd.tiddlywiki
The Hook Mechanism provides a way for plugins to hook into and modify default functionality. Hooks can be added to the wiki by calling the following from a plugin:
``$tw.hooks.addHook(name,function definition);``
Multiple hooks can be appended to the same name using repeated calls to the above. When a hook is invoked by name all registered functions will be called seqentially in their order of addition.
Though not essential care should be taken to ensure that hooks are added before they are invoked. For example: [[Hook: tc-opening-default-tiddlers-list]] should ideally be added before the story widget is invoked otherwise any hook specified additions to the default tiddlers will not be seen on the initial loading of the page, though will be visible if the user clicks the home button.
!!Example
A working example of addition to a hook that adds "test" to the default tiddlers.
```javascript
$tw.hooks.addHook("th-opening-default-tiddlers-list",function(list) {
list.push("test");
return list;
});
```

View File

@ -0,0 +1,8 @@
created: 20141122200310516
modified: 20141122200310516
title: Hook: tc-opening-default-tiddlers-list
type: text/vnd.tiddlywiki
This hook allows plugins to add aditional tiddlers to those specified in [[$:/DefaultTiddlers]].
The function definition takes a list of tiddlers as it's argument and should return a modified list of tiddlers to display when the wiki is first loaded or the home button is pressed. This hook is invoked after ``$tw.wiki.filterTiddlers`` is called on the contents of [[$:/DefaultTiddlers]] and it is therfore not correct to append additional filters to the list argument as they will not be subsequently applied.