From d8289eb59b6f384ab8b2f7710eed7505fc9a4050 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Sun, 6 Jun 2010 18:23:55 -0400 Subject: [PATCH] (new) auto stylesheet refreshing with '#!refresh' --- lib/less/browser.js | 71 +++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/less/browser.js b/lib/less/browser.js index 2df40095..634e0980 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -3,39 +3,66 @@ // var sheets = []; -if (!document.querySelectorAll && typeof(jQuery) === "undefined") { - log("No selector method found"); -} else { - sheets = (document.querySelectorAll || jQuery).call(document, 'link[rel="stylesheet/less"]'); -} - less.env = location.hostname == '127.0.0.1' || location.hostname == '0.0.0.0' || location.hostname == 'localhost' || location.protocol == 'file:' ? 'development' : 'production'; -for (var i = 0; i < sheets.length; i++) { - (function (sheet) { // Because the functions here are async, we need to create a closure - var css = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href); - var styles = css && JSON.parse(css); - xhr(sheet.href, function (data, lastModified) { - if (styles && (new(Date)(lastModified).valueOf() === - new(Date)(styles.timestamp).valueOf())) { - // Use local copy - createCSS(styles.css, sheet); +// Load the stylesheets when the body is ready +var readyTimer = setInterval(function () { + if (document.body) { + if (!document.querySelectorAll && typeof(jQuery) === "undefined") { + log("No selector method found"); + } else { + sheets = (document.querySelectorAll || jQuery).call(document, 'link[rel="stylesheet/less"]'); + } + clearInterval(readyTimer); + loadStyleSheets(function (sheet, env) { + if (env.local) { log("less: loading " + sheet.href + " from local storage."); } else { - // Use remote copy (re-parse) - new(less.Parser)({ optimization: 3 }).parse(data, function (e, root) { - if (e) { return error(e, sheet.href) } - createCSS(root.toCSS(), sheet, lastModified); - log("less: parsed " + sheet.href + " successfully."); - }); + log("less: parsed " + sheet.href + " successfully."); } }); - })(sheets[i]); + } +}, 10); + +// +// Auto-refresh +// +if (less.env === 'development') { + refreshTimer = setInterval(function () { + if (/!refresh/.test(location.hash)) { + loadStyleSheets(function () {}); + } + }, 1000); +} + +function loadStyleSheets(callback) { + for (var i = 0; i < sheets.length; i++) { + (function (sheet) { // Because the functions here are async, we need to create a closure + var css = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href); + var styles = css && JSON.parse(css); + + xhr(sheet.href, function (data, lastModified) { + if (styles && (new(Date)(lastModified).valueOf() === + new(Date)(styles.timestamp).valueOf())) { + // Use local copy + createCSS(styles.css, sheet); + callback(sheet, {local: true}) + } else { + // Use remote copy (re-parse) + new(less.Parser)({ optimization: 3 }).parse(data, function (e, root) { + if (e) { return error(e, sheet.href) } + createCSS(root.toCSS(), sheet, lastModified); + callback(sheet, {local: false}) + }); + } + }); + })(sheets[i]); + } } function createCSS(styles, sheet, lastModified) {