Skip to content

Commit

Permalink
Merge pull request #32 from blank-project/dev
Browse files Browse the repository at this point in the history
Prepare release 1.0.0-alpha.2
  • Loading branch information
erwanosouf authored Sep 30, 2017
2 parents 7647450 + 65beab2 commit a70f101
Show file tree
Hide file tree
Showing 27 changed files with 4,927 additions and 805 deletions.
2 changes: 1 addition & 1 deletion .bowerrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"directory": "public/components",
"directory": "public/lib",
"json": "bower.json"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules/
public/components
public/lib
.nyc_output
.sass-cache
npm-debug.log
Expand Down
36 changes: 26 additions & 10 deletions app/controllers/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,46 @@ module.exports = function (app) {
};

router.get('/', function (req, res, next) {
var query = {};
var query = {}, first = parseInt(req.query.first), size = parseInt(req.query.size);
if (req.query.tagId) {
query.tags = { $all : req.query.tagId };
}
if (req.query.search && req.query.search.trim()) {
query.$text = { $search: req.query.search };
}
// Sanitize first.
if (!first || first < 0 || size != req.query.previousSize) {
first = 0;
}
if (!size || size <= 0) {
size = 20;
}
console.log('Listing contacts');

co(function* () {
return yield {
contacts : Contact.find(query).populate({
path: 'tags'
, options: { sort: 'name'}
}).exec(),
}).skip(first).limit(size + 1).exec(),
tags : Tag.find().exec()
};

}).then(data => {
var contacts = data.contacts;
if (contacts.length > size) {
contacts.pop();
data.next = first + size;
data.hasNext = true;
}
if (first >= size) {
data.previous = first - size;
data.hasPrevious = true;
}
if (data.hasPrevious || data.hasNext) {
data.incomplete = true;
}
data.size = size;
data.title = 'Liste de Contact';
res.render('contactList', data);
}).catch(err => { next(err); });
Expand Down Expand Up @@ -77,11 +98,6 @@ router.get('/:contactId', function (req, res, next) {

router.post('/', function (req, res, next) {
console.log('Submitting contact ');
if (req.body.cancel) {
// Redirect to list.
res.redirect("/contacts/");
return;
}
var contact = null;
var id = req.body.id;
if (id) {
Expand Down Expand Up @@ -146,11 +162,11 @@ router.delete('/:contactId', function (req, res, next) {
console.log('Removing ' + id);
// Tag found at this point, add to tag set.
Contact.findByIdAndRemove(id).exec()
.then(() => {
res.sendStatus(200);
.then((data) => {
res.sendStatus(data ? 200 : 404);
})
.catch(err => {
next(err);
res.sendStatus(500);
});
});

Expand Down
47 changes: 34 additions & 13 deletions app/controllers/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ module.exports = function (app) {
app.use('/tags', router);
};

router.get('/edit/', function (req, res, next) {
res.render('tagEdit', { tag : {} });
});

router.get('/edit/:tagId', function (req, res, next) {
console.log('Editing tag');
var id = req.params.tagId;
console.log('id :' + id);
Tag.findById(id).exec().
then(data => {
res.render('tagEdit', {
tag : data
});
}).
catch(err => { next(err); });
});

router.get('/', function (req, res, next) {
console.log('Listing tags');
var tags = Tag.find({}).sort('name').exec();
Expand All @@ -18,17 +35,13 @@ router.get('/', function (req, res, next) {
});
});

router.get('/edit/', function (req, res, next) {
res.render('tagEdit', { tag : {} });
});

router.get('/edit/:tagId', function (req, res, next) {
console.log('Editing tag');
router.get('/:tagId', function (req, res, next) {
console.log('Showing tag');
var id = req.params.tagId;
console.log('id :' + id);
Tag.findById(id).exec().
then(data => {
res.render('tagEdit', {
res.render('tagView', {
tag : data
});
}).
Expand All @@ -37,11 +50,6 @@ router.get('/edit/:tagId', function (req, res, next) {

router.post('/', function (req, res, next) {
console.log('Submitting tag ');
if (req.body.cancel) {
// Redirect to list.
res.redirect("/tags/");
return;
}
var tag = null;
var id = req.body.id;
if (id) {
Expand All @@ -60,6 +68,19 @@ router.post('/', function (req, res, next) {
});
return tag.save();
}).
then(model => { res.redirect("") }).
then(model => { res.redirect("/tags/") }).
catch(err => { next(err); });
});

router.delete('/:tagId', function (req, res, next) {
var id = req.params.tagId;
console.log('id :' + id);
Tag.findByIdAndRemove(id).exec()
.then((data) => {
res.sendStatus(data ? 200 : 404);
})
.catch(err => {
res.sendStatus(500);
});

});
6 changes: 5 additions & 1 deletion app/views/contactEdit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
</div>
<fieldset class="padded">
<input type="submit" name="submit" value="Soumettre" class="pure-button pure-button-primary"/>
<input type="submit" name="cancel" value="Annuler" class="pure-button" />
{{#if id }}
<a href="/contacts/{{ id }}" class="pure-button">Annuler</a>
{{else}}
<a href="/contacts/" class="pure-button">Annuler</a>
{{/if}}
</fieldset>
{{/with}}
</form>
26 changes: 19 additions & 7 deletions app/views/contactList.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
<h1>Liste des contacts</h1>
<a class="pure-button pure-button-primary left-offset" href="edit" title="Nouveau"><i class="fa fa-plus"></i></a>
</div>
<table class="contact-list pure-table pure-table-striped">
<table id="contact-list" class="contact-list pure-table pure-table-striped">
<thead>
<tr>
<th></th>
<th>Nom</th>
<th>Mail</th>
<th>Téléphone</th>
Expand All @@ -19,11 +18,7 @@
</thead>
<tbody>
{{#contacts}}
<tr class="contact-line" data-id="{{ id }}">
<td>
<a class="fa fa-eye" href="/contacts/{{ id }}" title="Voir"></a>
<a class="fa fa-edit" href="/contacts/edit/{{ id }}" title="Modifier"></a>
</td>
<tr class="contact-line clickable" data-id="{{ id }}">
<td>{{ fullName }}</td>
<td>{{ email }}</td>
<td>{{ phone }}</td>
Expand All @@ -39,4 +34,21 @@
{{/contacts}}
</tbody>
</table>
<div class="padded">
<form method="GET" action="/contacts/" class="pure-form">
<input name="previousSize" type="hidden" value="{{ size }}" />
<button name="first" type="submit" {{#if hasPrevious}}value="{{ previous }}"{{else}}disabled{{/if}} class="pure-button fa fa-angle-double-left"></button>
<button name="first" type="submit" {{#if hasNext}}value="{{ next }}"{{else}}disabled{{/if}} class="pure-button fa fa-angle-double-right"></button>
{{#if incomplete}}
<input name="size" type="number" value="{{ size }}" min="0" max="200" step="10" size="3" />
{{/if}}
</form>
</div>
</div>
<script>
var contacts = document.getElementById('contact-list');
delegate(contacts, 'click', '.contact-line', function(e) {
var id = e.actualTarget.getAttribute("data-id");
window.location.assign("/contacts/" + id);
});
</script>
38 changes: 13 additions & 25 deletions app/views/contactView.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<tr>
<td>Etiquettes</td>
<td>
<div class="padded">
<div id="tag-list" class="padded">
{{#each tags}}
{{! TODO Find a way to factor tag and tagEdit}}
{{>tagEdit}}
Expand All @@ -44,7 +44,7 @@
<option value="{{ id }}">{{ name }}</option>
{{/each}}
</select>
<button type="submit" class="pure-button pure-input-rounded" >
<button type="submit" class="pure-button pure-input-rounded" {{#isEmpty ../tags }}disabled{{/isEmpty}}>
<i class="fa fa-plus"></i>
</button>
</form>
Expand All @@ -59,40 +59,28 @@
{{#with contact}}
<div class="padded">
<a href="edit/{{ id }}" class="pure-button pure-button-primary">Modifier</a>
<button id="delete" class="pure-button pure-button-error">Supprimer</button>
<button id="delete" class="pure-button button-error">Supprimer</button>
<div>
{{/with}}
<div class="padded">
<a href="/contacts/" class="pure-button">Retour à la liste</a>
</div>

<script>
var removeTag = function(id) {
axios.delete("/contacts/{{ contact.id }}/tags/" + id)
.then(function (response) {
location.reload(true);
})
.catch(console.log);
}, els = document.getElementsByClassName("tag-remove"), el;
for (var i = 0; i < els.length; i++) {
el = els[i];
var tagId = el.getAttribute("data-id");
el.addEventListener('click', function(ev) {
removeTag(tagId);
});
}
var tags = document.getElementById('tag-list');
delegate(tags, 'click', '.tag-remove', function(e) {
var id = e.actualTarget.getAttribute("data-id");
removeTagFromContact(id, '{{ contact.id }}').then(function (response) {
window.location.reload(true);
}).catch(console.log);
});
</script>
<script>
var remove = function(id) {
axios.delete("/contacts/" + id)
.then(function (response) {
var deleteButton = document.getElementById("delete");
deleteButton.addEventListener('click', function(ev) {
deleteContact('{{ contact.id }}').then(function (response) {
location.assign("/contacts/");
})
.catch(console.log);
}, deleteButton = document.getElementById("delete");
deleteButton.addEventListener('click', function(ev) {
remove('{{ contact.id }}');
});
</script>
6 changes: 4 additions & 2 deletions app/views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
<meta name="viewport" content="width=device-width">
<title>{{ title }}</title>

<link rel="stylesheet" href="/css/vendor/pure-min.css">
<link rel="stylesheet" href="/lib/vendor/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/lib/pure/pure-min.css">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="/css/style.css">

{{#if ENV_DEVELOPMENT}}
<script src="http://localhost:35729/livereload.js"></script>
{{/if}}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="/js/contacts-manager-client.js" type="text/javascript"></script>
<script src="/js/dom.js" type="text/javascript"></script>
</head>
<body>
<header>
Expand Down
6 changes: 5 additions & 1 deletion app/views/tagEdit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
</div>
<fieldset class="padded">
<input type="submit" name="submit" value="Soumettre" class="pure-button pure-button-primary" />
<input type="submit" name="cancel" value="Annuler" class="pure-button"/>
{{#if id }}
<a href="/tags/{{ id }}" class="pure-button">Annuler</a>
{{else}}
<a href="/tags/" class="pure-button">Annuler</a>
{{/if}}
</fieldset>
{{/with}}
</form>
15 changes: 9 additions & 6 deletions app/views/tagList.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
<h1>Liste des Etiquettes</h1>
<a class="pure-button pure-button-primary left-offset" href="edit" title="Nouveau"><i class="fa fa-plus"></i></a>
</div>
<table class="tag-list pure-table">
<table id="tag-list" class="tag-list pure-table">
<thead>
<tr>
<th></th>
<th>Nom</th>
</tr>
</thead>
<tbody>
{{#tags}}
<tr>
<td>
<a class="fa fa-edit" href="/tags/edit/{{ id }}" title="Modifier"></a>
</td>
<tr class="tag-line clickable" data-id="{{ id }}">
<td>
{{> tag }}
</td>
Expand All @@ -24,3 +20,10 @@
</tbody>
</table>
</div>
<script>
var table = document.getElementById('tag-list');
delegate(table, 'click', '.tag-line', function(e) {
var id = e.actualTarget.getAttribute("data-id");
window.location.assign("/tags/" + id);
});
</script>
28 changes: 28 additions & 0 deletions app/views/tagView.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{#with tag }}
<div class="padded">
<table class="tag pure-table">
<tr>
<td>Nom</td><td>{{ name }}</td>
</tr>
<tr>
<td>Couleur</td><td><span class="fa fa-square fa-lg" style="color:{{ color }};" ></span></td>
</tr>
</table>
<div>
<div class="padded">
<a href="edit/{{ id }}" class="pure-button pure-button-primary">Modifier</a>
<button id="delete" class="pure-button button-error">Supprimer</button>
<div>
<div class="padded">
<a href="/tags/" class="pure-button">Retour à la liste</a>
</div>
<script>
var deleteButton = document.getElementById("delete");
deleteButton.addEventListener('click', function(ev) {
deleteTag('{{ id }}').then(function (response) {
location.assign("/tags/");
})
.catch(console.log);
});
</script>
{{/with}}
Loading

0 comments on commit a70f101

Please sign in to comment.