service.searchbox_wev8.js 5.7 KB
define("mService/searchbox", ["mUtil"], function(mUtil) {
    var storage = {
        get: function(key){
            return JSON.parse(localStorage.getItem(key) || "[]");
        },
        set: function(key, value){
            try {
                var datas = this.get(key) || [], index = datas.indexOf(value), length = datas.length;
                if(length && index != -1){
                    datas.splice(index, 1);
                }
                if(length > 19){
                    datas.splice(0, length - 19);
                }
                datas.push(value);
                localStorage.setItem(key, JSON.stringify(datas));
            } catch (e) {
                mUtil.console.error(e.message);
            }
        },
        clear: function(key){
            localStorage.removeItem(key);
        }
    };
    
    return {
        mounted: function($page, cfg){
            var defCfg = {
                tip: "",
                scan: true,
                history: true,
                closeCallback : function(){mUtil.back();},
                success : function(){}
            };
            
            $.extend(defCfg, cfg);
            
            var $search = $page.find(".wev-search"),
                $searchKey = $page.find("input[type='search']"),
                $cancelBtn = $page.find(".btn.cancel"),
                 $clear = $page.find(".wev-clear-btn"),
                 $scan = $page.find(".wev-scan-btn"),
                 $historyWrap = $page.find(".wev-history"),
                 $historyContent = $page.find(".history-content"),
                 $delHistory = $page.find(".history-del"),
                 $noData = $page.find(".wev-no-data");
            
            mUtil.getMultiLabel({
                6037: "历史搜索",
                6038: "暂无历史搜索记录"
            }, function (tips) {
                $page.find(".history-title").html(tips['6037']);
                $noData.html(tips['6038']);
            });
            
             var storageKey = "mobile_searchbox_history_" + defCfg.compid,
                 recordHistory = defCfg.history;
            
            
            $page.find("form[disabledEnterSubmit]").keydown(function(event){
                if(event.keyCode == 13){return false;}
            });
            
            defCfg.scan && $scan.on("click", function(){
                Mobile_NS.scanQRCode(function(result){
                    $searchKey.val(result).trigger("input");
                    result = result.trim();
                    defCfg.onSearch && defCfg.onSearch.call(this, result);
                    if(recordHistory && result){
                        storage.set(storageKey, result);
                        history.render();
                    }
                });
            }).show();
            
            $searchKey.on("input", function(){
                var v = this.value;
                $search.toggleClass("wev-has-value", v != "");
            }).keyup(function(event){
                if(event.keyCode == 13){
                    var searchKey = this.value;
                    searchKey = searchKey.trim();
                    defCfg.onSearch && defCfg.onSearch.call(this, searchKey);
                    if(recordHistory && searchKey){
                        storage.set(storageKey, searchKey);
                        history.render();
                    }
                    this.blur();
                }
            }).attr("placeholder", defCfg.tip).focus();
            defCfg.searchkey && !defCfg.toThirdPage && $searchKey.val(defCfg.searchkey).focus().triggerHandler("input");
            
            $cancelBtn.on("click", function(){
                var closeCallback = defCfg.closeCallback;
                closeCallback && closeCallback();
            });
            
            $clear.on("click", function(){
                $searchKey.val("").focus().triggerHandler("input");
            });
            
            $delHistory.on("click", function(){
                mUtil.getLabel(6039, "确认删除全部历史记录?", function(msg){
                    Mobile_NS.confirm(msg, function(){
                        $historyContent.empty();
                        $delHistory.hide();
                        $noData.show();
                        storage.clear(storageKey);
                    });
                });
            });

            $page.find(".history-content").on("click", ".btn", function(){
                var searchKey = $(this).data("key") + "";
                defCfg.onSearch && defCfg.onSearch.call(this, searchKey);
                storage.set(storageKey, searchKey);
                history.render();
                $searchKey.val(searchKey).triggerHandler("input");
            });
            
            var history = {
                render: function(){
                    var hd = storage.get(storageKey).reverse();
                    if(hd.length){
                        var templateHtml = [
                                '{@each datas as d}',
                                '<div data-key="${d}" class="btn">$${d.replaceAll(" ", "&nbsp;")}</div>',
                                '{@/each}'
                            ].join('');
                        var html = mUtil.parseTemplate(templateHtml, {datas: hd});
                        $historyContent.html(html);
                        $delHistory.show();
                        $noData.hide();
                    }else{
                        $delHistory.hide();
                        $noData.show();
                    }
                }
            };
            if(recordHistory){
                history.render();
            }else{
                $historyWrap.hide();
                storage.clear(storageKey);
            }
        }
    };
});