common.js 10.6 KB
var gb_common = (function () {

    var reqCode80 = {
        0xA1: '请求恢复运营',
        0xA2: '申请调档',
        0xA3: '出场请求',
        0xA5: '进场请求',
        0xA7: '加油请求',
        0x50: '车辆故障',
        0x70: '路阻报告',
        0x60: '事故报告',
        0x11: '扣证纠纷',
        0x12: '报警'
    };

    var groupBy = function (list, field) {
        var rs = {},
            key;
        $.each(list, function () {
            key = this[field];
            if (!rs[key])
                rs[key] = [];

            rs[key].push(this);
        });

        return rs;
    };

    var compileTempByDom = function (dom, opts) {
        var tps = {},
            id;
        $('script[type="text/html"]', dom).each(function () {
            id = $(this).attr('id');
            if (id)
                tps[id] = template.compile($(this).html(), opts);
        });
        return tps;
    };

    var $get = function (url, data, successFun) {
        $.ajax({
            url: url,
            data: data,
            complete: function (xhr, ts) {
                ajaxComplete(xhr, ts, successFun);
            }
        });
    };

    var $post = function (url, data, successFun) {
        $.ajax({
            url: url,
            method: 'POST',
            data: data,
            complete: function (xhr, ts) {
                ajaxComplete(xhr, ts, successFun);
            }
        });
    };

    var $post_arr = function (url, data, successFun) {
        $.ajax({
            url: url,
            method: 'POST',
            traditional: true,
            data: data,
            complete: function (xhr, ts) {
                ajaxComplete(xhr, ts, successFun);
            }
        });
    };

    var $del = function (url, successFun) {
        $post(url, {'_method': 'delete'}, function (rs) {
            successFun && successFun(rs);
        });
    };

    var errorHead = '<span style="color:red;">服务器出现异常:</span>';

    function successHandle(json, handle) {
        var status = json.status;
        if (status == 407) {
            //alert('被注销的登录');
            location.href = '/';
            return;
        }

        if (!status) {
            handle && handle(json);
            return;
        }

        if (status == 'ERROR')
            UIkit.modal.alert(errorHead + (json.msg ? json.msg : '未知异常'), {labels: {Ok: '确定'}});
        else
            handle && handle(json);
    }

    function ajaxComplete(xhr, ts, succ) {
        if (ts == 'success') {
            successHandle(JSON.parse(xhr.responseText), succ);
        } else if (ts == 'error') {
            UIkit.modal.alert(errorHead + xhr.responseText, {labels: {Ok: '确定'}});
        }
    }

    var get_vals = function (json) {
        var array = [];
        for (var key in json) {
            array.push(json[key]);
        }

        return array;
    };

    var get_keys = function (json) {
        var array = [];
        for (var key in json) {
            array.push(key);
        }
        return array;
    };

    var get_device_tree_data = function (allGps, idBefore) {
        if (!idBefore)
            idBefore = '';
        var treeData = [];
        //var data = groupBy(get_vals(gb_data_gps.allGps), 'lineId');
        var data = groupBy(get_vals(allGps), 'lineId');
        var name;
        for (var code in data) {
            try {
                name = gb_data_basic.codeToLine[code].name;
            } catch (e) {
                continue;
            }

            data[name] = groupBy(data[code], 'upDown');
            treeData.push({
                'text': name,
                'a_attr': {'type': 'line', 'id': idBefore + 'line_' + code},
                'children': [{
                    'text': '上行',
                    'children': grabs(data[name][0], idBefore),
                    'a_attr': {
                        'type': 'route',
                        'route': code + '_0',
                        'id': idBefore + 'route_' + code + '_0'
                    }
                }, {
                    'text': '下行',
                    'children': grabs(data[name][1], idBefore),
                    'a_attr': {
                        'type': 'route',
                        'route': code + '_1',
                        'id': idBefore + 'route_' + code + '_1'
                    }
                }]
            });
        }

        function grabs(array, idBefore) {
            if (!array)
                return;
            var rs = [];
            $.each(array, function () {
                rs.push({
                    'text': this.nbbm,
                    'a_attr': {
                        'type': 'device',
                        'device': this.deviceId,
                        'id': idBefore + 'device_' + this.nbbm
                    },
                    'data': {lineId: this.lineId, upDown: this.upDown},
                    'icon': 'uk-icon-bus'
                });
            });
            return rs;
        }

        return treeData;
    };

    var lineAutocomplete = function (element) {
        //construction data
        var data = [],
            code2Name = gb_data_basic.lineCode2NameAll(),
            name;
        for (var code in code2Name) {
            name = code2Name[code];
            data.push({
                value: name,
                code: code,
                fullChars: pinyin.getFullChars(name).toUpperCase(),
                camelChars: pinyin.getCamelChars(name)
            });
        }
        init_autocom_pinyin(element, data);
    };

    var personAutocomplete = function (element, personMaps) {
        /*var data = [],name;
         for(var jobCode in personMaps){
         name=personMaps[jobCode];
         data.push({
         value: jobCode+'/'+name,
         fullChars: pinyin.getFullChars(name).toUpperCase(),
         camelChars: pinyin.getCamelChars(name)
         });
         }*/
        //init_autocom_pinyin(element, data);
        init_autocom_pinyin(element, gb_data_basic.allPersonnel());
    };

    var carAutocomplete = function (element, list) {
        var data = [];
        $.each(list, function () {
            data.push({value: this});
        });
        init_autocomplete(element, data);
    };

    var init_autocom_pinyin = function (element, data) {
        // init autocomplete
        var autocomplete = UIkit.autocomplete(element, {
            minLength: 1,
            delay: 50,
            source: function (release) {
                var q = $('input', element).val().toUpperCase(),
                    rs = [],
                    count = 0;

                $.each(data, function () {
                    if (this.value.indexOf(q) != -1 || this.fullChars.indexOf(q) != -1 || this.camelChars.indexOf(q) != -1) {
                        rs.push(this);
                        count++;
                    }

                    if (count >= 10)
                        return false;
                });

                release && release(rs);
            }
        });
    };

    var init_autocomplete = function (element, data) {
        var autocomplete = UIkit.autocomplete(element, {
            minLength: 1,
            delay: 50,
            source: function (release) {
                var q = $('input', element).val().toUpperCase(),
                    rs = [],
                    count = 0;

                $.each(data, function () {
                    if (this.value.indexOf(q) != -1) {
                        rs.push(this);
                        count++;
                    }
                    if (count >= 10)
                        return false;
                });
                release && release(rs);
            }
        });
    };

    // function whichTransitionEvent() {
    //     var t;
    //     var el = document.createElement('fakeelement');
    //     var transitions = {
    //         'transition': 'transitionend',
    //         'OTransition': 'oTransitionEnd',
    //         'MozTransition': 'transitionend',
    //         'WebkitTransition': 'webkitTransitionEnd',
    //         'MsTransition': 'msTransitionEnd'
    //     }
    //     for (t in transitions) {
    //         if (el.style[t] !== undefined) {
    //             return transitions[t];
    //         }
    //     }
    // }

    var accAdd = function (a, b) {
        var c, d, e;
        try {
            c = a.toString().split(".")[1].length;
        } catch (f) {
            c = 0;
        }
        try {
            d = b.toString().split(".")[1].length;
        } catch (f) {
            d = 0;
        }
        return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
    };

    function mul(a, b) {
        var c = 0,
            d = a.toString(),
            e = b.toString();
        try {
            c += d.split(".")[1].length;
        } catch (f) {
        }
        try {
            c += e.split(".")[1].length;
        } catch (f) {
        }
        return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
    }

    var numSubtr = function (a, b) {
        var c, d, e;
        try {
            c = a.toString().split(".")[1].length;
        } catch (f) {
            c = 0;
        }
        try {
            d = b.toString().split(".")[1].length;
        } catch (f) {
            d = 0;
        }
        return e = Math.pow(10, Math.max(c, d)), (a * e - b * e) / e;
    };

    var trim = function (str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g") {
            result = result.replace(/\s/g, "");
        }
        return result;
    };

    return {
        reqCode80: reqCode80,
        groupBy: groupBy,
        compileTempByDom: compileTempByDom,
        $get: $get,
        $post: $post,
        $post_arr: $post_arr,
        $del: $del,
        get_vals: get_vals,
        get_keys: get_keys,
        get_device_tree_data: get_device_tree_data,
        lineAutocomplete: lineAutocomplete,
        personAutocomplete: personAutocomplete,
        carAutocomplete: carAutocomplete,
        init_autocomplete: init_autocomplete,
        init_autocom_pinyin: init_autocom_pinyin,
        accAdd: accAdd,
        numSubtr: numSubtr,
        trim: trim

        //whichTransitionEvent:whichTransitionEvent
    };
})();