You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
4.0 KiB

14 years ago
var CouchPotato = new Class({
Implements: [Options],
defaults: {
page: 'movie',
action: 'index',
params: {}
},
pages: [],
tabs: [
{'href': '/movie/', 'title':'Gimmy gimmy gimmy!', 'label':'Wanted'},
{'href': '/manage/', 'title':'Do stuff to your existing movies!', 'label':'Manage'},
{'href': '/feed/', 'title':'Which wanted movies are released soon?', 'label':'Soon'},
{'href': '/log/', 'title':'Show recent logs.', 'class':'logLink', 'label':'Logs'},
{'href': '/config/', 'title':'Change settings.', 'id':'showConfig'}
14 years ago
],
initialize: function(options) {
var self = this;
self.setOptions(options);
14 years ago
self.c = $(document.body)
self.route = new Route(self.defaults);
self.api = new Api(self.options.api_url)
14 years ago
History.addEvent('change', self.createPage.bind(self));
History.handleInitialState();
14 years ago
self.createLayout()
self.createNavigation()
self.c.addEvent('click:relay(a)', self.openPage.bind(self))
},
openPage: function(e){
var self = this;
(e).stop()
var url = e.target.get('href')
History.push(url)
},
createNavigation: function(){
var self = this
self.tabs.each(function(tab){
new Element('li').adopt(
new Element('a', {
'href': tab.href,
'title': tab.title,
'text': tab.label
})
).inject(self.navigation)
})
14 years ago
},
14 years ago
createLayout: function(){
var self = this;
14 years ago
self.c.adopt(
self.header = new Element('div.header').adopt(
self.navigation = new Element('ul.navigation'),
self.add_form = new Element('div.add_form')
),
self.content = new Element('div.content'),
self.footer = new Element('div.footer')
)
},
createPage: function(url) {
var self = this;
self.route.parse(url);
var page_name = self.route.getPage().capitalize();
14 years ago
var action = self.route.getAction();
var params = self.route.getParams();
var pg = self.pages[page_name]
if(!pg){
pg = new Page[page_name]();
pg.setParent(self)
self.pages[page_name] = pg;
14 years ago
}
pg.open(action, params)
},
14 years ago
getApi: function(){
return this.api
14 years ago
}
});
var PageBase = new Class({
Implements: [Options],
initialize: function(options) {
},
open: function(action, params){
var self = this;
p('Opening: ' +self.getName() + ', ' + action + ', ' + Object.toQueryString(params));
try {
self[action+'Action'](params)
}
catch (e){
self.errorAction(e)
}
},
14 years ago
errorAction: function(e){
p('Error, action not found', e);
14 years ago
},
getName: function(){
return this.name
},
setParent: function(parent){
this.parent = parent
},
getParent: function(){
return this.parent
},
api: function(){
return this.parent.getApi()
}
});
var Api = new Class({
url: '',
initialize: function(url){
var self = this
self.url = url
self.req = new Request.JSON({
'method': 'get'
})
},
request: function(type, params, data){
var self = this;
self.req.setOptions({
'url': self.createUrl(type, params),
'data': data
})
self.req.send()
},
createUrl: function(action, params){
return this.url + (action || 'default') + '/?' + Object.toQueryString(params)
14 years ago
}
14 years ago
});
var Route = new Class({
defaults: {},
14 years ago
page: '',
action: 'index',
params: {},
initialize: function(defaults){
var self = this
self.defaults = defaults
},
parse: function(url_string){
14 years ago
var self = this;
var current = History.getPath().replace(/^\/+|\/+$/g, '')
var url = current.split('/')
self.page = (url.length > 0) ? url.shift() : self.defaults.page
self.action = (url.length > 0) ? url.shift() : self.defaults.action
self.params = self.defaults.params
if(url.length > 1){
var key
url.each(function(el, nr){
if(nr%2 == 0)
key = el
else if(key) {
self.params[key] = el
key = null
}
})
}
14 years ago
return self
},
getPage: function(){
return this.page
},
getAction: function(){
return this.action
},
getParams: function(){
return this.params
},
get: function(param){
return this.params[param]
}
});
var p = function(){
if(typeof(console) !== 'undefined' && console != null)
console.log(arguments)
}