Skip to content

Commit

Permalink
First version of the view
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrall committed Feb 23, 2013
1 parent 38aae46 commit 8c746fe
Show file tree
Hide file tree
Showing 57 changed files with 33,461 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -13,3 +13,5 @@ results
node_modules

npm-debug.log

.DS_Store
75 changes: 70 additions & 5 deletions app.js
@@ -1,5 +1,7 @@
var express = require('express'),
driver = require('couchbase');
driver = require('couchbase'),
routes = require('./routes')
;

dbConfiguration = {
"hosts": ["localhost:8091"],
Expand All @@ -16,9 +18,31 @@ driver.connect(dbConfiguration, function(err, cb) {
var app = module.exports = express();
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('view options', {
layout: false
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: 'cb-community-support'
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});


// *** routes
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);



// *** API and Couchbase access ****

function get(req, res, docType) {
cb.get(req.params.id, function(err, doc, meta) {
if (doc != null && doc.type) {
Expand All @@ -41,13 +65,28 @@ driver.connect(dbConfiguration, function(err, cb) {
var id = req.body.id;
if (id == null) {
// increment the sequence and save the doc
cb.incr("counter:"+req.body.type, function(err, value, meta) {
id = req.body.type + ":" + value;
if ( docType != "vote" ) {
cb.incr("counter:"+req.body.type, function(err, value, meta) {
id = req.body.type + ":" + value;
req.body.id = id;
cb.set(id, req.body, function(err, meta) {
res.send(200);
});
});
} else {
id = req.body.type + ":" + req.body.user_id +"-"+ req.body.idea_id;
req.body.id = id;
cb.set(id, req.body, function(err, meta) {
res.send(200);
var endureOpts = {
persisted: 1,
replicated: 0
};
cb.endure(id, endureOpts, function(err, meta) {
res.send(200);
});
});
});
}

} else {
cb.set(id, req.body, function(err, meta) {
res.send(200);
Expand Down Expand Up @@ -87,6 +126,24 @@ driver.connect(dbConfiguration, function(err, cb) {
});
});


app.get('/api/votes/:id?', function(req, res) {
var queryParams = {
stale : false,
include_docs : true
};
if (req.params.id != null) {
queryParams.startkey = req.params.id;
queryParams.endkey = req.params.id +"zz";
}

cb.view("dev_ideas", "votes_details_by_idea", queryParams, function(err, view) {
res.send(view);
});


});


// get document
app.get('/api/:type/:id', function(req, res) {
Expand All @@ -107,6 +164,14 @@ driver.connect(dbConfiguration, function(err, cb) {
}
});

// Delete vote document
app.delete('/api/vote/:id', function(req, res) {
// Todo test the type
cb.remove(req.params.id, function (err, meta) {
res.send(200);
});
});


app.get('/api/idea', function(req, res) {
cb.view("dev_ideas", "by_title", {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -5,6 +5,7 @@
"dependencies":
{
"express": "3.x",
"couchbase": "0.0.11"
"couchbase": "0.0.11",
"ejs": ">= 0.0.1"
}
}
Binary file added public/images/NuGetGallery.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/accent.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/aspNetHome.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/bullet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/community/couchbase-forum-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/community/couchbase-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/community/other-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/community/stackoverflow-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/community/twitter-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/facebook.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/facebookLogin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/findHosting.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/heroAccent.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/heroAccent2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/npm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/orderedListOne.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/orderedListThree.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/orderedListTwo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/twitter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/windowsLive.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions public/js/app.js
@@ -0,0 +1,17 @@
'use strict';

// Declare app level module which depends on filters, and services
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {templateUrl: 'partials/index.html', controller: EntriesListCtrl }).
when('/idea/new', {templateUrl: 'partials/idea-form.html', controller: IdeaFormCtrl }).
when('/idea/edit/:id', {templateUrl: 'partials/idea-form.html', controller: IdeaFormCtrl }).
when('/idea/:id', {templateUrl: 'partials/idea.html', controller: IdeaViewCtrl }).
when('/idea/:id/vote', {templateUrl: 'partials/vote-form.html', controller: VoteFormCtrl }).
when('/idea/:id/vote/:voteId', {templateUrl: 'partials/vote-form.html', controller: VoteFormCtrl }).
otherwise({redirectTo: '/'});

}]);


113 changes: 113 additions & 0 deletions public/js/controllers.js
@@ -0,0 +1,113 @@
'use strict';

/* Controllers */


function AppCtrl($scope, $http) {
$scope.user = null;
}
AppCtrl.$inject = ['$scope', '$http'];


function NavBarController($scope) {
}
NavBarController.$inject = ['$scope'];

function EntriesListCtrl($scope, $http) {
$http({method: 'GET', url: '/api/results' }).success(function(data, status, headers, config) {
$scope.entries = data;
})
.error(function(data, status, headers, config) {
$scope.name = 'Error!'
});
}
EntriesListCtrl.$inject = ['$scope', '$http'];

function IdeaViewCtrl($rootScope, $scope, $routeParams, $http, $location) {
$scope.numberOfVote = 0;
$http({method: 'GET', url: '/api/idea/'+ $routeParams.id }).success(function(data, status, headers, config) {
$scope.entry = data;
})
.error(function(data, status, headers, config) {
$scope.name = 'Error!'
});
$http({method: 'GET', url: '/api/votes/'+ $routeParams.id }).success(function(data, status, headers, config) {
$scope.votes = data;
$scope.numberOfVote = data.length;
})
.error(function(data, status, headers, config) {
$scope.name = 'Error!'
});

$scope.delete = function(vote) {
$http.delete('/api/vote/'+ vote.id).success(function(data) {
// remove the data from the vote list
for (var i in $scope.votes) {
if ( $scope.votes[i] == vote ) {
$scope.votes.splice(i, 1);
$scope.numberOfVote = $scope.numberOfVote -1;
}
}
});
}
}
IdeaViewCtrl.$inject = ['$rootScope', '$scope', '$routeParams','$http', '$location'];

function VoteFormCtrl($rootScope, $scope, $routeParams, $http, $location) {
$http({method: 'GET', url: '/api/idea/'+ $routeParams.id }).success(function(data, status, headers, config) {
$scope.entry = data;
})
.error(function(data, status, headers, config) {
$scope.name = 'Error!'
console.log("Error in VoteFormCtrl");
});

// if a parameter exit we should load the vote
if ($routeParams.voteId) {
$http({method: 'GET', url: '/api/vote/'+ $routeParams.voteId }).success(function(data, status, headers, config) {
$scope.vote = data;
})
.error(function(data, status, headers, config) {
$scope.name = 'Error!'
console.log("Error in VoteFormCtrl");
});
}

$scope.save = function() {
$scope.vote.type = "vote";
$scope.vote.user_id = $scope.user;
$scope.vote.idea_id = $routeParams.id;
$http.post('/api/vote',$scope.vote).success(function(data) {
$location.path('/idea/'+ $routeParams.id );
});
}

}
VoteFormCtrl.$inject = ['$rootScope', '$scope', '$routeParams','$http', '$location'];


/**
* Manage the Creation & Edit of ideas
*/
function IdeaFormCtrl($rootScope, $scope, $routeParams, $http, $location) {
$scope.idea = null;
if ($routeParams.id ) {
$http({method: 'GET', url: '/api/idea/'+ $routeParams.id }).success(function(data, status, headers, config) {
$scope.idea = data;
});
}

$scope.save = function() {
$scope.idea.type = "idea"; // set the type
$scope.idea.user_id = $scope.user;
$http.post('/api/idea',$scope.idea).success(function(data) {
$location.path('/');
});
}
$scope.cancel = function() {
$location.path('/');
}

}
IdeaFormCtrl.$inject = ['$rootScope', '$scope', '$routeParams','$http', '$location'];

62 changes: 62 additions & 0 deletions public/js/directives.js
@@ -0,0 +1,62 @@
'use strict';

/* Directives */


angular.module('myApp.directives', [])
.directive('appVersion', ['version', function(version) {
return function(scope, elm, attrs) {elm.text(version);};
}])
.directive('dropdown', function () {
return function (scope, elm, attrs) {
$(elm).dropdown();
};
})
.directive('buttonGroup', function(){
return {
restrict: 'E',
scope: {
states: '=',
state: '=',
onStateChange: '='
},
template: '<div class="btn-group">' +
'<button class="btn" ng-repeat="s in states" ng-click="select(s, $event)">' +
'{{s}}' +
'</button>' +
'</div>',
replace: true,
controller: function($scope, $element){

// Make sure that style is applied to initial state value
$scope.$watch(function () {
return $($element).find('.btn').length; // it checks if the buttons are added to the DOM
}, function (newVal) {
// it applies the selected style to the currently defined state, if any
if (newVal > 0) {
$($element).find('.btn').each(function(index, elm){
if ($(elm).text() == $scope.state) $(elm).addClass('btn-primary');
});
}
}, true);

// Apply style changes according to selection
$scope.select = function(s, evt){
$scope.state = s;

$($element).find('.btn').removeClass('btn-primary'); // reset styles on all buttons
angular.element(evt.srcElement).addClass('btn-primary'); // apply style only to selected button
};


// Execute callback if it was provided
$scope.$watch('state', function(){
if ($scope.onStateChange){
$scope.onStateChange();
}
}, true);
}
};
});


10 changes: 10 additions & 0 deletions public/js/filters.js
@@ -0,0 +1,10 @@
'use strict';

/* Filters */

angular.module('myApp.filters', []).
filter('interpolate', ['version', function(version) {
return function(text) {
return String(text).replace(/\%VERSION\%/mg, version);
}
}]);

0 comments on commit 8c746fe

Please sign in to comment.