write-search.js 1.1 KB
/**
 * 将搜索结果存localStorage
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/29
 */

let ranToken = ',',
    historyval = 'historys1';

// 获取分隔符
function getRanToken() {
    return ranToken;
}

// 存变量
function getHistoryval() {
    return historyval;
}

function setHistoryValFun(query) {
    let historys;

    try {
        if (localStorage) {
            historys = localStorage.getItem(historyval);

            historys = historys ? historys.replace(new RegExp((query + ranToken), 'g'), '') : '';

            if (historys === '') {
                query = ranToken + query;
            }

            historys += query + ranToken;
            localStorage.setItem(historyval, historys);
        }
    } catch (e) {
        console.log(e);
    }
}

// 绑定提交前的存local操作
function bindWirteLocal($form) {
    $form.on('submit', function() {
        let query = this.query.value;

        setHistoryValFun(query);
    });
}

exports.getRanToken = getRanToken;
exports.getHistoryval = getHistoryval;
exports.setHistoryValFun = setHistoryValFun;

exports.bindWirteLocal = bindWirteLocal;