diff options
author | Joey Hess <joey@kodama.kitenet.net> | 2008-10-17 20:28:18 -0400 |
---|---|---|
committer | Joey Hess <joey@kodama.kitenet.net> | 2008-10-17 20:28:18 -0400 |
commit | 79b376f9912891a8748fcbb4580969e4dbf7fc75 (patch) | |
tree | 40c99e6f94ddb7e0c9ecea6a1a109e015061dd68 /IkiWiki/Plugin | |
parent | 16d51e67a7e7257739e1cb3f9a3713d448065e5d (diff) |
Add an underlay for javascript, and add ikiwiki.js containing some utility code.
* Add an underlay for javascript, and add ikiwiki.js containing some utility
code.
* toggle: Stop embedding the full toggle code on each page using it, and
move it to toggle.js in the javascript underlay.
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r-- | IkiWiki/Plugin/attachment.pm | 7 | ||||
-rw-r--r-- | IkiWiki/Plugin/toggle.pm | 69 |
2 files changed, 17 insertions, 59 deletions
diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm index dcac3e820..6c1df488a 100644 --- a/IkiWiki/Plugin/attachment.pm +++ b/IkiWiki/Plugin/attachment.pm @@ -6,6 +6,7 @@ use strict; use IkiWiki 2.00; sub import { #{{{ + add_underlay("javascript"); hook(type => "getsetup", id => "attachment", call => \&getsetup); hook(type => "checkconfig", id => "attachment", call => \&checkconfig); hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup); @@ -104,10 +105,10 @@ sub formbuilder_setup (@) { #{{{ $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />'); $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />'); - # Add the javascript from the toggle plugin; - # the attachments interface uses it to toggle visibility. + # Add the toggle javascript; the attachments interface uses + # it to toggle visibility. require IkiWiki::Plugin::toggle; - $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript); + $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}, 1)); # Start with the attachments interface toggled invisible, # but if it was used, keep it open. if ($form->submitted ne "Upload Attachment" && diff --git a/IkiWiki/Plugin/toggle.pm b/IkiWiki/Plugin/toggle.pm index 610d38e3a..657d8d3c2 100644 --- a/IkiWiki/Plugin/toggle.pm +++ b/IkiWiki/Plugin/toggle.pm @@ -5,61 +5,8 @@ use warnings; use strict; use IkiWiki 2.00; -# Here's the javascript that makes this possible. A key feature is the use -# of css to hide toggleables, to avoid any flashing on page load. The css -# is only emitted after the javascript tests that it's going to be able to -# show the toggleables. -our $javascript=<<'EOF'; -<script type="text/javascript"> -<!-- -if (document.getElementById && document.getElementsByTagName && document.createTextNode) { - document.write('<style type="text/css">div.toggleable { display: none; }</style>'); - window.onload = inittoggle; -} - -function inittoggle() { - var as = getElementsByClass('toggle'); - for (var i = 0; i < as.length; i++) { - var id = as[i].href.match(/#(\w.+)/)[1]; - if (document.getElementById(id).className == "toggleable") - document.getElementById(id).style.display="none"; - as[i].onclick = function() { - toggle(this); - return false; - } - } -} - -function toggle(s) { - var id = s.href.match(/#(\w.+)/)[1]; - style = document.getElementById(id).style; - if (style.display == "none") - style.display = "block"; - else - style.display = "none"; -} - -function getElementsByClass(cls, node, tag) { - if (document.getElementsByClass) - return document.getElementsByClass(cls, node, tag); - if (! node) node = document; - if (! tag) tag = '*'; - var ret = new Array(); - var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)"); - var els = node.getElementsByTagName(tag); - for (i = 0; i < els.length; i++) { - if ( pattern.test(els[i].className) ) { - ret.push(els[i]); - } - } - return ret; -} - -//--> -</script> -EOF - sub import { #{{{ + add_underlay("javascript"); hook(type => "getsetup", id => "toggle", call => \&getsetup); hook(type => "preprocess", id => "toggle", call => \&preprocess_toggle); @@ -121,12 +68,22 @@ sub format (@) { #{{{ if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) { $params{content}=~s/<div class="toggleableend">//g; - if (! ($params{content}=~s!^<body>!<body>$javascript!m)) { + if (! ($params{content}=~s!^(<body>)!$1.include_javascript($params{page})!em)) { # no </body> tag, probably in preview mode - $params{content}=$javascript.$params{content}; + $params{content}=include_javascript($params{page}, 1).$params{content}; } } return $params{content}; } # }}} +sub include_javascript ($;$) { #{{{ + my $page=shift; + my $absolute=shift; + + return '<script src="'.urlto("ikiwiki.js", $page, $absolute). + '" type="text/javascript" charset="utf-8"></script>'."\n". + '<script src="'.urlto("toggle.js", $page, $absolute). + '" type="text/javascript" charset="utf-8"></script>'; +} #}}} + 1 |