Skip to content

Instantly share code, notes, and snippets.

@kevinblake
kevinblake / Modular.js
Created October 22, 2013 13:22
Modular JS
var MyNamespace = MyNamespace || {};
MyNamespace.UI = MyNamespace.UI || {};
MyNamespace.UI.ModuleName = function ($, window) {
"use strict";
var my = {};
my.options = {
@kevinblake
kevinblake / google-analytics-data-events.js
Last active May 29, 2021 08:24
Fire Google Analytics Events using data attributes for label, action and category fields
var googleDataEvents = {
pageTracker: null,
init: function (document) {
document.find("a[data-ga-label],area[data-ga-label]").click(this.trackLink);
},
trackLink: function (e) {
if (_gaq) {
e.preventDefault();
var l = $(this);
var label = l.attr("data-ga-label");
@kevinblake
kevinblake / DeviceTypeRedirect.cs
Created December 13, 2012 14:45
MVC4 action filter attribute to redirect to a different controller if a page is not valid for mobile/non-mobile context
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.WebPages;
namespace MyNamespace
{
public enum DeviceType {Mobile, Desktop}
public class DeviceTypeRedirect : ActionFilterAttribute
{
@kevinblake
kevinblake / run.bat
Created December 10, 2012 09:53
Run .NET solution without Visual Studio
SET PORT=8086
SET SOLUTIONNAME=VS.Solution.Filename.sln
SET WEBDIRECTORY=VS.Solution.Web.WorkingDirectory
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild %CD%\%SOLUTIONNAME%
start "IIS Express %PORT% (%SOLUTIONNAME%)" "%PROGRAMFILES%\IIS Express\iisexpress" /path:%CD%\%WEBDIRECTORY% /port:%PORT%
start "" "http://localhost:%PORT%/"
@kevinblake
kevinblake / ICacheService.cs
Created December 7, 2012 10:03
In Memory Cache wrapper class
using System;
using System.Web.Caching;
namespace Puzzlebox.Caching
{
interface ICacheService
{
T Get<T>(string cacheId, Func<T> getItemCallback) where T : class;
T Get<T>(string cacheId, CacheDependency cacheDependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, Func<T> getItemCallback) where T : class;
}
@kevinblake
kevinblake / gist:4225907
Created December 6, 2012 16:41
Using timeago.js with C#
List View bound fields
<asp:BoundField DataField="Timestamp" HeaderText="Timestamp" ReadOnly="True" SortExpression="Timestamp" HtmlEncode="False" DataFormatString="<abbr class='timeago' title='{0:yyyy'-'MM'-'dd HH':'mm':'ss'Z'}'>{0}</abbr>" />
In code:
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ssZ");
@kevinblake
kevinblake / YouTubeUrl.cs
Created November 14, 2012 09:58
Parse YouTube Id from Url
public static class YouTubeUrl
{
private const string UrlRegex = @"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)";
public static string GetVideoId(string videoUrl)
{
var regex = new Regex(UrlRegex);
var match = regex.Match(videoUrl);
@kevinblake
kevinblake / LinkRenderer.cs
Created November 12, 2012 14:26
Umbraco - render local links from string
public class LinkRenderer
{
public static string Transform(string richText)
{
// take a rich text string, parse the local links
Regex ll = new Regex("(/{localLink:)(.*?)}");
var linkRenderer = new LinkRenderer();
MatchEvaluator myEvaluator = new MatchEvaluator(MatchLocalLinks);
@kevinblake
kevinblake / gist:4000628
Created November 2, 2012 11:50
Rebuild Examine Indexes on Application Start
using System.Collections.Generic;
using Examine;
using umbraco.businesslogic;
namespace MyApplication
{
public class ExamineIndexRebuild : ApplicationStartupHandler
{
@kevinblake
kevinblake / RemovePreviewButton.cs
Created October 30, 2012 15:04 — forked from nul800sebastiaan/RemovePreviewButton.cs
Remove preview button in Umbraco
public class RemovePreviewButton : ApplicationBase
{
public RemovePreviewButton()
{
umbracoPage.Load += this.umbracoPage_Load;
}
private void umbracoPage_Load(object sender, EventArgs e)
{
var umbracoPage = (umbracoPage)sender;