added javascript madness to add new category

This commit is contained in:
tmont 2011-02-14 02:50:33 +00:00
parent 2409a207cf
commit dfa5277e8c
6 changed files with 117 additions and 6 deletions

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using JetBrains.Annotations;
using Portoa.Web.Controllers;
using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Models;
using VideoGameQuotes.Web.Security;
@ -21,7 +23,6 @@ namespace VideoGameQuotes.Web.Controllers {
public ActionResult Submit() {
var model = new QuoteSubmitModel();
ResetModel(model);
return View(model);
}
@ -29,6 +30,7 @@ namespace VideoGameQuotes.Web.Controllers {
public ActionResult Submit(QuoteSubmitModel model) {
if (!ModelState.IsValid) {
ResetModel(model);
return View(model);
}
@ -39,6 +41,26 @@ namespace VideoGameQuotes.Web.Controllers {
Text = model.QuoteText
};
//categories
if ((model.CategoryIds != null && model.CategoryIds.Count > 0)) {
//modifying systems, so remove errthing
quote.ClearCategories();
if (model.CategoryIds != null && model.CategoryIds.Count > 0) {
foreach (var categoryId in model.CategoryIds) {
quote.AddCategory(quoteService.GetCategory(categoryId));
}
}
//if (!string.IsNullOrEmpty(model.SystemName)) {
// game.AddSystem(new GamingSystem {
// Name = model.SystemName,
// Abbreviation = model.SystemAbbreviation,
// ReleaseDate = model.SystemReleaseDate
// });
//}
}
if (quote.Game == null) {
ResetModel(model);
return View(model);
@ -129,5 +151,17 @@ namespace VideoGameQuotes.Web.Controllers {
public ActionResult Quote(int id) {
return View(quoteService.GetQuote(id));
}
[HttpPost]
public JsonResult CreateCategory(Category category) {
try {
category = quoteService.SaveCategory(category);
var data = new Dictionary<string, string> { { "categoryId", category.Id.ToString() }, { "categoryName", category.Name } };
return Json(this.CreateJsonResponse(null, data));
} catch (Exception e) {
return Json(this.CreateJsonErrorResponse(e));
}
}
}
}

View File

@ -40,6 +40,7 @@ namespace VideoGameQuotes.Web {
routes.MapRoute("about", "about", new { controller = "Home", action = "About" });
routes.MapRoute("contact", "contact", new { controller = "Home", action = "Contact" });
routes.MapRoute("submit", "submit", new { controller = "Quote", action = "Submit" });
routes.MapRoute("create-category", "category/create", new { controller = "Quote", action = "CreateCategory" });
routes.MapRoute("individual-quote", "q/{id}/{*text}", new { controller = "Quote", action = "Quote" }, new { id = @"\d+" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

View File

@ -54,8 +54,9 @@ namespace VideoGameQuotes.Web.Models {
.Concat(AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name }));
}
private static string MakeTable<T>(int cellsPerRow, IEnumerable<T> items, Func<T, string> cellDataCallback) {
private static string MakeTable<T>(string tableId, int cellsPerRow, IEnumerable<T> items, Func<T, string> cellDataCallback) {
var table = new TagBuilder("table");
table.MergeAttribute("id", tableId);
var count = 0;
var row = new TagBuilder("tr");
var cell = new TagBuilder("td");
@ -84,6 +85,7 @@ namespace VideoGameQuotes.Web.Models {
public string MakePublisherTable(HtmlHelper<QuoteSubmitModel> html, int cellsPerRow = 8) {
return MakeTable(
"publisher-checkbox-table",
cellsPerRow,
AllPublishers,
publisher => CreateCheckbox(html, "PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name)
@ -92,6 +94,7 @@ namespace VideoGameQuotes.Web.Models {
public string MakeSystemTable(HtmlHelper<QuoteSubmitModel> html, int cellsPerRow = 8) {
return MakeTable(
"system-checkbox-table",
cellsPerRow,
AllSystems,
system => CreateCheckbox(html, "SystemIds", system.Id, "system_" + system.Id, system.Abbreviation)
@ -100,6 +103,7 @@ namespace VideoGameQuotes.Web.Models {
public string MakeCategoryTable(HtmlHelper<QuoteSubmitModel> html, int cellsPerRow = 8) {
return MakeTable(
"category-checkbox-table",
cellsPerRow,
AllCategories,
category => CreateCheckbox(html, "CategoryIds", category.Id, "category_" + category.Id, category.Name)
@ -108,6 +112,7 @@ namespace VideoGameQuotes.Web.Models {
public string MakeRegionTable(HtmlHelper<QuoteSubmitModel> html, int cellsPerRow = 8) {
return MakeTable(
"region-checkbox-table",
cellsPerRow,
(IEnumerable<Region>)Enum.GetValues(typeof(Region)),
region => CreateCheckbox(html, "GameRegions", region, "region_" + (int)region, region.ToString())

View File

@ -15,6 +15,7 @@ namespace VideoGameQuotes.Web.Services {
Publisher GetPublisher(int id);
GamingSystem GetSystem(int systemId);
Category GetCategory(int categoryId);
Category SaveCategory(Category category);
}
public class QuoteService : IQuoteService {
@ -86,5 +87,10 @@ namespace VideoGameQuotes.Web.Services {
public Category GetCategory(int categoryId) {
return categoryRepository.FindById(categoryId);
}
[UnitOfWork]
public Category SaveCategory(Category category) {
return categoryRepository.Save(category);
}
}
}

View File

@ -124,7 +124,6 @@
<script type="text/javascript">//<![CDATA[
$(document).ready(function() {
$("#create-game-link").click(function() {
//show the create game form
var $form = $("#create-game-form");
if ($form.is(":visible")) {
$("#create-game-link").text("Create new game");
@ -139,7 +138,6 @@
});
$("#create-system-link").click(function() {
//show the create game form
var $form = $("#create-system-form");
if ($form.is(":visible")) {
$("#create-system-link").text("Create new system");
@ -153,7 +151,6 @@
});
$("#create-publisher-link").click(function() {
//show the create game form
var $form = $("#create-publisher-form");
if ($form.is(":visible")) {
$("#create-publisher-link").text("Create new publisher");
@ -165,6 +162,71 @@
$("#publisher-select").toggle();
return false;
});
$("#create-category-link").click(function() {
var $table = $("#category-checkbox-table");
var $row = $table.find("tr:last");
if ($row.find("#new-category-name").length === 0) {
var $cell = $("<td/>");
if ($row.find("td").length === 8) {
$row = $("<tr/>");
$table.append($row);
}
$row.append($cell);
var $input = $("<input/>").attr("id", "new-category-name").attr("type", "text").val("Category name");
$input.bind("keypress", function(e) {
if (e.which === 13) {
e.preventDefault(); //make sure the parent form doesn't get submitted
$input.attr("disabled", "disabled");
$.ajax("/category/create", {
data: { Name: $input.val() },
type: "POST",
success: function(data, status, $xhr) {
if (data.Error !== null) {
alert("An error occurred: " + data.Error);
return;
}
//add category checkbox to table
var $checkbox = $("<input/>")
.attr({
"id": "category_" + data.Data.categoryId,
"type": "checkbox",
"name": "CategoryIds",
"checked": "checked"
}).val(data.Data.categoryId);
var $label = $("<label/>")
.attr("for", $checkbox.attr("id"))
.text(data.Data.categoryName);
$input.before($checkbox).before($label);
},
error: function($xhr, status, error) {
alert("An error occurred");
},
complete: function() {
$input.remove();
}
});
}
});
$cell.append($input);
$input.select();
$(this).text("Cancel new category");
} else {
$row.find("td:last").remove();
$(this).text("Create new category");
}
return false;
});
});
//]]></script>

View File

@ -132,7 +132,7 @@ legend {
border-bottom: 2px solid #000000;
}
#create-game-form, #create-system-form, #create-publisher-form {
#create-game-form, #create-system-form, #create-publisher-form, #create-category-form {
display: none;
}
#create-quote-form input[type="text"] {
@ -145,6 +145,9 @@ legend {
#create-quote-form td {
padding: 5px;
}
#new-category-name {
width: 100px !important;
}
#footer {
text-align: center;