forked from FirebaseExtended/angularfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangularfire.min.js
1 lines (1 loc) · 7.23 KB
/
angularfire.min.js
1
"use strict";var AngularFire;angular.module("firebase",[]).value("Firebase",Firebase),angular.module("firebase").factory("angularFire",["$q","$parse","$timeout",function(a,b,c){return function(d,e,f){var g=new AngularFire(a,b,c,d);return g.associate(e,f)}}]),AngularFire=function(a,b,c,d){if(this._q=a,this._parse=b,this._timeout=c,this._initial=!0,this._remoteValue=!1,"string"==typeof d)throw new Error("Please provide a Firebase reference instead of a URL, eg: new Firebase(url)");this._fRef=d},AngularFire.prototype={associate:function(a,b){var c=this,d=this._q.defer(),e=d.promise;return this._fRef.on("value",function(e){var f=e.val(),g=angular.fromJson(angular.toJson(c._parse(b)(a)));if(!angular.equals(f,g)){if(c._initial){var h=!1,i=Object.prototype.toString;if(f&&i.call(g)==i.call(f))if("[object Array]"==i.call(g))h=g.concat(f),angular.equals(h,f)||(c._fRef.ref().set(h),f=h);else if("[object Object]"==i.call(g)){h=g;for(var j in f)h[j]=f[j];c._fRef.ref().update(h),f=h}null===f&&void 0!==g&&(c._fRef.ref().set(g),f=g),c._initial=!1}var k=!1;d&&(k=d,d=!1),c._timeout(function(){c._resolve(a,b,k,f)})}}),e},disassociate:function(){var a=this;a._unregister&&a._unregister(),this._fRef.off("value")},_resolve:function(a,b,c,d){var e=this,f=angular.fromJson(angular.toJson(this._parse(b)(a)));if(null===d&&"object"==typeof f){var g=Object.prototype.toString;d=g.call(f)==g.call([])?[]:{}}this._remoteValue=angular.copy(d),this._parse(b).assign(a,angular.copy(d)),c&&(c.resolve(function(){e.disassociate()}),this._watch(a,b))},_watch:function(a,b){var c=this;c._unregister=a.$watch(b,function(){if(!c._initial){var d=angular.fromJson(angular.toJson(c._parse(b)(a)));if(!angular.equals(d,c._remoteValue)){var e=Object.prototype.toString;"[object Object]"==e.call(d)?c._fRef.set?c._fRef.set(d):c._fRef.ref().update(d):c._fRef.ref().set(d)}}},!0),a.$on("$destroy",function(){c.disassociate()})},_log:function(a){console&&console.log&&console.log(a)}},angular.module("firebase").factory("angularFireCollection",["$timeout",function(a){return function(b,c,d){function e(a,b){d&&"function"==typeof d&&d(a,b)}function f(a){return a?n[a]+1:0}function g(a,b){n[b.$id]=a,o.splice(a,0,b)}function h(a){var b=n[a];o.splice(b,1),n[a]=void 0}function i(a,b){o[a]=b}function j(a,b,c){o.splice(a,1),o.splice(b,0,c),k(a,b)}function k(a,b){var c=o.length;b=b||c,b>c&&(b=c);for(var d=a;b>d;d++){var e=o[d];e.$index=n[e.$id]=d}}if("string"==typeof b)throw new Error("Please provide a Firebase reference instead of a URL, eg: new Firebase(url)");var l=function(a,b){this.$ref=a.ref(),this.$id=a.name(),this.$index=b,angular.extend(this,{$priority:a.getPriority()},a.val())},m=[function(a){return null==a.$priority?0:angular.isNumber(a.$priority)?1:angular.isString(a.$priority)?2:void 0},function(a){return a.$priority?a.$priority:1/0},function(a){return a.$id}],n={},o=[];return c&&"function"==typeof c&&b.once("value",c),b.on("child_added",function(b,c){a(function(){var a=f(c),d=new l(b,a);g(a,d),k(a),e("item_added",d)})}),b.on("child_removed",function(b){a(function(){var a=b.name(),c=n[a],d=o[c];h(a),k(c),e("item_removed",d)})}),b.on("child_changed",function(b,c){a(function(){var a=n[b.name()],d=f(c),g=new l(b,a);e("item_removed",o[a]),i(a,g),d!==a&&j(a,d,g),e("item_added",g)})}),b.on("child_moved",function(b,c){a(function(){var a=n[b.name()],d=f(c),g=o[a];j(a,d,g),e("item_moved",g)})}),o.getByName=function(a){return o[n[a]]},o.getByNames=function(a){for(var b=[],c=0,d=a.length;d>c;c++)b.push(o[n[a[c]]]);return b},o.add=function(a,c){var d,e=angular.fromJson(angular.toJson(a));return d=c?b.ref().push(e,c):b.ref().push(e)},o.remove=function(a,b){var c=angular.isString(a)?o[n[a]]:a;b?c.$ref.remove(b):c.$ref.remove()},o.update=function(a,b){var c=angular.isString(a)?o[n[a]]:a,d=angular.fromJson(angular.toJson(c));b?c.$ref.update(d,b):c.$ref.update(d)},o.set=function(a,b){var c=angular.isString(a)?o[n[a]]:a,d=angular.fromJson(angular.toJson(c));b?c.$ref.set(d,b):c.$ref.set(d)},o.order=m,o}}]),angular.module("firebase").factory("angularFireAuth",["$injector","$rootScope","$parse","$timeout","$location","$q",function(a,b,c,d,e,f){function g(a){for(var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c="",d="",e=8,f=0;f<a.length&&"="!=a[f];++f){var g=b.indexOf(a.charAt(f));if(-1==g)throw new Error("Not base64.");for(var h=g.toString(2);h.length<6;)h="0"+h;for(d+=h;d.length>=e;){var i=d.slice(0,e);d=d.slice(e),c+=String.fromCharCode(parseInt(i,2))}}return c}function h(a){var b=a.split(".");if(!b instanceof Array||3!==b.length)throw new Error("Invalid JWT");var c="",d=b[1];return c=window.atob?window.atob(d):g(d),JSON.parse(decodeURIComponent(escape(c)))}function i(a,b,e,f){b&&d(function(){c(b).assign(a,e),f()})}function j(a,b,c){a.authRequired&&!c._authenticated&&(c._redirectTo=void 0===a.pathTo?e.path():a.pathTo===b?"/":a.pathTo,e.replace(),e.path(b))}var k=null;return a.has("$route")&&(k=a.get("$route")),{initialize:function(a,c){var d=this;if("string"==typeof a)throw new Error("Please provide a Firebase reference instead of a URL, eg: new Firebase(url)");if(c=c||{},this._scope=b,!c.scope)throw new Error("Scope not provided to angularFireAuth!");if(this._scope=c.scope,!c.name)throw new Error("Model name not provided to angularFireAuth!");if(this._name=c.name,this._cb=function(){},c.callback&&"function"==typeof c.callback&&(this._cb=c.callback),this._redirectTo=null,this._authenticated=!1,c.path&&null!==k&&(k.current&&j(k.current,c.path,d),b.$on("$routeChangeStart",function(a,b){j(b,c.path,d)})),this._ref=a,c.simple===!1)return i(this._scope,this._name,null,function(){}),void 0;if(!window.FirebaseSimpleLogin){var e=new Error("FirebaseSimpleLogin undefined, did you include firebase-simple-login.js?");return b.$broadcast("angularFireAuth:error",e),void 0}var f=new FirebaseSimpleLogin(this._ref,function(a,c){d._cb(a,c),a?b.$broadcast("angularFireAuth:error",a):c?d._loggedIn(c):d._loggedOut()});this._authClient=f},login:function(a,c){var d=this._watchForLogin();switch(a){case"github":case"persona":case"twitter":case"facebook":case"password":if(this._authClient)this._authClient.login(a,c);else{var e=new Error("Simple Login not initialized");b.$broadcast("angularFireAuth:error",e)}break;default:var f,g=this;try{f=h(a),this._ref.auth(a,function(a){a?b.$broadcast("angularFireAuth:error",a):g._loggedIn(f)})}catch(i){b.$broadcast("angularFireAuth:error",i)}}return d},createUser:function(a,c,e,f){var g=this;this._authClient.createUser(a,c,function(h,i){try{h?b.$broadcast("angularFireAuth:error",h):f||g.login("password",{email:a,password:c})}catch(j){b.$broadcast("angularFireAuth:error",j)}e&&d(function(){e(h,i)})})},logout:function(){this._authClient?this._authClient.logout():(this._ref.unauth(),this._loggedOut())},_loggedIn:function(a){var c=this;this._authenticated=!0,i(this._scope,this._name,a,function(){b.$broadcast("angularFireAuth:login",a),c._redirectTo&&(e.replace(),e.path(c._redirectTo),c._redirectTo=null)})},_loggedOut:function(){this._authenticated=!1,i(this._scope,this._name,null,function(){b.$broadcast("angularFireAuth:logout")})},_watchForLogin:function(){function a(a,b){d(function(){a?e.reject(a):e.resolve(b)});for(var f=0;f<c.length;f++)c[f]()}var c=[],e=f.defer();return c.push(b.$on("angularFireAuth:login",function(b,c){a(null,c)})),c.push(b.$on("angularFireAuth:error",function(b,c){a(c,null)})),e.promise}}}]);