<commit_before>/*
 * Tangram
 * Copyright 2009 Baidu Inc. All rights reserved.
 */

///import baidu.ui.createUI;
///import baidu.lang.Event;
///import baidu.object.extend;
///import baidu.object.each;
///import baidu.dom.g;
///import baidu.dom.remove;
///import baidu.dom._styleFilter.px;
///import baidu.dom.setStyles;
///import baidu.dom.addClass;
///import baidu.dom.removeClass;
///import baidu.ui.behavior.posable.setPositionByElement;
///import baidu.dom.show;
///import baidu.dom.hide;
///import baidu.string.format;
///import baidu.array.each;
///import baidu.lang.isFunction;
///import baidu.event.getTarget;
///import baidu.dom.getAncestorByTag;

/**
 * @class  Menubar 下拉菜单
 * @param {Object} [options]                             配置选项
 * @param {String} [options.width = '200']               选项宽度
 * @param {String} [options.height]                      选项高度
 * @param {Number} [options.zIndex = 1200]               菜单zIndex
 * @param {String} [options.position = 'bottomCenter']   相对位置
 * @param {Object} [options.data]                        数据项
 * @param {Number} [options.hideDelay = 300]             鼠标移出子菜单多长时间，菜单消失
 * @param {Function} [options.toggle]                    开关函数,返回false时不显示
 */
baidu.ui.Menubar = baidu.ui.createUI(function(options){
    var me = this;
    me.items = {};//建立数据索引存储区
    me.data = options.data || [];
    me._initialized = false; //判断是否已经初始化
    me.dispatchEvent("oninit");
}).extend(
    /**
     *  @lends baidu.ui.Menubar.prototype
     */
{
    uiType: "menubar",
    width: 200,//这个地方不要写成字符串
    height: '',
    zIndex: 1200,
    hideDelay: 300,
    position: 'bottomCenter',
    tplBody: '<div id="#{id}" class="#{class}">#{content}</div>',
    tplBranch: '<ul id="#{ulId}">#{subitems}</ul>',
    tplItem: '<li onmouseover="#{onmouseover}" onmouseout="#{onmouseout}"><a href="#" id="#{id}" class="#{class}" onclick="#{onclick}" title="#{title}">#{content}</a>#{branch}</li>',
    tplContent: '<span class="#{contentClass}">#{content}</span>',
    tplArrow: '<span class="#{arrow}"></span>',
	/**
	 * @private
	 */
    toggle: function(){return true},
    posable: true,
    
    /**
     * 获取Menubar组件的HTML String
	 * @private
     * @return {String}
     */
    getString: function(){
        var me = this;
        return baidu.string.format(me.tplBody, {
            id: me.getId(),
            "class": me.getClass(),
            guid: me.guid,
            content: me.getItemsString(me.data, 0)
        });
    },
    
    /**
     * 生成items字符串
	 * @private
     * @param {Object} items 数据
     * @param {String} branchId 条目ID
     * @return {String}
     */
    getItemsString: function(items, branchId){
        var me = this,
            htmlArr = [];
        baidu.array.each(items, function(itemData, idx){
            var itemArr = [],
                itemId = branchId + '-' + idx;
            me.items[itemId] = itemData;//建立数据索引，方便查找item数据
                itemArr.push(baidu.string.format(me.tplContent, {
                contentClass : me.getClass("content"),
                content : itemData.content || itemData.label
            }));

            if (itemData.items) {
                itemArr.push(baidu.string.format(me.tplArrow, {
                    arrow: me.getClass("arrow")
                }));
            }

            htmlArr.push(baidu.string.format(me.tplItem, {
                id: me.getItemId(itemId),
                "class": (itemData.disabled ? (me.getClass("item") + ' ' + me.getClass("item-disabled")) : me.getClass("item")),
                onclick: me.getCallString("itemClick", itemId),
                onmouseover: itemData.disabled || me.getCallRef() + ".itemMouseOver(event, '" + itemId + "')",
                onmouseout: itemData.disabled || me.getCallRef() + ".itemMouseOut(event, '" + itemId + "')",
                content: itemArr.join(''),
                branch: itemData.items ? me.getItemsString(itemData.items, itemId) : '',
                title: itemData.title
            }));
        });
        
        return baidu.string.format(me.tplBranch, {
            ulId: me.getBranchId(branchId),
            subitems: htmlArr.join('')
        });
    },
    
    /**
     * 渲染menubar
     * @param {HTMLElement} target 目标元素
     */
    render: function(target){
        var me = this;
        target = baidu.g(target);
        if(target){
            me.targetId = target.id || me.getId("target");
        }
        me.renderMain();
        me.dispatchEvent("onload");
    },
    
    /**
     * 单个条目被点击时触发
     * @param {String} idx item索引
     */
    itemClick: function(idx){
        var me = this;
        me._close();
        me.dispatchEvent("onitemclick", me.getItemEventData(idx));
    },
    
    /**
     * 事件触发数据
     * @param {String} idx item索引
     * @return {Object}   item对象
     */
    getItemEventData: function(idx){
        return {
            value: this.getItemData(idx),
            index: idx
        };
    },
    
    /**
     * 单个条目mouseover的响应
     * @param {Object} idx     索引
     */
    itemMouseOver: function(evt, idx){
        var me = this,
            target = baidu.event.getTarget(evt),
            itemData = me.getItemData(idx), 
            itemDom = me.getItem(idx),
            subItem;<commit_msg>修复Menubar的item链接 [Bugfix] #PUBLICGE-633 由于链接的href是#，当点击单个菜单项时，会使页面跳到最顶端 Menubar : 在itemClick中增加第二参数event，点击时阻止默认行为<commit_after>/*
 * Tangram
 * Copyright 2009 Baidu Inc. All rights reserved.
 */

///import baidu.ui.createUI;
///import baidu.lang.Event;
///import baidu.object.extend;
///import baidu.object.each;
///import baidu.dom.g;
///import baidu.dom.remove;
///import baidu.dom._styleFilter.px;
///import baidu.dom.setStyles;
///import baidu.dom.addClass;
///import baidu.dom.removeClass;
///import baidu.ui.behavior.posable.setPositionByElement;
///import baidu.dom.show;
///import baidu.dom.hide;
///import baidu.string.format;
///import baidu.array.each;
///import baidu.lang.isFunction;
///import baidu.event.getTarget;
///import baidu.event.preventDefault;
///import baidu.dom.getAncestorByTag;

/**
 * @class  Menubar 下拉菜单
 * @param {Object} [options]                             配置选项
 * @param {String} [options.width = '200']               选项宽度
 * @param {String} [options.height]                      选项高度
 * @param {Number} [options.zIndex = 1200]               菜单zIndex
 * @param {String} [options.position = 'bottomCenter']   相对位置
 * @param {Object} [options.data]                        数据项
 * @param {Number} [options.hideDelay = 300]             鼠标移出子菜单多长时间，菜单消失
 * @param {Function} [options.toggle]                    开关函数,返回false时不显示
 */
baidu.ui.Menubar = baidu.ui.createUI(function(options){
    var me = this;
    me.items = {};//建立数据索引存储区
    me.data = options.data || [];
    me._initialized = false; //判断是否已经初始化
    me.dispatchEvent("oninit");
}).extend(
    /**
     *  @lends baidu.ui.Menubar.prototype
     */
{
    uiType: "menubar",
    width: 200,//这个地方不要写成字符串
    height: '',
    zIndex: 1200,
    hideDelay: 300,
    position: 'bottomCenter',
    tplBody: '<div id="#{id}" class="#{class}">#{content}</div>',
    tplBranch: '<ul id="#{ulId}">#{subitems}</ul>',
    tplItem: '<li onmouseover="#{onmouseover}" onmouseout="#{onmouseout}"><a href="#" id="#{id}" class="#{class}" onclick="#{onclick}" title="#{title}">#{content}</a>#{branch}</li>',
    tplContent: '<span class="#{contentClass}">#{content}</span>',
    tplArrow: '<span class="#{arrow}"></span>',
	/**
	 * @private
	 */
    toggle: function(){return true},
    posable: true,
    
    /**
     * 获取Menubar组件的HTML String
	 * @private
     * @return {String}
     */
    getString: function(){
        var me = this;
        return baidu.string.format(me.tplBody, {
            id: me.getId(),
            "class": me.getClass(),
            guid: me.guid,
            content: me.getItemsString(me.data, 0)
        });
    },
    
    /**
     * 生成items字符串
	 * @private
     * @param {Object} items 数据
     * @param {String} branchId 条目ID
     * @return {String}
     */
    getItemsString: function(items, branchId){
        var me = this,
            htmlArr = [];
        baidu.array.each(items, function(itemData, idx){
            var itemArr = [],
                itemId = branchId + '-' + idx;
            me.items[itemId] = itemData;//建立数据索引，方便查找item数据
                itemArr.push(baidu.string.format(me.tplContent, {
                contentClass : me.getClass("content"),
                content : itemData.content || itemData.label
            }));

            if (itemData.items) {
                itemArr.push(baidu.string.format(me.tplArrow, {
                    arrow: me.getClass("arrow")
                }));
            }

            htmlArr.push(baidu.string.format(me.tplItem, {
                id: me.getItemId(itemId),
                "class": (itemData.disabled ? (me.getClass("item") + ' ' + me.getClass("item-disabled")) : me.getClass("item")),
                onclick: me.getCallRef() + ".itemClick('"+itemId+"', event);",
                onmouseover: itemData.disabled || me.getCallRef() + ".itemMouseOver(event, '" + itemId + "')",
                onmouseout: itemData.disabled || me.getCallRef() + ".itemMouseOut(event, '" + itemId + "')",
                content: itemArr.join(''),
                branch: itemData.items ? me.getItemsString(itemData.items, itemId) : '',
                title: itemData.title
            }));
        });
        
        return baidu.string.format(me.tplBranch, {
            ulId: me.getBranchId(branchId),
            subitems: htmlArr.join('')
        });
    },
    
    /**
     * 渲染menubar
     * @param {HTMLElement} target 目标元素
     */
    render: function(target){
        var me = this;
        target = baidu.g(target);
        if(target){
            me.targetId = target.id || me.getId("target");
        }
        me.renderMain();
        me.dispatchEvent("onload");
    },
    
    /**
     * 单个条目被点击时触发
     * @param {String} idx item索引
     * @param {Event} evt 浏览器的事件对象
     */
    itemClick: function(idx, evt){
        var me = this;
        baidu.event.preventDefault(evt);
        me._close();
        me.dispatchEvent("onitemclick", me.getItemEventData(idx));
    },
    
    /**
     * 事件触发数据
     * @param {String} idx item索引
     * @return {Object}   item对象
     */
    getItemEventData: function(idx){
        return {
            value: this.getItemData(idx),
            index: idx
        };
    },
    
    /**
     * 单个条目mouseover的响应
     * @param {Object} idx     索引
     */
    itemMouseOver: function(evt, idx){
        var me = this,
            target = baidu.event.getTarget(evt),
            itemData = me.getItemData(idx), 
            itemDom = me.getItem(idx),
            subItem;
{'fraction_non_alphanumeric': 0.16305258259655653, 'fraction_numerical': 0.004932526756630991, 'mean_word_length': 2.015151515151515, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6683532', 'n_tokens_mistral': 3700, 'n_tokens_neox': 3789, 'n_words': 611}