From 881fb0d31551bff7ca7709b08469227eec2a63bf Mon Sep 17 00:00:00 2001 From: Michael Save Date: Sun, 22 Apr 2012 17:50:53 +1000 Subject: [PATCH] Slightly less crappy stylesheet chooser --- inc/config.php | 2 +- .../Twig/Extensions/Extension/Tinyboard.php | 3 +- stylesheets/default.css | 0 templates/index.html | 12 +- templates/main.js | 110 ++++++++++++------ templates/page.html | 14 +-- templates/thread.html | 12 +- 7 files changed, 94 insertions(+), 59 deletions(-) delete mode 100644 stylesheets/default.css diff --git a/inc/config.php b/inc/config.php index c160c38b..1e132466 100644 --- a/inc/config.php +++ b/inc/config.php @@ -525,7 +525,7 @@ // $config['banner_height'] = 100; // Custom stylesheets available. The prefix for each stylesheet URI is defined below. - $config['stylesheets']['Yotsuba B'] = 'default.css'; + $config['stylesheets']['Yotsuba B'] = ''; // default $config['stylesheets']['Yotsuba'] = 'yotsuba.css'; // $config['stylesheets']['Futaba'] = 'futaba.css'; diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php index 7e2149e5..b515e8d2 100644 --- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php +++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php @@ -21,7 +21,8 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension 'poster_id' => new Twig_Filter_Function('poster_id', array('needs_environment' => false)), 'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter', array('needs_environment' => false)), 'count' => new Twig_Filter_Function('count', array('needs_environment' => false)), - 'until' => new Twig_Filter_Function('until', array('needs_environment' => false)) + 'until' => new Twig_Filter_Function('until', array('needs_environment' => false)), + 'addslashes' => new Twig_Filter_Function('addslashes', array('needs_environment' => false)), ); } diff --git a/stylesheets/default.css b/stylesheets/default.css deleted file mode 100644 index e69de29b..00000000 diff --git a/templates/index.html b/templates/index.html index f89199aa..1e7403eb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,13 +1,13 @@ - - {% if config.url_favicon %}{% endif %} + + {% if config.url_favicon %}{% endif %} {{ board.url }} - {{ board.name }} - - - {% if config.meta_keywords %}{% endif %} - + + + {% if config.meta_keywords %}{% endif %} + {% if config.default_stylesheet.1 != '' %}{% endif %} {% if not nojavascript %} {% if not config.additional_javascript_compile %} diff --git a/templates/main.js b/templates/main.js index de62bd8b..b0a96393 100644 --- a/templates/main.js +++ b/templates/main.js @@ -1,12 +1,71 @@ {% raw %} -var selectedstyle = '{% endraw %}{{ config.default_stylesheet.0 }}{% raw %}'; -var styles = [ - {% endraw %}{% for stylesheet in stylesheets %}{% raw %}['{% endraw %}{{ stylesheet.name }}{% raw %}', '{% endraw %}{{ stylesheet.uri }}{% raw %}']{% endraw %}{% if not loop.last %}{% raw %}, - {% endraw %}{% endif %}{% endfor %}{% raw %} -]; var saved = {}; + +var selectedstyle = '{% endraw %}{{ config.default_stylesheet.0|addslashes }}{% raw %}'; +var styles = { + {% endraw %} + {% for stylesheet in stylesheets %}{% raw %}'{% endraw %}{{ stylesheet.name|addslashes }}{% raw %}' : '{% endraw %}{{ stylesheet.uri|addslashes }}{% raw %}', + {% endraw %}{% endfor %}{% raw %} +}; + +function changeStyle(styleName, link) { + localStorage.stylesheet = styleName; + + if (!document.getElementById('stylesheet')) { + var s = document.createElement('link'); + s.rel = 'stylesheet'; + s.type = 'text/css'; + s.id = 'stylesheet'; + var x = document.getElementsByTagName('head')[0]; + x.appendChild(s); + } + + document.getElementById('stylesheet').href = styles[styleName]; + selectedstyle = styleName; + + if (document.getElementsByClassName('styles').length != 0) { + var styleLinks = document.getElementsByClassName('styles')[0].childNodes; + for (i = 0; i < styleLinks.length; i++) { + styleLinks[i].className = ''; + } + } + + if (link) { + link.className = 'selected'; + } +} + +if (localStorage.stylesheet) { + for (styleName in styles) { + if (styleName == localStorage.stylesheet) { + changeStyle(styleName); + break; + } + } +} + +function init_stylechooser() { + var newElement = document.createElement('div'); + newElement.className = 'styles'; + + for (styleName in styles) { + var style = document.createElement('a'); + style.innerHTML = '[' + styleName + ']'; + style.onclick = function() { + changeStyle(this.innerHTML.substring(1, this.innerHTML.length - 1), this); + }; + if (styleName == selectedstyle) { + style.className = 'selected'; + } + style.href = 'javascript:void(0);'; + newElement.appendChild(style); + } + + document.getElementsByTagName('body')[0].insertBefore(newElement, document.getElementsByTagName('body')[0].lastChild.nextSibling); +} + function get_cookie(cookie_name) { var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); if (results) @@ -16,7 +75,7 @@ function get_cookie(cookie_name) { } function highlightReply(id) { - if(typeof window.event != "undefined" && event.which == 2) { + if (typeof window.event != "undefined" && event.which == 2) { // don't highlight on middle click return true; } @@ -57,6 +116,7 @@ function dopost(form) { return form.elements['body'].value != "" || form.elements['file'].value != ""; } + function citeReply(id) { var body = document.getElementById('body'); @@ -76,21 +136,6 @@ function citeReply(id) { } } -function changeStyle(x) { - localStorage.stylesheet = styles[x][1]; - document.getElementById('stylesheet').href = styles[x][1]; - selectedstyle = styles[x][0]; -} - -if(localStorage.stylesheet) { - for(var x = 0; x < styles.length ; x++) { - if(styles[x][1] == localStorage.stylesheet) { - changeStyle(x); - break; - } - } -} - function rememberStuff() { if (document.forms.post) { if (document.forms.post.password) { @@ -112,7 +157,7 @@ function rememberStuff() { if (get_cookie('{% endraw %}{{ config.cookies.js }}{% raw %}')) { // Remove successful posts var successful = JSON.parse(get_cookie('{% endraw %}{{ config.cookies.js }}{% raw %}')); - for(var url in successful) { + for (var url in successful) { saved[url] = null; } sessionStorage.body = JSON.stringify(saved); @@ -132,25 +177,13 @@ function rememberStuff() { } function init() { - var newElement = document.createElement('div'); - newElement.className = 'styles'; + init_stylechooser(); - for(x = 0; x < styles.length; x++) { - var style = document.createElement('a'); - style.innerHTML = '[' + styles[x][0] + ']'; - style.href = 'javascript:changeStyle(' + x + ');'; - if(selectedstyle == styles[x][0]) - style.className = 'selected'; - newElement.appendChild(style); - } - - document.getElementsByTagName('body')[0].insertBefore(newElement, document.getElementsByTagName('body')[0].lastChild.nextSibling) - - if(document.forms.postcontrols) { + if (document.forms.postcontrols) { document.forms.postcontrols.password.value = localStorage.password; } - if(window.location.hash.indexOf('q') != 1 && window.location.hash.substring(1)) + if (window.location.hash.indexOf('q') != 1 && window.location.hash.substring(1)) highlightReply(window.location.hash.substring(1)); } @@ -164,7 +197,7 @@ function onready(fnc) { } function ready() { - for(var i = 0; i < onready_callbacks.length; i++) { + for (var i = 0; i < onready_callbacks.length; i++) { onready_callbacks[i](); } } @@ -174,3 +207,4 @@ onready(init); {% endraw %}{% if config.google_analytics %}{% raw %} var _gaq = _gaq || [];_gaq.push(['_setAccount', '{% endraw %}{{ config.google_analytics }}{% raw %}']);{% endraw %}{% if config.google_analytics_domain %}{% raw %}_gaq.push(['_setDomainName', '{% endraw %}{{ config.google_analytics_domain }}{% raw %}']){% endraw %}{% endif %}{% if not config.google_analytics_domain %}{% raw %}_gaq.push(['_setDomainName', 'none']){% endraw %}{% endif %}{% raw %};_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();{% endraw %}{% endif %} + diff --git a/templates/page.html b/templates/page.html index 39d73bbd..57ae14ac 100644 --- a/templates/page.html +++ b/templates/page.html @@ -1,16 +1,16 @@ - - {% if config.url_favicon %}{% endif %} + + {% if config.url_favicon %}{% endif %} {{ title }} - - - + + + {% if config.default_stylesheet.1 != '' %}{% endif %} {% if not nojavascript %}{% endif %} - {% if pm %}
You have an unread PM{% if pm.waiting > 0 %}, plus {{ pm.waiting }} more waiting{% endif %}.

{% endif %} + {% if pm %}
You have an unread PM{% if pm.waiting > 0 %}, plus {{ pm.waiting }} more waiting{% endif %}.

{% endif %}

{{ title }}

@@ -21,7 +21,7 @@
{{ body }} -
+
diff --git a/templates/thread.html b/templates/thread.html index 2708b04d..f852018e 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -1,13 +1,13 @@ - - {% if config.url_favicon %}{% endif %} + + {% if config.url_favicon %}{% endif %} {{ board.url }} - {{ board.name }} - - - {% if config.meta_keywords %}{% endif %} - + + + {% if config.meta_keywords %}{% endif %} + {% if config.default_stylesheet.1 != '' %}{% endif %} {% if not nojavascript %} {% if not config.additional_javascript_compile %}