Skip to content

Commit 6aa32d4

Browse files
committed
add delete snippet modal & fix deletion behavior
1 parent 057f43c commit 6aa32d4

8 files changed

Lines changed: 85 additions & 16 deletions

File tree

src/config/states.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ angular.module('codexen')
3838
},
3939
resolve: {
4040
mySnippets: function (Snippet) {
41-
return Snippet.findMine({
42-
'include': ['Tag']
43-
}).then(function (res) {
41+
return Snippet.findMine().then(function (res) {
4442
return res.data
4543
})
4644
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* global angular */
2+
angular.module('codexen')
3+
.controller('DeleteSnippetModalController', function ($modalInstance, Snippet, snippet) {
4+
var vm = this
5+
6+
vm.submit = function () {
7+
Snippet.delete(snippet.id)
8+
.success(function (snippet) {
9+
$modalInstance.close(snippet)
10+
})
11+
}
12+
13+
vm.cancel = function () {
14+
$modalInstance.dismiss()
15+
}
16+
})

src/controllers/states/SnippetsListController.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,28 @@ angular.module('codexen')
4747

4848
$scope.$on('snippetDeleted', function () {
4949
if ($state.is('snippets.detail')) {
50-
var currentSnippetId = $state.params.id
50+
var currentSnippetId = parseInt($state.params.id)
51+
// Delete snippet from snippet list
5152
for (var i = 0; i < vm.snippets.length; i++) {
52-
if (vm.snippets[i]._id === currentSnippetId) {
53-
var targetSnippet = null
54-
55-
if (i === 0) targetSnippet = vm.snippets[i + 1]
56-
else targetSnippet = vm.snippets[i - 1]
57-
58-
console.log('target', targetSnippet)
59-
$state.go('snippets.detail', {id: targetSnippet._id})
53+
if (vm.snippets[i].id === currentSnippetId) {
54+
vm.snippets.splice(i, 1)
55+
break
56+
}
57+
}
58+
// Delete snippet from `filtered list`
59+
// And redirect `next filtered snippet`
60+
for (var i = 0; i < vm.filtered.length; i++) {
61+
if (vm.filtered[i].id === currentSnippetId) {
62+
if (vm.filtered[i+1] != null) $state.go('snippets.detail', {id: vm.filtered[i+1].id})
63+
else if (vm.filtered[i-1] != null) $state.go('snippets.detail', {id: vm.filtered[i-1].id})
64+
else $state.go('snippets')
65+
66+
vm.filtered.splice(i, 1)
6067
break
6168
}
6269
}
70+
6371
}
64-
loadSnippets()
6572
})
6673

6774
$scope.$on('tagSelected', function (e, tag) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* global angular */
2+
angular.module('codexen')
3+
.directive('btnDeleteSnippet', function (Modal, $rootScope) {
4+
return {
5+
scope: {
6+
snippet: '=btnDeleteSnippet'
7+
},
8+
link: function (scope, el) {
9+
el.on('click', function () {
10+
Modal.deleteSnippet(scope.snippet)
11+
.result.then(function (snippet) {
12+
$rootScope.$broadcast('snippetDeleted', snippet)
13+
}, function () {
14+
console.log('delete snippet modal dismissed')
15+
})
16+
})
17+
}
18+
}
19+
})

src/filters/search-snippets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
angular.module('codexen')
33
.filter('searchSnippets', function ($filter) {
44
return function (input, needle) {
5-
if (!angular.isString(needle) || !angular.isArray(input)) return input
5+
if (!angular.isString(needle) || !angular.isArray(input)) return angular.copy(input)
66
if (needle.match(/#(.+)|tag:(.+)/)) {
77
var name = needle.match(/#(.+)/) ? needle.match(/#(.+)/)[1] : needle.match(/tag:(.+)/)[1]
88

src/services/Modal.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@ angular.module('codexen')
2020
})
2121
}
2222

23+
var deleteSnippet = function (snippet) {
24+
return $modal.open({
25+
resolve: {
26+
snippet: function () {
27+
return snippet
28+
}
29+
},
30+
templateUrl: 'tpls/modals/delete-snippet-modal.tpl.html',
31+
controller: 'DeleteSnippetModalController as vm'
32+
})
33+
}
34+
2335
return {
2436
newSnippet: newSnippet,
25-
editSnippet: editSnippet
37+
editSnippet: editSnippet,
38+
deleteSnippet: deleteSnippet
2639
}
2740
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div class="new-snippet-modal">
2+
<div class="modal-header">
3+
<h4>Delete Snippet</h4>
4+
</div>
5+
6+
<div class="modal-body">
7+
<p>
8+
Are you sure to delete it?
9+
</p>
10+
</div>
11+
12+
<div class="modal-footer">
13+
<button ng-click="vm.submit()" type="button" name="button" class="btn btn-danger">Delete It</button>
14+
<button ng-click="vm.cancel()" type="button" name="button" class="btn btn-default">Cancel</button>
15+
</div>
16+
</div>

src/tpls/states/snippets.detail.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<span class="detail-header-control pull-right">
1010
<button type="button" name="button" class="btn btn-default"><i class="fa fa-share"></i></button>
1111
<button btn-edit-snippet="vm.snippet" type="button" name="button" class="btn btn-default"><i class="fa fa-edit"></i></button>
12-
<button ng-click="vm.delete()" type="button" name="button" class="btn btn-danger"><i class="fa fa-trash"></i></button>
12+
<button btn-delete-snippet="vm.snippet" type="button" name="button" class="btn btn-danger"><i class="fa fa-trash"></i></button>
1313
</span>
1414
</div>
1515

0 commit comments

Comments
 (0)