update livereload.js

This commit is contained in:
John Bintz 2011-11-29 14:00:55 -05:00
parent 1e884ba174
commit e1d0ada7d6

View File

@ -1,10 +1,9 @@
(function() {
var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __options = {}, __reloader = {}, __livereload = {}, __startup = {};
var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __options = {}, __reloader = {}, __livereload = {}, __less = {}, __startup = {};
// customevents
(function() {
var CustomEvents;
CustomEvents = {
var CustomEvents;
CustomEvents = {
bind: function(element, eventName, handler) {
if (element.addEventListener) {
return element.addEventListener(eventName, handler, false);
@ -33,29 +32,27 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
throw new Error("Attempt to fire custom event " + eventName + " on something which isn't a DOMElement");
}
}
};
__customevents.bind = CustomEvents.bind;
__customevents.fire = CustomEvents.fire;
}).call(this);
};
__customevents.bind = CustomEvents.bind;
__customevents.fire = CustomEvents.fire;
// protocol
(function() {
var PROTOCOL_6, PROTOCOL_7, Parser, ProtocolError;
var __indexOf = Array.prototype.indexOf || function(item) {
var PROTOCOL_6, PROTOCOL_7, Parser, ProtocolError;
var __indexOf = Array.prototype.indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] === item) return i;
}
return -1;
};
__protocol.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
__protocol.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
__protocol.ProtocolError = ProtocolError = (function() {
};
__protocol.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
__protocol.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
__protocol.ProtocolError = ProtocolError = (function() {
function ProtocolError(reason, data) {
this.message = "LiveReload protocol error (" + reason + ") after receiving data: \"" + data + "\".";
}
return ProtocolError;
})();
__protocol.Parser = Parser = (function() {
})();
__protocol.Parser = Parser = (function() {
function Parser(handlers) {
this.handlers = handlers;
this.reset();
@ -123,15 +120,14 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
return message;
};
return Parser;
})();
}).call(this);
})();
// connector
(function() {
var Connector, PROTOCOL_6, PROTOCOL_7, Parser, _ref;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ref = __protocol, Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
__connector.Connector = Connector = (function() {
var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
_ref = __protocol, Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
Version = '2.0.3';
__connector.Connector = Connector = (function() {
function Connector(options, WebSocket, Timer, handlers) {
this.options = options;
this.WebSocket = WebSocket;
@ -140,8 +136,10 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
this._uri = "ws://" + this.options.host + ":" + this.options.port + "/livereload";
this._nextDelay = this.options.mindelay;
this._connectionDesired = false;
this.protocol = 0;
this.protocolParser = new Parser({
connected: __bind(function(protocol) {
this.protocol = protocol;
this._handshakeTimeout.stop();
this._nextDelay = this.options.mindelay;
this._disconnectionReason = 'broken';
@ -231,15 +229,28 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
return this.socket.close();
};
Connector.prototype._onopen = function(e) {
var hello;
this.handlers.socketConnected();
this._disconnectionReason = 'handshake-failed';
this._sendCommand({
hello = {
command: 'hello',
protocols: [PROTOCOL_6, PROTOCOL_7]
});
};
hello.ver = Version;
if (this.options.ext) {
hello.ext = this.options.ext;
}
if (this.options.extver) {
hello.extver = this.options.extver;
}
if (this.options.snipver) {
hello.snipver = this.options.snipver;
}
this._sendCommand(hello);
return this._handshakeTimeout.start(this.options.handshake_timeout);
};
Connector.prototype._onclose = function(e) {
this.protocol = 0;
this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
return this._scheduleReconnection();
};
@ -248,14 +259,12 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
return this.protocolParser.process(e.data);
};
return Connector;
})();
}).call(this);
})();
// timer
(function() {
var Timer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
__timer.Timer = Timer = (function() {
var Timer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
__timer.Timer = Timer = (function() {
function Timer(func) {
this.func = func;
this.running = false;
@ -281,16 +290,14 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
}
};
return Timer;
})();
Timer.start = function(timeout, func) {
})();
Timer.start = function(timeout, func) {
return setTimeout(func, timeout);
};
}).call(this);
};
// options
(function() {
var Options;
__options.Options = Options = (function() {
var Options;
__options.Options = Options = (function() {
function Options() {
this.host = null;
this.port = 35729;
@ -312,8 +319,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
}
};
return Options;
})();
Options.extract = function(document) {
})();
Options.extract = function(document) {
var element, keyAndValue, m, mm, options, pair, src, _i, _j, _len, _len2, _ref, _ref2;
_ref = document.getElementsByTagName('script');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -339,14 +346,12 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
}
}
return null;
};
}).call(this);
};
// reloader
(function() {
var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
splitUrl = function(url) {
var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
splitUrl = function(url) {
var hash, index, params;
if ((index = url.indexOf('#')) >= 0) {
hash = url.slice(index);
@ -365,8 +370,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
params: params,
hash: hash
};
};
pathFromUrl = function(url) {
};
pathFromUrl = function(url) {
var path;
url = splitUrl(url).url;
if (url.indexOf('file://') === 0) {
@ -375,8 +380,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
path = url.replace(/^([^:]+:)?\/\/([^:\/]+)(:\d*)?\//, '/');
}
return decodeURIComponent(path);
};
pickBestMatch = function(path, objects, pathFunc) {
};
pickBestMatch = function(path, objects, pathFunc) {
var bestMatch, object, score, _i, _len;
bestMatch = {
score: 0
@ -396,8 +401,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
} else {
return null;
}
};
numberOfMatchingSegments = function(path1, path2) {
};
numberOfMatchingSegments = function(path1, path2) {
var comps1, comps2, eqCount, len;
path1 = path1.replace(/^\/+/, '').toLowerCase();
path2 = path2.replace(/^\/+/, '').toLowerCase();
@ -412,11 +417,11 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
++eqCount;
}
return eqCount;
};
pathsMatch = function(path1, path2) {
};
pathsMatch = function(path1, path2) {
return numberOfMatchingSegments(path1, path2) > 0;
};
IMAGE_STYLES = [
};
IMAGE_STYLES = [
{
selector: 'background',
styleNames: ['backgroundImage']
@ -424,8 +429,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
selector: 'border',
styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage']
}
];
__reloader.Reloader = Reloader = (function() {
];
__reloader.Reloader = Reloader = (function() {
function Reloader(window, console, Timer) {
this.window = window;
this.console = console;
@ -433,18 +438,29 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
this.document = this.window.document;
this.stylesheetGracePeriod = 200;
this.importCacheWaitPeriod = 200;
this.plugins = [];
}
Reloader.prototype.addPlugin = function(plugin) {
return this.plugins.push(plugin);
};
Reloader.prototype.analyze = function(callback) {
return results;
};
Reloader.prototype.reload = function(path, options) {
var plugin, _i, _len, _ref;
_ref = this.plugins;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
plugin = _ref[_i];
if (plugin.reload && plugin.reload(path, options)) {
return;
}
}
if (options.liveCSS) {
if (path.match(/\.css$/i)) {
if (this.reloadStylesheet(path)) {
return;
}
}
if (path.match(/\.less$/i) && this.window.less && this.window.less.refresh) {
this.window.less.refresh(true);
return;
}
}
if (options.liveImg) {
if (path.match(/\.(jpe?g|png|gif)$/i)) {
@ -534,7 +550,7 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
}
};
Reloader.prototype.reloadStylesheet = function(path) {
var imported, link, links, match, _i, _j, _len, _len2;
var imported, link, links, match, style, _i, _j, _k, _len, _len2, _len3, _ref;
links = (function() {
var _i, _len, _ref, _results;
_ref = this.document.getElementsByTagName('link');
@ -548,8 +564,15 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
return _results;
}).call(this);
imported = [];
for (_i = 0, _len = links.length; _i < _len; _i++) {
link = links[_i];
_ref = this.document.getElementsByTagName('style');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
style = _ref[_i];
if (style.sheet) {
this.collectImportedStylesheets(style, style.sheet, imported);
}
}
for (_j = 0, _len2 = links.length; _j < _len2; _j++) {
link = links[_j];
this.collectImportedStylesheets(link, link.sheet, imported);
}
this.console.log("LiveReload found " + links.length + " LINKed stylesheets, " + imported.length + " @imported stylesheets");
@ -566,8 +589,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
}
} else {
this.console.log("LiveReload will reload all stylesheets because path '" + path + "' did not match any specific one");
for (_j = 0, _len2 = links.length; _j < _len2; _j++) {
link = links[_j];
for (_k = 0, _len3 = links.length; _k < _len3; _k++) {
link = links[_k];
this.reattachStylesheetLink(link);
}
}
@ -679,21 +702,21 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
return url + params + hash;
};
return Reloader;
})();
}).call(this);
})();
// livereload
(function() {
var Connector, LiveReload, Options, Reloader, Timer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Connector = __connector.Connector;
Timer = __timer.Timer;
Options = __options.Options;
Reloader = __reloader.Reloader;
__livereload.LiveReload = LiveReload = (function() {
var Connector, LiveReload, Options, Reloader, Timer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Connector = __connector.Connector;
Timer = __timer.Timer;
Options = __options.Options;
Reloader = __reloader.Reloader;
__livereload.LiveReload = LiveReload = (function() {
function LiveReload(window) {
this.window = window;
this.listeners = {};
this.plugins = [];
this.pluginIdentifiers = {};
this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.console : {
log: function() {},
error: function() {}
@ -715,7 +738,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
if (typeof (_base = this.listeners).connect === "function") {
_base.connect();
}
return this.log("LiveReload is connected to " + this.options.host + ":" + this.options.port + " (protocol v" + protocol + ").");
this.log("LiveReload is connected to " + this.options.host + ":" + this.options.port + " (protocol v" + protocol + ").");
return this.analyze();
}, this),
error: __bind(function(e) {
if (e instanceof ProtocolError) {
@ -767,7 +791,8 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
this.log("LiveReload received reload request for " + message.path + ".");
return this.reloader.reload(message.path, {
liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
liveImg: (_ref2 = message.liveImg) != null ? _ref2 : true
liveImg: (_ref2 = message.liveImg) != null ? _ref2 : true,
originalPath: message.originalPath || ''
});
};
LiveReload.prototype.performAlert = function(message) {
@ -779,26 +804,123 @@ var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __opti
this.log("LiveReload disconnected.");
return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
};
LiveReload.prototype.hasPlugin = function(identifier) {
return !!this.pluginIdentifiers[identifier];
};
LiveReload.prototype.addPlugin = function(pluginClass) {
var plugin;
if (this.hasPlugin(pluginClass.identifier)) {
return;
}
this.pluginIdentifiers[pluginClass.identifier] = true;
plugin = new pluginClass(this.window, {
_livereload: this,
_reloader: this.reloader,
_connector: this.connector,
console: this.console,
Timer: Timer,
generateCacheBustUrl: __bind(function(url) {
return this.reloader.generateCacheBustUrl(url);
}, this)
});
this.plugins.push(plugin);
this.reloader.addPlugin(plugin);
};
LiveReload.prototype.analyze = function() {
var plugin, pluginData, pluginsData, _i, _len, _ref;
if (!(this.connector.protocol >= 7)) {
return;
}
pluginsData = {};
_ref = this.plugins;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
plugin = _ref[_i];
pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {};
pluginData.version = plugin.constructor.version;
}
this.connector.sendCommand({
command: 'info',
plugins: pluginsData,
url: this.window.location.href
});
};
return LiveReload;
})();
// less
var LessPlugin;
__less = LessPlugin = (function() {
LessPlugin.identifier = 'less';
LessPlugin.version = '1.0';
function LessPlugin(window, host) {
this.window = window;
this.host = host;
}
LessPlugin.prototype.reload = function(path, options) {
if (this.window.less && this.window.less.refresh) {
if (path.match(/\.less$/i)) {
return this.reloadLess(path);
}
if (options.originalPath.match(/\.less$/i)) {
return this.reloadLess(options.originalPath);
}
}
return false;
};
LessPlugin.prototype.reloadLess = function(path) {
var link, links, _i, _len;
links = (function() {
var _i, _len, _ref, _results;
_ref = document.getElementsByTagName('link');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
link = _ref[_i];
if (link.href && link.rel === 'stylesheet/less' || (link.rel.match(/stylesheet/) && link.type.match(/^text\/(x-)?less$/))) {
_results.push(link);
}
}
return _results;
})();
}).call(this);
if (links.length === 0) {
return false;
}
for (_i = 0, _len = links.length; _i < _len; _i++) {
link = links[_i];
link.href = this.host.generateCacheBustUrl(link.href);
}
this.host.console.log("LiveReload is asking LESS to recompile all stylesheets");
this.window.less.refresh(true);
return true;
};
LessPlugin.prototype.analyze = function() {
return {
disable: !!(this.window.less && this.window.less.refresh)
};
};
return LessPlugin;
})();
// startup
(function() {
var CustomEvents, LiveReload;
CustomEvents = __customevents;
LiveReload = window.LiveReload = new (__livereload.LiveReload)(window);
LiveReload.on('shutdown', function() {
var CustomEvents, LiveReload, k, v;
CustomEvents = __customevents;
LiveReload = window.LiveReload = new (__livereload.LiveReload)(window);
for (k in window) {
v = window[k];
if (k.match(/^LiveReloadPlugin/)) {
LiveReload.addPlugin(v);
}
}
LiveReload.addPlugin(__less);
LiveReload.on('shutdown', function() {
return delete window.LiveReload;
});
LiveReload.on('connect', function() {
});
LiveReload.on('connect', function() {
return CustomEvents.fire(document, 'LiveReloadConnect');
});
LiveReload.on('disconnect', function() {
});
LiveReload.on('disconnect', function() {
return CustomEvents.fire(document, 'LiveReloadDisconnect');
});
CustomEvents.bind(document, 'LiveReloadShutDown', function() {
});
CustomEvents.bind(document, 'LiveReloadShutDown', function() {
return LiveReload.shutDown();
});
}).call(this);
});
})();