
Add methods to the base Object and String classes to facilitate jQuery form handling. Signed-off-by: Adam King <rak@linux.vnet.ibm.com> --- ui/js/src/kimchi.object.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++ ui/js/src/kimchi.string.js | 45 ++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 ui/js/src/kimchi.object.js create mode 100644 ui/js/src/kimchi.string.js diff --git a/ui/js/src/kimchi.object.js b/ui/js/src/kimchi.object.js new file mode 100644 index 0000000..6ef284a --- /dev/null +++ b/ui/js/src/kimchi.object.js @@ -0,0 +1,85 @@ +/* + * Project Kimchi + * + * Copyright IBM, Corp. 2014 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +Object.defineProperty(Object.prototype, "getDeepValue", { + value: function(key) { + var result=undefined; + try { + if(!Array.isArray(key)) { + key=key.parseKey(); + } + if(key.length!=0) { + var tmpName=key.shift(); + if(this[tmpName]!=undefined) { + result=this[tmpName]; + } + if(key.length!=0) { + result=result.getDeepValue(key); + } + } + } + catch (err) { + //do nothing + } + return(result); + } +}); + +Object.defineProperty(Object.prototype, "setDeepValue", { + value: function(key, val) { + var keys; + if(Array.isArray(key)) { + keys=key; + } + else { + keys=key.parseKey(); + } + if(keys.length!=0) { + var key=keys.shift(); + if(keys.length==0) { + if(this[key]==undefined) { + this[key]=val; + } + else if(Array.isArray(this[key])){ + this[key].push(val); + } + else { + var tmpArray=[] + tmpArray.push(this[key]); + tmpArray.push(val); + result[key]=tmpArray; + } + } + else { + if(this[key]==undefined) { + this[key]=new Object(); + this[key].setDeepValue(keys,val); + } + else if(Array.isArray(this[key])){ + var tmpO=new Object(); + this[key].push(tmpO); + tmpO.setDeepValue(keys,val); + } + else { + this[key].setDeepValue(keys,val); + } + } + } + return(this); + } +}); diff --git a/ui/js/src/kimchi.string.js b/ui/js/src/kimchi.string.js new file mode 100644 index 0000000..252ae50 --- /dev/null +++ b/ui/js/src/kimchi.string.js @@ -0,0 +1,45 @@ +/* + * Project Kimchi + * + * Copyright IBM, Corp. 2014 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +Object.defineProperty(String.prototype, "parseKey", { + value: function(parsedKey) { + try { + if (!Array.isArray(parsedKey)) { + parsedKey=[]; + } + } + catch (err) { + parsedKey=[]; + } + var openBracket=this.indexOf("["); + if (openBracket!=-1) { + var id=this.slice(0, openBracket); + parsedKey.push(id); + var closeBracket=this.lastIndexOf("]"); + if (closeBracket==-1) { + closeBracket=this.length; + } + var tmpName=this.slice(openBracket+1,closeBracket); + tmpName.parseKey(parsedKey); + } + else { + parsedKey.push(this); + } + return(parsedKey); + } +}); -- 1.8.1.4