UNPKG

flashpoint

Version:

Angular bindings for Fireproof. Replaces AngularFire.

4 lines 19.8 kB
/*! flashpoint 5.0.1, © 2015 J2H2 Inc. MIT License. * https://github.com/casetext/flashpoint */ !function(t,e){"function"==typeof define&&define.amd?define(["angular","firebase","fireproof"],e):"object"==typeof exports?e(require("angular"),require("firebase"),require("fireproof")):e(t.angular,t.Firebase,t.Fireproof)}(this,function(t,e,r){"use strict";function n(t,e,r,n){var i,o=n.onAttach(function(){i=t.$watch("fp.connected",function(e){e===!0&&t.$eval(r.onConnect)})}),c=n.onDetach(function(){i&&(i(),i=null)});t.$on("$destroy",function(){n.offAttach(o),n.offDetach(c)})}function i(e,r,n,i,o,c){function a(t){p.listenerSet&&p.listenerSet.clear(),p.auth=t,e.$evalAsync()}function s(t){p.connected=t.val(),e.$evalAsync()}function u(t){return p.authenticating=!1,p.authError=null,t}function l(t){return p.authenticating=!0,p.authError=t,r.reject(t)}function h(){p.accountChanging=!1,p.accountError=null}function f(t){return p.accountChanging=!1,p.accountError=t,r.reject(t)}var p=this,d=[],y=[];p.auth=null,p.authError=null,p.accountError=null,p.authenticating=!1,p.accountChanging=!1,p.detachFirebase=function(){p.listenerSet&&(p.listenerSet.clear(),delete p.listenerSet),delete p.connected,p.auth=null,p.authError=null,p.accountError=null,p.authenticating=!1,p.accountChanging=!1,p.root&&(p.root.offAuth(a),p.root.child(".info/connected").off("value",s),p.root.off(),y.forEach(function(t){t(p.root)}),delete p.root),e.$evalAsync()},p.onDetach=function(t){return y.push(t),p.root||t(),t},p.offDetach=function(t){y.splice(y.indexOf(t),1)},p.attachFirebase=function(t){p.root&&p.detachFirebase(),p.root=new i(new n(t)),p.listenerSet=new c(p.root,e),p.root.onAuth(a),p.connected=!0,p.root.child(".info/connected").on("value",s),d.forEach(function(t){t(p.root)})},p.onAttach=function(t){return d.push(t),p.root&&t(p.root),t},p.offAttach=function(t){d.splice(d.indexOf(t),1)},p.goOffline=function(){n.goOffline()},p.goOnline=function(){n.goOnline()},p.unauth=function(){p.authError=null,p.accountError=null,p.root.unauth()},p.authWithCustomToken=function(t){return p.authenticating=!0,p.root.authWithCustomToken(t).then(u,l)},p.authAnonymously=function(t){return p.authenticating=!0,p.root.authAnonymously(null,t).then(u,l)},p.authWithPassword=function(t,e){return p.authenticating=!0,p.root.authWithPassword({email:t,password:e}).then(u,l)},p.authWithOAuthPopup=function(t,e){return p.authenticating=!0,p.root.authWithOAuthPopup(t,null,e).then(u,l)},p.authWithOAuthToken=function(t,e,r){return p.authenticating=!0,p.root.authWithOAuthToken(t,e,null,r).then(u,l)},p.createUser=function(t,e){return p.accountChanging=!0,p.root.createUser({email:t,password:e}).then(h,f)},p.removeUser=function(t,e){return p.accountChanging=!0,p.root.removeUser({email:t,password:e}).then(h,f)},p.changeEmail=function(t,e,r){return p.accountChanging=!0,p.root.changeEmail({oldEmail:t,newEmail:e,password:r}).then(h,f)},p.changePassword=function(t,e,r){return p.accountChanging=!0,p.root.changePassword({email:t,oldPassword:e,newPassword:r}).then(h,f)},p.resetPassword=function(t){return p.accountChanging=!0,p.root.resetPassword({email:t}).then(h,f)},p.set=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),r=o(t);return p.root.child(r).set(e)},p.setPriority=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),r=o(t);return p.root.child(r).setPriority(e)},p.setWithPriority=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),r=t.pop(),n=o(t);return p.root.child(n).setWithPriority(r,e)},p.push=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),r=o(t);return p.root.child(r).push(e)},p.update=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),r=o(t);return p.root.child(r).update(e)},p.remove=function(){var t=Array.prototype.slice.call(arguments,0),e=o(t);return p.root.child(e).remove()},p.increment=function(){var e=Array.prototype.slice.call(arguments,0),n=o(e);return p.root.child(n).transaction(function(e){return t.isNumber(e)?e+1:null===e?1:void 0}).then(function(t){return t.committed?void 0:r.reject(new Error("Cannot increment the object at "+n))})},p.decrement=function(){var e=Array.prototype.slice.call(arguments,0),n=o(e);return p.root.child(n).transaction(function(e){return t.isNumber(e)?e-1:null===e?0:void 0}).then(function(t){return t.committed?void 0:r.reject(new Error("Cannot decrement the object at "+n))})},p.transaction=function(){var t=Array.prototype.slice.call(arguments,0),e=t.pop(),n=o(t);return p.root.child(n).transaction(function(t){return e(t)}).then(function(t){return t.committed?void 0:r.reject(new Error("Aborted"))})},p.val=function(){var t=o(Array.prototype.slice.call(arguments,0));if(t&&p.listenerSet)return p.listenerSet.add(t),p.listenerSet.values.hasOwnProperty(t)?p.listenerSet.values[t]:null},p.model=function(){var e=o(Array.prototype.slice.call(arguments,0));return function(r){return e&&p.listenerSet?t.isDefined(r)?p.set(e,r):(p.listenerSet.add(e),p.listenerSet.values.hasOwnProperty(e)?p.listenerSet.values[e]:null):void 0}},p.priority=function(){var t=o(Array.prototype.slice.call(arguments,0));if(t&&p.listenerSet)return p.listenerSet.add(t),p.listenerSet.priorities.hasOwnProperty(t)?p.listenerSet.priorities[t]:null},p.error=function(){var t=o(Array.prototype.slice.call(arguments,0));return t&&p.listenerSet&&p.listenerSet.errors.hasOwnProperty(t)?p.listenerSet.errors[t]:null},p.path=function(){var t=o(Array.prototype.slice.call(arguments,0));return t?t:null},e.$on("$destroy",function(){p.detachFirebase()})}t.module("flashpoint",[]),t.module("flashpoint").directive("firebase",["$animate",function(t){function e(e,r,n,i){var o,c,a=function(t){o&&t===c||"string"==typeof t&&""!==t&&(i.attachFirebase(t),o=!0,c=t)};n.$observe("firebase",a),e.$watch("fp.connected",function(e){e===!0?t.setClass(r,"fp-connected","fp-disconnected"):e===!1?t.setClass(r,"fp-disconnected","fp-connected"):t.setClass(r,"","fp-connected fp-disconnected")}),e.$watch("fp.auth",function(e){void 0===e?t.setClass(r,"","fp-unauthenticated fp-authenticated"):null===e?t.setClass(r,"fp-unauthenticated","fp-authenticated"):t.setClass(r,"fp-authenticated","fp-unauthenticated")}),e.$watch("fp.authError",function(e){e?t.addClass(r,"fp-auth-error"):t.removeClass(r,"fp-auth-error")}),e.$watch("fp.accountError",function(e){e?t.addClass(r,"fp-account-error"):t.removeClass(r,"fp-account-error")})}return{restrict:"A",controller:"FirebaseCtl",controllerAs:"fp",priority:1e3,scope:!0,link:{pre:e}}}]),t.module("flashpoint").directive("fpBindChildren",["$animate","$compile","_fpGetRef",function(e,r,n){function i(i){var o=i[0].querySelector("[fp-child-repeat]"),c=t.element(document.createElement("fp-child-repeat-placeholder"));if(!o)throw new Error("No fp-child-repeat was found in your fp-bind-children!");return o=t.element(o),o.after(c),o.remove(),function(i,c,a,s){function u(t){for(var e=0;e<i.$children.length;e++)if(i.$children[e].key()===t)return e;return-1}function l(){P&&P(),P=i.$watch(a.fpBindChildren,function(t){if(h(),t)try{A=n(s.root,t),A.on("child_added",f,m),A.on("child_removed",p,m),A.on("child_moved",d,m),A.on("child_changed",y,m)}catch(e){m(e)}})}function h(){e.removeClass(c,"fp-bind-children-error"),A&&(A.off("child_added",f),A.off("child_removed",p),A.off("child_moved",d),A.off("child_changed",y)),A=null,i.$children.forEach(function(t){var e=_[t.key()];e.remove()}),i.$children.length=0}function f(n,a){var l=u(a)+1;i.$children.splice(l,0,n);var h=o.clone(),f=i.$new(),p=r(h);p(f,null,{transcludeControllers:{firebase:{instance:s}}});var d=_[a]||v;f.$key=n.key(),f.$value=n.val(),f.$priority=n.getPriority(),f.$index=l,_[n.key()]=t.element(h),e.enter(h,c.parent(),d)}function p(t){e.leave(_[t.key()]);var r=u(t.key());i.$children.splice(r,1),e.leave(_[t.key()]).then(function(){_[t.key()]=null})}function d(t,r){var n=u(t.key());i.$children.splice(n,1);var o=u(r)+1;i.$children.splice(o,0,t),_[t.key()].scope().$key=t.key(),_[t.key()].scope().$value=t.val(),_[t.key()].scope().$priority=t.getPriority(),_[t.key()].scope().$index=o+1,e.move(_[t.key()],c.parent(),_[r])}function y(t){var r=u(t.key());i.$children.splice(r,1,t),_[t.key()].scope().$key=t.key(),_[t.key()].scope().$value=t.val(),_[t.key()].scope().$priority=t.getPriority(),e.addClass(_[t.key()],"fp-bind-children-changed").then(function(){e.removeClass(_[t.key()],"fp-bind-children-changed")})}function m(t){i.$error=t,e.addClass(c,"fp-bind-children-error")}var v=t.element(document.createComment("fp-child-repeat start")),g=t.element(document.createComment("fp-child-repeat end")),$=c.find("fp-child-repeat-placeholder");$.after(g),$.after(v),$.remove();var A,P,_={};i.$children=[];var w=s.onAttach(l),F=s.onDetach(function(){P&&(P(),P=null),h()});i.$on("$destroy",function(){v.remove(),g.remove(),P&&P(),h(),s.offAttach(w),s.offDetach(F)})}}return{restrict:"A",priority:900,require:"^firebase",compile:i}}]),t.module("flashpoint").directive("fpFeed",["$q","FPFeed",function(t,e){function r(t,r,n,i){var o=i.onAttach(function(r){t.$feed=new e(t.$eval(n.fpFeed),function(e,r){if(n.feedQuery)return t.$eval(n.feedQuery,{$ref:e,$start:r});throw new Error("fp-feed requires feed-query to be set")}),t.$feed.setTransform(function(e,r){return n.feedTransform?t.$eval(n.feedTransform,{$object:e,$root:r}):e}),t.$feed.setFilter(function(e,r,i,o){return n.feedFilter?t.$eval(n.feedFilter,{$object:e,$index:r,$newItems:i,$items:o}):function(){return!0}}),t.$feed.setSort(function(e,r){return n.feedSort?t.$eval(n.feedSort,{$a:e,$b:r,$left:e,$right:r}):e.ref().toString().localeCompare(r.ref().toString())}),t.$feed.connect(r)}),c=i.onDetach(function(){t.$feed&&t.$feed.disconnect()});t.$on("$destroy",function(){i.offAttach(o),i.offDetach(c),t.$feed.disconnect()})}return{require:"^firebase",link:r}}]),t.module("flashpoint").constant("FP_DEFAULT_PAGE_SIZE",3).directive("fpPage",["FP_DEFAULT_PAGE_SIZE","FPPage",function(e,r){function n(n,i,o,c){function a(t,r){return r=parseInt(r),isNaN(r)&&(r=e),function(e,n){var i=e.child(t).orderByPriority();return n?i.startAt(n.getPriority(),n.key()).limitToFirst(r+1):i.limitToFirst(r)}}function s(t,r){return r=parseInt(r),isNaN(r)&&(r=e),function(e,n){var i=e.child(t).orderByKey();return n?i.startAt(n.key()).limitToFirst(r+1):i.limitToFirst(r)}}function u(t,r){return r=parseInt(r),isNaN(r)&&(r=e),function(e,n){var i=e.child(t).orderByValue();return n?i.startAt(n.val(),n.key()).limitToFirst(r+1):i.limitToFirst(r)}}function l(r,n,i){return i=parseInt(i),isNaN(i)&&(i=e),function(e,o){var c=e.child(r).orderByChild(n);if(o){var a=o.val();return a=t.isObject(a)?a[n]:null,c.startAt(a,o.key()).limitToFirst(i+1)}return c.limitToFirst(i)}}var h={$orderByPriority:a,$orderByKey:s,$orderByChild:l,$orderByValue:u},f=c.onAttach(function(t){n.$page=new r(n.$eval(o.fpPage,h)),n.$page.connect(t)}),p=c.onDetach(function(){n.$page&&n.$page.disconnect()});n.$on("$destroy",function(){c.offAttach(f),c.offDetach(p),n.$page.disconnect()})}return{link:n,restrict:"A",priority:750,require:"^firebase"}}]),t.module("flashpoint").directive("onAuth",function(){function t(t,e,r,n){function i(e){r.onAuth&&t.$eval(r.onAuth,{$auth:e})}n.onAttach(function(t){t.onAuth(i)}),n.onDetach(function(t){t&&t.offAuth(i)})}return{priority:750,require:"^firebase",restrict:"A",link:{pre:t}}}),t.module("flashpoint").directive("onConnect",function(){return{require:"firebase",link:n}}),t.module("flashpoint").directive("onDisconnect",["$q","$log","fpValidatePath",function(t,e,r){function n(n,i,o,c){var a={},s=function(t){e.debug('onDisconnect: error evaluating "'+o.onDisconnect+'": '+t.code),o.onDisconnectError&&n.$eval(o.onDisconnectError,{$error:t})},u=function(e){return{$set:function(){var n=Array.prototype.slice.call(arguments,0),i=n.pop(),o=r(n);return o?(a[o]=!0,e.child(o).onDisconnect().set(i)["catch"](s)):t.reject(new Error("Invalid path"))},$update:function(){var n=Array.prototype.slice.call(arguments,0),i=n.pop(),o=r(n);return o?(a[o]=!0,e.child(o).onDisconnect().update(i)["catch"](s)):t.reject(new Error("Invalid path"))},$setWithPriority:function(){var n=Array.prototype.slice.call(arguments,0),i=n.pop(),o=n.pop(),c=r(n);return c?(a[c]=!0,e.child(c).onDisconnect().setWithPriority(o,i)["catch"](s)):t.reject(new Error("Invalid path"))},$remove:function(){var n=Array.prototype.slice.call(arguments,0),i=r(n);return i?(a[i]=!0,e.child(i).onDisconnect().remove()["catch"](s)):t.reject(new Error("Invalid path"))}}},l={$set:c.set.bind(c),$remove:c.remove.bind(c),$setWithPriority:c.setWithPriority.bind(c),$update:c.update.bind(c)},h=c.onAttach(function(t){n.$eval(o.onDisconnect,u(t))}),f=c.onDetach(function(t){for(var e in a)t.child(e).onDisconnect().cancel(),n.$eval(o.onDisconnect,l);a={}});n.$on("$destroy",function(){c.offAttach(h),c.offDetach(f)})}return{require:"firebase",restrict:"A",link:n}}]),t.module("flashpoint").filter("orderByKey",function(){return function e(r){return t.isArray(r)?r.map(e):t.isString(r)?r+".orderByKey":null}}).filter("orderByValue",function(){return function e(r){return t.isArray(r)?r.map(e):t.isString(r)?r+".orderByValue":null}}).filter("orderByPriority",function(){return function e(r){return t.isArray(r)?r.map(e):t.isString(r)?r+".orderByPriority":null}}).filter("orderByChild",function(){return function e(r,n){return t.isArray(r)?r.map(function(t){return e(t,n)}):t.isString(r)&&t.isString(n)&&n.length>0?r+".orderByChild:"+JSON.stringify(n):null}}).filter("startAt",function(){return function e(r,n,i){return t.isArray(r)?r.map(function(t){return e(t,n,i)}):void 0===n?null:(r+=".startAt:"+JSON.stringify(n),t.isString(i)||t.isNumber(i)?r+":"+JSON.stringify(i):r)}}).filter("endAt",function(){return function e(r,n,i){return t.isArray(r)?r.map(function(t){return e(t,n,i)}):void 0===n?null:(r+=".endAt:"+JSON.stringify(n),t.isString(i)||t.isNumber(i)?r+":"+JSON.stringify(i):r)}}).filter("limitToFirst",function(){return function e(r,n){return n=parseInt(n),isNaN(n)?null:t.isArray(r)?r.map(function(t){return e(t,n)}):r+".limitToFirst:"+JSON.stringify(n)}}).filter("limitToLast",function(){return function e(r,n){return n=parseInt(n),isNaN(n)?null:t.isArray(r)?r.map(function(t){return e(t,n)}):r+".limitToLast:"+JSON.stringify(n)}}).constant("_fpGetRef",function o(e,r){if(t.isArray(r))return r.map(function(t){return o(e,t)});if(t.isString(r)){var n=r.split(/\./g),i=e.child(n.shift());return n.reduce(function(t,e){var r=e.split(":")[0],n=e.split(":").slice(1).map(function(t){return JSON.parse(t)});switch(r){case"orderByKey":case"orderByValue":case"orderByPriority":return t[r]();case"orderByChild":case"startAt":case"endAt":case"limitToFirst":case"limitToLast":return t[r].apply(t,n)}},i)}return null}),t.module("flashpoint").factory("FPFeed",["$q",function(e){function r(e,r){if(arguments.length<2)throw new Error("FPFeed expects at least 2 arguments, got "+arguments.length);t.isArray(e)||(e=[e]),this._queryFn=r,this._positions=e.reduce(function(t,e){return t[e]=void 0,t},{}),this._presence={},this.items=[]}return r.prototype._transformFn=function(t){return t},r.prototype._filterFn=function(){return!0},r.prototype.setTransform=function(t){return this._transformFn=t,this},r.prototype.setSort=function(t){return this._sortFn=t,this},r.prototype.setFilter=function(t){return this._filterFn=t,this},r.prototype.more=function(){var t=this;return t._morePromise||(t._morePromise=e.all(Object.keys(t._positions).map(function(r){return t._queryFn(t.root.child(r),t._positions[r]).then(function(n){var i=[];return n.forEach(function(n){t._presence.hasOwnProperty(n.ref().toString())||(t._presence[n.ref().toString()]=!0,i.push(e.when(t._transformFn(n,t.root))),t._positions[r]=n)}),e.all(i)})})).then(function(e){var r=e.reduce(function(t,e){return t.concat(e)},[]).filter(function(e,r,n){return t._filterFn(e,r,n,t.items)});t._sortFn&&r.sort(t._sortFn),r.forEach(function(e){t.items.push(e)}),t._morePromise=null})),t._morePromise},r.prototype.connect=function(t){return this.disconnect(),this.root=t,this.more()},r.prototype.disconnect=function(){this._positions=Object.keys(this._positions).reduce(function(t,e){return t[e]=void 0,t},{}),this._presence={},this.items.length=0},r}]),t.module("flashpoint").factory("FPListenerSet",function(){function t(t,e){function r(){var t={};for(var e in n.watchers)n.watchers[e]&&n.liveWatchers[e]?t[e]=n.watchers[e]:n.remove(e);n.watchers=t,n.liveWatchers={},i=!1}var n=this,i=!1;n.watchers={},n.liveWatchers={},n.values={},n.priorities={},n.errors={},n.root=t,n.scope=e,n.scope.$watch(function(){i||(i=!0,e.$$postDigest(r))})}return t.prototype.add=function(t){var e=this;e.liveWatchers[t]=!0,e.watchers[t]||(e.watchers[t]=e.root.child(t).on("value",function(r){e.errors[t]=null,e.values[t]=r.val(),e.priorities[t]=r.getPriority(),e.scope.$evalAsync()},function(r){e.liveWatchers[t]=!1,e.watchers[t]=null,e.errors[t]=r,e.values[t]=null,e.priorities[t]=null,e.scope.$evalAsync()}))},t.prototype.has=function(t){return this.watchers.hasOwnProperty(t)},t.prototype.remove=function(t){this.watchers[t]&&(this.watchers[t].disconnect?this.watchers[t].disconnect():this.root.child(t).off("value",this.watchers[t]),this.values[t]=null,this.errors[t]=null,this.priorities[t]=null,this.watchers[t]=null)},t.prototype.clear=function(){for(var t in this.watchers)this.remove(t);this.watchers={}},t}),t.module("flashpoint").factory("FPPage",["$q",function(t){function e(t){this._pagingFn=t,this._pages=[],this._presence={},this.items=[],this.keys=[],this.priorities=[],this.values=[],this.disconnect()}return e.prototype.connect=function(t){return this.disconnect(),this.root=t,this.next()},e.prototype.disconnect=function(){this.root=null,this._pages.length=0,this.items.length=0,this.keys.length=0,this.values.length=0,this.priorities.length=0,this.number=0,this._presence={}},e.prototype._handleSnap=function(t){var e=[],r=this._presence;t.forEach(function(t){r.hasOwnProperty(t.key())||(r[t.key()]=!0,e.push(t))}),e.length>0?(this._pages.push(e),this._setFPPage(this._pages.length)):this._lastFPPage=this.number,this._currentOperation=null},e.prototype.next=function(){if(this.hasNext()){if(this._pages.length>this.number)return this._setFPPage(this.number+1),t.when();if(this._currentOperation)return this._currentOperation;if(0===this._pages.length)return this._currentOperation=this._pagingFn(this.root,null).then(this._handleSnap.bind(this)),this._currentOperation;if(this._pages[this._pages.length-1].length>0){var e=this._pages[this._pages.length-1];return this._currentOperation=this._pagingFn(this.root,e[e.length-1]).then(this._handleSnap.bind(this)),this._currentOperation}return t.when()}return t.when()},e.prototype.hasNext=function(){return this.hasOwnProperty("root")&&this._lastFPPage!==this.number},e.prototype.hasPrevious=function(){return this.hasOwnProperty("root")&&this.number>1},e.prototype.previous=function(){return this.hasPrevious()&&this._setFPPage(this.number-1),t.when()},e.prototype.reset=function(){return this.connect(this.root)},e.prototype._setFPPage=function(t){this.items.length=0,this.keys.length=0,this.values.length=0,this.priorities.length=0,this._pages[t-1].forEach(function(t){this.items.push(t),this.keys.push(t.key()),this.values.push(t.val()),this.priorities.push(t.getPriority())},this),this.number=t},e}]),t.module("flashpoint").factory("Firebase",function(){return e}).factory("ServerValue",["Firebase",function(t){return t.ServerValue}]).factory("Fireproof",["$rootScope","$q",function(t,e){return r.setNextTick(function(e){t.$evalAsync(e)}),r.bless(e),r}]),t.module("flashpoint").factory("fpValidatePath",function(){function t(t){var e=t.join("/");return 0===t.length||""===e||-1!==t.indexOf(null)||-1!==t.indexOf(void 0)?null:e}return t}),i.$inject=["$scope","$q","Firebase","Fireproof","fpValidatePath","FPListenerSet"],t.module("flashpoint").controller("FirebaseCtl",i)});