(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"]');
less.refresh = function () {
less.refresh = function (reload) {
loadStyleSheets(function (root, sheet, env) {
if (env.local) {
log("less: loading " + sheet.href + " from local storage.");
@ -62,7 +62,7 @@ less.refresh = function () {
createCSS(root.toCSS(), sheet, env.lastModified);
log("less: parsed " + sheet.href + " successfully.");
}
});
}, reload);
};
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++) {
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 timestamp = cache && cache.getItem(sheet.href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
xhr(sheet.href, async, function (data, lastModified) {
if (styles && (new(Date)(lastModified).valueOf() ===
new(Date)(styles.timestamp).valueOf())) {
xhr(sheet.href, function (data, lastModified) {
if (!reload && styles &&
(new(Date)(lastModified).valueOf() ===
new(Date)(styles.timestamp).valueOf())) {
// Use local copy
createCSS(styles.css, sheet);
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
if (lastModified && cache) {
log('saving ' + sheet.href + ' to cache.');
cache.setItem(sheet.href, styles);
cache.setItem(sheet.href + ':timestamp', lastModified);
}
}
function xhr(url, async, callback, errback) {
function xhr(url, callback, errback) {
var xhr = getXMLHttpRequest();
async = isFileProtocol ? false : (async || less.async);
var async = isFileProtocol ? false : less.async;
xhr.open('GET', url, async);
xhr.send(null);