-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtale.js
161 lines (139 loc) · 3.33 KB
/
tale.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// Class: Tale
//
// Used to provide access to passages. This is analogous to the
// TiddlyWiki class in the core TiddlyWiki code.
//
// Property: passages
// An associative array of <Passage> objects in the story.
// The key for this array is the title of the passage.
//
//
// Constructor: Tale
//
// Initializes a new Tale object with the contents of the
// DOM element with the id *storeArea*, constructing new <Passage>s
// as it traverses the tree.
//
// Parameters:
// none
//
function Tale()
{
this.passages = {};
if (document.normalize)
document.normalize();
var store = $('storeArea').childNodes;
for (var i = 0; i < store.length; i++)
{
var el = store[i];
if (el.getAttribute && (title = el.getAttribute('tiddler')))
this.passages[title] = new Passage(title, el, i);
};
}
//
// Method: has
//
// Checks whether the tale has a passage with either the title
// passed (if the key parameter is a string) or an id (if
// a number is passed instead).
//
// Parameters:
// key - the title or id of the passage to search for
//
// Returns:
// boolean
//
Tale.prototype.has = function (key)
{
// returns whether a passage exists
if (typeof key == 'string')
return (this.passages[key] != null);
else
{
for (i in this.passages)
if (this.passages[i].id == key)
return true;
return false;
};
};
//
// Method: get
//
// A getter function that returns a certain <Passage> object belonging
// to the tale. You may either retrieve a passage by its title or id.
//
// Parameters:
// key - the title or id of the passage to retrieve
//
// Returns:
// A <Passage> object. If no passage exists matching the passed key,
// a null value is returned.
//
// See also:
// <Tale.lookup>
//
Tale.prototype.get = function (key)
{
// returns a passage either by title or its id
if (typeof key == 'string')
return this.passages[key] || new Passage(key);
else
for (i in this.passages)
if (this.passages[i].id == key)
return this.passages[i];
};
//
// Method: lookup
//
// Searches the Tale for all passages matching a certain criteria.
// You may optionally specify a secondary field to sort the results on.
//
// Parameters:
// field - the name of the <Passage> property to search on
// value - the value to match
// sortField - the name of the <Passage> property to sort matches on.
// This always sorts in descending order. If you need ascending order,
// use the Array class's reverse() method.
//
// Returns:
// An array of <Passage>s. If no passages met the search criteria,
// an empty array is returned.
//
// See also:
// <Tale.get>
//
Tale.prototype.lookup = function(field, value, sortField)
{
var results = [];
for (var t in this.passages)
{
var passage = this.passages[t];
var found = false;
for (var i = 0; i < passage[field].length; i++)
if (passage[field][i] == value)
results.push(passage);
}
if (! sortField)
sortField = 'title';
results.sort(function (a,b) {if(a[sortField] == b[sortField]) return(0); else return (a[sortField] < b[sortField]) ? -1 : +1; });
return results;
};
//
// Method: reset
//
// Calls the <Passage.reset> method on all <Passage>s in the tale, restoring
// the story to its initial state.
//
// Parameters:
// none
//
// Returns:
// nothing
//
Tale.prototype.reset = function()
{
console.log('resetting all passages');
for (i in this.passages)
this.passages[i].reset();
};