Files
node/node_modules/derby-examples/gallery/lib/app/index.js
2012-05-30 23:00:06 -04:00

164 lines
4.3 KiB
JavaScript

// Generated by CoffeeScript 1.3.1
var app, fillDimension, get, lastLoad, parseUrl, photoDimension, photoDimensionFn, photoSrc, preCache, ready, view, _ref;
parseUrl = require('url').parse;
_ref = app = require('derby').createApp(module), get = _ref.get, view = _ref.view, ready = _ref.ready;
fillDimension = function(max, side, otherSide) {
if (side >= otherSide) {
return max;
} else {
return side * max / otherSide;
}
};
photoDimension = function(obj, source, size, type) {
var otherSide, side;
if (size == null) {
size = 's';
}
if (source === 'flickr') {
if (type === 'width') {
side = obj.o_width;
otherSide = obj.o_height;
} else {
side = obj.o_height;
otherSide = obj.o_width;
}
if (size === 's') {
return 75;
}
if (size === 'm') {
return fillDimension(240, side, otherSide);
}
}
};
photoDimensionFn = function(type) {
return function(obj, source, size, scale) {
if (scale == null) {
scale = 1;
}
if (!obj) {
return;
}
return Math.round(photoDimension(obj, source, size, type) * scale);
};
};
photoSrc = function(obj, source, size) {
if (size == null) {
size = 's';
}
if (!obj) {
return;
}
if (source === 'flickr') {
return ("http://farm" + obj.farm + ".staticflickr.com/") + ("" + obj.server + "/" + obj.id + "_" + obj.secret + "_" + size + ".jpg");
}
};
view.fn('photoWidth', photoDimensionFn('width'));
view.fn('photoHeight', photoDimensionFn('height'));
view.fn('photoSrc', photoSrc);
preCache = function(obj, source, callback) {
var cancelled, count, finish, img, src, wait;
src = photoSrc(obj, source, 'm');
img = document.createElement('img');
img.style.display = 'none';
img.src = src;
cancelled = false;
callback.cancel = function() {
return cancelled = true;
};
finish = function() {
document.body.removeChild(img);
if (!cancelled) {
return callback();
}
};
count = 2;
wait = function() {
return --count || finish();
};
img.onload = wait;
setTimeout(wait, 50);
return document.body.appendChild(img);
};
get('/:source/:type/:id/:image?', function(page, model, params, next) {
var id, image, pageIndex, query, search, source, type;
source = params.source, type = params.type, id = params.id, image = params.image, query = params.query;
search = parseUrl(params.url).search;
if (source !== 'flickr') {
next();
}
pageIndex = query.page ? query.page - 1 : 0;
return model.fetch("" + source + "." + type + ".id_" + id + ".photos.pages." + pageIndex, function(err, photos) {
model.ref('_pages', photos.parent());
model.ref('_page', '_pages', '_selectedPage');
model.set('_toggle', 0);
model.set('_fade0', 1);
model.set('_fade1', 0);
model.ref('_image0', '_page', '_selected0');
model.ref('_image1', '_page', '_selected1');
model.set('_selectedPage', pageIndex);
model.set('_selected0', image);
return page.render({
source: source,
search: search
});
});
});
lastLoad = null;
get({
from: '/:source/:type/:id/:image?',
to: '/:source/:type/:id/:image?'
}, function(model, params, next) {
var id, image, pageIndex, query, source, type;
source = params.source, type = params.type, id = params.id, image = params.image, query = params.query;
if (source !== 'flickr') {
next();
}
pageIndex = query.page ? query.page - 1 : 0;
model.set('_selectedPage', pageIndex);
if (lastLoad) {
lastLoad.cancel();
}
lastLoad = function() {
var toggleValue;
lastLoad = null;
toggleValue = +(!model.get('_toggle'));
model.set('_toggle', toggleValue);
model.set('_selected' + toggleValue, image);
return model.set('_fade' + toggleValue, 1);
};
return preCache(model.get('_page.' + image), source, lastLoad);
});
ready(function(model) {
app.select = function(e, el) {
var url;
model.set('_fade' + model.get('_toggle'), 0);
url = model.at(el).leaf() + window.location.search;
return view.history.push(url);
};
model.set('_showReconnect', true);
app.connect = function() {
model.set('_showReconnect', false);
setTimeout((function() {
return model.set('_showReconnect', true);
}), 1000);
return model.socket.socket.connect();
};
return app.reload = function() {
return window.location.reload();
};
});