2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/list/list.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
The list widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var ListWidget = function(renderer) {
|
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
2013-01-17 13:52:46 +00:00
|
|
|
// Initialise the listviews if they've not been done already
|
|
|
|
if(!this.listViews) {
|
|
|
|
ListWidget.prototype.listViews = {};
|
|
|
|
$tw.modules.applyMethods("listview",this.listViews);
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
// Generate widget elements
|
|
|
|
this.generate();
|
2013-01-01 17:51:02 +00:00
|
|
|
};
|
2012-12-14 16:01:37 +00:00
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
/*
|
|
|
|
These types are shorthands for particular filters
|
|
|
|
*/
|
|
|
|
var typeMappings = {
|
2013-03-15 22:00:19 +00:00
|
|
|
all: "[!is[system]sort[title]]",
|
|
|
|
recent: "[!is[system]sort[modified]]",
|
2012-12-13 21:34:31 +00:00
|
|
|
missing: "[is[missing]sort[title]]",
|
|
|
|
orphans: "[is[orphan]sort[title]]",
|
2013-03-15 22:00:19 +00:00
|
|
|
system: "[is[system]sort[title]]"
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
ListWidget.prototype.generate = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get our attributes
|
|
|
|
this.itemClass = this.renderer.getAttribute("itemClass");
|
|
|
|
this.template = this.renderer.getAttribute("template");
|
|
|
|
this.editTemplate = this.renderer.getAttribute("editTemplate");
|
|
|
|
this.emptyMessage = this.renderer.getAttribute("emptyMessage");
|
2013-06-09 18:25:11 +00:00
|
|
|
this["class"] = this.renderer.getAttribute("class");
|
|
|
|
// Set up the classes
|
|
|
|
var classes = ["tw-list-frame"];
|
|
|
|
if(this["class"]) {
|
|
|
|
$tw.utils.pushTop(classes,this["class"]);
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the list of tiddlers object
|
|
|
|
this.getTiddlerList();
|
|
|
|
// Create the list
|
|
|
|
var listMembers = [];
|
|
|
|
if(this.list.length === 0) {
|
|
|
|
// Check for an empty list
|
|
|
|
listMembers = [this.getEmptyMessage()];
|
|
|
|
} else {
|
|
|
|
// Create the list
|
|
|
|
for(var t=0; t<this.list.length; t++) {
|
|
|
|
listMembers.push(this.createListElement(this.list[t]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create the list frame element
|
2013-01-03 16:27:55 +00:00
|
|
|
this.tag = this.renderer.parseTreeNode.isBlock ? "div" : "span";
|
|
|
|
this.attributes = {
|
2013-06-09 18:25:11 +00:00
|
|
|
"class": classes.join(" ")
|
2013-01-03 16:27:55 +00:00
|
|
|
};
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,listMembers);
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.getTiddlerList = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
var filter;
|
|
|
|
if(this.renderer.hasAttribute("type")) {
|
|
|
|
filter = typeMappings[this.renderer.getAttribute("type")];
|
|
|
|
} else if(this.renderer.hasAttribute("filter")) {
|
|
|
|
filter = this.renderer.getAttribute("filter");
|
|
|
|
}
|
|
|
|
if(!filter) {
|
2013-03-15 22:00:19 +00:00
|
|
|
filter = "[!is[system]]";
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
2013-05-15 16:32:17 +00:00
|
|
|
this.list = this.renderer.renderTree.wiki.filterTiddlers(filter,this.renderer.tiddlerTitle);
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create and execute the nodes representing the empty message
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.getEmptyMessage = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
return {
|
|
|
|
type: "element",
|
|
|
|
tag: "span",
|
2013-03-19 22:39:04 +00:00
|
|
|
children: this.renderer.renderTree.wiki.parseText("text/vnd.tiddlywiki",this.emptyMessage,{parseAsInline: true}).tree
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a list element representing a given tiddler
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.createListElement = function(title) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Define an event handler that adds navigation information to the event
|
|
|
|
var handleEvent = function(event) {
|
|
|
|
event.navigateFromTitle = title;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
classes = ["tw-list-element"];
|
|
|
|
// Add any specified classes
|
|
|
|
if(this.itemClass) {
|
|
|
|
$tw.utils.pushTop(classes,this.itemClass);
|
|
|
|
}
|
|
|
|
// Return the list element
|
|
|
|
return {
|
|
|
|
type: "element",
|
2012-12-23 10:47:14 +00:00
|
|
|
tag: this.renderer.parseTreeNode.isBlock ? "div" : "span",
|
2012-12-13 21:34:31 +00:00
|
|
|
attributes: {
|
|
|
|
"class": {type: "string", value: classes.join(" ")}
|
|
|
|
},
|
|
|
|
children: [this.createListElementMacro(title)],
|
|
|
|
events: [
|
|
|
|
{name: "tw-navigate", handlerFunction: handleEvent},
|
2013-02-04 15:29:25 +00:00
|
|
|
{name: "tw-edit-tiddler", handlerFunction: handleEvent},
|
|
|
|
{name: "tw-save-tiddler", handlerFunction: handleEvent},
|
|
|
|
{name: "tw-close-tiddler", handlerFunction: handleEvent},
|
|
|
|
{name: "tw-new-tiddler", handlerFunction: handleEvent}
|
2012-12-13 21:34:31 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create the tiddler macro needed to represent a given tiddler
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.createListElementMacro = function(title) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Check if the tiddler is a draft
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(title),
|
|
|
|
isDraft = tiddler ? tiddler.hasField("draft.of") : false;
|
|
|
|
// Figure out the template to use
|
|
|
|
var template = this.template,
|
|
|
|
templateTree = undefined;
|
|
|
|
if(isDraft && this.editTemplate) {
|
|
|
|
template = this.editTemplate;
|
|
|
|
}
|
|
|
|
// Check for not having a template
|
|
|
|
if(!template) {
|
|
|
|
if(this.renderer.parseTreeNode.children && this.renderer.parseTreeNode.children.length > 0) {
|
|
|
|
// Use our content as the template
|
|
|
|
templateTree = this.renderer.parseTreeNode.children;
|
|
|
|
} else {
|
|
|
|
// Use default content
|
|
|
|
templateTree = [{
|
2013-01-03 16:27:55 +00:00
|
|
|
type: "element",
|
|
|
|
tag: "$view",
|
2012-12-13 21:34:31 +00:00
|
|
|
attributes: {
|
|
|
|
field: {type: "string", value: "title"},
|
|
|
|
format: {type: "string", value: "link"}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
// Create the transclude widget
|
2012-12-13 21:34:31 +00:00
|
|
|
return {
|
2013-01-03 16:27:55 +00:00
|
|
|
type: "element",
|
|
|
|
tag: "$transclude",
|
2013-03-28 21:29:42 +00:00
|
|
|
isBlock: this.renderer.parseTreeNode.isBlock,
|
2012-12-13 21:34:31 +00:00
|
|
|
attributes: {
|
|
|
|
target: {type: "string", value: title},
|
|
|
|
template: {type: "string", value: template}
|
|
|
|
},
|
|
|
|
children: templateTree
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove a list element from the list, along with the attendant DOM nodes
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.removeListElement = function(index) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the list element
|
2013-01-03 16:27:55 +00:00
|
|
|
var listElement = this.children[index];
|
2013-02-06 11:27:09 +00:00
|
|
|
// Invoke the listview to animate the removal
|
|
|
|
if(this.listview && this.listview.remove) {
|
|
|
|
if(!this.listview.remove(index)) {
|
|
|
|
// Only delete the DOM element if the listview.remove() returned false
|
|
|
|
listElement.domNode.parentNode.removeChild(listElement.domNode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Always remove the DOM node if we didn't invoke the listview
|
|
|
|
listElement.domNode.parentNode.removeChild(listElement.domNode);
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
// Then delete the actual renderer node
|
2013-01-03 16:27:55 +00:00
|
|
|
this.children.splice(index,1);
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.findListElementByTitle = function(startIndex,title) {
|
2013-01-03 16:27:55 +00:00
|
|
|
while(startIndex < this.children.length) {
|
|
|
|
if(this.children[startIndex].widget.children[0].attributes.target === title) {
|
2012-12-13 21:34:31 +00:00
|
|
|
return startIndex;
|
|
|
|
}
|
|
|
|
startIndex++;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2013-01-17 13:52:46 +00:00
|
|
|
ListWidget.prototype.postRenderInDom = function() {
|
|
|
|
this.listview = this.chooseListView();
|
|
|
|
this.history = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Select the appropriate list viewer
|
|
|
|
*/
|
|
|
|
ListWidget.prototype.chooseListView = function() {
|
|
|
|
// Instantiate the list view
|
|
|
|
var listviewName = this.renderer.getAttribute("listview");
|
|
|
|
var ListView = this.listViews[listviewName];
|
|
|
|
return ListView ? new ListView(this) : null;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Reexecute the widget if any of our attributes have changed
|
2013-01-17 13:52:46 +00:00
|
|
|
if(changedAttributes.itemClass || changedAttributes.template || changedAttributes.editTemplate || changedAttributes.emptyMessage || changedAttributes.type || changedAttributes.filter || changedAttributes.template || changedAttributes.history || changedAttributes.listview) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-13 21:34:31 +00:00
|
|
|
} else {
|
|
|
|
// Handle any changes to the list, and refresh any nodes we're reusing
|
|
|
|
this.handleListChanges(changedTiddlers);
|
2013-01-17 13:52:46 +00:00
|
|
|
// Update the history list
|
|
|
|
var history = this.renderer.getAttribute("history");
|
|
|
|
if(history && changedTiddlers[history]) {
|
|
|
|
this.handleHistoryChanges();
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ListWidget.prototype.handleListChanges = function(changedTiddlers) {
|
2012-12-13 21:34:31 +00:00
|
|
|
var t,
|
2013-01-18 15:36:49 +00:00
|
|
|
prevListLength = this.list.length,
|
|
|
|
self = this;
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the list of tiddlers, having saved the previous length
|
|
|
|
this.getTiddlerList();
|
|
|
|
// Check if the list is empty
|
|
|
|
if(this.list.length === 0) {
|
|
|
|
// Check if it was empty before
|
|
|
|
if(prevListLength === 0) {
|
|
|
|
// If so, just refresh the empty message
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// If the list wasn't empty before, empty it
|
|
|
|
for(t=prevListLength-1; t>=0; t--) {
|
|
|
|
this.removeListElement(t);
|
|
|
|
}
|
|
|
|
// Insert the empty message
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,[this.getEmptyMessage()]);
|
2013-01-03 16:27:55 +00:00
|
|
|
$tw.utils.each(this.children,function(node) {
|
2012-12-13 21:34:31 +00:00
|
|
|
if(node.renderInDom) {
|
2013-01-18 15:36:49 +00:00
|
|
|
self.renderer.domNode.appendChild(node.renderInDom());
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If it is not empty now, but was empty previously, then remove the empty message
|
|
|
|
if(prevListLength === 0) {
|
|
|
|
this.removeListElement(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step through the list and adjust our child list elements appropriately
|
|
|
|
for(t=0; t<this.list.length; t++) {
|
|
|
|
// Check to see if the list element is already there
|
|
|
|
var index = this.findListElementByTitle(t,this.list[t]);
|
|
|
|
if(index === undefined) {
|
|
|
|
// The list element isn't there, so we need to insert it
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children.splice(t,0,this.renderer.renderTree.createRenderer(this.renderer,this.createListElement(this.list[t])));
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.insertBefore(this.children[t].renderInDom(),this.renderer.domNode.childNodes[t]);
|
2013-02-06 11:27:09 +00:00
|
|
|
// Ask the listview to animate the insertion
|
|
|
|
if(this.listview && this.listview.insert) {
|
|
|
|
this.listview.insert(t);
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
} else {
|
|
|
|
// Delete any list elements preceding the one we want
|
|
|
|
for(var n=index-1; n>=t; n--) {
|
|
|
|
this.removeListElement(n);
|
|
|
|
}
|
|
|
|
// Refresh the node we're reusing
|
2013-01-03 16:27:55 +00:00
|
|
|
this.children[t].refreshInDom(changedTiddlers);
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove any left over elements
|
2013-01-03 16:27:55 +00:00
|
|
|
for(t=this.children.length-1; t>=this.list.length; t--) {
|
2012-12-13 21:34:31 +00:00
|
|
|
this.removeListElement(t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-17 13:52:46 +00:00
|
|
|
/*
|
|
|
|
Handle any changes to the history list
|
|
|
|
*/
|
|
|
|
ListWidget.prototype.handleHistoryChanges = function() {
|
|
|
|
// Get the history data
|
|
|
|
var historyAtt = this.renderer.getAttribute("history"),
|
|
|
|
newHistory = this.renderer.renderTree.wiki.getTiddlerData(historyAtt,[]);
|
|
|
|
// Ignore any entries of the history that match the previous history
|
|
|
|
var entry = 0;
|
|
|
|
while(entry < newHistory.length && entry < this.history.length && newHistory[entry].title === this.history[entry].title) {
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
// Navigate forwards to each of the new tiddlers
|
|
|
|
while(entry < newHistory.length) {
|
|
|
|
if(this.listview && this.listview.navigateTo) {
|
|
|
|
this.listview.navigateTo(newHistory[entry]);
|
|
|
|
}
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
// Update the history
|
|
|
|
this.history = newHistory;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.list = ListWidget;
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
})();
|