forked from FirebaseExtended/angularfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angularfire.min.js
1 lines (1 loc) · 4.9 KB
/
angularfire.min.js
1
"use strict";function AngularFire(e,t,i,n){this._q=e,this._parse=t,this._timeout=i,this._initial=!0,this._remoteValue=!1,this._fRef="string"==typeof n?new Firebase(n):n}angular.module("firebase",[]).value("Firebase",Firebase),angular.module("firebase").factory("angularFire",["$q","$parse","$timeout",function(e,t,i){return function(n,r,a,o){var u=new AngularFire(e,t,i,n);return u.associate(r,a,o)}}]),AngularFire.prototype={associate:function(e,t,i){var n=this;void 0==i&&(i=[]);var r=this._q.defer(),a=r.promise;return this._fRef.on("value",function(a){var o=!1;if(r&&(o=r,r=!1),n._remoteValue=i,a&&void 0!=a.val()){var u=a.val();if(typeof u!=typeof i)return n._log("Error: type mismatch"),void 0;var s=Object.prototype.toString;if(s.call(i)!=s.call(u))return n._log("Error: type mismatch"),void 0;if(n._remoteValue=angular.copy(u),angular.equals(u,n._parse(t)(e)))return}n._timeout(function(){n._resolve(e,t,o,n._remoteValue)})}),a},disassociate:function(){var e=this;e._unregister&&e._unregister(),this._fRef.off("value")},_resolve:function(e,t,i,n){var r=this;this._parse(t).assign(e,angular.copy(n)),this._remoteValue=angular.copy(n),i&&(i.resolve(function(){r.disassociate()}),this._watch(e,t))},_watch:function(e,t){var i=this;i._unregister=e.$watch(t,function(){if(i._initial)return i._initial=!1,void 0;var n=JSON.parse(angular.toJson(i._parse(t)(e)));angular.equals(n,i._remoteValue)||i._fRef.ref().set(n)},!0),e.$on("$destroy",function(){i.disassociate()})},_log:function(e){console&&console.log&&console.log(e)}},angular.module("firebase").factory("angularFireCollection",["$timeout",function(e){return function(t,i){function n(e,t){this.$ref=e.ref(),this.$id=e.name(),this.$index=t,angular.extend(this,{priority:e.getPriority()},e.val())}function r(e){return e?f[e]+1:0}function a(e,t){f[t.$id]=e,h.splice(e,0,t)}function o(e){var t=f[e];h.splice(t,1),f[e]=void 0}function u(e,t){h[e]=t}function s(e,t,i){h.splice(e,1),h.splice(t,0,i),c(e,t)}function c(e,t){var i=h.length;t=t||i,t>i&&(t=i);for(var n=e;t>n;n++){var r=h[n];r.$index=f[r.$id]=n}}var l,f={},h=[];return l="string"==typeof t?new Firebase(t):t,i&&"function"==typeof i&&l.once("value",i),l.on("child_added",function(t,i){e(function(){var e=r(i);a(e,new n(t,e)),c(e)})}),l.on("child_removed",function(t){e(function(){var e=t.name(),i=f[e];o(e),c(i)})}),l.on("child_changed",function(t,i){e(function(){var e=f[t.name()],a=r(i),o=new n(t,e);u(e,o),a!==e&&s(e,a,o)})}),l.on("child_moved",function(t,i){e(function(){var e=f[t.name()],n=r(i),a=h[e];s(e,n,a)})}),h.add=function(e,t){var i;return i=t?l.ref().push(e,t):l.ref().push(e)},h.remove=function(e,t){var i=angular.isString(e)?h[f[e]]:e;t?i.$ref.remove(t):i.$ref.remove()},h.update=function(e,t){var i=angular.isString(e)?h[f[e]]:e,n={};angular.forEach(i,function(e,t){0!==t.indexOf("$")&&(n[t]=e)}),t?i.$ref.set(n,t):i.$ref.set(n)},h}}]),angular.module("firebase").factory("angularFireAuth",["$rootScope","$parse","$timeout","$location","$route",function(e,t,i,n,r){function a(e){var t=e.split(".");if(!t instanceof Array||3!==t.length)throw new Error("Invalid JWT");var i=t[1];return JSON.parse(decodeURIComponent(escape(window.atob(i))))}function o(e,n,r,a){n&&i(function(){t(n).assign(e,r),a()})}function u(e,t,i){e.authRequired&&!i._authenticated&&(i._redirectTo=void 0===e.pathTo?n.path():e.pathTo===t?"/":e.pathTo,n.replace(),n.path(t))}return{initialize:function(t,i){var n=this;if(i=i||{},this._scope=e,i.scope&&(this._scope=i.scope),i.name&&(this._name=i.name),this._cb=function(){},i.callback&&"function"==typeof i.callback&&(this._cb=i.callback),this._redirectTo=null,this._authenticated=!1,i.path&&(r.current&&u(r.current,i.path,n),e.$on("$routeChangeStart",function(e,t){u(t,i.path,n)})),this._ref=new Firebase(t),i.simple&&i.simple===!1)return o(this._scope,this._name,null),void 0;if(!window.FirebaseSimpleLogin){var a=new Error("FirebaseSimpleLogin undefined, did you include firebase-simple-login.js?");return e.$broadcast("angularFireAuth:error",a),void 0}var s=new FirebaseSimpleLogin(this._ref,function(t,i){n._cb(t,i),t?e.$broadcast("angularFireAuth:error",t):i?n._loggedIn(i):n._loggedOut()});this._authClient=s},login:function(t,i){switch(t){case"github":case"persona":case"twitter":case"facebook":case"password":if(this._authClient)this._authClient.login(t,i);else{var n=new Error("Simple Login not initialized");e.$broadcast("angularFireAuth:error",n)}break;default:var r,o=this;try{r=a(t),this._ref.auth(t,function(t){t?e.$broadcast("angularFireAuth:error",t):o._loggedIn(r)})}catch(u){e.$broadcast("angularFireAuth:error",u)}}},logout:function(){this._authClient?this._authClient.logout():(this._ref.unauth(),this._loggedOut())},_loggedIn:function(t){var i=this;this._authenticated=!0,o(this._scope,this._name,t,function(){e.$broadcast("angularFireAuth:login",t),i._redirectTo&&(n.replace(),n.path(i._redirectTo),i._redirectTo=null)})},_loggedOut:function(){this._authenticated=!1,o(this._scope,this._name,null,function(){e.$broadcast("angularFireAuth:logout")})}}}]);