(function(){var t=260;u8.Base.Settings=function(t){$.extend(this,u8.Base.Settings.defaults,t);this._onLoad=new u8.Base.Event;this._providers={roaming:this.roaming&&new u8.Base.Settings.RoamingSettingsProvider($.extend({parent:this},this.roaming)),local:this.local&&new u8.Base.Settings.LocalSettingsProvider(this.local)}};u8.Base.Settings.defaults={roaming:{rootPath:"Web.Client",autoSave:true,autoSaveDelayInMillis:5e3},local:{rootPath:"Web.{identity:userName}"}};u8.Base.Settings.makeClass({name:"Settings"});$.extend(u8.Base.Settings.prototype,{get:function(t,e){$assert(this.isLoaded,"Settings.get() without the settings being loaded yet.");var i=this._getProvider(t);var s=i&&i.get(t);if(typeof s=="undefined"){if(typeof e=="function"){s=e()}else{s=e}}return s},set:function(t,e){$assert(this.isLoaded,"Settings.set() without the settings being loaded yet.");var i=this._getProvider(t);return i&&i.set(t,e)},extend:function(t,e){$assert(this.isLoaded,"Settings.extend() without the settings being loaded yet.");var i=this._getProvider(t);if(i){var s=i.get(t);if(s){e=$.extend(s,e)}return i.set(t,e)}},dispose:function(){if(!this._isDisposed){return}this._isDisposed=true},load:function(t){var e=this,i=this._providers.roaming;if(i){i.load(s)}else{s()}function s(){e.isLoaded=true;e._onLoad.raise(e,{});var i={};u8.services.events.raise("u8.services.settings.onLoad",e,i);if(t){t(e,i)}}},save:function(t){var e=this;var i=this._providers.roaming;i.isSaving=true;try{var s={};u8.services.events.raise("u8.services.settings.onPreSave",this,s);i.save(function(){u8.services.events.raise("u8.services.settings.onSave",e,s);if(t){t(e,s)}})}finally{i.isSaving=false}},clear:function(){this.isLoaded=false;for(var t in this._providers){var e=this._providers[t];if(e){e.clear()}}},ready:function(t){if(this.isLoaded){t(this,{})}else{this._onLoad.wait(t)}},_getProvider:function(t){var i=e(t);return i&&this._providers[i.provider]}});u8.services.add({name:"settings",type:"Settings",scope:"Session"});u8.Base.Settings.RoamingSettingsProvider=function(t){$.extend(this,t);this._data={}};$.extend(u8.Base.Settings.RoamingSettingsProvider.prototype,{load:function(t){function e(e,i){if(i.response){this._data={};var s=i.response.settings||{};for(var n in s){try{var a=s[n];this._data[n]={json:a,value:u8.Base.Json.parse(a)}}catch(t){u8.log.exception("settings","Failed to parse json setting '"+n+"'. The setting will be ignored!",t)}}}t()}u8.request("SettingsChannel.Find",{path:this.rootPath},e.bind(this),true)},save:function(t){var e=0;var i={};for(var s in this._data){var n=this._data[s];if(!n.action){continue}var a=typeof n.value=="undefined"?null:u8.Base.Json.stringify(n.value);if(n.json==a){delete n.action;continue}n.action="saving";i[s]=a;e++}if(e<=0){if(t){t(this,{})}return}var r=this;u8.request("SettingsChannel.Save",{values:i},function(e,a){if(!a.error){for(s in i){n=r._data[s];if(n&&n.action=="saving"){delete n.action}n.json=i[s]}}if(t){t(r,a)}})},set:function(t,e){t=this._assertValidPath(t);var i=this._data[t];var s=typeof e=="undefined";if(i){if(i.value===e){if(!e||typeof e!="object"){return undefined}}i.value=e;i.action=s?"deleted":"updated"}else{if(this.isDeleted){return undefined}this._data[t]={action:"new",value:e}}this._onSettingsChanged(t);return e},get:function(t){t=this._assertValidPath(t);var e=this._data[t];return e&&e.value},clear:function(){this._data={}},_assertValidPath:function(i){var s=e(i);i=s.path;if(!i.startsWith(this.rootPath)){i=this.rootPath+"/"+i}if(i.length>t){$error("Invalid settings path '"+i+"'! The path is too long. It must not exceed "+t+" characters.")}return i},_onSettingsChanged:function(t){u8.log.debug("settings","Settings changed for '"+t+"'.");if(this.isSaving){return}if(typeof this._autoSaveTimer!="undefined"){clearTimeout(this._autoSaveTimer);delete this._autoSaveTimer}if(this.autoSave){this._autoSaveTimer=setTimeout(this._onAutoSave.bind(this),this.autoSaveDelayInMillis||1)}},_onAutoSave:function(){if(this._isDisposed){return}this.save()}});u8.Base.Settings.LocalSettingsProvider=function(t){$.extend(this,t)};$.extend(u8.Base.Settings.LocalSettingsProvider.prototype,{load:function(t){t()},save:function(t){t(this,{})},get:function(t){t=this._assertValidPath(t);var e=u8.services.clientStorage.getItem(t);if(e){try{e=u8.Base.Json.parse(e)}catch(i){u8.log.exception("settings","Failed to parse json setting '"+t+"'. The setting will be ignored!",i);e=undefined}}return e},set:function(t,e){t=this._assertValidPath(t);if(typeof e=="undefined"){u8.services.clientStorage.removeItem(t)}else{var i=u8.Base.Json.stringify(e);u8.services.clientStorage.setItem(t,i)}return e},clear:function(){},_assertValidPath:function(t){var i=e(t);return i&&this.rootPath.format()+"/"+i.path}});function e(t){if(!t){return undefined}var e="roaming";var i=t.indexOf(":");if(i>0){e=t.substr(0,i)}if(i>=0){t=t.substr(i+1)}return{provider:e,path:t}}u8.using.loaders.push({name:"settings",load:function(t,e){u8.services.settings.ready(e)}})})();