diff --git a/src/controllers/states/SnippetsDetailController.js b/src/controllers/states/SnippetsDetailController.js index 393a34db..8c35b6af 100644 --- a/src/controllers/states/SnippetsDetailController.js +++ b/src/controllers/states/SnippetsDetailController.js @@ -1,6 +1,6 @@ /* global angular */ angular.module('codexen') - .controller('SnippetsDetailController', function (Snippet, $state, $rootScope, $scope) { + .controller('SnippetsDetailController', function (Snippet, $state, $rootScope, $scope, Modal) { var vm = this vm.isLoaded = false @@ -24,6 +24,18 @@ angular.module('codexen') }) } + + $scope.$on('taggingRequested', function (e) { + e.stopPropagation() + e.preventDefault() + Modal.editSnippet(angular.copy(vm.snippet)) + .result.then(function (snippet) { + $rootScope.$broadcast('snippetUpdated', snippet) + }, function () { + console.log('edit snippet modal dismissed') + }) + }) + $scope.$on('snippetUpdated', function (e, snippet) { console.log('event received', snippet) if (snippet.id === vm.snippet.id) vm.snippet = snippet diff --git a/src/controllers/states/SnippetsListController.js b/src/controllers/states/SnippetsListController.js index 49bd7fa8..179aded2 100644 --- a/src/controllers/states/SnippetsListController.js +++ b/src/controllers/states/SnippetsListController.js @@ -20,8 +20,11 @@ angular.module('codexen') // TODO: keyboard navigating UX - $scope.$on('$stateChangeStart', function (e, toState, toParams) { + $scope.$on('$stateChangeSuccess', function (e, toState, toParams) { + if (!toState.name.match(/snippets/)) return null + vm.snippetId = parseInt(toParams.id) + if (!vm.snippetId && vm.filtered[0]) { $state.go('snippets.detail', {id: vm.filtered[0].id}) } @@ -29,8 +32,17 @@ angular.module('codexen') $scope.$on('snippetUpdated', function (e, snippet) { + if (!mySnippets.some(function (_snippet, index) { + if (_snippet.id === snippet.id) { + mySnippets[index] = snippet + return true + } + return false + })) mySnippets.unshift(snippet) + + searchSnippets() + vm.snippetId = snippet.id $state.go('snippets.detail', {id: snippet.id}) - loadSnippets() }) $scope.$on('snippetDeleted', function () { diff --git a/src/directives/snippet-item.js b/src/directives/snippet-item.js new file mode 100644 index 00000000..359ae29c --- /dev/null +++ b/src/directives/snippet-item.js @@ -0,0 +1,24 @@ +/* global angular */ +angular.module('codexen') + .directive('snippetItem', function (Modal, $rootScope) { + return { + restrict: 'A', + transclude: true, + template: '
', + scope: { + snippet: '=snippetItem' + }, + link: function (scope, elem) { + scope.$on('taggingRequested', function (e) { + e.stopPropagation() + e.preventDefault() + Modal.editSnippet(angular.copy(scope.snippet)) + .result.then(function (snippet) { + $rootScope.$broadcast('snippetUpdated', snippet) + }, function () { + console.log('edit snippet modal dismissed') + }) + }) + } + } + }) diff --git a/src/directives/tag-list.js b/src/directives/tag-list.js index d28d32d7..8daf350b 100644 --- a/src/directives/tag-list.js +++ b/src/directives/tag-list.js @@ -3,9 +3,10 @@ angular.module('codexen') .directive('tagList', function () { return { restrict: 'A', - template: '

' + + template: '

' + ' ' + '' + + ' Not tagged yet' + '

', scope: { tags: '=tagList' @@ -16,6 +17,12 @@ angular.module('codexen') e.stopPropagation() }) }) + + scope.requestTagging = function (e) { + e.preventDefault() + e.stopPropagation() + scope.$emit('taggingRequested') + } } } }) diff --git a/src/tpls/modals/new-snippet-modal.tpl.html b/src/tpls/modals/new-snippet-modal.tpl.html index 42a77575..367c2b1f 100644 --- a/src/tpls/modals/new-snippet-modal.tpl.html +++ b/src/tpls/modals/new-snippet-modal.tpl.html @@ -13,7 +13,7 @@ {{$select.selected}} -
+
diff --git a/src/tpls/states/snippets.list.tpl.html b/src/tpls/states/snippets.list.tpl.html index 8a162174..c4ddc233 100644 --- a/src/tpls/states/snippets.list.tpl.html +++ b/src/tpls/states/snippets.list.tpl.html @@ -26,7 +26,7 @@ Sign In -
  • +