mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 07:32:59 +00:00 
			
		
		
		
	Update story macro to use rules to choose templates
Now special mapping tiddlers are used to determine which templates are used for a given tiddler
This commit is contained in:
		| @@ -41,8 +41,8 @@ exports.info = { | |||||||
| 	params: { | 	params: { | ||||||
| 		story: {byName: "default", type: "tiddler"}, | 		story: {byName: "default", type: "tiddler"}, | ||||||
| 		history: {byName: "default", type: "tiddler"}, | 		history: {byName: "default", type: "tiddler"}, | ||||||
| 		defaultViewTemplate: {byName: true, type: "tiddler"}, | 		viewTemplateMappings: {byName: true, type: "filter"}, | ||||||
| 		defaultEditTemplate: {byName: true, type: "tiddler"}, | 		editTemplateMappings: {byName: true, type: "filter"}, | ||||||
| 		storyviewTiddler: {byName: true, type: "tiddler"}, | 		storyviewTiddler: {byName: true, type: "tiddler"}, | ||||||
| 		storyview: {byName: true, type: "text"} | 		storyview: {byName: true, type: "text"} | ||||||
| 	} | 	} | ||||||
| @@ -59,20 +59,48 @@ exports.getHistory = function() { | |||||||
| 	this.history = this.wiki.getTiddlerData(this.params.history,{stack: []}); | 	this.history = this.wiki.getTiddlerData(this.params.history,{stack: []}); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| exports.getViewTemplate = function() { | exports.getTemplate = function(title,mappingFilter) { | ||||||
| 	if(this.hasParameter("defaultViewTemplate")) { | 	var tiddler = this.wiki.getTiddler(title), tiddlers = {}; | ||||||
| 		return this.params.defaultViewTemplate; | 	// Get the mapping tiddlers | ||||||
| 	} else { | 	var mappingTiddlers = this.wiki.filterTiddlers(mappingFilter), | ||||||
| 		return "$:/templates/ViewTemplate"; | 		rules = [], t, json, r, rule; | ||||||
|  | 	// Pull out the rules from each tiddler | ||||||
|  | 	for(t=0; t<mappingTiddlers.length; t++) { | ||||||
|  | 		json = this.wiki.getTiddlerData(mappingTiddlers[t],{rules: []}); | ||||||
|  | 		for(r=0; r<json.rules.length; r++) { | ||||||
|  | 			rules.push(json.rules[r]); | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | 	// Sort the rules by priority | ||||||
|  | 	rules.sort(function(a,b) { | ||||||
|  | 		return a.priority - b.priority; | ||||||
|  | 	}); | ||||||
|  | 	// Apply the rules | ||||||
|  | 	if(tiddler) { | ||||||
|  | 		tiddlers[title] = tiddler; | ||||||
|  | 		for(r=rules.length-1; r>=0; r--) { | ||||||
|  | 			rule = rules[r]; | ||||||
|  | 			if(this.wiki.filterTiddlers(rule.filter,null,tiddlers).length > 0) { | ||||||
|  | 				return rule.template; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		for(r=rules.length-1; r>=0; r--) { | ||||||
|  | 			rule = rules[r]; | ||||||
|  | 			if(rule.missingTiddler) { | ||||||
|  | 				return rule.template; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return null; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| exports.getEditTemplate = function() { | exports.getViewTemplate = function(title) { | ||||||
| 	if(this.hasParameter("defaultEditTemplate")) { | 	return this.getTemplate(title,this.params.viewTemplateMappings || "[tag[$:/tag/view-template-mapping]]"); | ||||||
| 		return this.params.defaultEditTemplate; | }; | ||||||
| 	} else { |  | ||||||
| 		return "$:/templates/EditTemplate"; | exports.getEditTemplate = function(title) { | ||||||
| 	} | 	return this.getTemplate(title,this.params.editTemplateMappings || "[tag[$:/tag/edit-template-mapping]]"); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* | /* | ||||||
| @@ -105,9 +133,9 @@ Create the tiddler macro needed to represent a given tiddler and its draft statu | |||||||
| exports.createStoryElementMacro = function(title,draft) { | exports.createStoryElementMacro = function(title,draft) { | ||||||
| 	var srcParams; | 	var srcParams; | ||||||
| 	if(draft) { | 	if(draft) { | ||||||
| 		srcParams = {target: draft, template: this.getEditTemplate()}; | 		srcParams = {target: draft, template: this.getEditTemplate(title)}; | ||||||
| 	} else { | 	} else { | ||||||
| 		srcParams = {target: title, template: this.getViewTemplate()}; | 		srcParams = {target: title, template: this.getViewTemplate(title)}; | ||||||
| 	} | 	} | ||||||
| 	return $tw.Tree.Macro("tiddler",{ | 	return $tw.Tree.Macro("tiddler",{ | ||||||
| 			srcParams: srcParams, | 			srcParams: srcParams, | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								core/templates/EditTemplateMappings.tid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								core/templates/EditTemplateMappings.tid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | title: $:/templates/EditTemplateMappings | ||||||
|  | tags: $:/tag/edit-template-mapping | ||||||
|  | type: application/json | ||||||
|  |  | ||||||
|  | {"rules":[{"filter":"[has[title]]","template":"$:/templates/EditTemplate","missingTiddler":true,"priority":0}]} | ||||||
							
								
								
									
										5
									
								
								core/templates/ViewTemplateMappings.tid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								core/templates/ViewTemplateMappings.tid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | title: $:/templates/ViewTemplateMappings | ||||||
|  | tags: $:/tag/view-template-mapping | ||||||
|  | type: application/json | ||||||
|  |  | ||||||
|  | {"rules":[{"filter":"[has[title]]","template":"$:/templates/ViewTemplate","missingTiddler":true,"priority":0}]} | ||||||
		Reference in New Issue
	
	Block a user
	 Jeremy Ruston
					Jeremy Ruston