common.js 8.58 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() {
        var treeData = [];
        var data = groupBy(get_vals(gb_data_gps.allGps), 'lineId');
        var name;
        for (var code in data) {
            name = gb_data_basic.codeToLine[code].name;
            data[name] = groupBy(data[code], 'upDown');
            treeData.push({
                'text': name,
                'a_attr':{'type': 'line'},
                'children': [{
                    'text': '上行',
                    'children': grabs(data[name][0]),
                    'a_attr': {
                      'type': 'route',
                      'route': code+'_0'
                    }
                }, {
                    'text': '下行',
                    'children': grabs(data[name][1]),
                    'a_attr': {
                      'type': 'route',
                      'route': code+'_1'
                    }
                }]
            });
        }

        function grabs(array) {
            if (!array)
                return;
            var rs = [];
            $.each(array, function() {
                rs.push({
                    'text': this.nbbm,
                    'a_attr': {
                        'type': 'device',
                        'device': this.deviceId
                    },
                    '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;
        // $.each(personList, function(){
        //   name=this.personnelName;
        //   data.push({
        //     value: this.jobCode+'/'+name,
        //     fullChars: pinyin.getFullChars(name).toUpperCase(),
        //     camelChars: pinyin.getCamelChars(name),
        //     brancheCompany: this.brancheCompany
        //   });
        // });
        // console.log('data', data);
        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);
    };

    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];
    //         }
    //     }
    // }

    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

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