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  * @type {Object}
 29  */
 30 ccui.helper = {
 31 	/**
 32 	 * Finds a widget whose tag equals to param tag from root widget.
 33 	 * @param {ccui.Widget} root
 34 	 * @param {number} tag
 35 	 * @returns {ccui.Widget}
 36 	 */
 37 	seekWidgetByTag: function (root, tag) {
 38 	    if (!root) {
 39 	        return null;
 40 	    }
 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 = this.seekWidgetByTag(child, tag);
 49 	        if (res != null) {
 50 	            return res;
 51 	        }
 52 	    }
 53 	    return null;
 54 	},
 55 
 56 	/**
 57 	 * Finds a widget whose name equals to param name from root widget.
 58 	 * @param {ccui.Widget} root
 59 	 * @param {String} name
 60 	 * @returns {ccui.Widget}
 61 	 */
 62 	seekWidgetByName: function (root, name) {
 63 	    if (!root) {
 64 	        return null;
 65 	    }
 66 	    if (root.getName() == name) {
 67 	        return root;
 68 	    }
 69 	    var arrayRootChildren = root.getChildren();
 70 	    var length = arrayRootChildren.length;
 71 	    for (var i = 0; i < length; i++) {
 72 	        var child = arrayRootChildren[i];
 73 	        var res = this.seekWidgetByName(child, name);
 74 	        if (res != null) {
 75 	            return res;
 76 	        }
 77 	    }
 78 	    return null;
 79 	},
 80 
 81 	/**
 82 	 * Finds a widget whose name equals to param name from root widget.
 83 	 * RelativeLayout will call this method to find the widget witch is needed.
 84 	 * @param {ccui.Widget} root
 85 	 * @param {String} name
 86 	 * @returns {ccui.Widget}
 87 	 */
 88 	seekWidgetByRelativeName: function (root, name) {
 89 	    if (!root) {
 90 	        return null;
 91 	    }
 92 	    var arrayRootChildren = root.getChildren();
 93 	    var length = arrayRootChildren.length;
 94 	    for (var i = 0; i < length; i++) {
 95 	        var child = arrayRootChildren[i];
 96 	        var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE);
 97 	        if (layoutParameter && layoutParameter.getRelativeName() == name) {
 98 	            return child;
 99 	        }
100 	    }
101 	    return null;
102 	},
103 
104 	/*temp action*/
105 	seekActionWidgetByActionTag: function (root, tag) {
106 	    if (!root) {
107 	        return null;
108 	    }
109 	    if (root.getActionTag() == tag) {
110 	        return root;
111 	    }
112 	    var arrayRootChildren = root.getChildren();
113 	    for (var i = 0; i < arrayRootChildren.length; i++) {
114 	        var child = arrayRootChildren[i];
115 	        var res = this.seekActionWidgetByActionTag(child, tag);
116 	        if (res != null) {
117 	            return res;
118 	        }
119 	    }
120 	    return null;
121 	}
122 
123 };
124 ccui.helper;
125