Created
October 15, 2012 21:08
-
-
Save jakejscott/3895499 to your computer and use it in GitHub Desktop.
simple SignalR based css file watcher live reload thingy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ($, window, undefined) { | |
'use strict'; | |
var $doc = $(document), | |
$head = $doc.find('head'), | |
Modernizr = window.Modernizr; | |
$.fn.foundationAlerts ? $doc.foundationAlerts() : null; | |
$.fn.foundationButtons ? $doc.foundationButtons() : null; | |
$.fn.foundationAccordion ? $doc.foundationAccordion() : null; | |
$.fn.foundationNavigation ? $doc.foundationNavigation() : null; | |
$.fn.foundationTopBar ? $doc.foundationTopBar() : null; | |
$.fn.foundationCustomForms ? $doc.foundationCustomForms() : null; | |
$.fn.foundationMediaQueryViewer ? $doc.foundationMediaQueryViewer() : null; | |
$.fn.foundationTabs ? $doc.foundationTabs({callback : $.foundation.customForms.appendCustomMarkup}) : null; | |
$.fn.foundationTooltips ? $doc.foundationTooltips() : null; | |
$('input, textarea').placeholder(); | |
// UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids | |
// $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'}); | |
// $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'}); | |
// $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'}); | |
// $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'}); | |
// Hide address bar on mobile devices | |
if (Modernizr.touch) { | |
$(window).load(function () { | |
setTimeout(function () { | |
window.scrollTo(0, 1); | |
}, 0); | |
}); | |
} | |
$doc.ready(function() { | |
console.log('mailboxd.'); | |
var cssWatcher = $.connection.cssWatcher; | |
var sequence = Math.floor(Math.random() * 1000000000001); | |
$.extend(cssWatcher, { | |
reload: function () { | |
var style = $doc.find('link'); | |
var url = style.attr('href'); | |
if (url.indexOf("?v=") == -1) { | |
url = url + '?v=' + sequence; | |
} | |
sequence++; | |
$head.append($('<link href="' + url + '" rel="stylesheet">')); | |
} | |
}); | |
$.connection.hub.start().done(function() { | |
cssWatcher.ping(function(pong) { | |
console.log(pong); | |
}); | |
}); | |
}); | |
})(jQuery, this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.IO; | |
using System.Web; | |
using SignalR; | |
using SignalR.Hubs; | |
[HubName("cssWatcher")] | |
public class CssWatcher : Hub | |
{ | |
public static void Init() | |
{ | |
var fileWriteTime = new Hashtable(); | |
var cssPath = Path.Combine(HttpRuntime.AppDomainAppPath, "stylesheets"); | |
var fsw = new FileSystemWatcher(cssPath) | |
{ | |
Filter = "*.css", | |
IncludeSubdirectories = false | |
}; | |
fsw.Changed += (sender, args) => | |
{ | |
try | |
{ | |
var fullPath = args.FullPath; | |
var currentLastWriteTime = File.GetLastWriteTime(fullPath).ToString(); | |
if (fileWriteTime.ContainsKey(fullPath) && fileWriteTime[fullPath].ToString() == currentLastWriteTime) | |
return; | |
var instance = GlobalHost.ConnectionManager.GetHubContext<CssWatcher>(); | |
instance.Clients.reload(); | |
fileWriteTime[fullPath] = currentLastWriteTime; | |
} | |
catch | |
{ | |
} | |
}; | |
fsw.EnableRaisingEvents = true; | |
} | |
public string Ping() | |
{ | |
return "Pong"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment