mirror of
https://github.com/less/less.js.git
synced 2026-02-03 19:45:00 -05:00
(new) auto stylesheet refreshing with '#!refresh'
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user