1 /**************************************************************************** 2 Copyright (c) 2011-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies Inc. 4 5 http://www.cocos2d-x.org 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 ****************************************************************************/ 25 26 /** 27 * UI Helper 28 * @namespace 29 * @name ccui.helper 30 */ 31 ccui.helper = { 32 /** 33 * Finds a widget whose tag equals to param tag from root widget. 34 * @param {ccui.Widget} root 35 * @param {number} tag 36 * @returns {ccui.Widget} 37 */ 38 seekWidgetByTag: function (root, tag) { 39 if (!root) { 40 return null; 41 } 42 if (root.getTag() == tag) { 43 return root; 44 } 45 var arrayRootChildren = root.getChildren(); 46 var length = arrayRootChildren.length; 47 for (var i = 0; i < length; i++) { 48 var child = arrayRootChildren[i]; 49 var res = ccui.helper.seekWidgetByTag(child, tag); 50 if (res != null) { 51 return res; 52 } 53 } 54 return null; 55 }, 56 57 /** 58 * Finds a widget whose name equals to param name from root widget. 59 * @param {ccui.Widget} root 60 * @param {String} name 61 * @returns {ccui.Widget} 62 */ 63 seekWidgetByName: function (root, name) { 64 if (!root) { 65 return null; 66 } 67 if (root.getName() == name) { 68 return root; 69 } 70 var arrayRootChildren = root.getChildren(); 71 var length = arrayRootChildren.length; 72 for (var i = 0; i < length; i++) { 73 var child = arrayRootChildren[i]; 74 var res = ccui.helper.seekWidgetByName(child, name); 75 if (res != null) { 76 return res; 77 } 78 } 79 return null; 80 }, 81 82 /** 83 * Finds a widget whose name equals to param name from root widget. 84 * RelativeLayout will call this method to find the widget witch is needed. 85 * @param {ccui.Widget} root 86 * @param {String} name 87 * @returns {ccui.Widget} 88 */ 89 seekWidgetByRelativeName: function (root, name) { 90 if (!root) { 91 return null; 92 } 93 var arrayRootChildren = root.getChildren(); 94 var length = arrayRootChildren.length; 95 for (var i = 0; i < length; i++) { 96 var child = arrayRootChildren[i]; 97 var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE); 98 if (layoutParameter && layoutParameter.getRelativeName() == name) { 99 return child; 100 } 101 } 102 return null; 103 }, 104 105 /*temp action*/ 106 seekActionWidgetByActionTag: function (root, tag) { 107 if (!root) { 108 return null; 109 } 110 if (root.getActionTag() == tag) { 111 return root; 112 } 113 var arrayRootChildren = root.getChildren(); 114 for (var i = 0; i < arrayRootChildren.length; i++) { 115 var child = arrayRootChildren[i]; 116 var res = ccui.helper.seekActionWidgetByActionTag(child, tag); 117 if (res != null) { 118 return res; 119 } 120 } 121 return null; 122 } 123 }; 124