|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function FirebaseRefNotProvidedError() { |
| 4 | + this.name = 'FirebaseRefNotProvidedError'; |
| 5 | + this.message = 'No Firebase URL registered. Use firebaseRefProvider.registerUrl() in the config phase to set up a root reference.'; |
| 6 | + this.stack = (new Error()).stack; |
| 7 | +} |
| 8 | +FirebaseRefNotProvidedError.prototype = Object.create(Error.prototype); |
| 9 | +FirebaseRefNotProvidedError.prototype.constructor = FirebaseRefNotProvidedError; |
| 10 | + |
| 11 | +function FirebaseRef() { |
| 12 | + this.urls = null; |
| 13 | + this.singleUrl = false; |
| 14 | + this.registerUrl = function registerUrl(urlOrConfig) { |
| 15 | + |
| 16 | + if (typeof urlOrConfig === 'string') { |
| 17 | + this.urls = {}; |
| 18 | + this.urls.default = urlOrConfig; |
| 19 | + this.singleUrl = true; |
| 20 | + } |
| 21 | + |
| 22 | + if (angular.isObject(urlOrConfig)) { |
| 23 | + this.urls = urlOrConfig; |
| 24 | + this.singleUrl = false; |
| 25 | + } |
| 26 | + |
| 27 | + }; |
| 28 | + |
| 29 | + this.$$checkUrls = function $$checkUrls(urlConfig) { |
| 30 | + if (!urlConfig) { |
| 31 | + return new FirebaseRefNotProvidedError(); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + this.$$createSingleRef = function $$createSingleRef(defaultUrl) { |
| 36 | + var error = this.$$checkUrls(defaultUrl); |
| 37 | + if (error) { throw error; } |
| 38 | + return new Firebase(defaultUrl); |
| 39 | + }; |
| 40 | + |
| 41 | + this.$$createMultipleRefs = function $$createMultipleRefs(urlConfig) { |
| 42 | + var error = this.$$checkUrls(urlConfig); |
| 43 | + if (error) { throw error; } |
| 44 | + var defaultUrl = urlConfig.default; |
| 45 | + var defaultRef = new Firebase(defaultUrl); |
| 46 | + delete urlConfig.default; |
| 47 | + angular.forEach(urlConfig, function(value) { |
| 48 | + if (!defaultRef.hasOwnProperty(value)) { |
| 49 | + defaultRef[value] = new Firebase(value); |
| 50 | + } else { |
| 51 | + throw new Error(value + ' is a reserved property name on firebaseRef.'); |
| 52 | + } |
| 53 | + }); |
| 54 | + return defaultRef; |
| 55 | + }; |
| 56 | + |
| 57 | + this.$get = function FirebaseRef_$get() { |
| 58 | + |
| 59 | + if (this.singleUrl) { |
| 60 | + return this.$$createSingleRef(this.urls.default); |
| 61 | + } else { |
| 62 | + return this.$$createMultipleRefs(this.urls); |
| 63 | + } |
| 64 | + |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +angular.module('firebase') |
| 69 | + .provider('firebaseRef', FirebaseRef); |
0 commit comments