Skip to content

Commit c81bdda

Browse files
committed
improve snippets.list UX & add instant edit handler for snippet with empty tag-list
1 parent 9762da2 commit c81bdda

6 files changed

Lines changed: 61 additions & 6 deletions

File tree

src/controllers/states/SnippetsDetailController.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global angular */
22
angular.module('codexen')
3-
.controller('SnippetsDetailController', function (Snippet, $state, $rootScope, $scope) {
3+
.controller('SnippetsDetailController', function (Snippet, $state, $rootScope, $scope, Modal) {
44
var vm = this
55

66
vm.isLoaded = false
@@ -24,6 +24,18 @@ angular.module('codexen')
2424
})
2525
}
2626

27+
28+
$scope.$on('taggingRequested', function (e) {
29+
e.stopPropagation()
30+
e.preventDefault()
31+
Modal.editSnippet(angular.copy(vm.snippet))
32+
.result.then(function (snippet) {
33+
$rootScope.$broadcast('snippetUpdated', snippet)
34+
}, function () {
35+
console.log('edit snippet modal dismissed')
36+
})
37+
})
38+
2739
$scope.$on('snippetUpdated', function (e, snippet) {
2840
console.log('event received', snippet)
2941
if (snippet.id === vm.snippet.id) vm.snippet = snippet

src/controllers/states/SnippetsListController.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ angular.module('codexen')
2020

2121
// TODO: keyboard navigating UX
2222

23-
$scope.$on('$stateChangeStart', function (e, toState, toParams) {
23+
$scope.$on('$stateChangeSuccess', function (e, toState, toParams) {
24+
if (!toState.name.match(/snippets/)) return null
25+
2426
vm.snippetId = parseInt(toParams.id)
27+
2528
if (!vm.snippetId && vm.filtered[0]) {
2629
$state.go('snippets.detail', {id: vm.filtered[0].id})
2730
}
2831
})
2932

3033

3134
$scope.$on('snippetUpdated', function (e, snippet) {
35+
if (!mySnippets.some(function (_snippet, index) {
36+
if (_snippet.id === snippet.id) {
37+
mySnippets[index] = snippet
38+
return true
39+
}
40+
return false
41+
})) mySnippets.unshift(snippet)
42+
43+
searchSnippets()
44+
vm.snippetId = snippet.id
3245
$state.go('snippets.detail', {id: snippet.id})
33-
loadSnippets()
3446
})
3547

3648
$scope.$on('snippetDeleted', function () {

src/directives/snippet-item.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* global angular */
2+
angular.module('codexen')
3+
.directive('snippetItem', function (Modal, $rootScope) {
4+
return {
5+
restrict: 'A',
6+
transclude: true,
7+
template: '<div ng-transclude></div>',
8+
scope: {
9+
snippet: '=snippetItem'
10+
},
11+
link: function (scope, elem) {
12+
scope.$on('taggingRequested', function (e) {
13+
e.stopPropagation()
14+
e.preventDefault()
15+
Modal.editSnippet(angular.copy(scope.snippet))
16+
.result.then(function (snippet) {
17+
$rootScope.$broadcast('snippetUpdated', snippet)
18+
}, function () {
19+
console.log('edit snippet modal dismissed')
20+
})
21+
})
22+
}
23+
}
24+
})

src/directives/tag-list.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ angular.module('codexen')
33
.directive('tagList', function () {
44
return {
55
restrict: 'A',
6-
template: '<p class="tags" ng-if="tags.length">' +
6+
template: '<p class="tags">' +
77
'<i class="fa fa-tags"></i> ' +
88
'<a tag-item="tag" ng-repeat="tag in tags" href></a>' +
9+
'<a ng-if="!tags.length" ng-click="requestTagging($event)" href> Not tagged yet</a>' +
910
'</p>',
1011
scope: {
1112
tags: '=tagList'
@@ -16,6 +17,12 @@ angular.module('codexen')
1617
e.stopPropagation()
1718
})
1819
})
20+
21+
scope.requestTagging = function (e) {
22+
e.preventDefault()
23+
e.stopPropagation()
24+
scope.$emit('taggingRequested')
25+
}
1926
}
2027
}
2128
})

src/tpls/modals/new-snippet-modal.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h4>New Snippet</h4>
1313
<ui-select ng-model="vm.mode" style="display: inline-block;" on-select="vm.log(vm.mode.name.toLowerCase())" theme="bootstrap">
1414
<ui-select-match placeholder="Select Type">{{$select.selected}}</ui-select-match>
1515
<ui-select-choices repeat="mode in vm.aceModes | filter: $select.search">
16-
<div ng-bind-html="mode | highlight: $select.search"></div>
16+
<div ng-bind="::mode"></div>
1717
</ui-select-choices>
1818
</ui-select>
1919
</div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ <h4>
2626
<a ui-sref="auth.signin" class="btn btn-default"><i class="fa fa-signin"></i> Sign In</a>
2727
</li>
2828

29-
<li ng-repeat="snippet in vm.filtered" ui-sref="snippets.detail({id:snippet.id})" ng-class="{active:vm.snippetId===snippet.id}">
29+
<li snippet-item="snippet" ng-repeat="snippet in vm.filtered" ui-sref="snippets.detail({id:snippet.id})" ng-class="{active:vm.snippetId===snippet.id}">
3030
<div class="media">
3131
<div class="media-left">
3232
<img width="25" height="25" class="img-circle" src="http://www.gravatar.com/avatar/ea0b6ad1c11700120d1af08810caa19d" alt="" />

0 commit comments

Comments
 (0)