Ticket #756: bakedoptions3.patch

File bakedoptions3.patch, 10.1 KB (added by FND, 3 years ago)

Revised patch that packs all the options into a single cookie 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 
     3chkAutoSave: false 
     4chkBackstage_cookie: false 
     5chkCaseSensitiveSearch: false 
     6chkConfirmDelete: true 
     7chkDisplayInstrumentation: false 
     8chkForceMinorUpdate: false 
     9chkGenerateAnRssFeed: false 
     10chkHttpReadOnly: true 
     11chkIncrementalSearch: true 
     12chkInsertTabs: false 
     13chkOpenInNewWindow: true 
     14chkRegExpSearch: false 
     15chkSaveBackups: true 
     16chkSaveEmptyTemplate: false 
     17chkSideBarTabs_cookie: false 
     18chkSliderOptionsPanel_cookie: false 
     19chkToggleLinks: false 
     20chkUsePreForStorage: true 
     21txtBackupFolder:  
     22txtEditorFocus: text 
     23txtFileSystemCharSet: UTF-8 
     24txtMainTab_cookie: Timeline 
     25txtMaxEditRows: 30 
     26txtMoreTab_cookie: Shadow 
     27txtTheme:  
     28txtUserName: 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; 
    20         var cookies = document.cookie.split(";"); 
    21         for(var c=0; c<cookies.length; c++) { 
    22                 var p = cookies[c].indexOf("="); 
     38        loadSystemSettings(); 
     39        loadCookies(); 
     40} 
     41// Deprecated name for backwards compatibility 
     42var loadOptionsCookie = initialiseOptions; 
     43 
     44function getCookies() 
     45{ 
     46        var cookieList = document.cookie.split(";"); 
     47        var cookies = {}; 
     48        for(var c=0; c<cookieList.length; c++) { 
     49                var p = cookieList[c].indexOf("="); 
    2350                if(p != -1) { 
    24                         var name = cookies[c].substr(0,p).trim(); 
    25                         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); 
     51                        var name = cookieList[c].substr(0,p).trim(); 
     52                        var value = cookieList[c].substr(p+1).trim() 
     53                        cookies[name] = decodeCookie(value); 
    2954                } 
    3055        } 
     56        return cookies; 
    3157} 
    3258 
    33 function saveOptionCookie(name) 
     59function loadCookies() 
    3460{ 
     61        var cookies = getCookies(); 
     62        if(cookies["TiddlyWiki"]) { 
     63                cookies = cookies["TiddlyWiki"].decodeHashMap(); 
     64        } 
     65        for(var n in cookies) { 
     66                if(config.optionSource[n] == 'cookie') 
     67                        setOption(n,cookies[n]); 
     68        } 
     69} 
     70 
     71function loadSystemSettings() 
     72{ 
     73        var settings = store.calcAllSlices("SystemSettings"); 
     74        for(var key in settings) { 
     75                var splitPos = key.indexOf('_'); 
     76                var name = key; 
     77                var source = "setting"; 
     78                if(splitPos !== -1) { 
     79                        source = key.substr(splitPos+1); 
     80                        name = key.substr(0,splitPos); 
     81                } 
     82                setOption(name,settings[key]); 
     83                config.optionSource[name] = source; 
     84        } 
     85} 
     86 
     87function onSystemSettingsChange() 
     88{ 
     89        if(!startingUp) { 
     90                loadSystemSettings(); 
     91        } 
     92} 
     93 
     94// Saves the named option in a cookie or SystemSettings as appropriate 
     95function saveOption(name) 
     96{ 
    3597        if(safeMode) 
    3698                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); 
    41         c += "; expires=Fri, 1 Jan 2038 12:00:00 UTC; path=/"; 
     99        if(config.optionSource[name] == 'cookie') { 
     100                saveCookie(name); 
     101        } else { 
     102                saveSystemSetting(name); 
     103        } 
     104} 
     105// Deprecated names for backwards compatibility 
     106var saveOptionCookie = saveOption; 
     107 
     108function saveCookie(name) 
     109{ 
     110        var cookies = {}; 
     111        for(var key in config.options) { 
     112                if(config.optionSource[key] == "cookie") { 
     113                        var value = getOption(key); 
     114                        value = value == null ? "" : value; 
     115                        cookies[key] = value; 
     116                } 
     117        } 
     118        var c = "TiddlyWiki=" + encodeCookie(String.encodeHashMap(cookies)) + "; expires=Fri, 1 Jan 2038 12:00:00 UTC; path=/"; 
    42119        document.cookie = c; 
     120        var cookies = getCookies(); 
     121        for(var c in cookies) { 
     122                if(c != "TiddlyWiki") 
     123                        removeCookie(c); 
     124        } 
    43125} 
    44126 
     127function removeCookie(name) 
     128{ 
     129        document.cookie = name + "=; expires=Thu, 01-Jan-1970 00:00:01 UTC; path=/;"; 
     130} 
     131 
     132function saveSystemSetting(name) 
     133{ 
     134        var settings = store.calcAllSlices("SystemSettings"); 
     135        var key; 
     136        for(key in config.options) { 
     137                if(config.optionSource[key] == undefined || config.optionSource[key] == "setting") { 
     138                        var value = getOption(key); 
     139                        value = value == null ? "" : value; 
     140                        if(settings[key] !== value) 
     141                                settings[key] = value; 
     142                } 
     143        } 
     144        var text = []; 
     145        for(key in settings) { 
     146                text.push("%0: %1".format([key,settings[key]])); 
     147        } 
     148        text.sort(); 
     149        store.saveTiddler("SystemSettings","SystemSettings",text.join("\n"),"System",new Date()); 
     150        autoSaveChanges(); 
     151} 
     152 
     153//# Flatten cookies to ANSI character set by substituting html character entities for non-ANSI characters 
    45154function encodeCookie(s) 
    46155{ 
    47156        return escape(convertUnicodeToHtmlEntities(s)); 
    48157} 
    49158 
     159//# Decode any html character entities to their unicode equivalent 
    50160function decodeCookie(s) 
    51161{ 
    52162        s = unescape(s); 
     
    54164        return s.replace(re,function($0) {return String.fromCharCode(eval($0.replace(/[&#;]/g,"")));}); 
    55165} 
    56166 
    57  
    58167config.macros.option.genericCreate = function(place,type,opt,className,desc) 
    59168{ 
    60169        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},