Files
cdnjs/ajax/libs/angularFire/0.5.0/angularfire.min.js
T

1 line
8.1 KiB
JavaScript

"use strict";var AngularFire,AngularFireAuth;angular.module("firebase",[]).value("Firebase",Firebase),angular.module("firebase").factory("$firebase",["$q","$parse","$timeout",function(a,b,c){return function(d){var e=new AngularFire(a,b,c,d);return e.construct()}}]),angular.module("firebase").filter("orderByPriority",function(){return function(a){if(!a.$getIndex||"function"!=typeof a.$getIndex){var b=Object.prototype.toString.call(a);if("object"==typeof a&&"[object Object]"==b){var c=[];for(var d in a)a.hasOwnProperty(d)&&c.push(a[d]);return c}return a}var e=[],f=a.$getIndex();if(f.length<=0)return a;for(var g=0;g<f.length;g++){var h=a[f[g]];h&&(h.$id=f[g],e.push(h))}return e}}),AngularFire=function(a,b,c,d){if(this._q=a,this._bound=!1,this._loaded=!1,this._parse=b,this._timeout=c,this._index=[],this._onChange=[],this._onLoaded=[],"string"==typeof d)throw new Error("Please provide a Firebase reference instead of a URL, eg: new Firebase(url)");this._fRef=d},AngularFire.prototype={construct:function(){var a=this,b={};return b.$bind=function(b,c){return a._bind(b,c)},b.$add=function(b,c){var d;return d="object"==typeof b?a._fRef.ref().push(a._parseObject(b),c):a._fRef.ref().push(b,c)},b.$save=function(b){b?a._fRef.ref().child(b).set(a._parseObject(a._object[b])):a._fRef.ref().set(a._parseObject(a._object))},b.$set=function(b){a._fRef.ref().set(b)},b.$remove=function(b){b?a._fRef.ref().child(b).remove():a._fRef.ref().remove()},b.$child=function(b){var c=new AngularFire(a._q,a._parse,a._timeout,a._fRef.ref().child(b));return c.construct()},b.$on=function(b,c){switch(b){case"change":a._onChange.push(c);break;case"loaded":a._onLoaded.push(c);break;default:throw new Error("Invalid event type "+b+" specified")}},b.$getIndex=function(){return angular.copy(a._index)},a._object=b,a._getInitialValue(),a._object},_getInitialValue:function(){var a=this,b=function(c){var d=c.val();if(null===d&&a._bound){var e=a._parseObject(a._parse(a._name)(a._scope));switch(typeof e){case"string":case"undefined":d="";break;case"number":d=0;break;case"boolean":d=!1}}switch(typeof d){case"string":case"number":case"boolean":a._updatePrimitive(d);break;case"object":a._getChildValues(),a._fRef.off("value",b);break;default:throw new Error("Unexpected type from remote data "+typeof d)}a._loaded=!0,a._broadcastEvent("loaded",d)};a._fRef.on("value",b)},_getChildValues:function(){function a(a,c){var d=a.name(),e=a.val(),f=b._index.indexOf(d);if(-1!==f&&b._index.splice(f,1),c){var g=b._index.indexOf(c);b._index.splice(g+1,0,d)}else b._index.unshift(d);null!==a.getPriority()&&(e.$priority=a.getPriority()),b._updateModel(d,e)}var b=this;b._fRef.on("child_added",a),b._fRef.on("child_moved",a),b._fRef.on("child_changed",a),b._fRef.on("child_removed",function(a){var c=a.name(),d=b._index.indexOf(c);b._index.splice(d,1),b._updateModel(c,null)})},_updateModel:function(a,b){var c=this;c._timeout(function(){if(null==b?delete c._object[a]:c._object[a]=b,c._broadcastEvent("change"),c._bound){var d=c._object,e=c._parse(c._name)(c._scope);angular.equals(d,e)||c._parse(c._name).assign(c._scope,angular.copy(d))}})},_updatePrimitive:function(a){var b=this;b._timeout(function(){if(b._object.$value&&angular.equals(b._object.$value,a)||(b._object.$value=a),b._broadcastEvent("change"),b._bound){var c=b._parseObject(b._parse(b._name)(b._scope));angular.equals(c,a)||b._parse(b._name).assign(b._scope,a)}})},_broadcastEvent:function(a,b){var c;switch(a){case"change":c=this._onChange;break;case"loaded":c=this._onLoaded;break;default:c=[]}if(c.length>0)for(var d=0;d<c.length;d++)"function"==typeof c[d]&&c[d](b)},_bind:function(a,b){var c=this,d=c._q.defer();c._name=b,c._bound=!0,c._scope=a;var e=c._parse(b)(a);void 0!==e&&"object"==typeof e&&c._fRef.update(c._parseObject(e));var f=a.$watch(b,function(){var d=c._parseObject(c._parse(b)(a));c._object.$value&&angular.equals(d,c._object.$value)||angular.equals(d,c._object)||void 0!==d&&c._loaded&&(c._fRef.set?c._fRef.set(d):c._fRef.ref().update(d))},!0);return a.$on("$destroy",function(){f()}),c._fRef.once("value",function(){d.resolve(f)}),d.promise},_parseObject:function(a){function b(a){for(var c in a)a.hasOwnProperty(c)&&("$priority"==c?(a[".priority"]=a.$priority,delete a.$priority):"object"==typeof a[c]&&b(a[c]));return a}var c=b(angular.copy(a));return angular.fromJson(angular.toJson(c))}},angular.module("firebase").factory("$firebaseAuth",["$q","$timeout","$injector","$rootScope","$location",function(a,b,c,d,e){return function(f,g){var h=new AngularFireAuth(a,b,c,d,e,f,g);return h.construct()}}]),AngularFireAuth=function(a,b,c,d,e,f,g){if(this._q=a,this._timeout=b,this._injector=c,this._location=e,this._rootScope=d,this._route=null,this._injector.has("$route")&&(this._route=this._injector.get("$route")),this._cb=function(){},this._options=g||{},this._options.callback&&"function"==typeof this._options.callback&&(this._cb=g.callback),this._deferred=null,this._redirectTo=null,this._authenticated=!1,"string"==typeof f)throw new Error("Please provide a Firebase reference instead of a URL, eg: new Firebase(url)");this._fRef=f},AngularFireAuth.prototype={construct:function(){var a=this,b={user:null,$login:a.login.bind(a),$logout:a.logout.bind(a),$createUser:a.createUser.bind(a)};if(a._options.path&&null!==a._route&&(a._route.current&&a._authRequiredRedirect(a._route.current,a._options.path),a._rootScope.$on("$routeChangeStart",function(b,c){a._authRequiredRedirect(c,a._options.path)})),a._object=b,a._options.simple!==!1){if(!window.FirebaseSimpleLogin){var c=new Error("FirebaseSimpleLogin undefined, did you include firebase-simple-login.js?");return a._rootScope.$broadcast("$firebaseAuth:error",c),void 0}var d=new FirebaseSimpleLogin(a._fRef,function(b,c){a._cb(b,c),b?(a._deferred&&(a._deferred.reject(b),a._deferred=null),a._rootScope.$broadcast("$firebaseAuth:error",b)):c?(a._deferred&&(a._deferred.resolve(c),a._deferred=null),a._loggedIn(c)):a._loggedOut()});return a._authClient=d,a._object}},login:function(a,b){var c=this,d=c._q.defer();switch(a){case"github":case"persona":case"twitter":case"facebook":case"password":case"anonymous":if(c._authClient)c._deferred=d,c._authClient.login(a,b);else{var e=new Error("Simple Login not initialized");d.reject(e),c._rootScope.$broadcast("$firebaseAuth:error",e)}break;default:try{var f=c._deconstructJWT(a);c._fRef.auth(a,function(a){a?(d.reject(a),c._rootScope.$broadcast("$firebaseAuth:error",a)):(c._deferred=d,c._loggedIn(f))})}catch(g){d.reject(g),c._rootScope.$broadcast("$firebaseAuth:error",g)}}return d.promise},logout:function(){this._authClient?this._authClient.logout():(this._fRef.unauth(),this._loggedOut())},createUser:function(a,b,c,d){var e=this;e._authClient.createUser(a,b,function(f,g){try{f?e._rootScope.$broadcast("$firebaseAuth:error",f):d||e.login("password",{email:a,password:b})}catch(h){e._rootScope.$broadcast("$firebaseAuth:error",h)}c&&e._timeout(function(){c(f,g)})})},changePassword:function(a,b,c,d){var e=this;e._authClient.changePassword(a,b,c,function(a,b){a&&e._rootScope.$broadcast("$firebaseAuth:error",a),d&&e._timeout(function(){d(a,b)})})},_loggedIn:function(a){var b=this;b._timeout(function(){b._object.user=a,b._authenticated=!0,b._rootScope.$broadcast("$firebaseAuth:login",a),b._redirectTo&&(b._location.replace(),b._location.path(b._redirectTo),b._redirectTo=null)})},_loggedOut:function(){var a=this;a._timeout(function(){a._object.user=null,a._authenticated=!1,a._rootScope.$broadcast("$firebaseAuth:logout")})},_authRequiredRedirect:function(a,b){a.authRequired&&!this._authenticated&&(this._redirectTo=void 0===a.pathTo?this._location.path():a.pathTo===b?"/":a.pathTo,this._location.replace(),this._location.path(b))},_decodeBase64:function(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},_deconstructJWT:function(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):this._decodeBase64(d),JSON.parse(decodeURIComponent(escape(c)))}};