alone.js 7.2 KB
/**
 * 单线路调度
 */
var _alone = (function(){
	var upCode = 0
		,downCode = 1;
	
	var aloneObject = {
		init: function(cb){
			_data.queryRealSchedule(lineCodes, function(schList){
				
				var schTempList;
				for(var lineCode in lineMap){
					schTempList = schList[lineCode];
					tab = $('#tab_line_' + lineCode);
					tab.append(template('alone_main_temp', lineMap[lineCode]));
					//有排班数据
					if(schTempList){
						var rs = splitDir(schTempList);
						//填充表格数据
						calculateLineNo(
								//上行
								tab.find('table[data-type=up] tbody').html(template('alone_plan_table_temp', {list: rs.up}))[0]
						);
						calculateLineNo(
								//下行
								tab.find('table[data-type=down] tbody').html(template('alone_plan_table_temp', {list: rs.down}))[0]
						);
						//滚动条	
						tab.find('._body').slimscroll({
								height: 'calc(100% - 80px)',
								alwaysVisible: true
						});
					}
					//svg
					_data.queryStationRoute(lineCode , 'lineSvg' + lineCode , drawSvg.initAloneSvg);
				}
				//初始化备注弹层
				initRemarksPop();
				//初始化信使
				initMessenger(lineCodes);
				
				//消息框滚动条
				$('.console-log .log-item-list').slimscroll({
					height: '100%'
				});
				
				cb && cb();
			});
		},
		//刷新单个班次
		refreshSchedule: function(schedule, isBatch){
			//更新 _data
			_data.updateSchedule(schedule);
			var tab = '#tab_line_' + schedule.xlBm;
			//xlBm
			var $tr = $('tr[data-id='+schedule.id+']', tab)
				,newTr = template('alone_plan_table_temp', {list: [schedule]});
			
			$tr.replaceWith(newTr);
			initRemarksPop();
			
			var type = schedule.xlDir==0?'up':'down';
			calculateLineNo($('.pb-table[data-type='+type+']', tab)[0]);
			
			if(!isBatch)
				goToSch(schedule);
		},
		//刷新多个班次,刷新完数据再统一计算行号
		refreshScheduleArray: function(array){
			if(!array || array.length == 0)
				return;
			$.each(array, function(){
				aloneObject.refreshSchedule(this, 1);
			});
			var table = $('tr[data-id='+array[0].id+']').parents('table')[0];
			calculateLineNo(table);
			initRemarksPop();
		},
		//将班次所在的表格做全量更新
		updateTableBySch: function(schedule){
			//将该线路走向班次 全部从服务器同步一次
			var xlDir = schedule.xlDir, xlBm = schedule.xlBm;
			$.get('/realSchedule/findByLineAndUpDown', {line: xlBm, upDown: xlDir}
				,function(list){
					$.each(list, function(){
						_data.pushSchedule(this);
					});
					
					/*var upDown = xlDir==0?'up':'down';
					var tab = $('#tab_line_' + xlBm);
					//重新渲染表格
					var table = tab.find('.pb-table[data-type='+upDown+']');*/
					var table = getTableBySch(sch);
					var schArray = _data.findSchByLine(xlBm, xlDir);
					calculateLineNo(
							table.find('tbody').html(template('alone_plan_table_temp', {list: schArray}))[0]
					);
					
					//定位到新添加的班次
					goToSch(schedule);
					/*var currTr = table.find('tr[data-id='+schedule.id+']')
						,top = parseInt(currTr.find('td[name=lineNo]').text()) * 37 - half;
					
					top = top>0?top:0;
					currTr.addClass('anim-delay animated flash');
					currTr.parents('._body').slimScroll({ scrollTo: top + 'px' });*/
			});
		},
		update2Table: function(xlBm, schedule){
			//上行
			$.get('/realSchedule/lineCode/' + xlBm, function(array){
				//加入_data缓存
				$.each(array, function(){
					_data.pushSchedule(this);
				});
				
				var rs = splitDir(array)
					,tab = $('#tab_line_' + xlBm)
					/*,half = tab.find('._body').height() / 2*/;
				//填充表格数据
				calculateLineNo(
						//上行
						tab.find('table[data-type=up] tbody').html(template('alone_plan_table_temp', {list: rs.up}))[0]
				);
				calculateLineNo(
						//下行
						tab.find('table[data-type=down] tbody').html(template('alone_plan_table_temp', {list: rs.down}))[0]
				);
				
				//定位到指定的班次
				goToSch(schedule);
				/*var currTr = tab.find('tr[data-id='+schedule.id+']')
					,top = parseInt(currTr.find('td[name=lineNo]').text()) * 37 - half;
			
				top = top>0?top:0;
				currTr.addClass('anim-delay animated flash');
				currTr.parents('._body').slimScroll({ scrollTo: top + 'px' });*/
			});
		},
		//重新计算行号
		calculateLineNo: calculateLineNo
	}
	
	//获取班次所在table
	function getTableBySch(sch){
		var upDown = sch.xlDir==0?'up':'down';
		var tab = $('#tab_line_' + sch.xlBm);
		return tab.find('.pb-table[data-type='+upDown+']');
	}
	
	//定位到班次所在的行
	function goToSch(sch){
		var table = getTableBySch(sch)
			,half = table.parents('._body').height() / 2
			,currTr = table.find('tr[data-id='+sch.id+']')
			,top = parseInt(currTr.find('td[name=lineNo]').text()) * 37 - half;
	
		top = top>0?top:0;
		currTr.addClass('anim-delay animated flash');
		currTr.parents('._body').slimScroll({ scrollTo: top + 'px' });
	} 
	
	//计算行号
	function calculateLineNo(table){
		var rows = table.rows;
		$.each(rows,function(i, r){
			if($(r).hasClass('child-task-list'))
				return true;
			
			var cells = r.cells;
			$(cells[0]).text(i + 1);
			
			$(r).data('no', (i + 1));
		});
	}
	
	function splitDir(list){
		/*list.sort(function(a, b){
			return a.fcno - b.fcno;
		});*/
		var rs = {up: [], down: []};
		$.each(list, function(){
			if(this.xlDir == upCode)
				rs.up.push(this);
			else
				rs.down.push(this);
		});
		
		return rs;
	}
	
	//切换托管状态
	function changeTGStatus(){
		var status = $(this).data('status');
		if(status == 0)
			status = 1;
		else
			status = 0;
		
		$(this).data('status', status);
		
		var lineCode = $(this).data('linecode');
		
		$.post('/realSchedule/trustStatus/change', {lineCode: lineCode, status: status}
			,function(rs){
				if(rs == 200){
					layer.msg('切换成功!');
					changeTgHtml(lineCode, status);
				}
		});
	}
	
	$('.portlet-fullscreen').on('click', '.tg_tools', changeTGStatus);
	var tgHtml = ['托管状态,计算机自动处理&nbsp;<i class="fa fa-question-circle tgQuestion" style="cursor: pointer;"></i>', '非托管状态,手动处理!'];
	var tgPopoverOps = {content: template('tg_question_info_temp', {}),html: true,placement: 'left',trigger: 'hover',container: '.portlet-fullscreen'};
	function initMessenger(lineCodes){
		//查询线路托管状态
		$.get('/realSchedule/trustStatus', {lineCodes: lineCodes},function(rs){
			for(var line in rs)
				changeTgHtml(line, rs[line]);
		});
	}
	
	function changeTgHtml(line, status){
		var tgTools = $('#tgTools_' + line);
		
		tgTools.html(tgHtml[status]).data('status', status);
		var array = $('#top-tabs-wrap a[data-id='+line+'] span zz');
		
		if(status == 0){
			//托管Question
			$('.tgQuestion', tgTools).popover(tgPopoverOps);
			tgTools.removeClass('hand');
			
			$(array[2]).text('托管');
		}
		else{
			tgTools.addClass('hand');
			$(array[2]).text('');
		}
		
		//tab 头改变
		
	}
	
	function initRemarksPop(){
		//备注POPOVER
		$('.remarks-popover', '.pb-table').popover({trigger: 'hover',container: '.portlet-fullscreen',placement:'bottom'});
	}
	return aloneObject;
})();