Skip to content

Commit

Permalink
use hashes instead of urls for effects
Browse files Browse the repository at this point in the history
  • Loading branch information
jfontan committed Dec 9, 2011
1 parent 2f50376 commit 7f8add1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions server/assets/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@

<div id="header">
<a href="http://glsl.heroku.com/"><h1>GLSL Sandbox</h1></a>
<a href="/new">Create new effect!</a> &nbsp;&nbsp;/&nbsp;
<a href="/e">Create new effect!</a> &nbsp;&nbsp;/&nbsp;
<a href="https://github.com/mrdoob/glsl-sandbox">github</a> &nbsp;&nbsp;/&nbsp;
gallery by <a href="http://twitter.com/thevaw">@thevaw</a> and <a href="http://twitter.com/feiss">@feiss</a> &nbsp;/&nbsp; editor by <a href="http://twitter.com/mrdoob">@mrdoob</a>, <a href="http://twitter.com/mrkishi">@mrkishi</a>, <a href="http://twitter.com/p01">@p01</a>, <a href="http://twitter.com/alteredq">@alteredq</a> and <a href="http://twitter.com/kusmabite">@kusmabite</a>
</div>

<div id="gallery">

<% effects.each do |effect| %>
<a href='/<%= effect['_id'] %>/<%= effect['versions'].length-1 %>'><img src='<%= effect['image'] %>'></a>
<a href='/e#<%= effect['_id'] %>.<%= effect['versions'].length-1 %>'><img src='<%= effect['image'] %>'></a>
<% end %>

</div>
Expand Down
11 changes: 6 additions & 5 deletions server/assets/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function am_i_owner() {
}

function load_url_code() {
if ( window.location.pathname!='/' && window.location.pathname!='/new') {
if ( window.location.hash!='') {

load_code(window.location.pathname.substr(1));
load_code(window.location.hash.substr(1));

} else {

Expand All @@ -48,6 +48,7 @@ function add_server_buttons() {
parentButton.textContent = 'parent';
parentButton.addEventListener( 'click', function() {
location.href = original_version;
load_url_code();
}, false );
toolbar.appendChild( parentButton );

Expand Down Expand Up @@ -96,18 +97,18 @@ function save() {
"user": get_user_id()
}

loc='/new';
loc='/e';

if(am_i_owner())
loc=window.location.href;
data["code_id"]=window.location.hash.substr(1);
else {
data["parent"]=window.location.pathname;
}

$.post(loc,
JSON.stringify(data),
function(result) {
window.location.replace('/'+result);
window.location.replace('/e#'+result);
}, "text");
}

Expand Down
14 changes: 12 additions & 2 deletions server/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require 'pp'

configure do
set :public_folder, 'public'
set :public_folder, 'server/assets'

GALLERY=ERB.new(File.read('server/assets/gallery.html'))

Expand All @@ -39,6 +39,10 @@
send_file 'static/index.html'
end

get '/e' do
send_file 'static/index.html'
end

get %r{^.*/js/jquery.js$} do
send_file 'server/assets/jquery.js'
end
Expand Down Expand Up @@ -71,7 +75,7 @@
send_file 'static/index.html'
end

get %r{/item/(\d+)(/(\d+))?} do
get %r{/item/(\d+)([/.](\d+))?} do
code_id=params[:captures][0].to_i
if params[:captures][1]
version_id=params[:captures][2].to_i
Expand Down Expand Up @@ -102,5 +106,11 @@
"#{code_id}/#{version}"
end

post '/e' do
body=request.body.read
$glsl.save_effect(body)
end




29 changes: 23 additions & 6 deletions server/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ def save_version(code_id, code)
'$push' => { :versions => data }
}
})

code=$glsl.get_code(code_id)

version=code['versions'].length-1

"#{code_id}.#{version}"
end

def save_effect(code)
code_data=JSON.parse(code)

if code_data['code_id'] && !code_data['code_id'].empty?
m=code_data['code_id'].match(/^(\d+)(\.\d+)?/)
if m
save_version(m[1].to_i, code)
else
save_new_effect(code)
end
else
save_new_effect(code)
end
end

def save_new_effect(code)
Expand All @@ -89,13 +110,9 @@ def save_new_effect(code)
data[:parent_version] = m[3].to_i if m[3]
end

pp data

@code.insert(data)

save_version(counter, code)

"#{counter}/0"
end

def get_page(page=0, effects_per_page=50)
Expand Down Expand Up @@ -137,8 +154,8 @@ def get_code_json(id, version=nil)

parent=nil
if code['parent']
parent="/#{code['parent']}"
parent+="/#{code['parent_version']}" if code['parent_version']
parent="/e##{code['parent']}"
parent+=".#{code['parent_version']}" if code['parent_version']
end
else
item=nil
Expand Down

0 comments on commit 7f8add1

Please sign in to comment.