Permalink
Browse files

more js automation, hash routes, redirect..

  • Loading branch information...
1 parent 67c7dff commit 32eb29e5eaed74c846a39d1ed4dde58f39b5758e cloudhead committed May 2, 2009
Showing with 73 additions and 95 deletions.
  1. +73 −95 htdocs/js/melon.js
View
168 htdocs/js/melon.js
@@ -1,86 +1,36 @@
-var Melon = {
- get: function( url, callback )
- {
- var buffer = [], i = 0;
-
- if( arguments.length == 4 ) {
- i = arguments[ 2 ]; buffer = arguments[ 3 ];
- }
-
- if( i in url ) $.getJSON( url[ i ], function( json ) {
- buffer[ i ] = json;
- Melon.get( url, callback, ++i, buffer );
- });
- else callback( buffer );
- }
-};
-
$( document ).ready( function() {
- var Path = {
- s: window.location.pathname.toString(),
- id: window.location.pathname.replace(/\//g, '-').replace(/^-/, '')
- };
+ var path = window.location.pathname.toString();
/* Specify routes */
- Router.map({ from: "/#/#/#/$", to: ["/posts/$1-$2-$3-$4",
- "/comments/$1-$2-$3-$4"], page: "single" },
- { from: "/#/#/#", to: "/posts/archive/$1-$2-$3" },
- { from: "/", to: "/posts/frontpage", page: "home"},
- { from: "/auth", to: "/login" },
- { from: "/*", to: "/posts/frontpage" });
+ Router.map({ from: "/#/#/#/$", to: { posts: "/posts/$1-$2-$3-$4",
+ comments: "/comments/$1-$2-$3-$4" }, page: "single" },
+ { from: "/#/#/#", to: { posts: "/posts/archive/$1-$2-$3"}, page: "archives" },
+ { from: "*", to: { posts: "/posts/frontpage" }, page: "home" }); // default
- var route = Router.match( Path.s );
-
- Melon.get( route.path, function( json ) {
- if( route.page ) $("body").attr("id", route.page ); // Set the body id
- if( json.length == 1 ) json = json[ 0 ];
- Pages[ route.page ]( json );
+ var route = Router.match( path );
+
+ if( route.page ) $("body").attr("id", route.page ); // Set the body id
+ $.each( route.path, function( id, uri ) {
+ Melon.get( uri, function( json ) {
+ if( $.isArray( json ) ) {
+ $.each( json, function() {
+ Pages[ route.page ]( Melon.clone( id, this ), this );
+ });
+ }
+ else {
+ Pages[ route.page ]( Melon.clone( id, json ), json );
+ }
+ Melon.cleanup( id );
+ });
});
-
- // for( var i = 0; i < route.path.length; i++ ) {
- // $.getJSON( route.path[i], function(json) {
- // Pages[ route.]
- // });
- // }
});
-function clone( name, input )
-{
- var element = $("#" + name + " ul > li:first").clone().appendTo("#" + name + " ul");
- element.attr("id", input.id);
-
- for( var key in input ) {
- element.find('.' + key ).html( input[ key ] );
- }
- return element;
-}
+
var Pages = {
- home: function( input )
+ home: function( selector, input )
{
- for( var i = 0; i < input.length; i++ ) {
- /* 2009-24-12-the-article => 2009/24/12/the-article */
- var date = input[ i ].id.slice( 0, 11 ).replace( /-/g, '/');
- var title = input[ i ].id.slice( 11 );
-
- clone("posts", input );
- post( input[ i ] ).find("h1").wrapInner("<a href='/" + date + title + "'></a>");
- }
- $("#posts ul > li:first").remove();
+ selector.find("h1").wrapInner("<a href='" + input.uri + "'></a>");
},
- single: function( input )
- {
- var posts = input[ 0 ];
- var comments = input[ 1 ];
-
- /* Posts */
- clone( "posts", posts );
-
- /* Comments */
- for( var i = 0; i < comments.length; i++ ) {
- var comment = clone("comments", comments);
- }
- $("#comments ul > li:first").remove(); // automate this
- $("#posts ul > li:first").remove(); //
- }
+ single: function( selector, input ) {}
};
var Router = {
map: function()
@@ -105,33 +55,61 @@ var Router = {
"\\/?$");
if( path.match( route ) ) {
- if( $.isArray( to ) ) {
- to = to.map( function( s ) {
- return path.replace( route, s ) + ".json";
- });
- }
- else {
- to = [ path.replace( route, to ) + ".json"];
- }
+ $.each( to, function( k, v ) {
+ to[ k ] = path.replace( route, v );
+ });
return { path: to, page: page };
}
}
- return false; // If nothing matched, we return false
- }
+ return false; // nothing was matched!
+ },
routes: []
};
+var Melon = {
+ get: function( path, callback )
+ {
+ $.getJSON( path + '.json', function( json ) {
+ if( json.redirect ) {
+ window.location = json.redirect;
+ }
+ else callback( json );
+ });
+ },
+ mget: function( path, callback ) // Recursive get
+ {
+ var buffer = [], i = 0;
+ if( arguments.length == 4 ) {
+ i = arguments[ 2 ]; buffer = arguments[ 3 ];
+ }
+
+ if( i in path ) $.getJSON( path[ i ], function( json ) {
+ buffer[ i ] = json;
+ Melon.get( path, callback, ++i, buffer );
+ });
+ else callback( buffer );
+ },
+ post: function( url, callback )
+ {
+
+ },
+ clone: function( id, input )
+ {
+ var element = $("#" + id + " ul > li:first").clone().appendTo("#" + id + " ul").attr("id", input.id );
+
+ for( var key in input ) { // $.each?
+ element.find('.' + key ).html( input[ key ] );
+ }
+ return element;
+ },
+ cleanup: function( id )
+ {
+ return $("#" + id + " ul > li:first").remove();
+ }
+};
-function action( caption, controller, action, key )
-{
- return "<a href='/" + controller + "/" + action + "/'>" + caption + "</a>";
-}
-function post( data )
+function action( caption, module, action, key )
{
- var post = $("#posts ul > li:last");
-
- for( var key in data ) {
- post.find('.' + key ).html( data[ key ] );
- }
- return post;
+ return "<a href='/" + module + "/" + action + "/'>" + caption + "</a>";
}
+

0 comments on commit 32eb29e

Please sign in to comment.