/*! Knockout Sync - v0.1.0 - 2013-04-16 * https://github.com/katowulf/knockout-sync * Copyright (c) 2013 Michael "Kato" Wulf; Licensed MIT, GPL */ (function(){var e=!1,t=/xyz/.test(function(){xyz})?/\b_super\b/:/.*/;this.Class=function(){},Class.extend=function(n){function o(){!e&&this.init&&this.init.apply(this,arguments)}var r=this.prototype;e=!0;var i=new this;e=!1;for(var s in n)i[s]=typeof n[s]=="function"&&typeof r[s]=="function"&&t.test(n[s])?function(e,t){return function(){var n=this._super;this._super=r[e];var i=t.apply(this,arguments);return this._super=n,i}}(s,n[s]):n[s];return o.prototype=i,o.prototype.constructor=o,o.extend=arguments.callee,o}})(),function(e){e||(e=window.console={log:function(e,t,n,r,i){},info:function(e,t,n,r,i){},warn:function(e,t,n,r,i){},error:function(e,t,n,r,i){}}),Function.prototype.bind||(Function.prototype.bind=function(e){var t=this,n=Array.prototype.slice.call(arguments,1);return function(){return t.apply(e,Array.prototype.concat.apply(n,arguments))}}),typeof e.log=="object"&&(e.log=Function.prototype.call.bind(e.log,e),e.info=Function.prototype.call.bind(e.info,e),e.warn=Function.prototype.call.bind(e.warn,e),e.error=Function.prototype.call.bind(e.error,e)),"group"in e||(e.group=function(t){e.info("\n------------\n"+t+"\n------------")}),"groupEnd"in e||(e.groupEnd=function(){}),"time"in e||function(){var t={};e.time=function(e){t[e]=(new Date).getTime()},e.timeEnd=function(n){var r=(new Date).getTime(),i=n in t?r-t[n]:0;e.info(n+": "+i+"ms")}}()}(window.console),function(e){"use strict";if(!e._){var t=Array.prototype,n={},r=t.slice,i=t.push,s=Object.prototype.hasOwnProperty,o=t.forEach,u=Array.isArray,a=Object.prototype.toString,f=t.map,l=t.concat,c=t.indexOf,h=e._={},p=h.each=function(e,t,r){if(e==null)return;if(o&&e.forEach===o)e.forEach(t,r);else if(e.length===+e.length){for(var i=0,s=e.length;ir||n===void 0)return 1;if(n-1)u.splice(t,1),r&&(t<=s&&s--,t<=o&&o--)}),this},has:function(e){return h(e,u)>-1},empty:function(){return u=[],this},disable:function(){return u=a=t=undefined,this},disabled:function(){return!u},lock:function(){return a=undefined,t||p.disable(),this},locked:function(){return!a},fireWith:function(e,t){return t=t||[],t=[e,t.slice?t.slice():t],u&&(!n||a)&&(r?a.push(t):l(t)),this},fire:function(){return p.fireWith(this,arguments),this},fired:function(){return!!n}};return p},v.Deferred=function(e){var t=[["resolve","done",v.Callbacks("once memory"),"resolved"],["reject","fail",v.Callbacks("once memory"),"rejected"],["notify","progress",v.Callbacks("memory")]],n="pending",r={state:function(){return n},always:function(){return i.done(arguments).fail(arguments),this},then:function(){var e=arguments;return v.Deferred(function(n){f(t,function(t,r){var s=t[0],o=e[r];i[t[1]](l(o)?function(){var e;try{e=o.apply(this,arguments)}catch(t){n.reject(t);return}e&&l(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[s!=="notify"?"resolveWith":s+"With"](this===i?n:this,[e])}:n[s])}),e=null}).promise()},promise:function(e){return e!=null?c(e,r):r}},i={};return r.pipe=r.then,f(t,function(e,s){var o=e[2],u=e[3];r[e[1]]=o.add,u&&o.add(function(){n=u},t[s^1][2].disable,t[2][2].lock),i[e[0]]=o.fire,i[e[0]+"With"]=o.fireWith}),r.promise(i),e&&e.call(i,i),i},v.when=function(e){var t=0,n=d(e)==="array"&&arguments.length===1?e:a.call(arguments),r=n.length;d(e)==="array"&&e.length===1&&(e=e[0]);var i=r!==1||e&&l(e.promise)?r:0,s=i===1?e:v.Deferred(),o=function(e,t,n){return function(r){t[e]=this,n[e]=arguments.length>1?a.call(arguments):r,n===u?s.notifyWith(t,n):--i||s.resolveWith(t,n)}},u,f,c;if(r>1){u=new Array(r),f=new Array(r),c=new Array(r);for(;t=0&&t++}.bind(this)),t})},read:function(){return this._synced?this.ready:this._then(function(){var e=_.Deferred(),t=r(e);return this.store.on("create",function(e,n,r,i){this._create(e,n,i),t()}.bind(this)),this.store.on("update",this._update.bind(this)),this.store.on("delete",this._delete.bind(this)),this.obs.watchChanges(this.store,this._local.bind(this)),e})},update:function(e,t){return this._then(function(){return this._update(e,t)>=0?e:(console.warn("CrudArray::update - invalid key (not in local data)",e),!1)})},"delete":function(e){return this._then(function(){var t=this._map.indexOf(e);return t>-1?this.obs.splice(t,1):!1})},dispose:function(){_.each(this.subs,function(e){e.dispose()})},_create:function(e,t,n){var r=this._map.indexOf(e),i=this._map.indexOf(n);if(r<0){var s=this.factory.make(e,t);return n===null||i>=0?(this.obs.splice(i+1,0,s),i+1):this.obs.push(s)}return this._update(e,t),-1},_update:function(t,r){var i=this._map.indexOf(t),s;if(i>=0){s=this.obs()[i];if(n(this.store.getFieldNames(),s,r)){var o=e.sync.applyUpdates(s,r);e.isObservable(o)||this.obs.splice(i,1,o)}}return i},_delete:function(e){var t=this._map.indexOf(e);t>=0&&this.obs.splice(t,1)},_local:function(t){_.each(t,function(t,n){switch(t.status){case"create":this.store.create(e.sync.prepStoreData(t.data,this.store));break;case"update":this.store.update(n,e.sync.prepStoreData(t.data,this.store));break;case"delete":this.store.delete(n);break;case"move":break;default:throw new Error("Invalid change status: "+t.status)}}.bind(this))},_then:function(e){return e=e.bind(this),this.ready=this.ready.then(function(){var t=_.Deferred();return _.when(e()).always(t.resolve),t}),this}})}(window.ko),function(){"use strict";ko.sync.Factory=Class.extend({init:function(e,t){this.store=e,this.opts=t||{}},make:function(e,t){var n=_.pick(t,this.store.getFieldNames());return this.opts.observeFields&&ko.utils.arrayForEach(this.opts.observeFields,function(e){n[e]=_.isArray(n[e])?ko.observableArray(n[e]):ko.observable(n[e])}),this.opts.observe?ko.observable(n):n}})}(),function(){"use strict";ko.sync.KeyMap=function(e,t){this._subs=[],this._idx={},this._init(e,t)},ko.utils.extend(ko.sync.KeyMap.prototype,{indexOf:function(e){return e&&this.hasKey(e)?this._idx[e]:-1},hasKey:function(e){return _.has(this._idx,e)},_changed:function(e){var t=this._idx;_.each(e,function(e,n){switch(e.status){case"delete":delete t[n];break;case"create":case"update":case"move":t[n]=e.idx;break;default:throw new Error("Invalid change status: "+e.status)}})},_init:function(e,t){_.each(ko.sync.unwrapAll(t),function(t,n){this._idx[e.getKey(t)]=n}),this._subs.push(t.watchChanges(e,this._changed.bind(this)))},dispose:function(){_.each(this._subs,function(e){e.dispose()}),this._idx=null,this._subs=null}})}(),function(e){"use strict";e.sync.Store=Class.extend({getKey:function(e){throw new Error("Implementations must declare getKey method")},getFieldNames:function(){throw new Error("Implementations must declare getFieldNames method")},create:function(e){throw new Error("Implementations must declare create method")},read:function(e){throw new Error("Implementations must declare read method")},update:function(e,t){throw new Error("Implementations must declare update method")},"delete":function(e){throw new Error("Implementations must declare delete method")},on:function(e,t,n){throw new Error("Implementations must declare on events for add, remove, and change")},dispose:function(){throw new Error("Implementations must declare dispose method")}})}(window.ko),function(){"use strict";function t(e,t,n,r,i){if(r===null)return r;var s=_.extend({},r);return s[e]=n,t&&(s[t]=i),s}function n(t,n,r){var i=_.extend({},r);return n&&n!==".priority"&&(i[".priority"]=i[n]===e?null:i[n],delete i[n]),delete i[t],i}function r(e){switch(e){case"create":return"child_added";case"update":return"child_changed";case"delete":return"child_removed";default:throw new Error("Invalid event type: "+e)}}function i(e,n,r,i){var s=e.on("value",function(e){var s=t(n,r,e.name(),e.val(),e.getPriority());i(e.name(),s,"update")});return{dispose:function(){e.off("value",s)}}}function s(e,n,i,s,o){var u=[];return _.each(n,function(n){var a=r(n),f=e.on(a,function(e,r){o(e.name(),t(i,s,e.name(),e.val(),e.getPriority()),n,r)});u.push(function(){e.off(a,f)})}.bind(this)),{dispose:function(){_.each(u,function(e){e()})}}}var e;ko.sync.stores.Firebase=ko.sync.Store.extend({init:function(e,t,n){n=_.extend({keyField:"_id",sortField:".priority"},n),this.fieldNames=t,this.ref=e,this.pull=e,this.kf=n.keyField,this.sf=n.sortField,this.subs=[],n&&this._applyOpts(n),this._initRef()},getKey:function(e){return e&&e[this.kf]?e[this.kf]:null},getFieldNames:function(){return this.fieldNames},create:function(t){return _.Deferred(function(r){if(t===e)r.reject("invalid data (undefined)");else{var i=this.getKey(t);if(i)this.update(i,t).done(r.resolve).fail(r.reject);else{console.log("Firebase::create",n(this.kf,this.sf,t));var s=this.ref.push(n(this.kf,this.sf,t),function(e){e?r.reject(e):r.resolve(s,t)}).name()}}}.bind(this))},read:function(e){return _.Deferred(function(n){console.log("Firebase::read",e),this.ref.child(e).once("value",function(e){n.resolve(t(this.kf,this.sf,e.name(),e.val(),e.getPriority()))}.bind(this),function(e){n.reject(e)})}.bind(this))},update:function(t,r){return _.Deferred(function(i){r===e?i.reject("invalid data (undefined)"):this.ref.child(t).set(n(this.kf,this.sf,r),function(e){e?i.reject(e):i.resolve(t,r)})}.bind(this))},"delete":function(e){return _.Deferred(function(t){this.ref.child(e).remove(function(n){n?t.reject(n):t.resolve(e)})}.bind(this))},on:function(e,t,n){return arguments.length===3?this._disp(i(this.ref.child(t),this.kf,this.sf,n)):(n=t,this._disp(s(this.pull,e.split(" "),this.kf,this.sf,n)))},dispose:function(){_.each(this.subs,function(e){e.dispose()}),this.ref=null,this.pull=null,this.subs=[]},_disp:function(e){return this.subs.push(e),e},_applyOpts:function(e){e.limit&&!_.has(e,"endAt")&&!_.has(e,"startAt")&&(this.pull=this.pull.endAt()),_.each(["limit","endAt","startAt"],function(t,n){_.has(e,n)&&(this.pull=this.pull[n](t))}.bind(this))},_initRef:function(){this.pull.on("child_added",function(){})}})}();