Skip to content

Commit

Permalink
adding to projects
Browse files Browse the repository at this point in the history
  • Loading branch information
bvallelunga committed Apr 27, 2014
1 parent 404b31e commit e4d8496
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 32 deletions.
4 changes: 4 additions & 0 deletions lib/core/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module.exports = function() {
});

String.prototype.sprintf = function(arguments) {
if(typeof arguments == "string") {
arguments = [arguments];
}

return sprintf(this, arguments);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = orm.express(config.orm, {
db.settings.set("properties.primary_key", "id");
db.settings.set("instance.cache", false);
db.settings.set("instance.autoSave", false);
db.settings.set("instance.autoFetch", false);
db.settings.set("instance.autoFetch", true);
db.settings.set("instance.autoFetchLimit", 2);

/* Use Plugins */
Expand All @@ -34,7 +34,7 @@ module.exports = orm.express(config.orm, {
models.projects = require("./projects")(db, models);

/* Associations */
models.projects.hasOne("owner", models.users, { autoFetch: true, reverse: "projects" });
models.projects.hasOne("owner", models.users, { reverse: "projects" });


/* Init */
Expand Down
25 changes: 25 additions & 0 deletions lib/models/projects/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
var rand = require("generate-key");
var request = require('request');

module.exports = function (db, models) {
return db.define("projects", {
pub_id: String,
name: String,
host: String,
rules: Object
}, {
timestamp: true,
hooks: {
beforeCreate: function() {
this.pub_id = rand.generateKey(Math.floor(Math.random() * 15) + 15);
},
afterLoad: function(next) {
var _this = this;

_this.get_image(function(image) {
_this.image = image;
next();
});
}
},
methods: {
get_image: function(callback) {
request("http://" + this.host, function(error, response, body) {
if(!error && response.statusCode == 200) {
var document = $(body);
var favicon = document.find('link[rel="shortcut icon"]').attr('href');
var open_graph = document.find('meta[property="og:image"]').attr('content');

callback(open_graph || favicon);
} else {
callback("");
}
});
}
},
validations: {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"i": "0.3.2",
"node-fs": "0.1.7",
"crontab": "0.3.4",
"npm": "1.3.9"
"npm": "1.3.9",
"request": "2.34.0",
"fast-url-parser": "1.0.6-0"
},
"devDependencies": {
"express-debug": "*"
Expand Down
17 changes: 17 additions & 0 deletions public/js/core/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
$(function() {
$(".popup").hAlign().vAlign();

$(".popup form").submit(function(e) {
e.preventDefault();
e.stopPropagation();

var form = $(this);

$.post($(this).attr("action"), $(this).serialize(), function(response) {
if(response.success) {
window.location.href = response.next;
} else {
form.find(".button").eq(0).addClass("error").val(response.message);
}
});
});
});
112 changes: 112 additions & 0 deletions public/less/core/popup.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
@import '../compless/all';
@import '../core/colors';

body {
background-image: url("/img/background.png");

.popup {
width: 280px;

form {
overflow: hidden;
background: #fff;
border: solid 1px darken(@popup, 10%);
.box-shadow(~"2px 0 4px rgba(0,0,0,0.05), -2px 0 4px rgba(0,0,0,0.05)");
.border-radius(4px);

.header {
color: lighten(@popup, 40%);
padding: 10px 0;
text-align: center;
text-shadow: 0 -1px 0 darken(@popup, 10%);
border-bottom: solid 1px darken(@popup, 10%);
.linear-gradient(0deg, darken(@popup, 5%), darken(@popup, 3%));
.box-shadow(inset 0 1px 0 lighten(@popup, 3%));
}

.info {
padding: 15px;
background: #FAFAFA;

.input {
width: 100%;
font-size: 13px;
color: #333;
background: #fff;
padding: 10px;
margin-bottom: 10px;
border: solid 1px darken(@popup_info, 5%);
.box-shadow(inset 0 1px 3px darken(@popup_info, 2%));
.border-radius(2px);

&:focus {
border: solid 1px spin(lighten(@popup, 10%), 0%);
}

&:last-child {
margin-bottom: 0;
}
}
}

.submit {
padding: 10px 15px;
background: lighten(@popup, 55%);
border-top: solid 1px lighten(@popup, 52%);

.button {
width: 100%;
text-align: center;
padding: 8px 0;
font-size: 14px;
color: #fff;
cursor: pointer;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.2);
border: solid 1px @popup;
.border-radius(2px);
.linear-gradient(0deg, lighten(@popup, 5%), lighten(@popup, 15%));

&:not(.error):hover {
.linear-gradient(0deg, lighten(@popup, 5%), lighten(@popup, 10%));

&:active {
.linear-gradient(0deg, lighten(@popup, 5%), lighten(@popup, 2%));
}
}

&.error {
border: solid 1px darken(@popup_error, 10%);
.linear-gradient(0deg, darken(@popup_error, 5%), darken(@popup_error, 2%));

&:hover {
.linear-gradient(0deg, darken(@popup_error, 8%), darken(@popup_error, 5%));

&:active {
.linear-gradient(0deg, darken(@popup_error, 11%), darken(@popup_error, 8%));
}
}
}
}
}
}

.link {
color: lighten(@popup, 30%);
margin-top: 10px;
text-align: center;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.2);

a {
color: lighten(@popup, 40%);

&:hover {
text-decoration: underline;

&:active {
color: lighten(@popup, 50%);
}
}
}
}
}
}
8 changes: 4 additions & 4 deletions routes/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
exports.login = function(req, res, next) {
res.render("auth/login", {
title: "Login",
js: req.js.renderTags("core", "auth"),
css: req.css.renderTags("core", "auth"),
js: req.js.renderTags("core"),
css: req.css.renderTags("core"),
next: req.param("next") || ""
});
}

exports.register = function(req, res, next) {
res.render("auth/register", {
title: "Register",
js: req.js.renderTags("core", "auth"),
css: req.css.renderTags("core", "auth")
js: req.js.renderTags("core"),
css: req.css.renderTags("core")
});
}
39 changes: 28 additions & 11 deletions routes/auth/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exports.restrict = function(req, res, next) {
if(req.session.user) {
next();
} else {
res.redirect("/login/?next=" + req.url);
res.error(404);
}
};

Expand All @@ -23,6 +23,26 @@ exports.xhr = function(req, res, next) {
}
}

exports.restrictReload = function(req, res, next) {
exports.restrict(req, res, function() {
exports.reload(req, res, next);
});
}

exports.restrictXhr = function(req, res, next) {
exports.xhr(req, res, function() {
exports.restrict(req, res, next);
});
}

exports.restrictReloadXhr = function(req, res, next) {
exports.xhr(req, res, function() {
exports.restrict(req, res, function() {
exports.reload(req, res, next);
});
});
}

/* Operations */
exports.login = function(req, res, next) {
req.models.users.one({
Expand All @@ -33,14 +53,12 @@ exports.login = function(req, res, next) {
req.session.user = user;
req.session.save();

res.json({
success: true,
res.success({
next: req.param("next") || config.general.default
});
} else {
res.json({
success: false,
error_message: "Invalid Credentials"
res.failure({
message: "Invalid Credentials"
});
}
});
Expand Down Expand Up @@ -71,15 +89,13 @@ exports.register = function(req, res, next) {
req.session.user = user;
req.session.save();

res.json({
success: true,
res.success({
next: req.param("next") || config.general.default
});

} else {
res.json({
success: false,
error_message: "Failed to Register"
res.failure({
message: "Failed to Register"
});
}
});
Expand All @@ -93,6 +109,7 @@ exports.reload = function(req, res, next) {
req.models.users.get(req.session.user.id, function(error, user) {
if(!error && user) {
req.session.user = user;
req.session.save();
}
});
}
Expand Down
38 changes: 33 additions & 5 deletions routes/dashboard/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
exports.index = function(req, res, next) {
res.render("dashboard/index", {
title: "Dashboard",
js: req.js.renderTags("core", "dashboard"),
css: req.css.renderTags("core", "dashboard"),
current_project: {}
req.models.users.get(req.session.user.id, function(error, user) {
if(!error && user) {
if(!user.projects.empty) {
if(req.param("project")) {
res.render("dashboard/index", {
title: "Dashboard",
js: req.js.renderTags("core", "dashboard"),
css: req.css.renderTags("core", "dashboard"),
user: user,
current_project: $.map(user.projects, function(project) {
if(project.pub_id == req.param("project")) {
return project;
}
})[0]
});
} else {
res.redirect("/dashboard/%s/".sprintf(user.projects[0].pub_id));
}
} else {
res.redirect("/dashboard/create/");
}
} else {
res.error(500, null, error);
}
});
}

exports.create = function(req, res, next) {
res.render("dashboard/create", {
title: "Create Project",
js: req.js.renderTags("core"),
css: req.css.renderTags("core"),
forced: req.session.user.projects.empty
});
}
6 changes: 5 additions & 1 deletion routes/dashboard/route.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = function(app, routes) {
app.get('/dashboard', routes.auth.util.restrict, routes.auth.util.reload, routes.dashboard.index);
app.get('/dashboard', routes.auth.util.restrict, routes.dashboard.index);
app.get('/dashboard/create', routes.auth.util.restrictReload, routes.dashboard.create);
app.get('/dashboard/:project', routes.auth.util.restrict, routes.dashboard.index);

app.post('/dashboard/create', routes.auth.util.restrict, routes.dashboard.util.create);
}
23 changes: 23 additions & 0 deletions routes/dashboard/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var url = require('fast-url-parser');

exports.create = function(req, res, next) {
var host = req.param("host").toLowerCase();

if(host.indexOf("http://") == -1 || host.indexOf("https://") == -1) {
host = "http://" + host;
}

req.models.projects.create({
name: req.param("name"),
host: url.parse(host).hostname,
owner_id: req.session.user.id
}, function(error, project) {
if(!error && project) {
res.success({
next: "/dashboard/%s/".sprintf(project.pub_id)
});
} else {
res.error(200, "Failed to Create Project", error);
}
});
}
Loading

0 comments on commit e4d8496

Please sign in to comment.