mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-16 11:15:12 +00:00
add Recipes(scaffolding)
This commit is contained in:
@@ -31,6 +31,5 @@ angular.module('codexen')
|
||||
$scope.$on('userSignOut', function () {
|
||||
vm.isAuthenticated = false
|
||||
vm.currentUser = null
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('DeleteRecipeModalController', function ($modalInstance, Recipe, recipe) {
|
||||
var vm = this
|
||||
|
||||
vm.submit = function () {
|
||||
Recipe.delete(recipe.id)
|
||||
.success(function (recipe) {
|
||||
$modalInstance.close(recipe)
|
||||
})
|
||||
}
|
||||
|
||||
vm.cancel = function () {
|
||||
$modalInstance.dismiss()
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('EditRecipeModalController', function (Recipe, Tag, $modalInstance, recipe) {
|
||||
var vm = this
|
||||
|
||||
vm.recipe = recipe
|
||||
|
||||
vm.submit = function () {
|
||||
var params = {
|
||||
title: vm.recipe.title,
|
||||
content: vm.recipe.content,
|
||||
Tags: angular.isArray(vm.recipe.Tags) ? vm.recipe.Tags.map(function (tag) { return tag.name }) : []
|
||||
}
|
||||
|
||||
Recipe.update(vm.recipe.id, params)
|
||||
.success(function (data) {
|
||||
$modalInstance.close(data)
|
||||
})
|
||||
}
|
||||
|
||||
// vm.tags = []
|
||||
vm.tagCandidates = []
|
||||
vm.refreshTagCandidates = function (tagName) {
|
||||
if (tagName == null || tagName === '') return null
|
||||
return Tag.findByName(tagName)
|
||||
.success(function (data) {
|
||||
console.log('tags fetched!!', data)
|
||||
vm.tagCandidates = data
|
||||
})
|
||||
}
|
||||
vm.transform = function (tagName) {
|
||||
return {
|
||||
id: 0,
|
||||
name: tagName
|
||||
}
|
||||
}
|
||||
|
||||
vm.cancel = function () {
|
||||
$modalInstance.dismiss()
|
||||
}
|
||||
|
||||
})
|
||||
@@ -17,6 +17,7 @@ angular.module('codexen')
|
||||
Snippet.update(vm.snippet.id, params)
|
||||
.success(function (data) {
|
||||
console.log('updated res :', data)
|
||||
$rootScope.$broadcast('snippetUpdated', snippet)
|
||||
$modalInstance.close(data)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('NewRecipeModalController', function (Recipe, Tag, $modalInstance) {
|
||||
var vm = this
|
||||
|
||||
vm.content = ''
|
||||
|
||||
vm.submit = function () {
|
||||
var params = {
|
||||
title: vm.title,
|
||||
content: vm.content,
|
||||
Tags: angular.isArray(vm.Tags) ? vm.Tags.map(function (tag) { return tag.name }) : []
|
||||
}
|
||||
|
||||
Recipe.create(params)
|
||||
.success(function (data) {
|
||||
$modalInstance.close(data)
|
||||
})
|
||||
}
|
||||
|
||||
// vm.tags = []
|
||||
vm.tagCandidates = []
|
||||
vm.refreshTagCandidates = function (tagName) {
|
||||
if (tagName == null || tagName === '') return null
|
||||
return Tag.findByName(tagName)
|
||||
.success(function (data) {
|
||||
console.log('tags fetched!!', data)
|
||||
vm.tagCandidates = data
|
||||
})
|
||||
}
|
||||
vm.transform = function (tagName) {
|
||||
return {
|
||||
id: 0,
|
||||
name: tagName
|
||||
}
|
||||
}
|
||||
|
||||
vm.cancel = function () {
|
||||
$modalInstance.dismiss()
|
||||
}
|
||||
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('NewSnippetModalController', function ($modalInstance, aceModes, $log, Snippet, $rootScope, Tag) {
|
||||
.controller('NewSnippetModalController', function ($modalInstance, aceModes, $log, Snippet, Tag) {
|
||||
var vm = this
|
||||
|
||||
vm.aceModes = aceModes
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('RecipesDetailController', function (Recipe, $state, $rootScope, $scope, Modal) {
|
||||
var vm = this
|
||||
|
||||
vm.isLoaded = false
|
||||
|
||||
var recipeId = $state.params.id
|
||||
|
||||
Recipe.show(recipeId)
|
||||
.success(function (data) {
|
||||
vm.recipe = data
|
||||
vm.isLoaded = true
|
||||
})
|
||||
|
||||
$scope.$on('taggingRequested', function (e) {
|
||||
e.stopPropagation()
|
||||
e.preventDefault()
|
||||
Modal.editRecipe(angular.copy(vm.recipe))
|
||||
.then(function (recipe) {
|
||||
console.log('edited', recipe)
|
||||
}, function () {
|
||||
console.log('edit recipe modal dismissed')
|
||||
})
|
||||
})
|
||||
|
||||
$scope.$on('recipeUpdated', function (e, recipe) {
|
||||
console.log('event received', recipe)
|
||||
if (recipe.id === vm.recipe.id) vm.recipe = recipe
|
||||
})
|
||||
|
||||
})
|
||||
98
src/browser/main/controllers/states/RecipesListController.js
Normal file
98
src/browser/main/controllers/states/RecipesListController.js
Normal file
@@ -0,0 +1,98 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('RecipesListController', function (Recipe, $state, $scope, $filter, myRecipes, User, $auth) {
|
||||
var vm = this
|
||||
|
||||
|
||||
vm.recipes = myRecipes
|
||||
|
||||
vm.searchRecipes = searchRecipes
|
||||
vm.searchRecipes()
|
||||
|
||||
vm.isAuthenticated = $auth.isAuthenticated()
|
||||
var reloadUser = function () {
|
||||
if (vm.isAuthenticated) {
|
||||
User.me().success(function (data) {
|
||||
vm.currentUser = data
|
||||
})
|
||||
}
|
||||
}
|
||||
reloadUser()
|
||||
|
||||
$scope.$on('$stateChangeSuccess', function (e, toState, toParams) {
|
||||
if (!toState.name.match(/recipes/)) return null
|
||||
|
||||
vm.recipeId = parseInt(toParams.id, 10)
|
||||
|
||||
if (!vm.recipeId && vm.filtered && vm.filtered[0]) {
|
||||
$state.go('recipes.detail', {id: vm.filtered[0].id})
|
||||
}
|
||||
})
|
||||
|
||||
$scope.$on('recipeUpdated', function (e, recipe) {
|
||||
if (!myRecipes.some(function (_recipe, index) {
|
||||
if (_recipe.id === recipe.id) {
|
||||
myRecipes[index] = recipe
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})) myRecipes.unshift(recipe)
|
||||
|
||||
searchRecipes()
|
||||
vm.recipeId = recipe.id
|
||||
$state.go('recipes.detail', {id: recipe.id})
|
||||
})
|
||||
|
||||
$scope.$on('recipeDeleted', function () {
|
||||
if ($state.is('recipes.detail')) {
|
||||
var currentRecipeId = parseInt($state.params.id, 10)
|
||||
// Delete recipe from recipe list
|
||||
for (var i = 0; i < vm.recipes.length; i++) {
|
||||
if (vm.recipes[i].id === currentRecipeId) {
|
||||
vm.recipes.splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
// Delete recipe from `filtered list`
|
||||
// And redirect `next filtered recipe`
|
||||
for (i = 0; i < vm.filtered.length; i++) {
|
||||
if (vm.filtered[i].id === currentRecipeId) {
|
||||
if (vm.filtered[i + 1] != null) $state.go('recipes.detail', {id: vm.filtered[i + 1].id})
|
||||
else if (vm.filtered[i - 1] != null) $state.go('recipes.detail', {id: vm.filtered[i - 1].id})
|
||||
else $state.go('recipes')
|
||||
|
||||
vm.filtered.splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
$scope.$on('tagSelected', function (e, tag) {
|
||||
e.stopPropagation()
|
||||
$scope.$apply(function () {
|
||||
vm.search = '#' + tag.name
|
||||
searchRecipes()
|
||||
})
|
||||
})
|
||||
|
||||
function loadRecipes () {
|
||||
if ($auth.isAuthenticated) {
|
||||
Recipe.findMine()
|
||||
.success(function (data) {
|
||||
vm.recipes = data
|
||||
})
|
||||
} else {
|
||||
vm.recipes = void 0
|
||||
}
|
||||
}
|
||||
|
||||
function searchRecipes () {
|
||||
vm.filtered = $filter('filter')(myRecipes, vm.search)
|
||||
if (vm.search && vm.filtered && vm.filtered[0] && (!vm.recipeId || vm.recipeId !== vm.filtered[0].id)) {
|
||||
$state.go('recipes.detail', {id: vm.filtered[0].id})
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
@@ -17,11 +17,11 @@ angular.module('codexen')
|
||||
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')
|
||||
})
|
||||
.then(function (snippet) {
|
||||
console.log('edited', snippet)
|
||||
}, function () {
|
||||
console.log('edit snippet modal dismissed')
|
||||
})
|
||||
})
|
||||
|
||||
$scope.$on('snippetUpdated', function (e, snippet) {
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
/* global angular */
|
||||
angular.module('codexen')
|
||||
.controller('SnippetsListController', function ($auth, Snippet, $scope, $state, $scope, $filter, mySnippets, User) {
|
||||
.controller('SnippetsListController', function ($auth, Snippet, $scope, $state, $filter, mySnippets, User) {
|
||||
var vm = this
|
||||
|
||||
vm.isLoading = false
|
||||
|
||||
vm.snippetId = parseInt($state.params.id)
|
||||
|
||||
vm.snippets = mySnippets
|
||||
@@ -33,14 +31,13 @@ angular.module('codexen')
|
||||
$scope.$on('$stateChangeSuccess', function (e, toState, toParams) {
|
||||
if (!toState.name.match(/snippets/)) return null
|
||||
|
||||
vm.snippetId = parseInt(toParams.id)
|
||||
vm.snippetId = parseInt(toParams.id, 10)
|
||||
|
||||
if (!vm.snippetId && vm.filtered[0]) {
|
||||
$state.go('snippets.detail', {id: vm.filtered[0].id})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
$scope.$on('snippetUpdated', function (e, snippet) {
|
||||
if (!mySnippets.some(function (_snippet, index) {
|
||||
if (_snippet.id === snippet.id) {
|
||||
@@ -57,7 +54,7 @@ angular.module('codexen')
|
||||
|
||||
$scope.$on('snippetDeleted', function () {
|
||||
if ($state.is('snippets.detail')) {
|
||||
var currentSnippetId = parseInt($state.params.id)
|
||||
var currentSnippetId = parseInt($state.params.id, 10)
|
||||
// Delete snippet from snippet list
|
||||
for (var i = 0; i < vm.snippets.length; i++) {
|
||||
if (vm.snippets[i].id === currentSnippetId) {
|
||||
@@ -67,10 +64,10 @@ angular.module('codexen')
|
||||
}
|
||||
// Delete snippet from `filtered list`
|
||||
// And redirect `next filtered snippet`
|
||||
for (var i = 0; i < vm.filtered.length; i++) {
|
||||
for (i = 0; i < vm.filtered.length; i++) {
|
||||
if (vm.filtered[i].id === currentSnippetId) {
|
||||
if (vm.filtered[i+1] != null) $state.go('snippets.detail', {id: vm.filtered[i+1].id})
|
||||
else if (vm.filtered[i-1] != null) $state.go('snippets.detail', {id: vm.filtered[i-1].id})
|
||||
if (vm.filtered[i + 1] != null) $state.go('snippets.detail', {id: vm.filtered[i + 1].id})
|
||||
else if (vm.filtered[i - 1] != null) $state.go('snippets.detail', {id: vm.filtered[i - 1].id})
|
||||
else $state.go('snippets')
|
||||
|
||||
vm.filtered.splice(i, 1)
|
||||
@@ -100,7 +97,7 @@ angular.module('codexen')
|
||||
}
|
||||
}
|
||||
|
||||
function searchSnippets() {
|
||||
function searchSnippets () {
|
||||
vm.filtered = $filter('searchSnippets')(mySnippets, vm.search)
|
||||
if (vm.search && vm.filtered[0] && (!vm.snippetId || vm.snippetId !== vm.filtered[0].id)) {
|
||||
$state.go('snippets.detail', {id: vm.filtered[0].id})
|
||||
|
||||
Reference in New Issue
Block a user