Added checkbox macro

This commit is contained in:
Jeremy Ruston 2012-11-30 09:22:17 +00:00
parent 25a8500f05
commit a4930b7e68
8 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,106 @@
/*\
title: $:/core/modules/macros/checkbox.js
type: application/javascript
module-type: macro
Checkbox macro
{{{
<<checkbox tag:done>>
<<checkbox tiddler:HelloThere tag:red>>
<<checkbox tag:done><
<<view title>>
>>
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "checkbox",
params: {
tiddler: {byName: true, type: "tiddler"},
tag: {byPos: 0, type: "text"},
"class": {byName: true, type: "text"}
}
};
exports.getValue = function() {
var tiddler = this.wiki.getTiddler(this.targetTitle);
return tiddler ? tiddler.hasTag(this.params.tag) : false;
};
exports.executeMacro = function() {
// Get the title of the target tiddler
if(this.hasParameter("tiddler")) {
this.targetTitle = this.params.tiddler;
} else {
this.targetTitle = this.tiddlerTitle;
}
// Checkbox attributes
var checkboxAttributes = {
type: "checkbox"
};
if(this.getValue()) {
checkboxAttributes.checked = "true";
}
// Label attributes
var labelAttributes = {
"class": []
};
if(this.hasParameter("class")) {
$tw.utils.pushTop(labelAttributes["class"],this.params["class"].split(" "));
}
if(this.classes) {
$tw.utils.pushTop(labelAttributes["class"],this.classes);
}
// Execute content
for(var t=0; t<this.content.length; t++) {
this.content[t].execute(this.parents,this.tiddlerTitle);
}
// Create elements
return $tw.Tree.Element("label",labelAttributes,[
$tw.Tree.Element("input",checkboxAttributes,[],{
events: ["change"],
eventHandler: this
}),
$tw.Tree.Element("span",{},this.content)
],{});
return ;
};
exports.handleEvent = function(event) {
if(event.type === "change") {
var checked = this.child.children[0].domNode.checked,
tiddler = this.wiki.getTiddler(this.targetTitle);
if(tiddler && tiddler.hasTag(this.params.tag) !== checked) {
var newTags = tiddler.fields.tags.slice(0),
pos = newTags.indexOf(this.params.tag);
if(pos !== -1) {
newTags.splice(pos,1);
}
if(checked) {
newTags.push(this.params.tag);
}
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{tags: newTags}));
}
}
};
exports.refreshInDom = function(changes) {
// Only change the checkbox if the tiddler has changed
if(this.dependencies.hasChanged(changes,this.targetTitle)) {
this.child.children[0].domNode.checked = this.getValue();
}
// Refresh any children
this.child.refreshInDom(changes);
};
})();

View File

@ -10,5 +10,6 @@ Here are a few features of TiddlyWiki that you can explore:
* Save this wiki as a static HTML file: <<button save-wiki param:"$:/core/templates/static.template.html" class:"btn"><Save Static>>
* Browse the list of AllTiddlers or the ShadowTiddlers
* Examine the example [[bitmap images|Motovun Jack.jpg]] and [[SVG images|Motovun Jack.svg]]
* Check out the TaskManagementExample
* Explore the RoadMap of TiddlyWiki5's ongoing development
* Play with the new CecilyView

View File

@ -0,0 +1,3 @@
title: Compose ballad
tags: task

View File

@ -0,0 +1,3 @@
title: Get the Ring
tags: task

View File

@ -0,0 +1,3 @@
title: Go to Mordor
tags: task done

View File

@ -0,0 +1,3 @@
title: Kill the Dragon
tags: task

View File

@ -0,0 +1,3 @@
title: Make the beds
tags: task

View File

@ -0,0 +1,17 @@
title: TaskManagementExample
! Outstanding tasks
<<list filter:"[tag[task]!tag[done]sort[created]]"><
<<checkbox tag:done>< <<view title link>> >>
>>
! Completed tasks
<<list filter:"[tag[task]tag[done]sort[created]]"><
<<checkbox tag:done>< <<view title link>> >>
>>