"use strict";define(function(require,exports,module){var e=window.require("bluebird");var o=window.require("co");var a=require("../../toolkit/js/global");function l(e,o,a){this.operType=e;this.deviceInfo=o;this.cb=a;this.init=t}function t(){var e=this;d(e.operType,e.deviceInfo);r(e.operType,e.deviceInfo,e.cb)}function d(e,o){var a="";var l="";if(e=="add"){a="添加设备";l=""}else if(e=="edit"){a="编辑设备";l='<select id="group_name" class="self-select"></select>'}var t='<div class="modal fade" id="commonAddDeviceModal" tabindex="-1" role="dialog">'+'<div class="modal-dialog">'+'<div class="modal-content">'+'<div class="modal-header self-modal-header">'+'<button type="button" class="close" data-dismiss="modal">&times;</button>'+'<h4 class="modal-title">'+a+"</h4>"+"</div>"+'<div class="modal-body" style="padding: 20px;">'+'<div style="font-size: 12px; color: #525252;">连接信息 (必填)</div>'+'<input class="form-control self-form-control" id="loca_dev_name" type="text" placeholder="设备名称">'+'<input class="form-control self-form-control" id="loca_ip_addr" type="text" placeholder="IP 地址">'+'<input class="form-control self-form-control" value="23" id="loca_port" type="text" placeholder="端口号">'+'<select id="protocol_type" class="self-select">'+'<option value="telnet" selected="selected">Telnet</option>'+'<option value="SSH">SSH</option>'+"</select>"+'<select id="code_option" class="self-select">'+"<option>UTF8,通用编码</option>"+"<option>GB2312,中文编码</option>"+"</select>"+l+'<div style="margin-top: 20px; font-size: 12px; color: #525252;">账户信息</div>'+'<input class="form-control self-form-control" id="loca_username" type="text" placeholder="用户名 (SSH连接必填)">'+'<input class="form-control self-form-control" id="loca_pwd" type="password" placeholder="密码 (SSH连接必填)">'+'<input class="form-control self-form-control" id="loca_enable_pwd" type="password" placeholder="特权密码 (若无可不填)">'+"</div>"+'<div class="modal-footer self-modal-footer">'+'<div class="btn-confirm">保存</div>'+'<div class="btn-cancel" data-dismiss="modal">取消</div>'+"</div>"+"</div>"+"</div>"+"</div>";$("body #commonAddDeviceModal").remove();$("body").append(t);$("#commonAddDeviceModal").modal("show");if(e=="add"){$("#loca_dev_name").attr("disabled",false)}else if(e=="edit"){$("#loca_dev_name").attr("disabled",true);c(o)}}function r(e,o,l){$("#protocol_type").change(function(){var e=$(this).val();if(e=="telnet"){$("#loca_port").val("23")}else if(e=="SSH"){$("#loca_port").val("22")}});$("#commonAddDeviceModal .btn-confirm").click(function(){if(!i()){return}$("#commonAddDeviceModal").modal("hide");var t=n();if(e=="add"){t.groupId=o.groupId;a.devicesIns.addNewDeviceLink(t,function(e,o){l(e,o)})}else{var d=$("#group_name").val();d=d.split(":")[0];t.groupId=d;if(o.deviceName!=t.deviceName){}else{var r=function c(e,o){l(e,o)};a.devicesIns.updateDeviceLink(t,r)}}})}function i(){var e=$("#loca_dev_name").val();if(e==""){alert(undefined,"请填写设备名称。");return false}else{if(e.length>32){alert(undefined,"设备名称长度不能超过32位！");return false}var o=new RegExp("[`~!@#$^&*()=|{}':;',\"\"\\[\\]<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");var a=o.test(e);if(a){alert(undefined,"设备名称只能包含：字母、数字、中文、点号、下划线或-");return false}}var l=$("#loca_ip_addr").val();if(l==""){alert(undefined,"IP地址不能为空。");return false}if(!window.isIpOk(l)){alert(undefined,"您输入IP格式不正确。");return false}var t=$("#protocol_type").val();if(t=="SSH"){if($("#loca_username").val()==""){alert(undefined,"SSH连接时用户名不能为空！");return false}if($("#loca_pwd").val()==""){alert(undefined,"SSH连接时密码不能为空！");return false}}return true}function n(){var e=$("#loca_dev_name").val();var o=$("#protocol_type").val();var a=$("#code_option").val();var l=$("#loca_username").val();var t=$("#loca_pwd").val();var d=$("#loca_ip_addr").val();var r=$("#loca_port").val();var i=$("#loca_enable_pwd").val();var n={deviceName:e,protocal:o,encoding:a.split(",")[0].toLowerCase(),username:l,password:t,host:d,port:r,containShell:true,enablePassword:i};return n}function c(e){$("#loca_dev_name").val(e.deviceName);$("#protocol_type").val(e.protocal);if(e.encoding=="utf8"){$("#code_option").val(e.encoding.toUpperCase()+",通用编码")}else if(e.encoding=="gb2312"){$("#code_option").val(e.encoding.toUpperCase()+",中文编码")}$("#loca_username").val(e.username);$("#loca_pwd").val(e.password);$("#loca_ip_addr").val(e.host);$("#loca_port").val(e.port);$("#loca_enable_pwd").val(e.enablePassword);function o(o,l,t){if(o!=null){return}for(var d=0;d<l.length;d++){var r=l[d].groupId+":"+l[d].groupName;var i=l[d].groupName;a.addOption(r,i,t)}for(var d=0;d<l.length;d++){if(l[d].groupId==e.groupId){$("#"+t).val(l[d].groupId+":"+l[d].groupName);break}}}a.devicesIns.queryAllSubGroups("group_name",o)}module.exports=l});