SearchBox_wev8.js 4.7 KB
define(['mUtil', "Component", "mService"],function(mUtil, Component, mService) {
    var SearchBox = function(options) {
        
        Component.super(this, options);
        
        this.type = "SearchBox";
        this.tpl = this.type + "_html";
        this.css = this.type + "_css";

        var vm = this.viewModel = {
            tip: '',
            scan: true,
            history: true,
            btns: [],
            onSearch: function(){},
            standalone: true
        };
        var _sb = {};
        
        this.beforeMount = function(){
            vm.leftBtns = [];
            vm.rightBtns = [];
            vm.btns.forEach(function(btn){
                btn.index = mUtil.UUID();
                if(btn.align == 'left'){
                    vm.leftBtns.push(btn);
                }else if(btn.align == 'right'){
                    vm.rightBtns.push(btn);
                }
            });
        };
        
        this.mounted = function(){
            var that = this,
                $comp = this.$el.children(".wev-comp-" + vm.compType),
                $search = $(".wev-search", $comp),
                i18n = require("i18n"),
                $page = that.$container;
            
            // 按钮单击事件
            $comp.on("click", ".btn", function(){
                var index = $(this).data("index");
                vm.btns.every(function(btn, i) {
                    if(btn.index == index){
                        //\s?兼容ios,ios下面是function ()
                        var fn = btn.click.toString().replace(/^function\s?\(\){/, '').replace(/}$/, '');
                        var code = new Function('var that= _u.getInstance("'+that.id+'");' + fn);

                        mUtil.eval(code, that.pageid);
                        return false;
                    }
                	return true;
                });
            });
            var classAnimation = "show_hide_searchbox", openServicePage = function(preload){
                mService.show("searchbox", {
                    preload: preload,
                    compid: that.id,
                    tip: vm.tip,
                    scan: vm.scan,
                    history: vm.history,
                    classAnimation: classAnimation,
                    searchkey: vm.searchkey,
                    toThirdPage: vm.toThirdPage,
                    closeCallback : function(){
                        mUtil.back();
                    },
                    onSearch: function(key){
                        vm.searchkey = key;
                        _sb.onSearch(key, true);
                    }
                });
            };
            
            $search.on("click.active", ".wev-placeholder", function (e) {
                openServicePage(false);
            });
            //预加载页面,解决ios第一次加载页面输入法无法弹出问题
            openServicePage(true);
        };

        _sb.onSearch = function(key, needBack){
            var that = this;
            if(mUtil.isObject(vm.onSearch)){
                var search = vm.onSearch, query = "";
                if (!mUtil.isEmpty(key)){
                    query = search.query.replace("$KEY$",encodeURIComponent(key));
                }
                if(mUtil.getPageId(search.pageid) == that.pageid){
                    Mobile_NS.refreshSpecifiedList(search.pageid, search.compoid, query).then(function(){
                        needBack && mUtil.back();
                    });
                    var $placeholder = this.$el.find(".wev-ellipsis"),
                        $searchIcon = $placeholder.find(".wev-icon-search"),//搜索图标
                        $searchTip = $placeholder.find(".wev-placeholder-text"),//搜索提示
                        tip = key || vm.tip;

                    key != "" ? $searchIcon.hide() : $searchIcon.show();
                    $searchTip.html(tip.replaceAll(" ", " "));
                    $placeholder.toggleClass("wev-placeholder-inner", !key).toggleClass("wev-placeholder-left", key);//根据是否有搜索关键字决定显示在左侧或中间
                }else{
                    var opts = {
                        onPageLoad: function(){
                            Mobile_NS.refreshList(search.compoid, query);
                        }
                    };
                    vm.toThirdPage = true;
                    $u("/mobilemode/appHomepageView.jsp?appHomepageId="+search.pageid, opts);
                }
            }else{
                mUtil.eval(vm.onSearch, that.pageid, key);
            }
        };

        this.onSearch = function(key){
            _sb.onSearch(key, false);
        }

        _sb.onSearch = _sb.onSearch.bind(this);
    };

    return Component.init(SearchBox);
});