data.js 4.74 KB
/**
 * 数据处理模块
*/
var _data = (function(){
	
	var storage = window.localStorage;
	//写入模拟数据
	storage.setItem('lineControlItems',JSON.stringify( 
			[
			 {id: 10232, name: '604路', start: '三林世博家园', end: '西营路德州路'},
			 {id: 10566, name: '778路', start: '莱阳路五莲路', end: '张江地铁站'} ,
			 {id: 10904, name: '新川专线', start: '上海火车站(北广场)', end: '华戴路川环南路'},
			 {id: 10069, name: '85路', start: '陆家嘴地铁站', end: '长岛路东陆路'},
			 {id: 10474, name: '987路', start: '源华路双桥路', end: '世纪大道地铁站'},
			 {id: 10507, name: '636路', start: '龚丰路溪平路', end: '张江地铁站'},
			 {id: 10702, name: '浦东23路', start: '南汇汽车站', end: '五七场部'},
			 {id: 10220, name: '573路', start: '宁桥路申江路', end: '东方路栖霞路'}
			]));
	
	
	var dataObject = {
		getLines: function(){
			return JSON.parse(storage.getItem('lineControlItems'));
		},
		getRealVehic: function(lineArray, cb){
			var tabList = [
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'}
			];
			var d = {
					'10232_0': tabList,
					'10232_1': tabList,
					'10566_0': tabList,
					'10566_1': tabList,
					'10904_0': tabList,
					'10904_1': tabList,
					'10069_0': tabList,
					'10069_1': tabList,
					'10474_0': tabList,
					'10474_1': tabList,
					'10507_0': tabList,
					'10507_1': tabList,
					'10702_0': tabList,
					'10702_1': tabList,
					'10220_0': tabList,
					'10220_1': tabList
			};
			cb && cb(d);
		}
		//查询站点路由
		,queryStationRoute : function(lineId,container,  cb){
			$get('/stationroute/all', {'line.lineCode_eq': lineId}, function(routes){
				var svgData = analyData(routes);
				
				cb && cb(svgData, container);
			});
		}
	};
	
 	//加载模板文件
 	getTemp('temps/home_tp.html');
 	getTemp('temps/home_table_tp.html');
	
	function getTemp(url){
		$.get(url, function(template){
			$('#temps').append(template);
		});
	}
	
	var upSort = function(a, b){
		return a.outStationNmber - b.outStationNmber;
	}
	
	var downSort = function(a, b){
		return b.outStationNmber - a.outStationNmber;
	}
	
	var station_indexof = function(array, station , start){
		var res = -1
		
		for(var i = start, obj; obj = array[i++];){
			
			if(obj.station.stationName == station.stationName){
				res = i;
				break;
			}
		}
		
		return res;
	}
	
	/**
	 * 解析数据成svg想要的格式
	 */
	function analyData(routes){
		//按上下行拆分
		var up=[],down=[];
		for(var i = 0, route; route = routes[i++];){
			if(route.directions==0)
				up.push(route);
			else if(route.directions==1)
				down.push(route);
		}
		//排序
		up.sort(upSort);
		down.sort(downSort);
		
		//合并
		var data = [];
		for(var j = 0; j < up.length; j ++){
			var upS = up[j].station
				, downS = down[j].station
				,op = {name: [upS.stationName], id: [upS.stationCod, downS.stationCod], type: 2};
			
			//编码相同
			if(upS.stationName != downS.stationName){
				var dIndex = station_indexof(down, upS, j);
				if(dIndex == -1){
					op.type = 0;
					op.id = [upS.stationCod, -1];
					//占位
					down.splice(j, 0, {});
				}else{
					for(var t = j; t < dIndex - 1; t++){
						var temp = down[t].station;
						data.push({name: [temp.stationName], type:1, id: [temp.stationCod]});
					}
					//delete
					down.splice(j, dIndex - 1 - j);
					j --;
					continue;
				}
			}
			data.push(op);
		}
		
		//将上下行挨着的独立站点合并
		var len = data.length - 1, first, sec;
		for(var s = 0; s < len; s ++){
			first = data[s];
			sec = data[s + 1];
			
			if(first.type == 0
					&& sec.type == 1){
				data.splice(s, 2, {name: [first['name'][0],sec['name'][0]], type:3, id: [first['id'][0],sec['id'][0]]});
				len --;
			}
			else if(first.type == 1 && sec.type == 0){
				data.splice(s, 2, {name: [first['name'][0],sec['name'][0]], type:3, id: [first['id'][0],sec['id'][0]]});
				len --;
			}
		}
		return data;
	}
	
	//queryStationRoute();
	return dataObject;
})();