(api) less.refresh(true) reloads stylesheets from source

This commit is contained in:
cloudhead 2010-06-16 18:57:51 -04:00
parent f3a3ece9c0
commit 2d55321810

View File

@ -54,7 +54,7 @@ var cache = (typeof(window.localStorage) === 'undefined') ? null : window.localS
// //
var sheets = select('link[rel="stylesheet/less"]'); var sheets = select('link[rel="stylesheet/less"]');
less.refresh = function () { less.refresh = function (reload) {
loadStyleSheets(function (root, sheet, env) { loadStyleSheets(function (root, sheet, env) {
if (env.local) { if (env.local) {
log("less: loading " + sheet.href + " from local storage."); log("less: loading " + sheet.href + " from local storage.");
@ -62,7 +62,7 @@ less.refresh = function () {
createCSS(root.toCSS(), sheet, env.lastModified); createCSS(root.toCSS(), sheet, env.lastModified);
log("less: parsed " + sheet.href + " successfully."); log("less: parsed " + sheet.href + " successfully.");
} }
}); }, reload);
}; };
less.refresh(); less.refresh();
@ -75,20 +75,21 @@ function select(str) {
} }
} }
function loadStyleSheets(callback, async) { function loadStyleSheets(callback, reload) {
for (var i = 0; i < sheets.length; i++) { for (var i = 0; i < sheets.length; i++) {
loadStyleSheet(sheets[i], callback, async); loadStyleSheet(sheets[i], callback, reload);
} }
} }
function loadStyleSheet(sheet, callback, async) { function loadStyleSheet(sheet, callback, reload) {
var css = cache && cache.getItem(sheet.href); var css = cache && cache.getItem(sheet.href);
var timestamp = cache && cache.getItem(sheet.href + ':timestamp'); var timestamp = cache && cache.getItem(sheet.href + ':timestamp');
var styles = { css: css, timestamp: timestamp }; var styles = { css: css, timestamp: timestamp };
xhr(sheet.href, async, function (data, lastModified) { xhr(sheet.href, function (data, lastModified) {
if (styles && (new(Date)(lastModified).valueOf() === if (!reload && styles &&
new(Date)(styles.timestamp).valueOf())) { (new(Date)(lastModified).valueOf() ===
new(Date)(styles.timestamp).valueOf())) {
// Use local copy // Use local copy
createCSS(styles.css, sheet); createCSS(styles.css, sheet);
callback(null, sheet, { local: true }); callback(null, sheet, { local: true });
@ -146,15 +147,15 @@ function createCSS(styles, sheet, lastModified) {
// Don't update the local store if the file wasn't modified // Don't update the local store if the file wasn't modified
if (lastModified && cache) { if (lastModified && cache) {
log('saving ' + sheet.href + ' to cache.');
cache.setItem(sheet.href, styles); cache.setItem(sheet.href, styles);
cache.setItem(sheet.href + ':timestamp', lastModified); cache.setItem(sheet.href + ':timestamp', lastModified);
} }
} }
function xhr(url, async, callback, errback) { function xhr(url, callback, errback) {
var xhr = getXMLHttpRequest(); var xhr = getXMLHttpRequest();
var async = isFileProtocol ? false : less.async;
async = isFileProtocol ? false : (async || less.async);
xhr.open('GET', url, async); xhr.open('GET', url, async);
xhr.send(null); xhr.send(null);