Skip to content

Instantly share code, notes, and snippets.

View andrewbranch's full-sized avatar

Andrew Branch andrewbranch

View GitHub Profile
@andrewbranch
andrewbranch / ErrorsController.cs
Last active December 18, 2015 02:18
Files (or snippets of files) to set up a highly flexible custom errors system that works for jQuery AJAX and synchronous calls alike for an ASP.NET MVC project.
using System.Net;
using System.Web.Mvc;
namespace ProjectName.Controllers
{
public class ErrorController : Controller
{
public ViewResult NotFound() {
Response.StatusCode = (int)HttpStatusCode.NotFound;
@andrewbranch
andrewbranch / Country.ascx
Last active November 4, 2023 11:01
Easy state and country dropdowns for MVC .NET, current as of 8/2013
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<% string defaultCountry = "United States";
if (ViewData.ContainsKey("default")) {
defaultCountry = (string)ViewData["default"];
}
%>
<select id="<%= Html.IdFor(model => model) %>" name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>"<% if (ViewData.ContainsKey("html")) { foreach (var a in HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["html"])) { %> <%= a.Key %>="<%= a.Value %>"<% } } %>>
<% if (String.IsNullOrWhiteSpace(Model) && !Inspect.Models.Location.Countries.Keys.Contains(defaultCountry)) { %><option value="<%: defaultCountry %>"><%: defaultCountry %></option><% } %>
<% foreach (var country in Inspect.Models.Location.Countries.Keys) { %>
@andrewbranch
andrewbranch / getAuburnBuildings.js
Last active December 21, 2015 04:59
Gets an array of Auburn building names from the web API. Works from auburn.edu domain and subdomains.
var auburnBuildingNames;
window.getAuburnBuildingNames = function(callback) {
if (auburnBuildingNames) {
callback(auburnBuildingNames);
} else {
var buildings;
$.getJSON("https://cws.auburn.edu/map/api/3.0/building", function(data) {
buildings = $.map(data, function(building, i) {
return building.name;
@andrewbranch
andrewbranch / BundleConfig.cs
Last active December 21, 2015 11:38
Bundling with BundleTransformer in ASP.NET MVC. Less files need to have their Build Action set to "Content" in their file properties in order to publish successfully.
using System.Web;
using System.Web.Optimization;
using BundleTransformer.Core.Transformers;
namespace Project {
public class BundleConfig {
public static void RegisterBundles(BundleCollection bundles) {
@andrewbranch
andrewbranch / ExampleView.aspx
Created September 10, 2013 16:44
HtmlHelper to generate JavaScript object from C# object
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<%= Html.WriteToJavaScript(Model, "window.model") %>
</asp:Content>
@andrewbranch
andrewbranch / autoshrink.js
Last active July 31, 2019 22:07
Responds to window resize events. Max font size taken from initial font size (specify with CSS).
(function($) {
function getTextWidth($element) {
var tester = $("<div/>").text($element.text())
.css({ "position": "absolute", "float": "left", "white-space": "nowrap", "visibility": "hidden", "font": $element.css("font"), "text-transform": $element.css("text-transform"), "letter-spacing": $element.css("letter-spacing") })
.appendTo($element.parent()),
width = tester.innerWidth();
tester.remove();
return width;
}
@andrewbranch
andrewbranch / select2-override.css
Last active October 16, 2017 14:04
Flat styling of select2.
.select2-container .select2-choice {
height: 34px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
background-color: #fff;
background-image: none;
background: #fff;
}
@andrewbranch
andrewbranch / KSSBluetoothController.h
Last active December 28, 2015 17:49
SwitchaBLE Light State characteristic write codes
typedef NS_OPTIONS(NSInteger, LightState) {
LightStateOff = 0,
LightStateOn = 1 << 0,
LightStateToggle = 1 << 1,
LightStatePulse = 1 << 2,
LightStateStrobe = 1 << 3
};
@andrewbranch
andrewbranch / fixBootstrapModalScrollbar.js
Last active December 31, 2015 07:09
One-up Bootstrap 3.0.0–3.0.3’s method of accounting for scrollbar width when modal is open.
// This function courtesy of lostsource: http://stackoverflow.com/questions/13382516
function getScrollbarWidth() {
var outer = document.createElement("div");
outer.style.visibility = "hidden";
outer.style.width = "100px";
document.body.appendChild(outer);
var widthNoScroll = outer.offsetWidth;
outer.style.overflow = "scroll";
var inner = document.createElement("div");
inner.style.width = "100%";
@andrewbranch
andrewbranch / EntityNotFoundException.cs
Last active August 29, 2015 13:55
Show a custom error page for a custom exception. For example, when trying to access a non-existent database record (app/people/details/9999999).
// You could put more information and functionality here if you want.
// I just needed an exception distinguishable from other exceptions.
// I implemented a "FindOrDie" method in my repository code, from which
// I throw this exception whenever a query comes up empty.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;