1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-11 10:40:29 +00:00
TiddlyWiki5/editions/tw2/source/tiddlywiki/js/Refresh.js
2012-11-16 21:20:27 +00:00

193 lines
5.0 KiB
JavaScript
Executable File

//--
//-- Refresh mechanism
//--
//# List of notification functions to be called when certain tiddlers are changed or deleted
config.notifyTiddlers = [
{name: "SystemSettings", notify: onSystemSettingsChange},
{name: "StyleSheetLayout", notify: refreshStyles},
{name: "StyleSheetColors", notify: refreshStyles},
{name: "StyleSheet", notify: refreshStyles},
{name: "StyleSheetPrint", notify: refreshStyles},
{name: "PageTemplate", notify: refreshPageTemplate},
{name: "SiteTitle", notify: refreshPageTitle},
{name: "SiteSubtitle", notify: refreshPageTitle},
{name: "WindowTitle", notify: refreshPageTitle},
{name: "ColorPalette", notify: refreshColorPalette},
{name: null, notify: refreshDisplay}
];
//# refresher functions
config.refreshers = {
link: function(e,changeList)
{
var title = e.getAttribute("tiddlyLink");
refreshTiddlyLink(e,title);
return true;
},
tiddler: function(e,changeList)
{
var title = e.getAttribute("tiddler");
var template = e.getAttribute("template");
if(changeList && (changeList.indexOf && changeList.indexOf(title) != -1) && !story.isDirty(title))
story.refreshTiddler(title,template,true);
else
refreshElements(e,changeList);
return true;
},
content: function(e,changeList)
{
var title = e.getAttribute("tiddler");
var force = e.getAttribute("force");
var args = e.getAttribute("args");
if(force != null || changeList == null || (changeList.indexOf && changeList.indexOf(title) != -1)) {
jQuery(e).empty();
config.macros.tiddler.transclude(e,title,args);
return true;
} else
return false;
},
macro: function(e,changeList)
{
var macro = e.getAttribute("macroName");
var params = e.getAttribute("params");
if(macro)
macro = config.macros[macro];
if(macro && macro.refresh)
macro.refresh(e,params);
return true;
}
};
config.refresherData = {
styleSheet: "StyleSheet",
defaultStyleSheet: "StyleSheet",
pageTemplate: "PageTemplate",
defaultPageTemplate: "PageTemplate",
colorPalette: "ColorPalette",
defaultColorPalette: "ColorPalette"
};
function refreshElements(root,changeList)
{
var c,nodes = root.childNodes;
for(c=0; c<nodes.length; c++) {
var e = nodes[c], type = null;
if(e.getAttribute && (e.tagName ? e.tagName != "IFRAME" : true))
type = e.getAttribute("refresh");
var refresher = config.refreshers[type];
var refreshed = false;
if(refresher != undefined)
refreshed = refresher(e,changeList);
if(e.hasChildNodes() && !refreshed)
refreshElements(e,changeList);
}
}
function applyHtmlMacros(root,tiddler)
{
var e = root.firstChild;
while(e) {
var nextChild = e.nextSibling;
if(e.getAttribute) {
var macro = e.getAttribute("macro");
if(macro) {
e.removeAttribute("macro");
var params = "";
var p = macro.indexOf(" ");
if(p != -1) {
params = macro.substr(p+1);
macro = macro.substr(0,p);
}
invokeMacro(e,macro,params,null,tiddler);
}
}
if(e.hasChildNodes())
applyHtmlMacros(e,tiddler);
e = nextChild;
}
}
function refreshPageTemplate(title)
{
var stash = jQuery("<div/>").appendTo("body").hide()[0];
var display = story.getContainer();
var nodes,t;
if(display) {
nodes = display.childNodes;
for(t=nodes.length-1; t>=0; t--)
stash.appendChild(nodes[t]);
}
var wrapper = document.getElementById("contentWrapper");
var isAvailable = function(title) {
var s = title ? title.indexOf(config.textPrimitives.sectionSeparator) : -1;
if(s!=-1)
title = title.substr(0,s);
return store.tiddlerExists(title) || store.isShadowTiddler(title);
};
//# protect against non-existent pageTemplate
if(!title || !isAvailable(title))
title = config.refresherData.pageTemplate;
if(!isAvailable(title))
title = config.refresherData.defaultPageTemplate; //# this one is always avaialable
wrapper.innerHTML = store.getRecursiveTiddlerText(title,null,10);
applyHtmlMacros(wrapper);
refreshElements(wrapper);
display = story.getContainer();
jQuery(display).empty();
if(!display)
display = createTiddlyElement(wrapper,"div",story.containerId());
nodes = stash.childNodes;
for(t=nodes.length-1; t>=0; t--)
display.appendChild(nodes[t]);
jQuery(stash).remove();
}
function refreshDisplay(hint)
{
if(typeof hint == "string")
hint = [hint];
var e = document.getElementById("contentWrapper");
refreshElements(e,hint);
if(backstage.isPanelVisible()) {
e = document.getElementById("backstage");
refreshElements(e,hint);
}
}
function refreshPageTitle()
{
document.title = getPageTitle();
}
function getPageTitle()
{
return wikifyPlainText(store.getTiddlerText("WindowTitle",""),null,tiddler);
}
function refreshStyles(title,doc)
{
setStylesheet(title == null ? "" : store.getRecursiveTiddlerText(title,"",10),title,doc || document);
}
function refreshColorPalette(title)
{
if(!startingUp)
refreshAll();
}
function refreshAll()
{
refreshPageTemplate();
refreshDisplay();
refreshStyles("StyleSheetLayout");
refreshStyles("StyleSheetColors");
refreshStyles(config.refresherData.styleSheet);
refreshStyles("StyleSheetPrint");
}