<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>记忆库管理 - <!--#include project_name--></title>
<link type="text/css" href="/css/base.css" rel="stylesheet" />	
<link type="text/css" href="/css/tmlist.css" rel="stylesheet" />	
<link type="text/css" href="/css/data_table.css" rel="stylesheet" />	
<link type="text/css" href="/css/cleditor.css" rel="stylesheet" />	
<script src="/jquery/jquery.min.js" type="text/javascript"></script>
<script src="/jquery/jquery-ui.min.js" type="text/javascript"></script>
<script src="/jquery/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="/jquery/jquery.cleditor.min.js" type="text/javascript"></script>
<script src="/jquery/common.min.js" type="text/javascript"></script>

<script type="text/javascript">
	// plugins
	jQuery.fn.dataTableExt.oApi.fnFilterOnReturn = function(oSettings){
    	var _that = this;
	    this.each(function (i) {
    	    $.fn.dataTableExt.iApiIndex = i;
        	var $this = this;
	        var anControl = $('input', _that.fnSettings().aanFeatures.f);
    	    anControl.unbind('keyup').bind('keypress', function(e){
        	    if (e.which == 13) {
            	    $.fn.dataTableExt.iApiIndex = i;
	                _that.fnFilter(anControl.val());
    	        }
	        });
    	    return this;
	    });
    	return this;
	}
	function send_mail(receiver, message_title, content)
	{
		$("#edit_dialog").dialog({
			modal: true,
			resizable:false,
			width: 700,
			height: 450,
			title: "发送信息",
			buttons:{
				"确定": function(){
					var receiver = $.trim($("#receiver").val());
					if (receiver.length > 0){
						var new_title = $.trim($("#title").val());
						if (new_title.length > 0){
							var content = $.trim($("#edit_dialog #text_editor").val());
							if (content.length > 0){
								$.ajax({
									type: "POST",
									url: "/send_mail",
									async: false,
									data: {user_name:receiver, title:new_title, content: content},
									dataType: "text",
									success: function(data, status){
										// check result
										if (data.substr(0,1) == ">"){
											// refresh
											window.location.reload();
						  				  }else{
											alert(data);
											$("#input").focus();
							  			  }
					  				},
									error: function(){
										alert("无法发送邮件。");
					  				}
					  			});
							  }else{
								alert("内容未输入。");
								$("#edit_dialog #text_editor").focus();
							  }
						  }else{
							alert("标题未输入。");
							$("#title").focus();
						  }
					  }else{
						alert("收件人未输入。");
						$("#receiver").focus();
					  }
				  }
			},
			open: function(){
				$(this).html('<div class="ui-widget"><table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin:0 0 5px 0;"><tr><td width="50px">收件人:</td><td width="21%"><input id="receiver" type="text" style="width:90%"/></td><td width="60px">邮件标题:</td><td width="*"><input id="title" type="text" style="width:408px"/></td></tr></table></div><textarea id="text_editor" name="text_editor"></textarea>');
				var editor = $("#edit_dialog #text_editor").cleditor({
					width:		660,
   					height:     320,
	        	  	controls:   "bold italic underline strikethrough subscript superscript | font size style | color highlight removeformat | bullets numbering | alignleft center alignright justify | rule icon image link unlink | source",
	          		colors:     "FFF FCC FC9 FF9 FFC 9F9 9FF CFF CCF FCF CCC F66 F96 FF6 FF3 6F9 3FF 6FF 99F F9F BBB F00 F90 FC6 FF0 3F3 6CC 3CF 66C C6C 999 C00 F60 FC3 FC0 3C0 0CC 36F 63F C3C 666 900 C60 C93 990 090 399 33F 60C 939 333 600 930 963 660 060 366 009 339 636 000 300 630 633 330 030 033 006 309 303",
       				fonts:      "宋体,楷体,黑体,幼圆,隶书,Arial,Arial Black,Comic Sans MS,Courier New,Narrow,Garamond,Georgia,Impact,Sans Serif,Serif,Tahoma,Trebuchet MS,Verdana",
   	   				sizes:      "1,2,3,4,5,6,7",
   					styles:     [["Paragraph", "<p>"], ["Header 1", "<h1>"], ["Header 2", "<h2>"], ["Header 3", "<h3>"],  ["Header 4","<h4>"],  ["Header 5","<h5>"], ["Header 6","<h6>"]],
   					useCSS:   	false, // use CSS to style HTML when possible (not supported in ie)
   					bodyStyle:  "margin:4px; font:10pt Arial,Verdana; cursor:text"
		      	});
				if (receiver.length > 0){
					$("#receiver").val(receiver);
				  }
				if (message_title.length > 0){
					$("#title").val(message_title);
				  }
				if (content.length > 0){
			      	$("#edit_dialog #text_editor").val(content).blur();
				  }
			  	$("#title").focus();
			}
		});
	}
	
	$(function(){
		// note: 1em = 16px, 100%, 12pt
		var work_height = $(window).height()-32-28-8;
		$("#navigator").height(work_height + "px");
		$("#content").height(work_height + "px");

		// disable cache
		$.ajaxSetup({cache: false});

		// function to resize content areas when window is resized
		$(window).resize(function(){
			var work_height = $(window).height()-32-28-8;
			$("#navigator").height(work_height + "px");
			$("#content").height(work_height + "px");
	    });

		// load navigator
		$("#navigator").load("/navigator?id=2");
		$("#user").load("/user_panel", function(){$("#login").click(function(){login();});$("#logout").click(function(){logout();});});
		
		$("#collapse").click(function(){
			var $nav = $("#navigator");
			var $link = $("#collapse a");
			if ($nav.is(":visible")){
				$nav.hide();
				$link.html("&gt;&gt;&gt;").attr("title","展开导航窗口");
			  }else{
				$nav.show();
				$link.html("&lt;&lt;&lt;").attr("title","折叠导航窗口");
			  }
			return false;
		});

		// intialize variables
		<!--#include init_variables-->

		// data table
		var oTable = $('#tmlist').dataTable({
        	"bSort": false,
        	"bServerSide": true,
        	"bProcessing": false,
        	"sPaginationType": "full_numbers",
        	"sAjaxSource": "/project/" + current_group + "/get_tm_records",
        	"fnServerParams": function(aoData){
      			aoData.push({"name": "sl", "value": source_language});
      			aoData.push({"name": "tl", "value": target_language});
      			aoData.push({"name": "type", "value": tm_type});
      			if (userid) aoData.push({"name": "user", "value": userid});
    		},
    		"fnRowCallback": function(nRow){
				$(nRow).click(function (){
		        	if ($(this).hasClass('row_selected')){
	    		        $(this).removeClass('row_selected');
		    	      }else{
        		    	oTable.$('tr.row_selected').removeClass('row_selected');
		            	$(this).addClass('row_selected');
        			  }
				});
			},
			"aoColumnDefs":[
    			{
        			"fnRender": function (oObj){
            			return '<input type=\"checkbox\" value="'+ oObj.aData[0] +'">';
		        		},
        			"aTargets":[0]
    			},
    			{
        			"fnRender": function(oObj, val){
            			return "<a href='/project/" + current_group + "/browse_tm?type=" + tm_type + "&src=" + source_language + "&tar=" + target_language + "&user=" + val + "'>" + val + "</a>";
		        		},
        			"aTargets":[2]
    			},
    			{
        			"fnRender": function(oObj, val){
            			return htmlEncode(val);
		        		},
        			"aTargets":[3, 4, 5]
    			}
    		],
    		"oLanguage":{
				"sProcessing": "正在从服务器读取数据...",
				"sLengthMenu": "每页 _MENU_ 记录",
				"sZeroRecords": "没有匹配的记录",
				"sInfo": "记录_START_到_END_  共_TOTAL_记录",
				"sInfoFiltered": "(从_MAX_记录中筛选)",
				"sInfoEmpty": "共0记录",
        		"sSearch": "搜索:",
				"oPaginate": {
					"sFirst":    "首页",
					"sPrevious": "上一页",
					"sNext":     "下一页",
					"sLast":     "尾页"
				}
        	}
	   	});
		oTable.fnFilterOnReturn();
		$.fn.dataTableExt.oPagination.iFullNumbersShowPages = 3;

		// add buttons
		var buttons = "";
		if (tm_type == 0){
			buttons = '<button id="new_record">新建记录</button><button id="edit_record">修改记录</button>';
			if (role == 2 || role == 3){
				buttons += '<button id="send_message">发送信息</button><button id="confirm_record">核准记录</button>';
			  }
			buttons += '<button id="select_all">选择全部</button><button id="select_none">清除选择</button><button id="remove_record">删除记录</button>';
		  }else{
			if (role == 2 || role == 3){
				buttons = '<button id="new_record">新建记录</button><button id="edit_record">修改记录</button><button id="send_message">发送信息</button><button id="select_all">选择全部</button><button id="select_none">清除选择</button><button id="remove_record">删除记录</button>';
			  }
		  }
		if (buttons.length > 0){
			$("#container .button_panel").html(buttons);
		  }
		
		// buttons
		$("button", ".button_panel").button();
		$("#select_all").click(function(){
			$("table td :checkbox").attr("checked", true);
			return false;
		});
		$("#select_none").click(function(){
			$("table td :checkbox").attr("checked", false);
			return false;
		});
		$("#new_record").click(function(){
			// show dialog
			$("#dialog").dialog({
				modal: true,
				resizable:false,
				title: "新建记录",
				width: 500,
				height: 275,
				buttons:{
					"确定": function(){
						// check content
						var source = $.trim($("#dialog #source").val());
						if (source.length > 0){
							var target = $.trim($("#dialog #target").val());
							if (target.length > 0){
								var note = $.trim($("#dialog #note").val());
								$.ajax({
									type: "GET",
									url: "/project/" + current_group + "/store_tm",
			   						data: {src:source, tar:target, note:note, sl:source_language, tl:target_language, type:tm_type},
									dataType: "text",
									success: function(data){
										// check result
										if (data.substr(0,1) == ">"){
											// refresh
											oTable.fnDraw();
											// close window
											$("#dialog").dialog("destroy");
										  }else{
											alert(data);
										  }
									  },
					   				error: function(){
					   					alert("无法新建记录。");
							   		  }
		  						});
							  }else{
							  	alert("译文未输入。")
								$("#dialog #target").focus();
					  		  }
						  }else{
						  	alert("原文未输入。")
							$("#dialog #source").focus();
						  }
					}
				},
				open: function(){
					$(this).empty();
					$(this).html('<form><fieldset><div><label for="source">原文：</label><textarea id="source"/></div><div><label for="target">译文：</label><textarea id="target"/></div><div><label for="note">备注：</label><textarea id="note"/></div></fieldset></form>');
					$("#dialog #source").focus();
				}
			});
			return false;
		});
		$("#edit_record").click(function(){
        	var selected_id = $('table tr.row_selected:first td:eq(1)');
        	if (selected_id.length > 0){
        		var selected_row = $('table tr.row_selected');
        		var row_index = $('tbody tr').index(selected_row);
				var id = selected_id.text();
        		var current_source = $('table tr.row_selected:first td:eq(3)').text();
        		var current_target = $('table tr.row_selected:first td:eq(4)').text();
        		var current_note = $('table tr.row_selected:first td:eq(5)').text();
				// show dialog
				$("#dialog").dialog({
					modal: true,
					resizable:false,
					title: "修改记录",
					width: 500,
					height: 275,
					buttons:{
						"确定": function(){
							// check content
							var source = $.trim($("#dialog #source").val());
							if (source.length > 0){
								var target = $.trim($("#dialog #target").val());
								if (target.length > 0){
									var note = $.trim($("#dialog #note").val());
									if (source != current_source || target != current_target || note != current_note){
										// update record
										$.ajax({
											type: "GET",
											url: "/project/" + current_group + "/update_tm",
			   								data: {src:source, tar:target, note:note, sl:source_language, tl:target_language, id:id, type:tm_type},
											dataType: "text",
											success: function(data){
												// check result
												if (data.substr(0,1) == ">"){
													// refresh
													if (row_index >= 0){
	  													oTable.fnUpdate(source, row_index, 3, false, false);
	  													oTable.fnUpdate(target, row_index, 4, false, false);
				  										oTable.fnUpdate(note,   row_index, 5, false, false);
													  }
													// close window
													$("#dialog").dialog("destroy");
												  }else{
													alert(data);
												  }
											  },
							   				error: function(){
							   					alert("无法更新记录。");
									   		  }
				  						});
									  }else{
										// close window
										$("#dialog").dialog("destroy");
									  }
								  }else{
								  	alert("译文未输入。")
									$("#dialog #target").focus();
						  		  }
							  }else{
							  	alert("原文未输入。")
								$("#dialog #source").focus();
							  }
						}
					},
					open: function(){
						$(this).empty();
						$(this).html('<form><fieldset><div><label for="source">原文：</label><textarea id="source"/></div><div><label for="target">译文：</label><textarea id="target"/></div><div><label for="note">备注：</label><textarea id="note"/></div></fieldset></form>');
						if (current_source.length > 0){
							$("#dialog #source").val(current_source);
						  }
						if (current_target.length > 0){
							$("#dialog #target").val(current_target);
						  }
						if (current_note.length > 0){
							$("#dialog #note").val(current_note);
						  }
						$("#dialog #source").focus();
					}
				});
        	  }else{
        	  	alert("请先选择记录。");
        	  }
			return false;
		});
		$("#send_message").click(function(){
        	var selected_id = $('table tr.row_selected:first td:eq(1)');
        	if (selected_id.length > 0){
				var content = "原文: ";
				content += htmlEncode($('table tr.row_selected:first td:eq(3)').text());
				content += "<br/>\n译文: ";
				content += htmlEncode($('table tr.row_selected:first td:eq(4)').text());
				content += "<br/>";
				// send mail
				send_mail($('table tr.row_selected:first td:eq(2)').text(), "", content);
        	  }else{
        	  	alert("请先选择记录。");
        	  }
			return false;
		});
		$("#confirm_record").click(function(){
			var record_list = '';
			$("table td :checkbox").each(function(){
				if ($(this).attr("checked")){
					if (record_list.length > 0) record_list += ',';
					record_list += $(this).attr("value");
				  }
			});
			if (record_list.length > 0){
				if (confirm("核准所选记录?")){
					$.ajax({
						type: "GET",
						url: "/project/" + current_group + "/confirm_tm",
   						data: {sl:source_language, tl:target_language, record:record_list},
						dataType: "text",
						success: function(data){
							// check result
							if (data.substr(0,1) == ">"){
								// refresh
								oTable.fnDraw();
							  }else{
								alert(data);
							  }
						  },
		   				error: function(){
		   					alert("无法核准记录。");
				   		  }
					});
				  }
			  }else{
        	  	alert("请先选择记录。");
			  }
			return false;
		});
		$("#remove_record").click(function(){
			var record_list = '';
			$("table td :checkbox").each(function(){
				if ($(this).attr("checked")){
					if (record_list.length > 0) record_list += ',';
					record_list += $(this).attr("value");
				  }
			});
			if (record_list.length > 0){
				if (confirm("删除所选记录?")){
					$.ajax({
						type: "GET",
						url: "/project/" + current_group + "/remove_tm",
   						data: {sl:source_language, tl:target_language, record:record_list},
						dataType: "text",
						success: function(data){
							// check result
							if (data.substr(0,1) == ">"){
								// refresh
								oTable.fnDraw();
							  }else{
								alert(data);
							  }
						  },
		   				error: function(){
		   					alert("无法删除记录。");
				   		  }
					});
				  }
			  }else{
        	  	alert("请先选择记录。");
			  }
			return false;
		});
	});
</script>
<style type="text/css">
	html,body{margin:0; padding:0;}
	html{overflow:visible;}
	body{font-size:11pt; min-width:500px;overflow-y:hidden !important; overflow-x:auto !important;}
	
	#banner #logo{font:200% 黑体; float:left; padding:3px 5px 0}
	#banner #user{font:80% 宋体; float:right; padding:5px 8px}

	#user a{text-decoration:none;}
	#user span{margin:2px 0 0 5px;}

	#menubar{background-color:#555; clear:both; height:28px}
	#menubar #collapse{padding:3px 0 0 0; float:left; color:#fff;}
	#menubar a:link, #menubar a:visited{color:white; text-decoration:none; vertical-align:middle;}
	#menubar a:hover{color:red;}
	#menubar #collapse a{font-weight:bold;}
	#menubar ul{padding:3px 10px 3px 5px; margin:0; text-align: right}
	#menubar li{padding:0 5px; font:90% "黑体"; color:#fff; display:inline}
	
	#navigator{font:宋体; width:160px; float:left; margin:5px; padding:5px;}
	#navigator a{display:block; width:125px; word-break:keep-all; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; margin:2.5px;}
	#navigator a:link, #navigator a:visited{font:80% "宋体";padding:0;}
	
	#content{padding:0 5px; margin:0;overflow:auto}
	
	.button_panel{font:80% 宋体; margin: 5px 2px; float: left; clear: both;}

	.ui-dialog-title{font-size:80%;padding:0;}
	.ui-dialog fieldset { padding:0; border:0; margin-top:10px; }
	.ui-dialog form div { margin:5px 0;}
	.ui-dialog .ui-dialog-buttonpane{margin:0.2em 0 0; padding:0.2em 1em 0.1em 0.4em;}
	.ui-dialog .ui-dialog-buttonpane button{margin:0.2em 0.4em 0.1em 0; padding:0;}
	.ui-widget .ui-widget{ font-size: 0.7em;}

	#dialog{font-size:70%;margin:0;padding:2px 10px;}
	#dialog form{margin:0;padding:0}
	#dialog label{ float:left; width:50px; text-align:right;}
	#dialog input, #dialog select, #dialog textarea{ padding:1px 1px; border:1px solid #ccc; width:400px;}
	#dialog textarea{resize:none; height:5em;}
	#dialog textarea#note{height:3em;}
</style>
</head>
<body id="home">
<div id="banner">
	<div id="logo">雪人CAT网络协同翻译平台</div>
	<div id="user">
	</div>
</div>
<div id="menubar">
	<ul>
		<li id="collapse"><a href="#" title="折叠导航窗口">&lt;&lt;&lt;</a></li>
		<li><a href="/index.htm" id="homepage">首页</a></li>
		<li><a href="/project.htm" id="project">项目</a></li>
		<li><a href="/articles/all/list" id="document">文档</a></li>
		<li><a href="/forum.htm" id="discuss">论坛</a></li>
		<li><a href="/message.htm" id="message">通讯</a></li>
		<li><a href="/help.htm" id="help">帮助</a></li>
	</ul>
</div>
<div class="ui-widget ui-state-default ui-corner-all" id="navigator"></div>
<div id="content">
	<div id="dt_tmlist">
		<div id="container">
			<h1 id='project_title'>记忆库项目</h1>
			<div id="dynamic">
				<table cellpadding="0" cellspacing="0" border="0" class="display" id="tmlist" width='100%'>
					<thead>
						<tr>
							<th width="12px">&nbsp;</th>
							<th width="50px">ID</th>
							<th width="8%">译员</th>
							<th width="38%">原文</th>
							<th width="38%">译文</th>
							<th width="10%">备注</th>
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>
				<div id="spacer"></div>
			</div>
			<div class="button_panel"/>
		</div>
	</div>
</div>
<div id="dialog"></div>
<div id="edit_dialog"></div>
</body>
</html>
