Ticket #756: bakedoptions2.patch

File bakedoptions2.patch, 9.5 KB (added by FND, 3 years ago)

Second pass at a patch by JeremyRuston?

  • shadows/split.recipe

     
    55shadow: StyleSheetLayout.tiddler 
    66shadow: StyleSheetLocale.tiddler 
    77shadow: StyleSheetPrint.tiddler 
     8shadow: SystemSettings.tiddler 
    89shadow: PageTemplate.tiddler 
    910shadow: ViewTemplate.tiddler 
    1011shadow: EditTemplate.tiddler 
  • shadows/SystemSettings.tiddler

     
     1<div title="SystemSettings"> 
     2<pre>|''chkAnimate_cookie:''|false| 
     3|''chkAutoSave:''|false| 
     4|''chkBackstage_cookie:''|false| 
     5|''chkCaseSensitiveSearch:''|false| 
     6|''chkConfirmDelete:''|true| 
     7|''chkDisplayInstrumentation:''|false| 
     8|''chkForceMinorUpdate:''|false| 
     9|''chkGenerateAnRssFeed:''|false| 
     10|''chkHttpReadOnly:''|true| 
     11|''chkIncrementalSearch:''|true| 
     12|''chkInsertTabs:''|false| 
     13|''chkOpenInNewWindow:''|true| 
     14|''chkRegExpSearch:''|false| 
     15|''chkSaveBackups:''|true| 
     16|''chkSaveEmptyTemplate:''|false| 
     17|''chkSideBarTabs_cookie:''|false| 
     18|''chkSliderOptionsPanel_cookie:''|false| 
     19|''chkToggleLinks:''|false| 
     20|''chkUsePreForStorage:''|true| 
     21|''txtBackupFolder:''|| 
     22|''txtEditorFocus:''|text| 
     23|''txtFileSystemCharSet:''|UTF-8| 
     24|''txtMainTab_cookie:''|More| 
     25|''txtMaxEditRows:''|30| 
     26|''txtMoreTab_cookie:''|Missing| 
     27|''txtTheme:''|| 
     28|''txtUserName:''|YourName|</pre> 
     29</div> 
     30 No newline at end of file 
  • js/TiddlyWiki.js

     
    151151        return textOut.join(""); 
    152152}; 
    153153 
    154 TiddlyWiki.prototype.slicesRE = /(?:^([\'\/]{0,2})~?([\.\w]+)\:\1\s*([^\n]+)\s*$)|(?:^\|([\'\/]{0,2})~?([\.\w]+)\:?\4\|\s*([^\|\n]+)\s*\|$)/gm; 
     154TiddlyWiki.prototype.slicesRE = /(?:^([\'\/]{0,2})~?([\.\w]+)\:\1[\t\x20]*([^\n]*)[\t\x20]*$)|(?:^\|([\'\/]{0,2})~?([\.\w]+)\:?\4\|[\t\x20]*([^\|\n]*)[\t\x20]*\|$)/gm; 
    155155 
    156156// @internal 
    157157TiddlyWiki.prototype.calcAllSlices = function(title) 
  • js/main.js

     
    3333        story = new Story("tiddlerDisplay","tiddler"); 
    3434        addEvent(document,"click",Popup.onDocumentClick); 
    3535        saveTest(); 
    36         loadOptionsCookie(); 
    3736        for(var s=0; s<config.notifyTiddlers.length; s++) 
    3837                store.addNotification(config.notifyTiddlers[s].name,config.notifyTiddlers[s].notify); 
    3938        t1 = new Date(); 
    4039        loadShadowTiddlers(); 
    4140        t2 = new Date(); 
    4241        store.loadFromDiv("storeArea","store",true); 
     42        loadOptionsCookie(); 
    4343        t3 = new Date(); 
    4444        invokeParamifier(params,"onload"); 
    4545        t4 = new Date(); 
  • js/Options.js

     
    44 
    55config.optionHandlers = { 
    66        'txt': { 
    7                 get: function(name) {return encodeCookie(config.options[name].toString());}, 
    8                 set: function(name,value) {config.options[name] = decodeCookie(value);} 
     7                get: function(name) {return config.options[name].toString();}, 
     8                set: function(name,value) {config.options[name] = value;} 
    99        }, 
    1010        'chk': { 
    1111                get: function(name) {return config.options[name] ? "true" : "false";}, 
     
    1313        } 
    1414}; 
    1515 
    16 function loadOptionsCookie() 
     16function setOption(name,value) 
    1717{ 
     18        var optType = name.substr(0,3); 
     19        if(config.optionHandlers[optType] && config.optionHandlers[optType].set) 
     20                config.optionHandlers[optType].set(name,value); 
     21} 
     22 
     23// Gets the value of an option as a string. Most code should just read from config.options.* directly 
     24function getOption(name) 
     25{ 
     26        var optType = name.substr(0,3); 
     27        if(config.optionHandlers[optType] && config.optionHandlers[optType].get) 
     28                return config.optionHandlers[optType].get(name); 
     29        else 
     30                return null; 
     31} 
     32 
     33//# Loads up config.options from cookies and SystemSettings 
     34function initialiseOptions() 
     35{ 
    1836        if(safeMode) 
    1937                return; 
     38        loadSystemSettings(); 
     39        loadCookies(); 
     40} 
     41// Deprecated name for backwards compatibility 
     42var loadOptionsCookie = initialiseOptions; 
     43 
     44function loadCookies() 
     45{ 
     46        var cookies = {}; 
    2047        var cookies = document.cookie.split(";"); 
    2148        for(var c=0; c<cookies.length; c++) { 
    2249                var p = cookies[c].indexOf("="); 
    2350                if(p != -1) { 
    2451                        var name = cookies[c].substr(0,p).trim(); 
    2552                        var value = cookies[c].substr(p+1).trim(); 
    26                         var optType = name.substr(0,3); 
    27                         if(config.optionHandlers[optType] && config.optionHandlers[optType].set) 
    28                                 config.optionHandlers[optType].set(name,value); 
     53                        if(config.optionSource[name] == 'cookie') 
     54                                setOption(name,decodeCookie(value)); 
    2955                } 
    3056        } 
    3157} 
    3258 
    33 function saveOptionCookie(name) 
     59function loadSystemSettings() 
    3460{ 
     61        var settings = store.calcAllSlices("SystemSettings"); 
     62        for(var key in settings) { 
     63                var splitPos = key.indexOf('_'); 
     64                var name = key; 
     65                var source = "setting"; 
     66                if(splitPos !== -1) { 
     67                        source = key.substr(splitPos+1); 
     68                        name = key.substr(0,splitPos); 
     69                } 
     70                setOption(name,settings[key]); 
     71                config.optionSource[name] = source; 
     72        } 
     73} 
     74 
     75function onSystemSettingsChange() 
     76{ 
     77        if(!startingUp) { 
     78                loadSystemSettings(); 
     79        } 
     80} 
     81 
     82// Saves the named option in a cookie or SystemSettings as appropriate 
     83function saveOption(name) 
     84{ 
    3585        if(safeMode) 
    3686                return; 
    37         var c = name + "="; 
    38         var optType = name.substr(0,3); 
    39         if(config.optionHandlers[optType] && config.optionHandlers[optType].get) 
    40                 c += config.optionHandlers[optType].get(name); 
     87        if(config.optionSource[name] == 'cookie') { 
     88                if(store.getTiddlerSlice("SystemSettings",name + "_cookie") == getOption(name)) { 
     89                        removeCookie(name); 
     90                } else { 
     91                        saveCookie(name); 
     92                } 
     93        } else { 
     94                saveSystemSetting(name); 
     95        } 
     96} 
     97// Deprecated names for backwards compatibility 
     98var saveOptionCookie = saveOption; 
     99 
     100function saveCookie(name) 
     101{ 
     102        var c = name + "=" + encodeCookie(getOption(name)); 
    41103        c += "; expires=Fri, 1 Jan 2038 12:00:00 UTC; path=/"; 
    42104        document.cookie = c; 
    43105} 
    44106 
     107function removeCookie(name) 
     108{ 
     109        document.cookie = name + "=; expires=Thu, 01-Jan-1970 00:00:01 UTC; path=/;"; 
     110} 
     111 
     112function saveSystemSetting(name) 
     113{ 
     114        var settings = store.calcAllSlices("SystemSettings"); 
     115        var key; 
     116        for(key in config.options) { 
     117                if(config.optionSource[key] == "setting") { 
     118                        var value = getOption(key); 
     119                        value = value == null ? "" : value; 
     120                        if(settings[key] !== value) 
     121                                settings[key] = value; 
     122                } 
     123        } 
     124        var text = []; 
     125        for(key in settings) { 
     126                text.push("|''%0:''|%1|".format([key,settings[key]])); 
     127        } 
     128        text.sort(); 
     129        store.saveTiddler("SystemSettings","SystemSettings",text.join("\n"),"System",new Date()); 
     130        autoSaveChanges(); 
     131} 
     132 
     133//# Flatten cookies to ANSI character set by substituting html character entities for non-ANSI characters 
    45134function encodeCookie(s) 
    46135{ 
    47136        return escape(convertUnicodeToHtmlEntities(s)); 
    48137} 
    49138 
     139//# Decode any html character entities to their unicode equivalent 
    50140function decodeCookie(s) 
    51141{ 
    52142        s = unescape(s); 
     
    54144        return s.replace(re,function($0) {return String.fromCharCode(eval($0.replace(/[&#;]/g,"")));}); 
    55145} 
    56146 
    57  
    58147config.macros.option.genericCreate = function(place,type,opt,className,desc) 
    59148{ 
    60149        var typeInfo = config.macros.option.types[type]; 
  • js/Config.js

     
    6363        }; 
    6464config.optionsDesc = {}; 
    6565 
     66//# config.optionSource["chkAnimate"] can be: 
     67//#     cookie: the option gets stored in a cookie, with the default value coming from SystemSettings 
     68//#             volatile: the option isn't persisted at all, and reverts to the default specified in SystemSettings when the document is reloaded 
     69//#             setting: the option is stored in the SystemSettings tiddler 
     70//#     The default is "setting" 
     71config.optionSource = {}; 
     72 
    6673// Default tiddler templates 
    6774var DEFAULT_VIEW_TEMPLATE = 1; 
    6875var DEFAULT_EDIT_TEMPLATE = 2; 
  • js/Lingo.js

     
    462462        StyleSheetLayout: "This shadow tiddler contains CSS definitions related to the layout of page elements. ''DO NOT EDIT THIS TIDDLER'', instead make your changes in the StyleSheet shadow tiddler", 
    463463        StyleSheetLocale: "This shadow tiddler contains CSS definitions related to the translation locale", 
    464464        StyleSheetPrint: "This shadow tiddler contains CSS definitions for printing", 
     465        SystemSettings: "This tiddler is used to store configuration options for this TiddlyWiki document", 
    465466        TabAll: "This shadow tiddler contains the contents of the 'All' tab in the right-hand sidebar", 
    466467        TabMore: "This shadow tiddler contains the contents of the 'More' tab in the right-hand sidebar", 
    467468        TabMoreMissing: "This shadow tiddler contains the contents of the 'Missing' tab in the right-hand sidebar", 
     
    472473        ToolbarCommands: "This shadow tiddler determines which commands are shown in tiddler toolbars", 
    473474        ViewTemplate: "The HTML template in this shadow tiddler determines how tiddlers look" 
    474475        }); 
    475  
  • js/Refresh.js

     
    44 
    55//# List of notification functions to be called when certain tiddlers are changed or deleted 
    66config.notifyTiddlers = [ 
     7        {name: "SystemSettings", notify: onSystemSettingsChange}, 
    78        {name: "StyleSheetLayout", notify: refreshStyles}, 
    89        {name: "StyleSheetColors", notify: refreshStyles}, 
    910        {name: "StyleSheet", notify: refreshStyles},