Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/frank/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,13 @@ def self.new(&block)

builder = Rack::Builder.new do
use Frank::Middleware::Statik, :root => Frank.static_folder
use Frank::Middleware::Refresh, :watch => [ Frank.dynamic_folder, Frank.static_folder, Frank.layouts_folder ]
watch_dirs = [ Frank.dynamic_folder, Frank.static_folder, Frank.layouts_folder ]
unless ENV['FRANK_REFRESH_DIRS'].nil?
watch_dirs.concat ENV['FRANK_REFRESH_DIRS'].split(/,/) unless ENV['FRANK_REFRESH_DIRS'].nil?
watch_dirs.uniq!
puts "Watching directories #{watch_dirs.join(', ')}"
end
use Frank::Middleware::Refresh, :watch => watch_dirs
run base
end

Expand Down
25 changes: 23 additions & 2 deletions lib/frank/template_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,30 @@ def refresh
<script type="text/javascript">
(function(){
var when = #{Time.now.to_i};
var loading = false
function addLoadingIndicator() {
var body = document.getElementsByTagName("body")[0]
var div = document.createElement('div')
div.innerHTML = '<p style="margin: 2px 0; padding: 2px 0"><div style="cursor: pointer; background: #f00; color: #faa;' +
'float: right; margin-right: 5px;width: 1em; height; 1ex"' +
'onclick="this.parentNode.style.display=\\'none\\'; return false">x</div>' +
'Frank has detected changes at ' + new Date() +
'</p><p style="margin: 2px 0; padding: 2px 0">Reloading. Please wait ...</p>'
div.setAttribute('style', 'z-index: 1000; position: fixed; top: 0; right: 40%;width: 460px; height: 110px;' +
'background: #308030; color: white; opacity: 0.8; ' +
'font-family: sans; font-weight: bold; font-size: 16px;' +
'text-align: center; padding: 2px 5px 5px 5px' +
'border: 10px solid #104010')
body.appendChild(div)
}

function process( raw ){
if( parseInt(raw) > when ) {
window.location.reload();
if (!loading) {
loading = true
addLoadingIndicator();
window.location.reload();
}
}
}

Expand All @@ -65,7 +85,8 @@ def refresh
};
req.open('GET', '/__refresh__', true);
req.send( null );
setTimeout( arguments.callee, 1000 );
if (!loading && !window.stop_refresh_check)
setTimeout( arguments.callee, 1000 );
})();

})();
Expand Down
10 changes: 10 additions & 0 deletions spec/template_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def app
template = @app.render('refresh.haml')
template.should include("<script type=\"text/javascript\">\n (function(){")
end

it 'should have provision for a loading indicator in the refresh javascript' do
template = @app.render('refresh.haml')
template.should include("addLoadingIndicator()")
end

it 'should have provision for stopping automatic refresh in the refresh javascript' do
template = @app.render('refresh.haml')
template.should include("!window.stop_refresh_check")
end

it 'renders content_for haml in the layout' do
template = @app.render('content_for_haml.haml')
Expand Down