Last active
January 12, 2017 20:08
-
-
Save marckrenn/3d97b1fd151581c32c4f88d498ddb69e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | |
hasProp = {}.hasOwnProperty; | |
Firebase = (function(superClass) { | |
var request; | |
extend(Firebase, superClass); | |
Firebase.define("status", { | |
get: function() { | |
return this._status; | |
} | |
}); | |
function Firebase(options) { | |
var base, base1, base2; | |
this.options = options != null ? options : {}; | |
this.projectID = (base = this.options).projectID != null ? base.projectID : base.projectID = null; | |
this.secret = (base1 = this.options).secret != null ? base1.secret : base1.secret = null; | |
this.debug = (base2 = this.options).debug != null ? base2.debug : base2.debug = false; | |
if (this._status == null) { | |
this._status = "disconnected"; | |
} | |
this.secretEndPoint = this.secret ? "?auth=" + this.secret : ""; | |
Firebase.__super__.constructor.apply(this, arguments); | |
if (this.debug) { | |
console.log("Firebase: Connecting to Firebase Project '" + this.projectID + "' ... \n URL: 'https://" + this.projectID + ".firebaseio.com'"); | |
} | |
this.onChange("connection"); | |
} | |
request = function(project, secret, path, callback, method, data, parameters, debug) { | |
var url, xhttp; | |
url = "https://" + project + ".firebaseio.com" + path + ".json" + secret; | |
if (parameters !== void 0) { | |
if (parameters.shallow) { | |
url += "&shallow=true"; | |
} | |
if (parameters.format === "export") { | |
url += "&format=export"; | |
} | |
switch (parameters.print) { | |
case "pretty": | |
url += "&print=pretty"; | |
break; | |
case "silent": | |
url += "&print=silent"; | |
} | |
if (typeof parameters.download === "string") { | |
url += "&download=" + parameters.download; | |
window.open(url, "_self"); | |
} | |
if (typeof parameters.orderBy === "string") { | |
url += "&orderBy=" + '"' + parameters.orderBy + '"'; | |
} | |
if (typeof parameters.limitToFirst === "number") { | |
url += "&limitToFirst=" + parameters.limitToFirst; | |
} | |
if (typeof parameters.limitToLast === "number") { | |
url += "&limitToLast=" + parameters.limitToLast; | |
} | |
if (typeof parameters.startAt === "number") { | |
url += "&startAt=" + parameters.startAt; | |
} | |
if (typeof parameters.endAt === "number") { | |
url += "&endAt=" + parameters.endAt; | |
} | |
if (typeof parameters.equalTo === "number") { | |
url += "&equalTo=" + parameters.equalTo; | |
} | |
} | |
xhttp = new XMLHttpRequest; | |
if (debug) { | |
console.log("Firebase: New '" + method + "'-request with data: '" + (JSON.stringify(data)) + "' \n URL: '" + url + "'"); | |
} | |
xhttp.onreadystatechange = (function(_this) { | |
return function() { | |
if (parameters !== void 0) { | |
if (parameters.print === "silent" || typeof parameters.download === "string") { | |
return; | |
} | |
} | |
switch (xhttp.readyState) { | |
case 0: | |
if (debug) { | |
console.log("Firebase: Request not initialized \n URL: '" + url + "'"); | |
} | |
break; | |
case 1: | |
if (debug) { | |
console.log("Firebase: Server connection established \n URL: '" + url + "'"); | |
} | |
break; | |
case 2: | |
if (debug) { | |
console.log("Firebase: Request received \n URL: '" + url + "'"); | |
} | |
break; | |
case 3: | |
if (debug) { | |
console.log("Firebase: Processing request \n URL: '" + url + "'"); | |
} | |
break; | |
case 4: | |
if (callback != null) { | |
callback(JSON.parse(xhttp.responseText)); | |
} | |
if (debug) { | |
console.log("Firebase: Request finished, response: '" + (JSON.parse(xhttp.responseText)) + "' \n URL: '" + url + "'"); | |
} | |
} | |
if (xhttp.status === "404") { | |
if (debug) { | |
return console.warn("Firebase: Invalid request, page not found \n URL: '" + url + "'"); | |
} | |
} | |
}; | |
})(this); | |
xhttp.open(method, url, true); | |
xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8"); | |
return xhttp.send(data = "" + (JSON.stringify(data))); | |
}; | |
Firebase.prototype.get = function(path, callback, parameters) { | |
return request(this.projectID, this.secretEndPoint, path, callback, "GET", null, parameters, this.debug); | |
}; | |
Firebase.prototype.put = function(path, data, callback, parameters) { | |
return request(this.projectID, this.secretEndPoint, path, callback, "PUT", data, parameters, this.debug); | |
}; | |
Firebase.prototype.post = function(path, data, callback, parameters) { | |
return request(this.projectID, this.secretEndPoint, path, callback, "POST", data, parameters, this.debug); | |
}; | |
Firebase.prototype.patch = function(path, data, callback, parameters) { | |
return request(this.projectID, this.secretEndPoint, path, callback, "PATCH", data, parameters, this.debug); | |
}; | |
Firebase.prototype["delete"] = function(path, callback, parameters) { | |
return request(this.projectID, this.secretEndPoint, path, callback, "DELETE", null, parameters, this.debug); | |
}; | |
Firebase.prototype.onChange = function(path, callback) { | |
var currentStatus, source, url; | |
if (path === "connection") { | |
url = "https://" + this.projectID + ".firebaseio.com/.json" + this.secretEndPoint; | |
currentStatus = "disconnected"; | |
source = new EventSource(url); | |
source.addEventListener("open", (function(_this) { | |
return function() { | |
if (currentStatus === "disconnected") { | |
_this._status = "connected"; | |
if (callback != null) { | |
callback("connected"); | |
} | |
if (_this.debug) { | |
console.log("Firebase: Connection to Firebase Project '" + _this.projectID + "' established"); | |
} | |
} | |
return currentStatus = "connected"; | |
}; | |
})(this)); | |
return source.addEventListener("error", (function(_this) { | |
return function() { | |
if (currentStatus === "connected") { | |
_this._status = "disconnected"; | |
if (callback != null) { | |
callback("disconnected"); | |
} | |
if (_this.debug) { | |
console.warn("Firebase: Connection to Firebase Project '" + _this.projectID + "' closed"); | |
} | |
} | |
return currentStatus = "disconnected"; | |
}; | |
})(this)); | |
} else { | |
url = "https://" + this.projectID + ".firebaseio.com" + path + ".json" + this.secretEndPoint; | |
source = new EventSource(url); | |
if (this.debug) { | |
console.log("Firebase: Listening to changes made to '" + path + "' \n URL: '" + url + "'"); | |
} | |
source.addEventListener("put", (function(_this) { | |
return function(ev) { | |
if (callback != null) { | |
callback(JSON.parse(ev.data).data, "put", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"), 1)); | |
} | |
if (_this.debug) { | |
return console.log("Firebase: Received changes made to '" + path + "' via 'PUT': " + (JSON.parse(ev.data).data) + " \n URL: '" + url + "'"); | |
} | |
}; | |
})(this)); | |
return source.addEventListener("patch", (function(_this) { | |
return function(ev) { | |
if (callback != null) { | |
callback(JSON.parse(ev.data).data, "patch", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"), 1)); | |
} | |
if (_this.debug) { | |
return console.log("Firebase: Received changes made to '" + path + "' via 'PATCH': " + (JSON.parse(ev.data).data) + " \n URL: '" + url + "'"); | |
} | |
}; | |
})(this)); | |
} | |
}; | |
return Firebase; | |
})(Framer.BaseClass); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment