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 * ccui.helper is the singleton object which is the Helper object contains some functions for seek widget 28 * @class 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 if (root.getTag() == tag) 42 return root; 43 44 var arrayRootChildren = root.getChildren(); 45 var length = arrayRootChildren.length; 46 for (var i = 0; i < length; i++) { 47 var child = arrayRootChildren[i]; 48 var res = ccui.helper.seekWidgetByTag(child, tag); 49 if (res != null) 50 return res; 51 } 52 return null; 53 }, 54 55 /** 56 * Finds a widget whose name equals to param name from root widget. 57 * @param {ccui.Widget} root 58 * @param {String} name 59 * @returns {ccui.Widget} 60 */ 61 seekWidgetByName: function (root, name) { 62 if (!root) 63 return null; 64 if (root.getName() == name) 65 return root; 66 var arrayRootChildren = root.getChildren(); 67 var length = arrayRootChildren.length; 68 for (var i = 0; i < length; i++) { 69 var child = arrayRootChildren[i]; 70 var res = ccui.helper.seekWidgetByName(child, name); 71 if (res != null) 72 return res; 73 } 74 return null; 75 }, 76 77 /** 78 * Finds a widget whose name equals to param name from root widget. 79 * RelativeLayout will call this method to find the widget witch is needed. 80 * @param {ccui.Widget} root 81 * @param {String} name 82 * @returns {ccui.Widget} 83 */ 84 seekWidgetByRelativeName: function (root, name) { 85 if (!root) 86 return null; 87 var arrayRootChildren = root.getChildren(); 88 var length = arrayRootChildren.length; 89 for (var i = 0; i < length; i++) { 90 var child = arrayRootChildren[i]; 91 var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE); 92 if (layoutParameter && layoutParameter.getRelativeName() == name) 93 return child; 94 } 95 return null; 96 }, 97 98 /** 99 * Finds a widget whose action tag equals to param name from root widget. 100 * @param {ccui.Widget} root 101 * @param {Number} tag 102 * @returns {ccui.Widget} 103 */ 104 seekActionWidgetByActionTag: function (root, tag) { 105 if (!root) 106 return null; 107 if (root.getActionTag() == tag) 108 return root; 109 var arrayRootChildren = root.getChildren(); 110 for (var i = 0; i < arrayRootChildren.length; i++) { 111 var child = arrayRootChildren[i]; 112 var res = ccui.helper.seekActionWidgetByActionTag(child, tag); 113 if (res != null) 114 return res; 115 } 116 return null; 117 } 118 }; 119