cleanup browser.js, make ajax synch by default, with option to turn off

This commit is contained in:
cloudhead
2010-06-14 21:30:56 -04:00
parent 4135bbccbe
commit 6705f8e501

View File

@@ -7,9 +7,36 @@ less.env = location.hostname == '127.0.0.1' ||
location.port.length > 0 ? 'development'
: 'production';
// Load styles asynchronously (default: false)
//
// This is set to `false` by default, so that the body
// doesn't start loading before the stylesheets are parsed.
// Setting this to `true` can result in flickering.
//
less.async = false;
//
// Watch mode
//
less.watch = function () { return this.watchMode = true };
less.unwatch = function () { return this.watchMode = false };
if (less.env === 'development') {
if (/!watch/.test(location.hash)) {
less.watch();
}
less.watchTimer = setInterval(function () {
if (less.watchMode) {
loadStyleSheets(function (root, sheet, lastModified) {
if (root) {
createCSS(root.toCSS(), sheet, lastModified);
}
});
}
}, 1000);
}
//
// Select all links with the 'rel' attribute set to "less"
//
@@ -24,25 +51,6 @@ loadStyleSheets(function (root, sheet, env) {
}
});
if (less.env === 'development' && /!refresh/.test(location.hash)) {
less.watchMode = true;
}
//
// Auto-refresh
//
if (less.env === 'development') {
refreshTimer = setInterval(function () {
if (less.watchMode) {
loadStyleSheets(function (root, sheet, lastModified) {
if (root) {
createCSS(root.toCSS(), sheet, lastModified);
}
});
}
}, 1000);
}
function select(str) {
if (!document.querySelectorAll && typeof(jQuery) === "undefined") {
log("No selector method found");
@@ -51,18 +59,18 @@ function select(str) {
}
}
function loadStyleSheets(callback) {
function loadStyleSheets(callback, async) {
for (var i = 0; i < sheets.length; i++) {
loadStyleSheet(sheets[i], callback);
loadStyleSheet(sheets[i], callback, async);
}
}
function loadStyleSheet(sheet, callback) {
function loadStyleSheet(sheet, callback, async) {
var css = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href);
var timestamp = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
xhr(sheet.href, function (data, lastModified) {
xhr(sheet.href, async, function (data, lastModified) {
if (styles && (new(Date)(lastModified).valueOf() ===
new(Date)(styles.timestamp).valueOf())) {
// Use local copy
@@ -121,7 +129,7 @@ function createCSS(styles, sheet, lastModified) {
}
}
function xhr(url, callback, errback) {
function xhr(url, async, callback, errback) {
var xhr = getXMLHttpRequest();
if (window.location.protocol === "file:") {
@@ -133,7 +141,7 @@ function xhr(url, callback, errback) {
errback(xhr.status);
}
} else {
xhr.open('GET', url, true);
xhr.open('GET', url, async || less.async);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
@@ -235,6 +243,9 @@ function error(e, href) {
}
}
//
// Used by `@import` directives
//
less.Parser.importer = function (path, paths, callback) {
loadStyleSheet({ href: path, title: path }, function (root) {
callback(root);