1+ ( function webpackUniversalModuleDefinition ( root , factory ) {
2+ if ( typeof exports === 'object' && typeof module === 'object' )
3+ module . exports = factory ( ) ;
4+ else if ( typeof define === 'function' && define . amd )
5+ define ( [ ] , factory ) ;
6+ else if ( typeof exports === 'object' )
7+ exports [ "VueFire" ] = factory ( ) ;
8+ else
9+ root [ "VueFire" ] = factory ( ) ;
10+ } ) ( this , function ( ) {
11+ return /******/ ( function ( modules ) { // webpackBootstrap
12+ /******/ // The module cache
13+ /******/ var installedModules = { } ;
14+
15+ /******/ // The require function
16+ /******/ function __webpack_require__ ( moduleId ) {
17+
18+ /******/ // Check if module is in cache
19+ /******/ if ( installedModules [ moduleId ] )
20+ /******/ return installedModules [ moduleId ] . exports ;
21+
22+ /******/ // Create a new module (and put it into the cache)
23+ /******/ var module = installedModules [ moduleId ] = {
24+ /******/ exports : { } ,
25+ /******/ id : moduleId ,
26+ /******/ loaded : false
27+ /******/ } ;
28+
29+ /******/ // Execute the module function
30+ /******/ modules [ moduleId ] . call ( module . exports , module , module . exports , __webpack_require__ ) ;
31+
32+ /******/ // Flag the module as loaded
33+ /******/ module . loaded = true ;
34+
35+ /******/ // Return the exports of the module
36+ /******/ return module . exports ;
37+ /******/ }
38+
39+
40+ /******/ // expose the modules object (__webpack_modules__)
41+ /******/ __webpack_require__ . m = modules ;
42+
43+ /******/ // expose the module cache
44+ /******/ __webpack_require__ . c = installedModules ;
45+
46+ /******/ // __webpack_public_path__
47+ /******/ __webpack_require__ . p = "" ;
48+
49+ /******/ // Load entry module and return exports
50+ /******/ return __webpack_require__ ( 0 ) ;
51+ /******/ } )
52+ /************************************************************************/
53+ /******/ ( [
54+ /* 0 */
55+ /***/ function ( module , exports ) {
56+
57+ var Vue // late binding
58+
59+ /**
60+ * Check if a value is an object.
61+ *
62+ * @param {* } val
63+ * @return {boolean }
64+ */
65+ function isObject ( val ) {
66+ return Object . prototype . toString . call ( val ) === '[object Object]'
67+ }
68+
69+ /**
70+ * Convert firebase snapshot into a bindable data record.
71+ *
72+ * @param {FirebaseSnapshot } snapshot
73+ * @return {Object }
74+ */
75+ function createRecord ( snapshot ) {
76+ var value = snapshot . val ( )
77+ var res = isObject ( value )
78+ ? value
79+ : { '.value' : value }
80+ res [ '.key' ] = snapshot . key ( )
81+ return res
82+ }
83+
84+ /**
85+ * Find the index for an object with given key.
86+ *
87+ * @param {array } array
88+ * @param {string } key
89+ * @return {number }
90+ */
91+ function indexForKey ( array , key ) {
92+ for ( var i = 0 ; i < array . length ; i ++ ) {
93+ if ( array [ i ] [ '.key' ] === key ) {
94+ return i
95+ }
96+ }
97+ /* istanbul ignore next */
98+ return - 1
99+ }
100+
101+ /**
102+ * Bind a firebase data source to a key on a vm.
103+ *
104+ * @param {Vue } vm
105+ * @param {string } key
106+ * @param {object } source
107+ */
108+ function bind ( vm , key , source ) {
109+ var asObject = false
110+ var cancelCallback = null
111+ // check { source, asArray, cancelCallback } syntax
112+ if ( isObject ( source ) && source . hasOwnProperty ( 'source' ) ) {
113+ asObject = source . asObject
114+ cancelCallback = source . cancelCallback
115+ source = source . source
116+ }
117+ if ( ! isObject ( source ) ) {
118+ throw new Error ( 'VueFire: invalid Firebase binding source.' )
119+ }
120+ // get the original ref for possible queries
121+ var ref = source
122+ if ( typeof source . ref === 'function' ) {
123+ ref = source . ref ( )
124+ }
125+ vm . $firebaseRefs [ key ] = ref
126+ vm . _firebaseSources [ key ] = source
127+ // bind based on initial value type
128+ if ( asObject ) {
129+ bindAsObject ( vm , key , source , cancelCallback )
130+ } else {
131+ bindAsArray ( vm , key , source , cancelCallback )
132+ }
133+ }
134+
135+ /**
136+ * Bind a firebase data source to a key on a vm as an Array.
137+ *
138+ * @param {Vue } vm
139+ * @param {string } key
140+ * @param {object } source
141+ * @param {function|null } cancelCallback
142+ */
143+ function bindAsArray ( vm , key , source , cancelCallback ) {
144+ var array = [ ]
145+ Vue . util . defineReactive ( vm , key , array )
146+
147+ var onAdd = source . on ( 'child_added' , function ( snapshot , prevKey ) {
148+ var index = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
149+ array . splice ( index , 0 , createRecord ( snapshot ) )
150+ } , cancelCallback )
151+
152+ var onRemove = source . on ( 'child_removed' , function ( snapshot ) {
153+ var index = indexForKey ( array , snapshot . key ( ) )
154+ array . splice ( index , 1 )
155+ } , cancelCallback )
156+
157+ var onChange = source . on ( 'child_changed' , function ( snapshot ) {
158+ var index = indexForKey ( array , snapshot . key ( ) )
159+ array . splice ( index , 1 , createRecord ( snapshot ) )
160+ } , cancelCallback )
161+
162+ var onMove = source . on ( 'child_moved' , function ( snapshot , prevKey ) {
163+ var index = indexForKey ( array , snapshot . key ( ) )
164+ var record = array . splice ( index , 1 ) [ 0 ]
165+ var newIndex = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
166+ array . splice ( newIndex , 0 , record )
167+ } , cancelCallback )
168+
169+ vm . _firebaseListeners [ key ] = {
170+ child_added : onAdd ,
171+ child_removed : onRemove ,
172+ child_changed : onChange ,
173+ child_moved : onMove
174+ }
175+ }
176+
177+ /**
178+ * Bind a firebase data source to a key on a vm as an Object.
179+ *
180+ * @param {Vue } vm
181+ * @param {string } key
182+ * @param {Object } source
183+ * @param {function|null } cancelCallback
184+ */
185+ function bindAsObject ( vm , key , source , cancelCallback ) {
186+ Vue . util . defineReactive ( vm , key , { } )
187+ var cb = source . on ( 'value' , function ( snapshot ) {
188+ vm [ key ] = createRecord ( snapshot )
189+ } , cancelCallback )
190+ vm . _firebaseListeners [ key ] = { value : cb }
191+ }
192+
193+ /**
194+ * Unbind a firebase-bound key from a vm.
195+ *
196+ * @param {Vue } vm
197+ * @param {string } key
198+ */
199+ function unbind ( vm , key ) {
200+ var source = vm . _firebaseSources && vm . _firebaseSources [ key ]
201+ if ( ! source ) {
202+ throw new Error (
203+ 'VueFire: unbind failed: "' + key + '" is not bound to ' +
204+ 'a Firebase reference.'
205+ )
206+ }
207+ var listeners = vm . _firebaseListeners [ key ]
208+ for ( var event in listeners ) {
209+ source . off ( event , listeners [ event ] )
210+ }
211+ vm [ key ] = null
212+ vm . $firebaseRefs [ key ] = null
213+ vm . _firebaseSources [ key ] = null
214+ vm . _firebaseListeners [ key ] = null
215+ }
216+
217+ /**
218+ * Ensure the related bookkeeping variables on an instance.
219+ *
220+ * @param {Vue } vm
221+ */
222+ function ensureRefs ( vm ) {
223+ if ( ! vm . $firebaseRefs ) {
224+ vm . $firebaseRefs = Object . create ( null )
225+ vm . _firebaseSources = Object . create ( null )
226+ vm . _firebaseListeners = Object . create ( null )
227+ }
228+ }
229+
230+ var VueFireMixin = {
231+ init : function ( ) {
232+ var bindings = this . $options . firebase
233+ if ( ! bindings ) return
234+ ensureRefs ( this )
235+ for ( var key in bindings ) {
236+ bind ( this , key , bindings [ key ] )
237+ }
238+ } ,
239+ beforeDestroy : function ( ) {
240+ if ( ! this . $firebaseRefs ) return
241+ for ( var key in this . $firebaseRefs ) {
242+ if ( this . $firebaseRefs [ key ] ) {
243+ this . $unbind ( key )
244+ }
245+ }
246+ this . $firebaseRefs = null
247+ this . _firebaseSources = null
248+ this . _firebaseListeners = null
249+ }
250+ }
251+
252+ /**
253+ * Install function passed to Vue.use() in manual installation.
254+ *
255+ * @param {function } _Vue
256+ */
257+ function install ( _Vue ) {
258+ Vue = _Vue
259+ Vue . mixin ( VueFireMixin )
260+
261+ // use object-based merge strategy
262+ var mergeStrats = Vue . config . optionMergeStrategies
263+ mergeStrats . firebase = mergeStrats . methods
264+
265+ // extend instance methods
266+ Vue . prototype . $bindAsObject = function ( key , source , cancelCallback ) {
267+ ensureRefs ( this )
268+ bind ( this , key , {
269+ source : source ,
270+ asObject : true ,
271+ cancelCallback : cancelCallback
272+ } )
273+ }
274+
275+ Vue . prototype . $bindAsArray = function ( key , source , cancelCallback ) {
276+ ensureRefs ( this )
277+ bind ( this , key , {
278+ source : source ,
279+ cancelCallback : cancelCallback
280+ } )
281+ }
282+
283+ Vue . prototype . $unbind = function ( key ) {
284+ unbind ( this , key )
285+ }
286+ }
287+
288+ // auto install
289+ /* istanbul ignore if */
290+ if ( typeof window !== 'undefined' && window . Vue ) {
291+ install ( window . Vue )
292+ }
293+
294+ module . exports = install
295+
296+
297+ /***/ }
298+ /******/ ] )
299+ } ) ;
300+ ;
0 commit comments