-
Notifications
You must be signed in to change notification settings - Fork 4
/
TableViewPlugin.js
146 lines (121 loc) · 4.93 KB
/
TableViewPlugin.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
/***
|TableViewPlugin||
|Version|0.6|
|Author|Ben Gillies|
|Type|plugin|
|Description|view tiddlers in a table suitable for copying into excel|
!Usage
Creates a table view where each row is a configurable list of tiddlers transposed into a single tiddler.
call with:
<<table filter:[filter[here]] heading:heading_name heading:heading_name heading:heading_name>>
filter:[filter[here]] is the filter you want to use to select tiddlers eg - [tag[company]] would use all tiddlers tagged company
heading:heading_name is a table heading. Note this must be the same as the tiddler name being used in that column.
eg:
<<table filter:[tag[company]] heading:title heading:description heading:status>>
Would create a table with all tiddlers tagged company. The first column is the tiddler title.
The second column comes from a separate tiddler of the form TiddlerName_description.
The third column comes from a separate tiddler of the form TiddlerName_status.
Whether separate tiddlers are pulled in or not is dng_name heading:heading_name heading:heading_name>>
filter:[filter[here]] is the filter you want to use to select tiddlers eg - [tag[company]] would use all tiddlers tagged company
heading:heading_name is a table heading. Note this must be the same as the tiddler name being used in that column.
eg:
<<table fi all columns
heading:matchPattern then specifies a name to strip from the end of each line, leaving just the column titles.
For example:
<<table filter:[tag[company]] columnTiddler:CompanyStructure heading:Section>>
would take each line from CompanyStructure, strip the word "Section" from the end, and use that as the column headings.
In this case, title is always used as the default first column.
!Code
***/
//{{{
if(!version.extensions.TableViewPlugin)
{ //# ensure that the plugin is only installed once
version.extensions.TableViewPlugin = { installed: true }
};
config.macros.table ={
handler: function(place, macroName, params, wikifier, paramString, tiddler){
//parse the params into tableHeadings and excludeBags
params = paramString.parseParams()[0];
var tiddlers = store.filterTiddlers(params['filter']);
var headings = params['heading'];
var extension = params['extension'];
if (extension) {
extension = extension[0];
}
var taggedHeadings = params['tags'];
if (taggedHeadings) {
taggedHeadings = taggedHeadings[0].readBracketedList();
}
var imageURL = params['imageURL'];
if (imageURL) {
imageURL = imageURL[0];
}
var imageHeadings = params['images'];
if (imageHeadings) {
imageHeadings = imageHeadings[0].readBracketedList();
}
var columnTiddler = false;
if ('columnTiddler' in params) {
columnTiddler = store.getTiddler(params['columnTiddler']);
var pattern = new RegExp(headings + '$');
headings = columnTiddler.text.split('\n');
for (var i=0; i<headings.length; i++) {
headings[i] = headings[i].split(pattern)[0];
}
}
//set up the table
var table = jQuery('<table class="reportTable" />');
jQuery('<div class="reportContainer" />').
append(table).appendTo(place);
//construct the table heading
var tableHeading = jQuery('<tr />').appendTo(jQuery('<thead/>').appendTo(table));
if (columnTiddler) {
jQuery('<th />').text('title').appendTo(tableHeading);
}
for (var i=0; i<headings.length; i++) {
jQuery('<th />').text(headings[i]).appendTo(tableHeading);
}
//construct the table body
var tableBody = jQuery('<tbody />').appendTo(table);
for (var i=0; i < tiddlers.length; i++) {
var tiddler = tiddlers[i];
var currentRow = jQuery('<tr />').appendTo(tableBody);
if (columnTiddler) {
var titlePlace = jQuery('<td />').appendTo(currentRow);
createTiddlyLink(titlePlace[0], tiddler.title, true);
}
for (var j=0; j<headings.length; j++) {
var heading = headings[j];
var currentCell = jQuery('<td />').appendTo(currentRow);
//get the value for this cell from a similarly named tiddler (eg - Foo_fieldName)
var fetchValue = function() {
if ((taggedHeadings) && (taggedHeadings.contains(heading))) {
return (tiddler.tags.contains(heading)) ? "*" : "";
} else if ((imageHeadings) && (imageHeadings.contains(heading))) {
return '[img[%0/%1.%2]]'.format(imageURL, tiddler.title, extension);
}
var subTiddlerName = tiddler.title + '_' + heading;
var cellTiddler = store.getTiddler(subTiddlerName);
if (cellTiddler) {
return cellTiddler.text;
} else {
return '';
}
}
if (heading === 'title') {
createTiddlyLink(currentCell[0], tiddler.title, true);
} else {
var text = tiddler[heading] ||
tiddler.fields[heading] ||
store.getTiddlerSlice(tiddler.title, heading) ||
store.getTiddlerText(tiddler.title +
config.textPrimitives.sectionSeparator +
heading) ||
fetchValue();
wikify(text, currentCell[0]);
}
}
}
}
}
//}}}