NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / model-sync-local / model-sync-local-min.js
blobe32ac0ad803ec2ff7651d8af3f540428776135ee
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add("model-sync-local",function(e,t){function n(){}n._NON_ATTRS_CFG=["root"],n._hasLocalStorage=function(){var t=e.config.win.localStorage,n=e.guid();try{return t.setItem(n,n),t.removeItem(n),!0}catch(r){return!1}}(),n._data={},n._idMap={},n.prototype={root:"",storage:null,initializer:function(t){var r,i;t||(t={}),"root"in t&&(this.root=t.root||""),!this.root&&this.model&&this.model.prototype.root&&(this.root=this.model.prototype.root),n._hasLocalStorage&&(this.storage=e.config.win.localStorage,r=this.storage.getItem(this.root));if(r)try{n._data[this.root]=e.JSON.parse(r)}catch(s){n._data[this.root]=[]}else n._data[this.root]||(n._data[this.root]=[]);n._idMap[this.root]||(n._idMap[this.root]={}),e.Array.each(n._data[this.root],function(e){var t=e.id;t&&(n._idMap[this.root][t]=e)},this)},sync:function(e,t,n){t||(t={});var r,i;try{switch(e){case"read":this._isYUIModelList?r=this._index(t):r=this._show(t);break;case"create":r=this._create(t);break;case"update":r=this._update(t);break;case"delete":r=this._destroy(t)}}catch(s){i=s.message}r?n(null,r):i?n(i):n("Data not found in LocalStorage")},generateID:function(t){return e.guid(t+"_")},_index:function(){return n._data[this.root]},_show:function(){return n._idMap[this.root][this.get("id")]||null},_create:function(){var e=this.toJSON(),t=n._data[this.root],r=n._idMap[this.root];return e.id=this.generateID(this.root),t.push(e),r[e.id]=e,this._save(),e},_update:function(){var t=e.merge(this.toJSON());return n._idMap[this.get("id")]=t,this._save(),t},_destroy:function(){return delete n._idMap[this.get("id")],this._save(),this.toJSON()},_save:function(){n._hasLocalStorage&&this.storage&&this.storage.setItem(this.root,e.JSON.stringify(n._data[this.root]))}},e.namespace("ModelSync").Local=n},"3.13.0",{requires:["model","json-stringify"]});