Skip to content

Instantly share code, notes, and snippets.

@certainty
Created October 22, 2012 08:38
Show Gist options
  • Save certainty/3930362 to your computer and use it in GitHub Desktop.
Save certainty/3930362 to your computer and use it in GitHub Desktop.
/**
* @class Ext.omcx.I18N.PackedObject
*
*/
Ext.extend(Ext.omcx.I18N.PackedObject,Ext.util.Observable,{
/**
* Lookup the object stored under the given key
* @methodOf Ext.omcx.I18N.PackedObject
* @param {String} key
* @params default_value Will be returned if no entry can be found for key
*/
ref: function(key,default_value){
var path = key.split(this._delimiter);
return Ext.value(this._find(this._repository,path),Ext.value(default_value,null));
},
/**
* @methodOf Ext.omcx.I18N.PackedObject
* @param {String} key The key to store the value under
* @params value The value store under key
*/
set: function(key,value){
var path = key.split(this._delimiter);
this.fireEvent('before_set',key,value);
this._insert(this._repository,path,value);
this.fireEvent('set',key,value);
},
/**
* @methodOf Ext.omcx.I18N.PackedObject
* @private
* @param node
* @param func
*/
_walk: function(node,func){
Simplicity.Debug.warn('not yet implemented');
},
/**
* @methodOf Ext.omcx.I18N.PackedObject
* @private
* @param node
* @param path
*/
_find: function(node,path){
if(path.length == 0){
if(!node){
return null;
}
if(Ext.isArray(node)){
for(var i = 0, len = node.length; i < len; i++){
if(!Ext.isObject(node[i])){
return node[i];
}
}
return null;
}
return node;
}else{
var key = path.shift();
if(Ext.isArray(node)){
for(var i = 0,len = node.length; i < len; i++){
if(Ext.isObject(node[i]) && node[i][key]){
return this._find(node[i][key],path);
}
}
return null;
}else if(Ext.isObject(node)){
if(!node[key]){
return null;
}else{
return this._find(node[key],path);
}
}else{
return null;
}
}
},
_insert: function(node,path,value){
var key = path.shift();
//debugger;
if(path.length == 0){
if(Ext.isObject(node)){
node[key] = value;
return node;
}else{
throw "This should never happen";
}
}else{
if(Ext.isArray(node)){
for(var i = 0,len = node.length; i < len; i++){
if(Ext.isObject(node[i]) && node[i][key]){
return this._insert(node[i][key],path,value);
}
}
var new_tree = {};
new_tree[key] = this._insert({},path,value);
node.push(new_tree);
return node;
}else if(Ext.isObject(node)){
if(!node[key]){
node[key] = {};
return this._insert(node[key],path,value);
}else{
if(Ext.isArray(node[key])){
//branch
//we need to look ahead
var next_key = path[0];
for(var i = 0,len = node[key].length; i < len; i++){
if(Ext.isObject(node[key][i]) && node[key][i][next_key]){
return this._insert(node[key][i],path,value);
}
}
//not in array
node[key].push(this._insert({},path,value));
return node[key];
}else if(Ext.isObject(node[key])){
//branch
return this._insert(node[key],path,value)
}else{
//leaf node
node[key] = [node[key],this._insert({},path,value)];
return node[key];
}
}
}else{
throw "This should never happen";
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment