Skip to content

Instantly share code, notes, and snippets.

View GeoffCox's full-sized avatar
🏠
Working from home

Geoff Cox GeoffCox

🏠
Working from home
View GitHub Profile
@GeoffCox
GeoffCox / LFtoCRLF.regex
Last active November 17, 2015 16:22
Regex to replace LFWithCRLF
Find: (?<!\r)\n
Replace: \r\n
Using regular expressions
@GeoffCox
GeoffCox / measureSvgText.js
Last active August 29, 2015 14:28
Measure text in SVG
function measureSvgText(text, className) {
if (!text || text.length === 0) {
return { width: 0, height: 0 };
}
var svg = d3.select('body').append('svg');
if (className) {
svg.attr('class', className);
}
@GeoffCox
GeoffCox / measureHtmlText.js
Created August 25, 2015 17:22
Measure text in HTML
var measureHtmlText = function (text, className) {
var ruler = $('<span>');
ruler.addClass(className)
.css('display', 'none')
.css('white-space', 'nowrap')
.appendTo($('body'));
ruler.text(text);
var width = ruler.width();
var height = ruler.height();
ruler.remove();
@GeoffCox
GeoffCox / git.config
Last active December 16, 2016 01:19
Update GIT.config to use Visual Studio for mergetool
[diff]
tool = vsdiffmerge
[difftool]
prompt = false
[difftool "vsdiffmerge"]
cmd = "'C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/vsdiffmerge.exe' $LOCAL $REMOTE //t"
keepbackup = false
trustexitcode = true
@GeoffCox
GeoffCox / CloseWithoutPrompt.js
Created April 16, 2015 16:41
Close browser without prompting the user
// This opens a window and sets the opener to the current window, then closes it.
var win = window.open("", "_top", "", true);
win.opener = window;
win.close();
@GeoffCox
GeoffCox / CopyToClipboard.js
Created April 16, 2015 16:39
Copy HTML to clipboard in any browser
// It may be worth making a copy of the element within <body> that is hidden
// or even producing a section of items to copy.
// This way you can remove any interactivity styles that make the copied content not
// look good when pasted into other applications.
var element = document.getElementById('ClipboardTemp');
// This technique is simulating the user selecting these elements.
// Think of a ControlRange as a selection of controls and then executing the command like the user pressing 'Ctrl+C'
element.contentEditable = 'true';
var controlRange;
@GeoffCox
GeoffCox / Global.asax.cs
Created April 16, 2015 00:07
Allow multiple origins for CORS using ASP.NET MVC
protected void Application_BeginRequest()
{
this.SetupCorsHeaders();
//OPTIONS request comes before the POST to know the permissions. this forces to send the reply.
if (((IList)Request.Headers.AllKeys).Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
@GeoffCox
GeoffCox / OverlayJSON.cs
Last active August 29, 2015 14:17
Overlay values over default values in a JSON object
private static object ApplyDefaultData(object data, object defaultData)
{
if (defaultData != null)
{
var target = new ExpandoObject();
// I apply the default values
string defaultJson = JsonConvert.SerializeObject(defaultData, _jsonSerializerSettings);
JsonConvert.PopulateObject(defaultJson, target, _jsonSerializerSettings);
@GeoffCox
GeoffCox / ScaleFontSizeToFit
Created March 9, 2015 22:30
Scale font-size until text fits within element
function scaleFontToFit(element) {
if (element) {
// for an element
var $element = $(element);
if ($element.css("overflow") == "hidden") {
var elementHeight = $element.height();
var fontSize = parseInt($element.css("font-size"), 10);
var $fullElement = $(element).clone().css('overflow', 'visible').height('auto');
@GeoffCox
GeoffCox / HandleWebApiErrorAttribute.cs
Created January 8, 2015 21:26
Web API Error Attribute
// This attribute helps return exception text from web API methods when applied to a MVC Web API controller.
public class HandleWebApiErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var exception = context.Exception as Exception;
if (exception != null)
{
var message = exception.Message;
message = message.Replace('\r', ' ').Replace('\n', ' ');