| 1 | package Plack::Middleware::AutoReloadCSS; | ||
| 2 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Plack::Util (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Plack::Util::Accessor qw(interval); | ||
| 8 | |||
| 9 | use parent qw(Plack::Middleware); | ||
| 10 | our $VERSION = '0.02'; | ||
| 11 | |||
| 12 | sub call { | ||
| 13 | my($self, $env) = @_; | ||
| 14 | |||
| 15 | $self->response_cb($self->app->($env), sub { | ||
| 16 | my $res = shift; | ||
| 17 | my $content_type = Plack::Util::header_get($res->[1], 'Content-Type'); | ||
| 18 | |||
| 19 | if (!Plack::Util::status_with_no_entity_body($res->[0]) && | ||
| 20 | (($content_type || '') =~ m{^(?:text/html|application/xhtml\+xml)})) { | ||
| 21 | return sub { | ||
| 22 | my $chunk = shift; | ||
| 23 | return if !defined $chunk; | ||
| 24 | |||
| 25 | $chunk =~ s{</body>}{$self->auto_reload_script . '</body>'}ei; | ||
| 26 | $chunk; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | }); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub auto_reload_script { | ||
| 33 | my $self = shift; | ||
| 34 | my $interval = defined $self->interval ? $self->interval : 1000; | ||
| 35 | |||
| 36 | return <<"SCRIPT"; | ||
| 37 | <script type="text/javascript"> | ||
| 38 | |||
| 39 | // CSS auto-reload (c) Nikita Vasilyev | ||
| 40 | // http://nv.github.com/css_auto-reload/ | ||
| 41 | |||
| 42 | CSSStyleSheet.prototype.reload = function reload(){ | ||
| 43 | // Reload one stylesheet | ||
| 44 | // usage: document.styleSheets[0].reload() | ||
| 45 | // return: URI of stylesheet if it could be reloaded, overwise undefined | ||
| 46 | if (this.href) { | ||
| 47 | var href = this.href; | ||
| 48 | var i = href.indexOf('?'), | ||
| 49 | last_reload = 'last_reload=' + (new Date).getTime(); | ||
| 50 | if (i < 0) { | ||
| 51 | href += '?' + last_reload; | ||
| 52 | } else if (href.indexOf('last_reload=', i) < 0) { | ||
| 53 | href += '&' + last_reload; | ||
| 54 | } else { | ||
| 55 | href = href.replace(/last_reload=\\d+/, last_reload); | ||
| 56 | } | ||
| 57 | return this.ownerNode.href = href; | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | StyleSheetList.prototype.reload = function reload(){ | ||
| 62 | // Reload all stylesheets | ||
| 63 | // usage: document.styleSheets.reload() | ||
| 64 | for (var i=0; i<this.length; i++) { | ||
| 65 | this[i].reload() | ||
| 66 | } | ||
| 67 | }; | ||
| 68 | |||
| 69 | StyleSheetList.prototype.start_autoreload = function start_autoreload(miliseconds /*Number*/){ | ||
| 70 | // usage: document.styleSheets.start_autoreload() | ||
| 71 | if (!start_autoreload.running) { | ||
| 72 | var styles = this; | ||
| 73 | start_autoreload.running = setInterval(function reloading(){ | ||
| 74 | styles.reload(); | ||
| 75 | }, miliseconds || this.reload_interval); | ||
| 76 | } | ||
| 77 | return start_autoreload.running; | ||
| 78 | }; | ||
| 79 | |||
| 80 | StyleSheetList.prototype.stop_autoreload = function stop_autoreload(){ | ||
| 81 | // usage: document.styleSheets.stop_autoreload() | ||
| 82 | clearInterval(this.start_autoreload.running); | ||
| 83 | this.start_autoreload.running = null; | ||
| 84 | }; | ||
| 85 | |||
| 86 | StyleSheetList.prototype.toggle_autoreload = function toggle_autoreload(){ | ||
| 87 | // usage: document.styleSheets.toggle_autoreload() | ||
| 88 | return this.start_autoreload.running ? this.stop_autoreload() : this.start_autoreload(); | ||
| 89 | }; | ||
| 90 | |||
| 91 | document.styleSheets.start_autoreload($interval); | ||
| 92 | </script> | ||
| 93 | SCRIPT | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | __END__ | ||