mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-11 18:00:26 +00:00
Introduce Story object
A lightweight object to encapsulate manipulation of the story and history lists
This commit is contained in:
parent
a4d0210f90
commit
8444961cd2
136
core/modules/story.js
Normal file
136
core/modules/story.js
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/story.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: global
|
||||||
|
|
||||||
|
Lightweight object for managing interactions with the story and history lists.
|
||||||
|
|
||||||
|
\*/
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
/*jslint node: true, browser: true */
|
||||||
|
/*global $tw: false */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Construct Story object with options:
|
||||||
|
wiki: reference to wiki object to use to resolve tiddler titles
|
||||||
|
storyTitle: title of story list tiddler
|
||||||
|
historyTitle: title of history list tiddler
|
||||||
|
*/
|
||||||
|
function Story(options) {
|
||||||
|
options = options || {};
|
||||||
|
this.wiki = options.wiki || $tw.wiki;
|
||||||
|
this.storyTitle = options.storyTitle || "$:/StoryList";
|
||||||
|
this.historyTitle = options.historyTitle || "$:/HistoryList";
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.navigateTiddler = function(navigateTo,navigateFromTitle,navigateFromClientRect) {
|
||||||
|
this.addToStory(navigateTo,navigateFromTitle);
|
||||||
|
this.addToHistory(navigateTo,navigateFromClientRect);
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.getStoryList = function() {
|
||||||
|
return this.wiki.getTiddlerList(this.storyTitle) || [];
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.addToStory = function(navigateTo,navigateFromTitle,options) {
|
||||||
|
options = options || {};
|
||||||
|
var storyList = this.getStoryList();
|
||||||
|
// See if the tiddler is already there
|
||||||
|
var slot = storyList.indexOf(navigateTo);
|
||||||
|
// Quit if it already exists in the story river
|
||||||
|
if(slot >= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// First we try to find the position of the story element we navigated from
|
||||||
|
var fromIndex = storyList.indexOf(navigateFromTitle);
|
||||||
|
if(fromIndex >= 0) {
|
||||||
|
// The tiddler is added from inside the river
|
||||||
|
// Determine where to insert the tiddler; Fallback is "below"
|
||||||
|
switch(options.openLinkFromInsideRiver) {
|
||||||
|
case "top":
|
||||||
|
slot = 0;
|
||||||
|
break;
|
||||||
|
case "bottom":
|
||||||
|
slot = storyList.length;
|
||||||
|
break;
|
||||||
|
case "above":
|
||||||
|
slot = fromIndex;
|
||||||
|
break;
|
||||||
|
case "below": // Intentional fall-through
|
||||||
|
default:
|
||||||
|
slot = fromIndex + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The tiddler is opened from outside the river. Determine where to insert the tiddler; default is "top"
|
||||||
|
if(options.openLinkFromOutsideRiver === "bottom") {
|
||||||
|
// Insert at bottom
|
||||||
|
slot = storyList.length;
|
||||||
|
} else {
|
||||||
|
// Insert at top
|
||||||
|
slot = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the tiddler
|
||||||
|
storyList.splice(slot,0,navigateTo);
|
||||||
|
// Save the story
|
||||||
|
this.saveStoryList(storyList);
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.saveStoryList = function(storyList) {
|
||||||
|
var storyTiddler = this.wiki.getTiddler(this.storyTitle);
|
||||||
|
this.wiki.addTiddler(new $tw.Tiddler(
|
||||||
|
{title: this.storyTitle},
|
||||||
|
storyTiddler,
|
||||||
|
{list: storyList}
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.addToHistory = function(navigateTo,navigateFromClientRect) {
|
||||||
|
var titles = $tw.utils.isArray(navigateTo) ? navigateTo : [navigateTo];
|
||||||
|
// Add a new record to the top of the history stack
|
||||||
|
var historyList = this.wiki.getTiddlerData(this.historyTitle,[]);
|
||||||
|
$tw.utils.each(titles,function(title) {
|
||||||
|
historyList.push({title: title, fromPageRect: navigateFromClientRect});
|
||||||
|
});
|
||||||
|
this.wiki.setTiddlerData(this.historyTitle,historyList,{"current-tiddler": titles[titles.length-1]});
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyCloseTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyCloseAllTiddlers = function() {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyCloseOtherTiddlers = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyEditTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyDeleteTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storySaveTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyCancelTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
Story.prototype.storyNewTiddler = function(targetTitle) {
|
||||||
|
// TBD
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.Story = Story;
|
||||||
|
|
||||||
|
|
||||||
|
})();
|
@ -1175,14 +1175,8 @@ fromPageRect: page coordinates of the origin of the navigation
|
|||||||
historyTitle: title of history tiddler (defaults to $:/HistoryList)
|
historyTitle: title of history tiddler (defaults to $:/HistoryList)
|
||||||
*/
|
*/
|
||||||
exports.addToHistory = function(title,fromPageRect,historyTitle) {
|
exports.addToHistory = function(title,fromPageRect,historyTitle) {
|
||||||
historyTitle = historyTitle || "$:/HistoryList";
|
var story = new $tw.Story({wiki: this, historyTitle: historyTitle});
|
||||||
var titles = $tw.utils.isArray(title) ? title : [title];
|
story.addToHistory(title,fromPageRect);
|
||||||
// Add a new record to the top of the history stack
|
|
||||||
var historyList = this.getTiddlerData(historyTitle,[]);
|
|
||||||
$tw.utils.each(titles,function(title) {
|
|
||||||
historyList.push({title: title, fromPageRect: fromPageRect});
|
|
||||||
});
|
|
||||||
this.setTiddlerData(historyTitle,historyList,{"current-tiddler": titles[titles.length-1]});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user