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.

444 lines
8.9 KiB

14 years ago
var CouchPotato = new Class({
Implements: [Events, Options],
14 years ago
defaults: {
page: 'wanted',
14 years ago
action: 'index',
params: {}
},
pages: [],
block: [],
14 years ago
setup: function(options) {
14 years ago
var self = this;
self.setOptions(options);
14 years ago
self.c = $(document.body)
self.route = new Route(self.defaults);
14 years ago
self.createLayout();
self.createPages();
if(window.location.hash)
History.handleInitialState();
else
self.openPage(window.location.pathname);
14 years ago
History.addEvent('change', self.openPage.bind(self));
self.c.addEvent('click:relay(a[href^=/]:not([target]))', self.pushState.bind(self));
},
14 years ago
getOption: function(name){
14 years ago
try {
return this.options[name];
}
catch(e){
return null
}
},
pushState: function(e){
var self = this;
if((!e.meta && Browser.Platform.mac) || (!e.control && !Browser.Platform.mac)){
(e).preventDefault();
var url = e.target.get('href');
if(History.getPath() != url)
History.push(url);
}
14 years ago
},
14 years ago
createLayout: function(){
var self = this;
14 years ago
self.block.header = new Block();
14 years ago
self.c.adopt(
14 years ago
$(self.block.header).addClass('header').adopt(
new Element('div').adopt(
self.block.navigation = new Block.Navigation(self, {}),
self.block.search = new Block.Search(self, {}),
self.block.more = new Block.Menu(self, {})
)
14 years ago
),
self.content = new Element('div.content'),
self.block.footer = new Block.Footer(self, {})
);
[new Element('a.orange', {
'text': 'Restart',
'events': {
'click': self.restartQA.bind(self)
}
}),
new Element('a.red', {
'text': 'Shutdown',
'events': {
'click': self.shutdownQA.bind(self)
}
}),
new Element('a', {
'text': 'Check for updates',
'events': {
'click': self.checkForUpdate.bind(self)
}
})].each(function(a){
self.block.more.addLink(a)
})
new ScrollSpy({
min: 10,
onLeave: function(){
$(self.block.header).removeClass('with_shadow')
},
onEnter: function(){
$(self.block.header).addClass('with_shadow')
}
})
14 years ago
},
createPages: function(){
14 years ago
var self = this;
Object.each(Page, function(page_class, class_name){
pg = new Page[class_name](self, {});
self.pages[class_name] = pg;
14 years ago
$(pg).inject(self.content);
});
self.fireEvent('load');
},
14 years ago
openPage: function(url) {
14 years ago
var self = this;
14 years ago
var current_url = url.replace(/^\/+|\/+$/g, '');
if(current_url == self.current_url)
return;
13 years ago
self.route.parse();
var page_name = self.route.getPage().capitalize();
var action = self.route.getAction();
var params = self.route.getParams();
14 years ago
if(self.current_page)
self.current_page.hide()
try {
var page = self.pages[page_name] || self.pages.Wanted;
page.open(action, params, current_url);
14 years ago
page.show();
}
catch(e){
14 years ago
console.error("Can't open page:" + url, e)
}
14 years ago
self.current_page = page;
14 years ago
self.current_url = current_url;
},
getBlock: function(block_name){
return this.block[block_name]
},
getPage: function(name){
return this.pages[name]
},
shutdown: function(){
var self = this;
self.blockPage('You have shutdown. This is what suppose to happen ;)');
Api.request('app.shutdown', {
'onComplete': self.blockPage.bind(self)
});
self.checkAvailable(1000);
},
shutdownQA: function(){
var self = this;
new Question('Are you sure you want to shutdown CouchPotato?', '', [{
'text': 'Shutdown',
'class': 'shutdown red',
'events': {
'click': function(e){
(e).preventDefault();
self.shutdown();
}
}
}, {
'text': 'No, nevah!',
'cancel': true
}]);
},
13 years ago
restart: function(message, title){
var self = this;
13 years ago
self.blockPage(message || 'Restarting... please wait. If this takes to long, something must have gone wrong.', title);
Api.request('app.restart');
self.checkAvailable(1000);
},
restartQA: function(message, title){
var self = this;
new Question('Are you sure you want to restart CouchPotato?', '', [{
'text': 'Restart',
'class': 'restart orange',
'events': {
'click': function(e){
(e).preventDefault();
self.restart(message, title);
}
}
}, {
'text': 'No, nevah!',
'cancel': true
}]);
},
checkForUpdate: function(func){
var self = this;
Updater.check(func)
self.blockPage('Please wait. If this takes to long, something must have gone wrong.', 'Checking for updates');
self.checkAvailable(3000);
},
checkAvailable: function(delay){
var self = this;
(function(){
Api.request('app.available', {
'onFailure': function(){
self.checkAvailable.delay(1000, self);
self.fireEvent('unload');
},
'onSuccess': function(){
self.unBlockPage();
}
});
}).delay(delay || 0)
},
blockPage: function(message, title){
var self = this;
var body = $(document.body);
self.mask = new Element('div.mask').adopt(
new Element('div').adopt(
new Element('h1', {'text': title || 'Unavailable'}),
new Element('div', {'text': message || 'Something must have crashed.. check the logs ;)'})
)
).fade('hide').inject(document.body).fade('in');
createSpinner(self.mask, {
'top': -50
});
},
unBlockPage: function(){
var self = this;
self.mask.get('tween').start('opacity', 0).chain(function(){
this.element.destroy()
});
14 years ago
},
createUrl: function(action, params){
return this.options.base_url + (action ? action+'/' : '') + (params ? '?'+Object.toQueryString(params) : '')
},
notify: function(options){
return this.growl.notify({
title: "this scrolls away",
text: "test - hello there. mouseover to pause away action"
});
}
});
window.App = new CouchPotato();
14 years ago
var Route = new Class({
defaults: {},
14 years ago
page: '',
action: 'index',
params: {},
initialize: function(defaults){
var self = this
self.defaults = defaults
},
13 years ago
parse: function(){
14 years ago
var self = this;
var path = History.getPath().replace(Api.getOption('url'), '/').replace(App.getOption('base_url'), '/')
14 years ago
var current = path.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 = Object.merge({}, 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
}
})
}
else if(url.length == 1){
self.params[url] = true;
}
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)
};
14 years ago
(function(){
var events;
var check = function(e) {
var target = $(e.target);
var parents = target.getParents();
events.each(function(item) {
var element = item.element;
if (element != target && !parents.contains(element))
item.fn.call(element, e);
});
};
Element.Events.outerClick = {
onAdd : function(fn) {
if (!events) {
document.addEvent('click', check);
events = [];
}
events.push( {
element : this,
fn : fn
});
},
onRemove : function(fn) {
events = events.filter(function(item) {
return item.element != this || item.fn != fn;
}, this);
if (!events.length) {
document.removeEvent('click', check);
events = null;
}
}
};
})();
14 years ago
function randomString(length, extra) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz" + (extra ? '-._!@#$%^&*()+=' : '');
var stringLength = length || 8;
var randomString = '';
for (var i = 0; i < stringLength; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomString += chars.charAt(rnum);
}
return randomString;
}
(function(){
var keyPaths = [];
var saveKeyPath = function(path) {
keyPaths.push({
sign: (path[0] === '+' || path[0] === '-')? parseInt(path.shift()+1) : 1,
path: path
});
};
var valueOf = function(object, path) {
var ptr = object;
path.each(function(key) { ptr = ptr[key] });
return ptr;
};
var comparer = function(a, b) {
for (var i = 0, l = keyPaths.length; i < l; i++) {
aVal = valueOf(a, keyPaths[i].path);
bVal = valueOf(b, keyPaths[i].path);
if (aVal > bVal) return keyPaths[i].sign;
if (aVal < bVal) return -keyPaths[i].sign;
}
return 0;
};
Array.implement('sortBy', function(){
keyPaths.empty();
Array.each(arguments, function(argument) {
switch (typeOf(argument)) {
case "array": saveKeyPath(argument); break;
case "string": saveKeyPath(argument.match(/[+-]|[^.]+/g)); break;
}
});
return this.sort(comparer);
});
14 years ago
})();
var createSpinner = function(target, options){
var opts = Object.merge({
lines: 12,
length: 5,
width: 4,
radius: 9,
color: '#fff',
speed: 1.9,
trail: 53,
shadow: false,
hwaccel: true,
className: 'spinner',
zIndex: 2e9,
top: 'auto',
left: 'auto'
}, options);
return new Spinner(opts).spin(target);
}