Initial commit
[yaffs-website] / node_modules / livereload-js / lib / livereload.js
1 (function() {
2   var Connector, LiveReload, Options, Reloader, Timer,
3     __hasProp = {}.hasOwnProperty;
4
5   Connector = require('./connector').Connector;
6
7   Timer = require('./timer').Timer;
8
9   Options = require('./options').Options;
10
11   Reloader = require('./reloader').Reloader;
12
13   exports.LiveReload = LiveReload = (function() {
14     function LiveReload(window) {
15       var k, v, _ref;
16       this.window = window;
17       this.listeners = {};
18       this.plugins = [];
19       this.pluginIdentifiers = {};
20       this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.location.href.match(/LR-verbose/) ? this.window.console : {
21         log: function() {},
22         error: this.window.console.error.bind(this.window.console)
23       } : {
24         log: function() {},
25         error: function() {}
26       };
27       if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
28         this.console.error("LiveReload disabled because the browser does not seem to support web sockets");
29         return;
30       }
31       if ('LiveReloadOptions' in window) {
32         this.options = new Options();
33         _ref = window['LiveReloadOptions'];
34         for (k in _ref) {
35           if (!__hasProp.call(_ref, k)) continue;
36           v = _ref[k];
37           this.options.set(k, v);
38         }
39       } else {
40         this.options = Options.extract(this.window.document);
41         if (!this.options) {
42           this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
43           return;
44         }
45       }
46       this.reloader = new Reloader(this.window, this.console, Timer);
47       this.connector = new Connector(this.options, this.WebSocket, Timer, {
48         connecting: (function(_this) {
49           return function() {};
50         })(this),
51         socketConnected: (function(_this) {
52           return function() {};
53         })(this),
54         connected: (function(_this) {
55           return function(protocol) {
56             var _base;
57             if (typeof (_base = _this.listeners).connect === "function") {
58               _base.connect();
59             }
60             _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ").");
61             return _this.analyze();
62           };
63         })(this),
64         error: (function(_this) {
65           return function(e) {
66             if (e instanceof ProtocolError) {
67               if (typeof console !== "undefined" && console !== null) {
68                 return console.log("" + e.message + ".");
69               }
70             } else {
71               if (typeof console !== "undefined" && console !== null) {
72                 return console.log("LiveReload internal error: " + e.message);
73               }
74             }
75           };
76         })(this),
77         disconnected: (function(_this) {
78           return function(reason, nextDelay) {
79             var _base;
80             if (typeof (_base = _this.listeners).disconnect === "function") {
81               _base.disconnect();
82             }
83             switch (reason) {
84               case 'cannot-connect':
85                 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec.");
86               case 'broken':
87                 return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec.");
88               case 'handshake-timeout':
89                 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec.");
90               case 'handshake-failed':
91                 return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec.");
92               case 'manual':
93                 break;
94               case 'error':
95                 break;
96               default:
97                 return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec.");
98             }
99           };
100         })(this),
101         message: (function(_this) {
102           return function(message) {
103             switch (message.command) {
104               case 'reload':
105                 return _this.performReload(message);
106               case 'alert':
107                 return _this.performAlert(message);
108             }
109           };
110         })(this)
111       });
112       this.initialized = true;
113     }
114
115     LiveReload.prototype.on = function(eventName, handler) {
116       return this.listeners[eventName] = handler;
117     };
118
119     LiveReload.prototype.log = function(message) {
120       return this.console.log("" + message);
121     };
122
123     LiveReload.prototype.performReload = function(message) {
124       var _ref, _ref1;
125       this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2)));
126       return this.reloader.reload(message.path, {
127         liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
128         liveImg: (_ref1 = message.liveImg) != null ? _ref1 : true,
129         originalPath: message.originalPath || '',
130         overrideURL: message.overrideURL || '',
131         serverURL: "http://" + this.options.host + ":" + this.options.port
132       });
133     };
134
135     LiveReload.prototype.performAlert = function(message) {
136       return alert(message.message);
137     };
138
139     LiveReload.prototype.shutDown = function() {
140       var _base;
141       if (!this.initialized) {
142         return;
143       }
144       this.connector.disconnect();
145       this.log("LiveReload disconnected.");
146       return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
147     };
148
149     LiveReload.prototype.hasPlugin = function(identifier) {
150       return !!this.pluginIdentifiers[identifier];
151     };
152
153     LiveReload.prototype.addPlugin = function(pluginClass) {
154       var plugin;
155       if (!this.initialized) {
156         return;
157       }
158       if (this.hasPlugin(pluginClass.identifier)) {
159         return;
160       }
161       this.pluginIdentifiers[pluginClass.identifier] = true;
162       plugin = new pluginClass(this.window, {
163         _livereload: this,
164         _reloader: this.reloader,
165         _connector: this.connector,
166         console: this.console,
167         Timer: Timer,
168         generateCacheBustUrl: (function(_this) {
169           return function(url) {
170             return _this.reloader.generateCacheBustUrl(url);
171           };
172         })(this)
173       });
174       this.plugins.push(plugin);
175       this.reloader.addPlugin(plugin);
176     };
177
178     LiveReload.prototype.analyze = function() {
179       var plugin, pluginData, pluginsData, _i, _len, _ref;
180       if (!this.initialized) {
181         return;
182       }
183       if (!(this.connector.protocol >= 7)) {
184         return;
185       }
186       pluginsData = {};
187       _ref = this.plugins;
188       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
189         plugin = _ref[_i];
190         pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {};
191         pluginData.version = plugin.constructor.version;
192       }
193       this.connector.sendCommand({
194         command: 'info',
195         plugins: pluginsData,
196         url: this.window.location.href
197       });
198     };
199
200     return LiveReload;
201
202   })();
203
204 }).call(this);