Rev | Line | |
---|
[483] | 1 | dojo.provide("dojox.lang.oo.rearrange"); |
---|
| 2 | |
---|
| 3 | (function(){ |
---|
| 4 | var extraNames = dojo._extraNames, extraLen = extraNames.length, |
---|
| 5 | opts = Object.prototype.toString, empty = {}; |
---|
| 6 | |
---|
| 7 | dojox.lang.oo.rearrange = function(bag, map){ |
---|
| 8 | // summary: |
---|
| 9 | // Process properties in place by removing and renaming them. |
---|
| 10 | // description: |
---|
| 11 | // Properties of an object are to be renamed or removed specified |
---|
| 12 | // by "map" argument. Only own properties of "map" are processed. |
---|
| 13 | // example: |
---|
| 14 | // | oo.rearrange(bag, { |
---|
| 15 | // | abc: "def", // rename "abc" attribute to "def" |
---|
| 16 | // | ghi: null // remove/hide "ghi" attribute |
---|
| 17 | // | }); |
---|
| 18 | // bag: Object |
---|
| 19 | // the object to be processed |
---|
| 20 | // map: Object |
---|
| 21 | // the dictionary for renaming (false value indicates removal of the named property) |
---|
| 22 | // returns: Object |
---|
| 23 | // the original object |
---|
| 24 | |
---|
| 25 | var name, newName, prop, i, t; |
---|
| 26 | |
---|
| 27 | for(name in map){ |
---|
| 28 | newName = map[name]; |
---|
| 29 | if(!newName || opts.call(newName) == "[object String]"){ |
---|
| 30 | prop = bag[name]; |
---|
| 31 | if(!(name in empty) || empty[name] !== prop){ |
---|
| 32 | if(!(delete bag[name])){ |
---|
| 33 | // can't delete => hide it |
---|
| 34 | bag[name] = undefined; |
---|
| 35 | } |
---|
| 36 | if(newName){ |
---|
| 37 | bag[newName] = prop; |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | if(extraLen){ |
---|
| 43 | for(i = 0; i < extraLen; ++i){ |
---|
| 44 | name = extraNames[i]; |
---|
| 45 | // repeating the body above |
---|
| 46 | newName = map[name]; |
---|
| 47 | if(!newName || opts.call(newName) == "[object String]"){ |
---|
| 48 | prop = bag[name]; |
---|
| 49 | if(!(name in empty) || empty[name] !== prop){ |
---|
| 50 | if(!(delete bag[name])){ |
---|
| 51 | // can't delete => hide it |
---|
| 52 | bag[name] = undefined; |
---|
| 53 | } |
---|
| 54 | if(newName){ |
---|
| 55 | bag[newName] = prop; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | return bag; // Object |
---|
| 63 | }; |
---|
| 64 | })(); |
---|
Note: See
TracBrowser
for help on using the repository browser.