123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661 |
- 'use strict';
- app.controller('cmdbViewCtrl', ["$rootScope","$scope", "$state", "$timeout", "$interval", "$aside", "SweetAlert", "toaster", "Restangular", "api_bpm_domain", "api_configure_data", "api_cmdb", "api_configure_form", function ($rootScope, $scope, $state, $timeout, $interval, $aside, SweetAlert, toaster, Restangular, api_bpm_domain, api_configure_data, api_cmdb, api_configure_form) {
- var width = $scope.width = 650;
- var height = $scope.height = 400;
- var apple_selected, tree, treedata_avm, treedata_geography;
- $scope.my_tree_handler = function (branch) {
- //var _ref;
- var classify=$scope.classify=branch.prefix.toLowerCase()+branch.sign;
- api_cmdb.query({'sign':classify}).then(function(data){
- var myData = Restangular.stripRestangular(data);
- console.log(myData.data.node);
- if(myData.data&&myData.status==200){
- var ret = myData.data;
- redrawSvg(myData);
- }
- });
- };
- $scope.my_data = [];
- function convertListToTree(data, treeMap){
- var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
- var root = null; //Initially set our loop to null
- var parentNode = null;
- //loop over data
- for(var i = 0; i < data.length; i++) {
- var datum = data[i];
- //each node will have children, so let's give it a "children" poperty
- datum.children = [];
- //add an entry for this node to the map so that any future children can
- //lookup the parent
- idToNodeMap[datum.id] = datum;
- //Does this node have a parent?
- if(typeof datum.parent === "undefined" || datum.parent == null) {
- //Doesn't look like it, so this node is the root of the tree
- root = datum;
- treeMap[datum.id] = root;
- } else {
- //This node has a parent, so let's look it up using the id
- parentNode = idToNodeMap[datum.parent.id];
- //We don't need this property, so let's delete it.
- delete datum.parent;
- //Let's add the current node as a child of the parent node.
- parentNode.children.push(datum);
- }
- }
- return root;
- }
- function convertParentToChildList(data){
- var treeMap = {};
- var list=[];
- convertListToTree(data, treeMap);
- angular.forEach(treeMap,function(item){
- list.push(item);
- });
- return list;
- }
- $scope.my_tree = tree = {};
- $scope.try_async_load = function () {
- $scope.my_data = [];
- $scope.select_treedata = [];
- $scope.doing_async = true;
- api_configure_data.fetchDataList('ciclassify',{'idx':0,'sum':100}).then(function(result){
- //console.log(result['list']);
- $scope.select_treedata = $scope.my_data = convertParentToChildList(result['list']);
- $scope.doing_async = false;
- tree.expand_all();
- //console.log(treelist);
- });
- };
- $scope.select_treedata = [];
- $scope.propTypeOptions = [];
- $scope.try_async_load();
-
- $scope.onFilterCallback = function(item){
- //console.log(item);
- if(angular.isDefined(item.children)&&item.children.length>=1){
- //not valid
- }else{
- var tempclassify=item.prefix.toLowerCase()+item.sign;
- $scope.cifilter_classic=tempclassify;
- api_configure_form.renderTabForm(tempclassify).then(function(data){
- var myData = Restangular.stripRestangular(data);
- $scope.propTypeOptions = myData;
- });
- }
- }
- $scope.onPropTypeChange = function(form){
- $scope.propOptions = form.fields;
- }
- $scope.onPropChange = function(prop){
- $scope.cifilter_prop = prop;
- }
- $scope.searchCI = function(searchKey, propObj, ciclassify){
- //if(searchKey!=null&&searchKey.length>1){
- var searchData = {};
- if(angular.isUndefined(propObj)){
- propObj = $scope.cifilter_prop;
- }
- if(angular.isUndefined(ciclassify)){
- ciclassify = $scope.cifilter_classic;
- }
- searchData['sign']=ciclassify;
- if(angular.isDefined(propObj)){
- searchData[propObj.key] = searchKey;
- }
- api_cmdb.query(searchData).then(function(response){
- var data=Restangular.stripRestangular(response);
- //此处不清空nodes
- var node=data.data.node;
- var tempNode=[];
- //nodes 或者links 要去重
- for(var i=0;i<node.length;i++){
- var tmp=0;
- for(var j=0;j<nodes.length;j++){
- if(node[i].uuid==nodes[j].uuid){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempNode.push(node[i]);
- }
- }
- for(var i=0;i<tempNode.length;i++){//nodes 不清空
- nodes.push(tempNode[i]);
- }
-
-
- restart();
- })
- //}
-
- }
- $scope.refresh = function(){
- nodes.length=0;
- links.length=0;
- restart();
- }
- $scope.remove = function(){
- if($scope.selected_link==null){
- SweetAlert.swal({
- title: "错误的操作!",
- text: "没有资产关系被选中!",
- type: "error"
- });
- }else if($scope.selected_node!=null){
- SweetAlert.swal({
- title: "错误的操作!",
- text: "资产不能在此处删除!",
- type: "error"
- });
- }else if($scope.selected_link.id==-1){
- SweetAlert.swal({
- title: "错误的操作!",
- text: "资产关系尚未提交,不能删除!",
- type: "error"
- });
- }else if($scope.selected_link!=null){
- var id=$scope.selected_link.id;
- var label = $scope.selected_link.name;
- for(var i=0;i<linkType.length;i++){
- if(linkType[i].type==$scope.selected_link.name){
- label=linkType[i].label;
- }
- }
- SweetAlert.swal({
- title: "确认删除资产关系-"+ label +"?" ,
- text: "删除资产关系-"+ label + "以后,你将不能恢复该数据!",
- type: "warning",
- cancelButtonText: "取消",
- showCancelButton: true,
- confirmButtonColor: "#DD6B55",
- confirmButtonText: "确认删除!"
- }, function (isConfirm) {
- if (isConfirm) {
- api_cmdb.removeRef(id).then(function(response){
- if(response&&response.status==200){
- links.splice(links.indexOf($scope.selected_link), 1);
- restart();
- }else{
- SweetAlert.swal({
- title: "系统错误!",
- text: "系统错误,请稍候重试或者联系管理员!",
- type: "error"
- });
- }
- });
- }
- });
- }
- }
- $scope.traversal = function(){
- if($scope.selected_node==null){
- SweetAlert.swal({
- title: "错误的操作!",
- text: "没有资产被选中!",
- type: "error"
- });
- }else{
- var uuid = $scope.selected_node.uuid;
- api_cmdb.traversal(uuid).then(function(response){
- var d=response.data;
- var node=d.node;
- var link=d.edge;
- var tempNode=[];
- var tempLink=[];
- //nodes 或者links 要去重
- for(var i=0;i<node.length;i++){
- var tmp=0;
- for(var j=0;j<nodes.length;j++){
- if(node[i].id==nodes[j].id){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempNode.push(node[i]);
- }
- }
-
- for(var i=0;i<link.length;i++){
- var tmp=0;
- for(var j=0;j<links.length;j++){
- if(link[i].id==links[j].id){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempLink.push(link[i]);
- }
- }
- //数据写入页面
- for(var i=0;i<tempNode.length;i++){//nodes 不清空
- nodes.push(tempNode[i]);
- }
- for(var i=0;i<tempLink.length;i++){//link 不清空
- links.push(tempLink[i]);
- }
- //links转换
- for(var i=0;i<links.length;i++){
- for(var j=0;j<nodes.length;j++){
- if(links[i].source==nodes[j].id){
- links[i].source=nodes[j];
- }
- if(links[i].target==nodes[j].id){
- links[i].target=nodes[j];
- }
- }
- }
-
- restart();
- })
- }
- }
- var svg = d3.select('#cmdbSVG').append('svg').attr('width', width).attr('height', height);
- // set up initial nodes and links
- // - nodes are known by 'id', not by index in array.
- // - reflexive edges are indicated on the node (as a bold black circle).
- // - links are always source < target; edge directions are set by 'left' and 'right'.
- var nodes = [],links = [];
- var treeNodes=[];
- //读取labels (node)
- var labels=[];
- //console.log(window.basePath);
- // d3.xhr(window.basePath+"/api/cm/labels").send("GET", function(error, json) {
- // if (!error){
- // labels=JSON.parse(json.responseText);
- // //加载资产类型下拉框
- // //treeNodes
- // for(var i=0;i<labels.length;i++){
- // var tmp=labels[i];
- // if(tmp.pid!=null){
- // tmp['pid']=tmp['pid'].id;
- // }else{
- // tmp['pid']=0;
- // }
- // treeNodes.push(tmp);
- // }
- // window.labelNodes = treeNodes;
- // $.fn.zTree.init($("#treeDemo"), setting, treeNodes);
- // //label 类型显示
- // d3.select("#rowDiv").selectAll('li')
- // .data(labels).enter()
- // .append('li')
- // .html(function(d){
- // return '<i class="icon iconfont">'+d.iconname+'</i><p>'+d.label+'</p>';
- // }).on('click',function(d){
- // //查询节点
- // query(d.sign,"");
- // });
- // }else{
- // jAlert("服务器请求异常",'提示');
- // }
- // });
- function hideLinkForm(hide){
- $scope.hideLink=hide;
- }
- function hideFeedForm(hide){
- $scope.hideFeed=hide;
- }
- function redrawSvg(json){
- nodes.length=0;//先清空
- links.length=0;
- var data=json.data;
- for(var i=0;i<data.node.length;i++){
- nodes.push(data.node[i]);
- }
- //TODO
- //hideFeedForm(true);;
- //hideLinkForm(true);
- restart();
- }
- $scope.query = function(){
- //if(d3.event.keyCode=13){
- queryProp("","");
- //}
- }
- var queryProp = function(label, prop){
- var data = {sign:label,q:prop};
- api_cmdb.query(data).then(function(response){
- if(response&&(response.status==200)){
- var data=response.data;
- //此处不清空nodes
- for(var i=0;i<data.node.length;i++){
- nodes.push(data.node[i]);
- }
- restart();
- }else{
- SweetAlert.swal({
- title: "提示!",
- text: "访问服务器出错!",
- type: "error"
- });
- }
- })
- }
- /*
- //查询输入框 回车事件
- d3.select("#query").on('keyup',function(){
- if(d3.event.keyCode==13){
- var q=this.value;
- query("",q);
- }
- });
- //label 查询或者条件查询
- function query(label,q){
- $.ajax({
- type: "GET",
- url: window.basePath +"/api/cmdb/ci",
- data: {lb :label ,q: q},
- dataType: "json",
- success:function(json){
- nodes.length=0;//先清空
- links.length=0;
- var data=json.data;
- for(var i=0;i<data.node.length;i++){
- nodes.push(data.node[i]);
- }
- hideFeedForm(true);;
- hideLinkForm(true);
- restart();
- },
- error:function(){
- SweetAlert.swal({
- title: "提示!",
- text: "访问服务器出错!",
- type: "error"
- });
- }
- });
- }
- */
- //读取status (节点 连线)
- var statuses=[];
- api_configure_data.fetchDataList('cistatus',{'idx':0,'sum':100}).then(function(result){
- if (result&&result.status==200){
- d3.select('#nodeStatus').selectAll('option')
- .data(statuses).enter()
- .append('option')
- .attr('value',function(d){return d.code;})
- .html(function(d){ return d.desc;});
- }else{
- SweetAlert.swal({
- title: "提示!",
- text: "服务器请求异常!",
- type: "error"
- });
- }
- });
- //读取types (node)
- // api_configure_data.fetchDataList('cistatus',{'idx':0,'sum':100}).then(function(result){
- // if (result&&result.status==200){
- // d3.select('#nodeType').selectAll('option')
- // .data(types).enter()
- // .append('option')
- // .attr('value',function(d){return d.source;})
- // .html(function(d){ return d.description;});
- // }else{
- // SweetAlert.swal({
- // title: "提示!",
- // text: "服务器请求异常!",
- // type: "error"
- // });
- // }
- // });
- /*
- //读取所有params
- initParams();
- var params=[];
- //更新加载最新的params
- function initParams(){
- d3.xhr(window.basePath+"/api/cm/dynparams").send("GET", function(error, json) {
- if (!error){
- params=JSON.parse(json.responseText);
- if(selected_node){
- shownode();
- }else if(selected_link){
- showLink();
- }
- }else{
- jAlert("服务器请求异常","提示");
- }
- });
- }
- */
- //查询relationship_type表 name 为表里的type,label为显示值
- var linkType=[];
- function fetchEdgeTypes(){
- api_configure_data.fetchDataList('ciedgetype',{'idx':0,'sum':100}).then(function(response){
- if(response){
- if(response['list']){
- linkType = response['list'];
- d3.select('#linkName').selectAll('option')
- .data(linkType).enter()
- .append('option')
- .attr('value',function(d){return d.type;})
- .html(function(d){ return d.label;});
- }else{
- SweetAlert.swal({
- title: "提示!",
- text: "服务器请求异常!",
- type: "error"
- });
- }
- }else{
- SweetAlert.swal({
- title: "提示!",
- text: "服务器请求异常!",
- type: "error"
- });
- }
- })
- }
- fetchEdgeTypes();
- //设置cost 只能输入整形 最大值为1
- // d3.select('#linkCost').on('keyup',function(){
- // this.value= this.value.replace(/[^\d.]/g,function(){jAlert('只能输入数字!',"提示");return '';});
- // if(this.value>1){
- // jAlert('最大输入值为1','提示');
- // this.value='';
- // return;
- // }
- // });
- //添加属性 属性类型
- var proTypes=[
- {id:1,key:'string',value:'文本类型'},
- {id:2,key:'date',value:'日期类型'},
- {id:3,key:'int',value:'数字类型'},
- {id:4,key:'float',value:'浮点类型'}
- ];
-
- // init D3 force layout
- var force = d3.layout.force()
- .nodes(nodes)
- .links(links)
- .size([width, height])
- .gravity(.05)
- .linkDistance(150)
- .linkStrength(2)
- .charge(-500)
- .on('tick', tick);
- // define arrow markers for graph links
- svg.append('svg:defs').append('svg:marker')
- .attr('id', 'end-arrow')
- .attr('viewBox', '0 -5 10 10')
- .attr('refX', 6)
- .attr('markerWidth', 3)
- .attr('markerHeight', 3)
- .attr('orient', 'auto')
- .append('svg:path')
- .attr('d', 'M0,-5L10,0L0,5')
- .attr('fill', '#000');
- svg.append('svg:defs').append('svg:marker')
- .attr('id', 'start-arrow')
- .attr('viewBox', '0 -5 10 10')
- .attr('refX', 4)
- .attr('markerWidth', 3)
- .attr('markerHeight', 3)
- .attr('orient', 'auto')
- .append('svg:path')
- .attr('d', 'M10,-5L0,0L10,5')
- .attr('fill', '#000');
- // line displayed when dragging new nodes
- var drag_line = svg.append('svg:path')
- .attr('class', 'link dragline hidden')
- .attr('d', 'M0,0L0,0');
- // handles to link and node element groups
- //(1)var path = svg.append('svg:g').selectAll('g')
- var path = svg.append('svg:g').selectAll('g'),
- circle = svg.append('svg:g').selectAll('g');
- // mouse event vars
- var selected_node = null,
- selected_link = null,
- mousedown_link = null,
- mousedown_node = null,
- mouseup_node = null;
- function resetMouseVars() {
- mousedown_node = null;
- mouseup_node = null;
- mousedown_link = null;
- }
- // update force layout (called automatically each iteration)
- function tick() {
- // draw directed edges with proper padding from node centers
- path.selectAll('path').attr('d', function(d) {
- var deltaX = d.target.x - d.source.x,
- deltaY = d.target.y - d.source.y,
- dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY),
- normX = deltaX / dist,
- normY = deltaY / dist,
- sourcePadding = d.left ? 27 : 22,
- targetPadding = d.right ? 27 : 22,
- sourceX = d.source.x + (sourcePadding * normX),
- sourceY = d.source.y + (sourcePadding * normY),
- targetX = d.target.x - (targetPadding * normX),
- targetY = d.target.y - (targetPadding * normY);
-
-
- return 'M' + sourceX + ',' + sourceY + 'L' + targetX + ',' + targetY;
- });
-
-
- circle.attr('transform', function(d) {
- return 'translate(' + d.x + ',' + d.y + ')';
- });
- }
- // update graph (called when needed)
- function restart() {
- //绑定link 数据
- path.datum(links);
- // path (link) group
- path = path.data(links,function(d) { return d.id; });
-
- var p = path.enter().append('svg:g');
-
- //update existing links
- path.selectAll('path').classed('selected', function(d) { return d === selected_link; })
- .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
- .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; });
-
- // add new links
- p.append('svg:path')
- .attr('class', 'link')
- .attr('id', function(d) { return "path_" + d.id; })
- // .attr('startOffset', '0%')
- .classed('selected', function(d) { return d === selected_link; })
- .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
- .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; })
- .on('mousedown', function(d) {
- if(d3.event.ctrlKey) return;
-
- //去除没有提交的连线
- for(var i=0;i<links.length;i++){
- if(links[i].id==-1 && d.id!=-1){
- links.splice(i, 1);
- }
- }
-
- // select link
- mousedown_link = d;
- if(mousedown_link === selected_link) {
- selected_link = $scope.selected_link = null;
- selected_node = $scope.selected_node = null;
- // d3.select("#linkDetail").attr('style','display:none');
- hideLinkForm(true);//return ;
- } else {
- selected_link = $scope.selected_link = mousedown_link;
- selected_node = $scope.selected_node = null;
- //linkName
- //d3.select("#linkDetail").attr('style','display:inline-block;width:100%;');
- //showLink();
- //show("linkNamebox");//return ;
- openAside(selected_link, linkType);
- }
- selected_node = $scope.selected_node = null;
- restart();
- });
-
-
- p.append('svg:text')
- .attr('x', 30)
- .attr('y', 20)
- .attr('class','fontM')
- .append('textPath')
- .attr('xlink:xlink:href',function(d, i){ return "#path_" + d.id; })
- .html(function(d){
- for(var i=0;i<linkType.length;i++){
- if(linkType[i].type==d.name){
- return linkType[i].label;
- }
- }
- });
- // remove old links
- path.exit().remove();
- // circle (node) group
- // NB: the function arg is crucial here! nodes are known by id, not by index!
- circle.datum(nodes);
- circle = circle.data(nodes, function(d) { return d.id; });
-
- // update existing nodes (reflexive & selected visual states)
- circle.selectAll('circle')
- .style('fill', function(d) {return (d === selected_node) ?d3.rgb(colors(d.label)).brighter().toString() : colors(d.label); })
- .style('fill-opacity', 0.75) // add by xi
- .classed('reflexive', function(d) { return d.reflexive; });
-
- // add new nodes
- var g = circle.enter().append('svg:g');
- //g.append('svg:circle').attr('r',22).style('fill','rgb(255,255,255)').style('stroke','rgb(255,255,255)').style('stroke-width','2');
- //g.append('svg:circle').attr('r',24).style('fill','rgb(255,255,255)').style('stroke', function(d) { return d3.rgb(colors(d.label)).darker().toString(); }).style('stroke-width','2');
- g.append('svg:circle').attr('r',24).style('fill', function(d){ return (d === selected_node) ? 'rgb(153,153,153)' : 'rgb(255,255,255)';}).style('stroke', function(d) { return d3.rgb(colors(d.label)).darker().toString(); }).style('stroke-width','2');
-
- g.append('svg:circle')
- .attr('class', 'node')
- .attr('r', 18)
- .style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.label)).brighter().toString() : colors(d.label); })
- .style('stroke', function(d) { return d3.rgb(colors(d.label)).darker().toString(); })
- .style('fill-opacity', 0.25) //add by xj
- .style('stroke-opacity', 0.5) //add by xj
- .classed('reflexive', function(d) { return d.reflexive; });
- g.on('mouseover', function(d) {
- if(!mousedown_node || d === mousedown_node) return;
- // enlarge target node
- d3.select(this).attr('transform', 'scale(1.1)');
- })
- .on('mouseout', function(d) {
- if(!mousedown_node || d === mousedown_node) return;
- // unenlarge target node
- d3.select(this).attr('transform', '');
- })
- .on('mousedown', function(d) {
- if(d3.event.ctrlKey) return;
- hideLinkForm(true);
- //去除没有提交的连线
- for(var i=0;i<links.length;i++){
- if(links[i].id==-1 ){
- links.splice(i, 1);
- }
- }
- // select node
- mousedown_node = d;
- if(mousedown_node === selected_node) {
- selected_node = $scope.selected_node = null;
- hideFeedForm(true);;
- }else {
- selected_node = $scope.selected_node = mousedown_node;
- selected_link = $scope.selected_link = null;
- // 点击节点 查询关系及节点 /traversal/{id}/{relation}
- api_cmdb.findRefById(selected_node.id).then(function(result) {
- var d = result.data;
- var node = d.node;
- var link = d.edge;
- var tempNode = [];
- var tempLink = [];
- //nodes 或者links 要去重
- for (var i = 0; i < node.length; i++) {
- var tmp = 0;
- for (var j = 0; j < nodes.length; j++) {
- if (node[i].id == nodes[j].id) {
- tmp++;
- break;
- }
- }
- if (tmp == 0) {
- tempNode.push(node[i]);
- }
- }
- for (var i = 0; i < link.length; i++) {
- var tmp = 0;
- for (var j = 0; j < links.length; j++) {
- if (link[i].id == links[j].id) {
- tmp++;
- break;
- }
- }
- if (tmp == 0) {
- tempLink.push(link[i]);
- }
- }
- //数据写入页面
- for (var i = 0; i < tempNode.length; i++) { //nodes 不清空
- nodes.push(tempNode[i]);
- }
- for (var i = 0; i < tempLink.length; i++) { //link 不清空
- links.push(tempLink[i]);
- }
- //links转换
- for (var i = 0; i < links.length; i++) {
- for (var j = 0; j < nodes.length; j++) {
- if (links[i].source == nodes[j].id) {
- links[i].source = nodes[j];
- }
- if (links[i].target == nodes[j].id) {
- links[i].target = nodes[j];
- }
- }
- }
- restart();
- });
- /*
- // 点击节点 查询关系及节点 /traversal/{id}/{relation}
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cmdb/ci/"+selected_node.id+"/refs",
- dataType: "json",
- success:function(json){
- var d=json.data;
- var node=d.node;
- var link=d.edge;
- var tempNode=[];
- var tempLink=[];
- //nodes 或者links 要去重
- for(var i=0;i<node.length;i++){
- var tmp=0;
- for(var j=0;j<nodes.length;j++){
- if(node[i].id==nodes[j].id){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempNode.push(node[i]);
- }
- }
-
- for(var i=0;i<link.length;i++){
- var tmp=0;
- for(var j=0;j<links.length;j++){
- if(link[i].id==links[j].id){
- tmp++;
- break;
- }
-
- }
- if(tmp==0){
- tempLink.push(link[i]);
- }
- }
-
-
-
- //数据写入页面
- for(var i=0;i<tempNode.length;i++){//nodes 不清空
- nodes.push(tempNode[i]);
- }
- for(var i=0;i<tempLink.length;i++){//link 不清空
- links.push(tempLink[i]);
- }
- //links转换
- for(var i=0;i<links.length;i++){
- for(var j=0;j<nodes.length;j++){
- if(links[i].source==nodes[j].id){
- links[i].source=nodes[j];
- }
- if(links[i].target==nodes[j].id){
- links[i].target=nodes[j];
- }
- }
- }
-
- restart();
- },
- error:function(){jAlert("访问服务器出错!",'提示');}
- });
- */
- //更改编辑内容
- //shownode();
- }
-
- // reposition drag line
- drag_line
- .style('marker-end', 'url(#end-arrow)')
- .classed('hidden', false)
- .attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + mousedown_node.x + ',' + mousedown_node.y);
- restart();
- })
- .on('mouseup', function(d) {
- if(!mousedown_node) return;
- // needed by FF
- drag_line
- .classed('hidden', true)
- .style('marker-end', '');
- // check for drag-to-self
- mouseup_node = d;
- if(mouseup_node === mousedown_node) { resetMouseVars(); return; }
- // unenlarge target node
- d3.select(this).attr('transform', '');
- // add link to graph (update if exists)
- // NB: links are strictly source < target; arrows separately specified by booleans
- var source, target, direction;
- if(mousedown_node.id < mouseup_node.id) {
- source = mousedown_node;
- target = mouseup_node;
- direction = 'right';
- } else {
- source = mouseup_node;
- target = mousedown_node;
- direction = 'left';
- }
- var link;
- link = links.filter(function(l) {
- return (l.source === source && l.target === target);
- })[0];
- if(link) {
- //变为双向 direction 方向
- link[direction] = true;
- //更新
- selected_link=link;
- var l= selected_link;
- var idVal=l.id;
- // 连线提交 TODO 双向提交 后台问题
- console.log("put relation type");
- //TODO
- api_cmdb.createRef({'id':l.id,'name':l.name,'source':l.source.id,'target':l.target.id,'left':l.left,'right':l.right}).then(function(resposne){
- if(response&&response.status==200){
- selected_link.name=linkName;
- links.push(selected_link);
- restart();
- }else{
- }
- })
- // $.ajax({
- // type: "PUT",
- // contentType:"application/json; charset=UTF-8",
- // url: window.basePath+"/api/cmdb/edge/"+idVal,
- // data: JSON.stringify({'id':l.id,'name':l.name,'source':l.source.id,'target':l.target.id,'left':l.left,'right':l.right}),
- // dataType: "json",
- // success:function(data){
- // if(data.status==200){
- // console.log(data.data.edge[0]);
-
- // selected_link.name=linkName;
- // //selected_link.status=linkStatus;
- // selected_link.cost=linkCost;
-
- // restart();
- // showLink();
- // }
-
- // },
- // error: function(data){jAlert("服务器请求异常",'提示');}
- // });
-
-
- } else {
- cancel();
- //添加连线
- link = {id:-1,source: source, target: target,name:'', left: false, right: false};
- link[direction] = true;
- selected_link=link;
- links.push(link);
- //d3.select("#linkDetail").attr('style','display:inline-block;width:100%;');
- //showLink();
- openAside(link, linkType);
- //show("linkNamebox");
- }
-
- // select new link
- selected_link = $scope.selected_link = link;
- selected_node = $scope.selected_node = null;
- restart();
- });
- // show node name
- g.append('svg:text')
- .attr('x', 0)
- .attr('y', 34)
- .attr('class', 'id')
- .text(function(d) { return d.name; });//TODO 改为 d.name
- //设置图片
- g.append('svg:foreignObject')
- .attr("width", 32)
- .attr("height", 32)
- .attr('x', "-16px")
- .attr('y', "-11px")
- .html(function(d){
- for(var i=0;i<labels.length;i++){
- if(d.label==labels[i].sign){
- return '<i class="icon iconfont">'+labels[i].iconname+'</i>';
- }
-
- }
- });
- // remove old nodes
- circle.exit().remove();
- // set the graph in motion
- force.start();
- }
- function mousedown() {
- // prevent I-bar on drag
- //d3.event.preventDefault();
- // because :active only works in WebKit?
- svg.classed('active', true);
- if(mousedown_node){
- viewNode(mousedown_node);
- }
- if(d3.event.ctrlKey || mousedown_node || mousedown_link) return;
- // insert new node at point 点击添加node
- // var point = d3.mouse(this),
- // node = {id: ++lastNodeId, reflexive: false};
- // node.x = point[0];
- // node.y = point[1];
- // nodes.push(node);
-
- restart();
- }
- function viewNode(node){
- var title = $scope.selected_node.name+"-["+$scope.selected_node.uuid+"]";
- if(!$scope.selected_node.props.state){$scope.selected_node.props.state="空"}
- else if($scope.selected_node.props.state){
- if($scope.selected_node.props.state=="1"){$scope.selected_node.props.state="未审核"}
- else if($scope.selected_node.props.state=="2"){$scope.selected_node.props.state="已审核"}
- else if($scope.selected_node.props.state=="3"){$scope.selected_node.props.state="不匹配"}
- else if($scope.selected_node.props.state=="4"){$scope.selected_node.props.state="丢失"}
- };
- if(!$scope.selected_node.name){$scope.selected_node.name="空"}
- if(!$scope.selected_node.status){$scope.selected_node.statu="空"}
- if(!$scope.selected_node.props.useradmin){$scope.selected_node.props.useradmin="空"}
- toaster.pop({
- // type: 'info',
- // title: title,
- body: '<br/><p>搜索代码:' + $scope.selected_node.uuid + '</p>' +
- '<p>资产标题:' + $scope.selected_node.name + '</p>' +
- '<p>状态:' + $scope.selected_node.status + '</p>' +
- '<p>审核状态:' + $scope.selected_node.props.state + '</p>' +
- '<p>维护人员:' + $scope.selected_node.props.useradmin + '</p>',
- bodyOutputType: 'trustedHtml',
- timeout: 3000
- })
- }
- function mousemove() {
- if(!mousedown_node) return;
- // update drag line
- drag_line.attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + d3.mouse(this)[0] + ',' + d3.mouse(this)[1]);
- restart();
- }
- function mouseup() {
- if(mousedown_node) {
- // hide drag line
- drag_line
- .classed('hidden', true)
- .style('marker-end', '');
- }
- // because :active only works in WebKit?
- svg.classed('active', false);
- // clear mouse event vars
- resetMouseVars();
- }
- function spliceLinksForNode(node) {
- var toSplice = links.filter(function(l) {
- return (l.source === node || l.target === node);
- });
- toSplice.map(function(l) {
- links.splice(links.indexOf(l), 1);
- });
- }
- // only respond once per keydown
- var lastKeyDown = -1;
- function keydown() {
- //d3.event.preventDefault();
- if(lastKeyDown !== -1) return;
- lastKeyDown = d3.event.keyCode;
- // ctrl
- if(d3.event.keyCode === 17) {
- circle.call(force.drag);
- svg.classed('ctrl', true);
- }
- if(!selected_node && !selected_link) return;
- switch(d3.event.keyCode) {
- case 8: // backspace
- // case 46: // delete
- // if(selected_node) {
- // nodes.splice(nodes.indexOf(selected_node), 1);
- // spliceLinksForNode(selected_node);
- // } else if(selected_link) {
- // links.splice(links.indexOf(selected_link), 1);
- // }
- // selected_link = null;
- // selected_node = null;
- // restart();
- // break;
- case 66: // B
- if(selected_link) {
- // set link direction to both left and right
- selected_link.left = true;
- selected_link.right = true;
- }
- restart();
- break;
- case 76: // L
- if(selected_link) {
- // set link direction to left only
- selected_link.left = true;
- selected_link.right = false;
- }
- restart();
- break;
- case 82: // R
- if(selected_node) {
- // toggle node reflexivity
- selected_node.reflexive = !selected_node.reflexive;
- } else if(selected_link) {
- // set link direction to right only
- selected_link.left = false;
- selected_link.right = true;
- }
- restart();
- break;
- }
- }
- function keyup() {
- lastKeyDown = -1;
- // ctrl
- if(d3.event.keyCode === 17) {
- circle
- .on('mousedown.drag', null)
- .on('touchstart.drag', null);
- svg.classed('ctrl', false);
- }
- }
- // app starts here
- svg.on('mousedown', mousedown)
- .on('mousemove', mousemove)
- .on('mouseup', mouseup);
- d3.select(window)
- .on('keydown', keydown)
- .on('keyup', keyup);
- //restart();
- //GT 颜色与数值对照表
- function colors(sign){
- //num=num>20?num%20:parseInt(num);
- for(var i=0;i<labels.length;i++){
- var entity=labels[i];
- if(sign==entity.sign){
- return entity.color;
- }
- if(i==labels.length-1 && sign!=entity.sign){
- return '#ffeeee';
- }
- }
- }
- function postion(sign){
- for(var i=0;i<labels.length;i++){
- var entity=labels[i];
- if(sign==entity.sign){
- return i;
- }
- }
- return 1;
- }
- //添加设备
- function addnode() {
- //先编辑提交 到数据库添加一条数据得到ID 后显示节点
- var assetNo=d3.select("#nodeAssetNo").property('value');//资产编号
- var name=d3.select("#nodeName").property('value');//资产名称
- var type=d3.select("#nodeType").property('value');//资产类型
- var label=d3.select("#nodeLabel").property('value');//label
- var status=d3.select("#nodeStatus").property('value');//资产状态
-
- $.ajax({
- type: "POST",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cmdb/ci",
- data: JSON.stringify({'name':name,'status':status,'type':type,'label':label,'assetNo':assetNo,props:selected_node.props }),
- dataType: "json",
- success:function(data){
- //成功后得到ID
- if(data.status==200){
- var node=data.data.node[0];
- node.x = 634;
- node.y = 265.5625;
-
- nodes.push(node);
- selected_node=node;
- restart();
- hideFeedForm(true);;
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
-
-
-
- }
- //显示节点信息
- function shownode(){
- d3.select("#nodeAssetNo").property('value',selected_node.assetNo==undefined?"":selected_node.assetNo);//资产编号
- d3.select("#nodeName").property('value',selected_node.name==undefined?"":selected_node.name);//资产名称
- d3.select("#nodeType").property('value',selected_node.type==undefined?"":selected_node.type);//资产类型
- d3.select("#nodeLabel").property('value',selected_node.label==undefined?"":selected_node.label);//label
- for(var i=0;i<treeNodes.length;i++){
- if(selected_node.label==treeNodes[i].sign){
- d3.select("#citySel").property('value',treeNodes[i].label);
- break;
- }
- }
- d3.select("#nodeStatus").property('value',selected_node.status==undefined?"":selected_node.status);//资产状态
-
- //先移除div中原来的标签
- d3.select('#nodeTab').selectAll('tr').remove('tr');
-
- if(selected_node.label){
- var label=null;
- /*
- //根据选择‘节点’的label 查询label的ID
- d3.xhr(window.basePath+"/api/cm/findLabel?sign="+selected_node.label).send("GET", function(error, json) {
- if (!error){
- label =JSON.parse(json.responseText);
-
- if(selected_node.props){
- var keys=[];
- for(var key in selected_node.props){
- keys.push(key);
- }
-
- //写入固定属性
- for(var i=0;i<params.length;i++){
- var p=params[i];
- var num=0;
- for(var j=0;j<keys.length;j++){
- if(p.sourceType==1 && p.sourceId==label.id && p.key==keys[j]){//节点已有属性
- num++;
- break;
- }
- }
- if(p.sourceType==1 && p.sourceId==label.id && p.fix==1 && num==0){//节点 中没有的固定属性
- selected_node['props'][p.key]='';
- }
-
- }
- keys=[];
- for(var key in selected_node.props){
- keys.push(key);
- }
-
- d3.select('#nodeTab').selectAll('tr')
- .data(keys).enter()
- .append('tr')//.nextSibling
- .attr('id',function(d){
- return 'tr'+d;
- })
- .html(function(d){//undefined
- var str="";
- if(label!=null){
- if(d==""){//新增后props 属性
- var strS="";
-
- strS='<input type="text" id="input'+d+'" value="'+selected_node.props[d]+'" disabled/>';
- str+='<td style="padding-left: 0px;width:145px;"><span id="span'
- +d+'" style="width:50px;text-align:right;">未定义:</span><input type="hidden" id="select'+
- d+'" style="width: 90px;margin-left: 52px;"/></td>'+
- '<td style="padding-left: 21px;">'+strS+'</td><td>'
- +'<input id="edtBtn'+d+'" name="edtBtn" type="button" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnClk(\'text\',\''+d+'\',\'未定义\',\'0\')" />'
- +' <input type="button" value="" style="background-image:url(css/img/tup/del.jpg);width:22px;height:22px;" onclick=\'delBtnClk("'+d+'")\'/></td>';
- return str;
- }
-
- for(var i=0;i<params.length;i++){
- var p=params[i];
-
- //sourceType==1 类型为节点 key相等 labelId相同
- if((p.sourceType==1 && d==p.key && p.sourceId==label.id)){//新增加行 动态属性
- var strS="";
- if(p.valueType=="date"){
- strS='<input id="input'+d+'" placeholder="请输入日期" style="width: 151px;" class="laydate-icon" onClick="laydate({istime: true, format: \'YYYY-MM-DD hh:mm:ss\'})" value="'
- +selected_node.props[d]+'" disabled>';
- }else{
- strS='<input type="text" id="input'+d+'" value="'+selected_node.props[d]+'" disabled/>';
- }
- if(p.fix==1){//固定属性 不允许编辑显示属性名 不允许删除
-
- str+='<td style="padding-left: 0px;width:145px;"><span id="span'
- +d+'">'+p.descName+':</span></td>'+
- '<td style="padding-left: 21px;">'+
- strS+'</td><td>'
- +'<input id="edtBtn'+d+'" name="edtBtn" type="button" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnClk(\''
- +p.valueType+'\',\''+d+'\',\''+p.descName+'\',\''+p.fix+'\')" />'
- +' </td>';
- break;
- }else{
- //类型设置 读取JSON string date int float
- str+='<td style="padding-left: 0px;width:145px;"><span id="span'
- +d+'">'+p.descName+':</span><input type="hidden" id="select'+
- d+'" style="width: 90px;margin-left: 52px;"/></td>'+
- '<td style="padding-left: 21px;">'+
- strS+'</td><td>'
- +'<input id="edtBtn'+d+'" name="edtBtn" type="button" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnClk(\''
- +p.valueType+'\',\''+d+'\',\''+p.descName+'\',\''+p.fix+'\')" />'
- +' <input type="button" value="" style="background-image:url(css/img/tup/del.jpg);width:22px;height:22px;" onclick=\'delBtnClk("'+d+'")\'/></td>';
- break;
- }
-
- }
- }
-
- }
-
- return str;
- });
- }
- }else{
- jAlert("服务器请求异常!",'提示');
- }
- });
- */
- }
-
-
-
- }
- //显示连线信息
- //
- var openAside = function (model, linkTypes) {
- var modalInstance = $aside.open({
- templateUrl: 'assets/views/system/tpl/asideContent.html',
- placement: 'right',
- size: 'sm',
- backdrop: true,
- controller: function ($scope, $modalInstance, ModelData, optionLinkTypes) {
- $scope.modelData = ModelData;
- $scope.linktypeOptions = optionLinkTypes;
- if($scope.modelData.name!=""){
- angular.forEach($scope.linktypeOptions,function(item){
- if(item.type==$scope.modelData.name){
- $scope.modelData.linkName=item;
- }
- })
- }
- $scope.ok = function (e) {
- $modalInstance.close($scope.modelData);
- //e.stopPropagation();
- };
- $scope.cancel = function (e) {
- $modalInstance.dismiss();
- //e.stopPropagation();
- };
- },
- resolve: {
- ModelData: function(){
- return model;
- },
- optionLinkTypes: function(){
- return linkTypes;
- }
- }
- });
- modalInstance.result.then(function (selectedItem) {
- //TODO create Edge
- console.log(selectedItem);
- if(selectedItem.id==-1){
- api_cmdb.createRef({'id':selectedItem.id,'name':selectedItem.linkName.type,'source':selectedItem.source.id,'target':selectedItem.target.id,'left':selectedItem.left,'right':selectedItem.right}).then(function(response){
- if(response&&response.status==200){
- //model.name=selectedItem.linkName.type;
- var link = response.data.edge[0];
- for (var j = 0; j < nodes.length; j++) {
- if (link.source == nodes[j].id) {
- link.source = nodes[j];
- }
- if (link.target == nodes[j].id) {
- link.target = nodes[j];
- }
- }
- links.push(link);
- // selected_link.name=linkName;
- // selected_link.cost=linkCost;
- restart();
- }else{
- }
- })
- }else{
- api_cmdb.putRef({'id':selectedItem.id,'name':selectedItem.linkName.type,'source':selectedItem.source.id,'target':selectedItem.target.id,'left':selectedItem.left,'right':selectedItem.right}, selectedItem.id).then(function(response){
- if(response&&response.status==200){
- //model=selectedItem;
- angular.forEach(links,function(link,index){
- if(link.name == model.name){
- links[index]=response.data.edge[0];
- for (var j = 0; j < nodes.length; j++) {
- if (links[index].source == nodes[j].id) {
- links[index].source = nodes[j];
- }
- if (links[index].target == nodes[j].id) {
- links[index].target = nodes[j];
- }
- }
- }
- })
- restart();
- }else{
- }
- })
- }
-
- }, function () {
- //console.log('Modal dismissed at: ' + new Date());
- });
- };
- //
- //
- function showLink(){
- var linkeName = $scope.linkName = selected_link.name==undefined?"":selected_link.name;
- $scope.linkCost = selected_link.cost==undefined?"":selected_link.cost;
- // d3.select("#linkName").property('value',selected_link.name==undefined?"":selected_link.name);
- // d3.select("#linkCost").property('value',selected_link.cost==undefined?"":selected_link.cost);
-
- //var linkeName=d3.select("#linkName").property('value');
- //先移除div中原来的标签
- d3.select('#linkTab').selectAll('tr').remove('tr');
-
- var linkId;
- //sourceTYpe=2 and sourceId=连线nameId
- //当前选择的连线name Id
- for(var i=0;i<linkType.length;i++){
- if(linkeName==linkType[i].type){
- linkId=linkType[i].id;
- }
- }
- var props=selected_link.props;
- var keys=[];
- for(var key in props){
- keys.push(key);
- }
- // //写入固定属性
- // for(var i=0;i<params.length;i++){
- // var p=params[i];
- // var num=0;
- // for(var j=0;j<keys.length;j++){
- // if(p.sourceType==2 && p.sourceId==linkId && p.key==keys[j]){//连线已有属性
- // num++;
- // break;
- // }
- // }
- // if(p.sourceType==2 && p.sourceId==linkId && p.fix==1 && num==0){//连线 中没有的固定属性
- // selected_link['props'][p.key]='';
- // }
-
- // }
- keys=[];
- for(var key in selected_link.props){
- keys.push(key);
- }
- if(keys){
- d3.select('#linkTab').selectAll('tr')
- .data(keys).enter()
- .append('tr')//.nextSibling
- .attr('id',function(d){
- return 'trLink'+d;
- }).html(function(d){
- var str="";
- if(d==""){//新增后props 属性
- var strS="";
-
- strS='<input type="text" id="inputLink'+d+'" value="'+props[d]+'" disabled/>';
- str+='<td style="padding-left: 0px;width:145px;"><span id="spanLink'
- +d+'" style="width:50px;text-align:right;">未定义:</span><input type="hidden" id="selectLink'+
- d+'" style="width: 90px;margin-left: 52px;"/></td>'+
- '<td style="padding-left: 21px;">'+//111111111111111111111 padding-left: 60px;
- strS +'</td><td>'
- +'<input id="edtBtnLink'+d+'" type="button" name="edtBtn" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnLinkClk(\'text\',\''
- +d+'\',\'未定义\',\'0\')" />'
- +' <input type="button" value="" style="background-image:url(css/img/tup/del.jpg);width:22px;height:22px;" onclick=\'delBtnLinkClk("'+d+'")\'/></td>';
- return str;
- }
-
- for(var j=0;j<params.length;j++){
- var p=params[j];
- if((p.sourceType==2 && d==p.key && p.sourceId==linkId)){//新增加行
- var strS="";
- if(p.valueType=="date"){
- strS='<input id="inputLink'+d+'" placeholder="请输入日期" style="width: 151px;" class="laydate-icon" onClick="laydate({istime: true, format: \'YYYY-MM-DD hh:mm:ss\'})" value="'+props[d]+'" disabled>';
- }else{
- strS='<input type="text" id="inputLink'+d+'" value="'+props[d]+'" disabled/>';
-
- }
- if(p.fix==1){//固定属性 不允许编辑显示属性名 不允许删除
- //类型设置 读取JSON string date int float 22222222222222222padding-left: 21px;
- str+='<td style="width:145px;"><span id="spanLink'+d+'">'+p.descName+':</span></td>'+
- '<td style="padding-left: 21px;">'+strS+'</td><td>'+'<input id="edtBtnLink'+d
- +'" type="button" name="edtBtn" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnLinkClk(\''
- +p.valueType+'\',\''+d+'\',\''+p.descName+'\',\''+p.fix+'\')" />'
- +' </td>';
- break;
- }else{
- //类型设置 读取JSON string date int float
- str+='<td style="width:145px;"><span id="spanLink'
- +d+'">'+p.descName+':</span><input type="hidden" id="selectLink'+
- d+'" style="width: 90px;margin-left: 52px;"/></td>'+
- '<td style="padding-left: 21px;">'+//333333333333333 padding-left: 21px;
- strS
- +'</td><td>'
- +'<input id="edtBtnLink'+d+'" type="button" name="edtBtn" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnLinkClk(\''
- +p.valueType+'\',\''+d+'\',\''+p.descName+'\',\''+p.fix+'\')" />'
- +' <input type="button" value="" style="background-image:url(css/img/tup/del.jpg);width:22px;height:22px;" onclick=\'delBtnLinkClk("'+d+'")\'/></td>';
- break;
- }
-
-
- }
- }
- return str;
- });
- }
- hideLinkForm(false);
- }
- //删除(修改状态为报废)
- function delnode_update(){
- if(selected_node || selected_link){
- jConfirm('确定删除?', '提示', function(r) {
- if(r){
- if(selected_node){//删除配置
- //节点ID(节点删除执行成功,数据库节点未删除 ,删除节点关系时执行失败)
- var id=selected_node.id;
- var name=selected_node.name;
- var type=selected_node.type;//资产类型
- var label=selected_node.label; //资产类型
- var assetNo=selected_node.assetNo;//资产编号
- var props =selected_node.props;
- var status=CMConstants.DISCARDE;
- //删除后,状态变为废弃
- $.ajax({
- type: "PUT",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cmdb/ci/"+id,
- data: JSON.stringify({'id':id,'name':name,'status':status,'type':type,'label':label,'assetNo':assetNo,'props':props}),
- dataType: "json",
- success:function(data){
- if(data.status==200){
-
- jAlert("删除成功!",'提示');
-
- selected_node.type=type;//资产类型
- selected_node.status=status;
- selected_node.label=label; //资产类型
- selected_node.assetNo=assetNo; //资产编号
- selected_node.props=props;
- selected_node.name=name;
- restart();
- query(label,"");
- //shownode();
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
-
- }
-
- }else{
- jAlert("请求异常",'提示');
- }
-
- });
-
- }else{
- jAlert('请先选择删除项!','提示');
- }
-
- }
- //删除
- function delnode(){
- if(selected_node || selected_link){
- jConfirm('确定删除?', '提示', function(r) {
- if(r){
- if(selected_node){//删除配置
- //节点ID(节点删除执行成功,数据库节点未删除 ,删除节点关系时执行失败)
- var id=selected_node.id;
- //先删除节点关系
- d3.xhr(window.basePath+"/api/cmdb/ci/"+id+"/refs").send("DELETE", function(error, json) {
- if (!error){
- if(json.status==200){
- var edge=JSON.parse(json.responseText).data.edge;
- for(var i=0;i<edge.length;i++){
- for(var j=0;j<links.length;j++){
- if(edge[i].id==links[j].id){
- links.splice(j, 1);
- break;
- }
- }
-
- }
- //再删除节点
- d3.xhr(window.basePath+"/api/cmdb/ci/"+id).send("DELETE", function(error, json) {
- if (!error){
- if(json.status==200){
- //删除节点
- nodes.splice(nodes.indexOf(selected_node), 1);
- jAlert("删除成功!",'提示');
- hideFeedForm(true);;
- }
- restart();
- }else{
- jAlert("请求异常",'提示');
- }
- });
- }
-
-
- }else{
- jAlert("请求异常",'提示');
- }
- });
-
-
-
- }
- //用户删除关系
- if(selected_link){
- //得到关系ID 删除关系
- var id=selected_link.id;
- d3.xhr(window.basePath+"/api/cmdb/edge/"+id).send("DELETE", function(error, json) {
- if (!error){
- links.splice(links.indexOf(selected_link), 1);
- jAlert("删除成功!",'提示');
- hideLinkForm(true);
- restart();
- }else{
- jAlert("请求异常",'提示');
- }
- });
- }
- }
-
- });
- }else{
- jAlert('请先选择删除项!','提示');
- }
-
- }
- var CI ={};
- CI.add = addnode;
- window.CI = CI;
- d3.select("#traversal").selectAll('a').data([{name:'遍历',id:'traversalBtn'}]).enter()
- .append('a')
- .html(function (d) { return d.name; })
- .on('click', function (d) {
- if(d.id=="traversalBtn"){
- if(selected_node == null){
- alert("没有选择节点。。。");
- }else{
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cmdb/traversal/"+selected_node.assetNo,
- dataType: "json",
- success:function(json){
- var d=json.data;
- var node=d.node;
- var link=d.edge;
- var tempNode=[];
- var tempLink=[];
- //nodes 或者links 要去重
- for(var i=0;i<node.length;i++){
- var tmp=0;
- for(var j=0;j<nodes.length;j++){
- if(node[i].id==nodes[j].id){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempNode.push(node[i]);
- }
- }
-
- for(var i=0;i<link.length;i++){
- var tmp=0;
- for(var j=0;j<links.length;j++){
- if(link[i].id==links[j].id){
- tmp++;
- break;
- }
- }
- if(tmp==0){
- tempLink.push(link[i]);
- }
- }
- //数据写入页面
- for(var i=0;i<tempNode.length;i++){//nodes 不清空
- nodes.push(tempNode[i]);
- }
- for(var i=0;i<tempLink.length;i++){//link 不清空
- links.push(tempLink[i]);
- }
- //links转换
- for(var i=0;i<links.length;i++){
- for(var j=0;j<nodes.length;j++){
- if(links[i].source==nodes[j].id){
- links[i].source=nodes[j];
- }
- if(links[i].target==nodes[j].id){
- links[i].target=nodes[j];
- }
- }
- }
-
- restart();
- },
- error:function(){alert("访问服务器出错!");}
- });
- }
- }
- });
- d3.select("#layout").selectAll('a').data([{name:'布局',id:'layoutBtn'}]).enter()
- .append('a')
- .html(function (d) { return d.name; })
- .on('click', function (d) {
- if(d.id=="layoutBtn"){
- var k = Math.sqrt(nodes.length / (width * height));
- //var len = labels.length;
- // nodes.forEach(function(o, i) {
- // //console.log( "postion:" + postion(o.label) % len);
- // o.x += postion(o.label) & 1 ? 140 : -140;
- // o.y += postion(o.label) & 2 ? 140 : -140;
- // console.log("[" +o.id + "-" + o.name + "]:" + o.label + "(" + o.x + "," + o.y + ");");
- // });
- force.charge((-10/k)*1.5);
- force.gravity(100*k);
- force.resume();
- }
- });
- //添加node 点击事件
- d3.select("#addsss").selectAll('a').data([{name:'添加',id:'addBtn'}]).enter()
- .append('a')
- .html(function (d) { return d.name; })
- .on('click', function (d) {
- if(d.id=="addBtn"){
- selected_node={id:-1};
-
- //内容置空
- d3.select("#nodeAssetNo").property('value','');//资产编号
- d3.select("#nodeName").property('value','');//资产名称
- d3.select("#nodeType").property('value','');//资产类型
- d3.select("#nodeLabel").property('value','');//label
- d3.select("#nodeStatus").property('value','');//资产状态
- d3.select('#nodeTab').selectAll('tr').remove();
-
- show("feedbox");return false;
- }
- });
- //删除
- d3.select("#delsss").selectAll('a').data([{name:'删除',id:'delBtn'}]).enter()
- .append('a')
- .html(function (d) { return d.name; })
- .on('click', function (d) {
- if(d.id=="delBtn"){
- //delnode();
- delnode_update();
- }
- });
- //修改
- d3.select("#uppsss").selectAll('a').data([{name:'修改',id:'edtBtn'}]).enter()
- .append('a')
- .html(function (d) { return d.name; })
- .on('click', function (d) {
- if(d.id=="edtBtn"){
- if(selected_node!=null){
- show("feedbox");
- return false;
- }else{
- jAlert("请选择设备",'提示');
- }
- }
- });
- //节点提交
- d3.select("#subBtn").on('click',function(d){
- var assetNo=d3.select("#nodeAssetNo").property('value');//资产编号
- var label=d3.select("#nodeLabel").property('value');//label
-
- var props =selected_node.props;
- for(var key in props){
- if(key==""){
- jAlert("您有未定义属性,请删除或编辑!",'提示');
- return;
- }
- }
- if(assetNo==""){
- jAlert("请输入资产编号!",'提示');
- return;
- }else if(label==null ||label==""){
- jAlert("请选择资产类型!",'提示');
- return;
- }else{
- //添加或者更新
- if(selected_node.id==-1){//添加
- addnode();
- }else{//更新
- var idVal=selected_node.id;
- var name=d3.select("#nodeName").property('value');//资产名称
- var type=d3.select("#nodeType").property('value');//资产类型
-
- var status=d3.select("#nodeStatus").property('value');//资产状态
- //节点提交
- $.ajax({
- type: "PUT",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cmdb/ci/"+idVal,
- data: JSON.stringify({'id':idVal,'name':name,'status':status,'type':type,'label':label,'assetNo':assetNo,'props':selected_node.props }),
- dataType: "json",
- success:function(data){
- if(data.status==200){
- jAlert("更新成功!",'提示');
- selected_node.assetNo=assetNo;
- selected_node.name=name;
- selected_node.type=type;
- selected_node.label=label;
- selected_node.status=status;
-
- restart();
- //shownode();
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
- }
- // restart();
- }
-
- });
- //连线提交
- d3.select("#subBtn1").on('click',function(d){
- var linkName=d3.select("#linkName").property('value');
- var linkCost=d3.select("#linkCost").property('value');
- var idVal=selected_link.id;
-
- var props =selected_link.props;
- for(var key in props){
- if(key==""){
- jAlert("您有未定义属性,请删除或编辑!",'提示');
- return;
- }
- }
-
- if(idVal==-1){//添加
- selected_link.name=linkName;
- //selected_link.status=linkStatus;
- selected_link.cost=linkCost;
- var l=selected_link;
- $.ajax({
- type: "POST",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cmdb/edge",
- data: JSON.stringify({'id':l.id,'name':l.name,'status':l.status,'cost':l.cost,'left':l.left,'right':l.right,'source':l.source.id,'target':l.target.id,'props':l.props}),
- dataType: "json",
- success:function(data){
- if(data.status==200){
- var link=data.data.edge[0];
- jAlert("添加成功!",'提示');
- selected_link.id=link.id;
-
- restart();
- hideLinkForm(true);
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
- }else{
- // 连线提交
- $.ajax({
- type: "PUT",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cmdb/edge/"+idVal,
- data: JSON.stringify({'id':idVal,'name':linkName,'cost':linkCost,'props':selected_link.props }),
- dataType: "json",
- success:function(data){
- if(data.status==200){
- jAlert("更新成功!",'提示');
-
- selected_link.name=linkName;
- //selected_link.status=linkStatus;
- selected_link.cost=linkCost;
-
- restart();
- showLink();
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
- }
- // restart();
-
- });
- //全局变量 正在编辑的属性ID
- var etdBtnId="";
- //编辑按钮点击事件
- function edtBtnClk(type,etdId,value,fix){
- var v=d3.select('#edtBtn'+etdId).property('name');
- if(v=='edtBtn'){
- d3.select('#input'+etdId).property('disabled','');//可编辑
- if(type=="date"){//加载日期
-
- //$("#input"+etdId).datepicker({dateFormat: 'yy-mm-dd'});$.datepicker.regional[ "zh-cn" ];
- }else if(type=='int'){//只能为整型
- d3.select('#input'+etdId).on('keyup',function(){
- this.value= this.value.replace(/[^\d]/g, '');
- });
- }else if(type=='float'){
- d3.select('#input'+etdId).on('keyup',function(){
- this.value= this.value.replace(/[^\d.]/g,'');
- });
- }
- //span文字隐藏 select2显示
- // d3.select('.addtd').classed('addtds');
- if(fix==0){//动态属性 显示名称隐藏
- d3.select('#span'+etdId).attr('style','display:none');
- }
-
- //d3.select('tr#tr'+etdId + 'td + td').attr('style','padding-left:0px;');
-
- //$('#select'+etdId).select2({ multiple: true, data:selectData});//分号选择
- //根据用户选择的label 和节点类型查询
- var t=d3.select('#nodeLabel').property('value');
- var typeId="";//选中的资源label的ID
- for(var i=0;i<labels.length;i++){
- if(labels[i].sign==t){
- typeId=labels[i].id;
- break;
- }
- }
- var res=[];
-
- //改变属性
- d3.select('#edtBtn'+etdId).property('name','edt');
- d3.select('#edtBtn'+etdId).attr('style','background-image:url(css/img/tup/queding.jpg);width:22px;height:22px;');
-
- $('#select'+etdId).select2({
- ajax: {
- url: window.basePath+'/api/cm/findByPro',//sourceType=1 节点
- type:'GET',
- dataType: 'json',
- data: function(){
- return {
- sourceType : 1,
- sourceId: typeId
- };
- },
- results: function (data) {
- res=data;
-
- var bindData = [];
- for(var i = 0;i < data.length;i++){
- bindData.push({'id':data[i].key,'text':data[i].descName});
- }
-
- return {results: bindData};
- }
- },
- createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
- //multiple: true,//单选 or 多选
-
- }).on("select2-selecting", function(e) {
-
- etdBtnId=this.id.substr('select'.length);
- var result = [];
- for(var i = 0;i < res.length;i++){
- result.push({'id':res[i].key,'text':res[i].descName});
- }
-
- if(result.length==0){
- //置空
- d3.select("#proType").property('value','');
- d3.select("#proNo").property('value','');
-
- d3.select('#overlay').style('display','block');
- d3.select('#win').style('display','block');
- return;
- }
- for(var i=0;i<result.length;i++){
- if(e.choice.id==result[i].id && e.choice.text==result[i].text){//用户选择的是已有的属性
- //写入nodes
- var props=selected_node.props;
- // 用户选择不能重复
- for(var key in props){
- if(e.val==key && e.val!=etdId){
- jAlert("此资产已有此属性,请另外选择!",'提示',function(){
- $('#select'+etdId).select2('data', {id:etdId,text:value});
-
- });
- return;
- }
- }
- //用户选择验证通通过 写入到nodes中
- for(var p in props){
- if(p==etdId){
- var text=props[p];
- delete props[p];
- selected_node.props[e.choice.id]=text;
- shownode();//此处不重新加载(shownode) 会有问题
- return;
- }
- }
-
- }else if((i==result.length-1) && (e.choice!=result[i])){//用户新输入的
-
- //置空
- d3.select("#proType").property('value','');
- d3.select("#proNo").property('value','');
-
- d3.select('#overlay').style('display','block');
- d3.select('#win').style('display','block');
- return;
- }
- }
- });
- //绑定默认值
- $('#select'+etdId).select2('data', {id:etdId,text:value});
- }else{//确定
- d3.select('#edtBtn'+etdId).property('name','edtBtn');
- d3.select('#edtBtn'+etdId).attr('style','background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;');
-
- //数据写入nodes
- var props=selected_node.props;
- for(var key in props){
- if(key==etdId){
- var text=d3.select('#input'+etdId).property('value');
- var k=null;
- if(fix==0){
- k=d3.select('#select'+etdId).property('value');
- }else{
- k=etdId;
- }
- delete props[etdId];
- props[k]=text;
- shownode();
- return;
- }
- }
- }
- }
- //编辑按钮点击事件
- function edtBtnLinkClk(type,etdId,value,fix){
- var linkeName=d3.select('#linkName').property('value');
- if(linkeName==""){
- jAlert("请先选取连线名称!",'提示');
- return;
- }
- var v=d3.select('#edtBtnLink'+etdId).property('name');
-
- if(type=="date"){//加载日期
- // $("#inputLink"+etdId).datepicker({dateFormat: 'yy-mm-dd'});$.datepicker.regional[ "zh-cn" ];
- }else if(type=='int'){//只能为整型
- d3.select('#inputLink'+etdId).on('keyup',function(){
- this.value= this.value.replace(/[^\d]/g, '');
- });
- }else if(type=='float'){
- d3.select('#inputLink'+etdId).on('keyup',function(){
- this.value= this.value.replace(/[^\d.]/g,'');
- });
- }
-
- if(v=='edtBtn'){
- d3.select('#inputLink'+etdId).property('disabled','');//可编辑
- //改变属性
- d3.select('#edtBtnLink'+etdId).attr('style','background-image:url(css/img/tup/queding.jpg);width:22px;height:22px;');
- d3.select('#edtBtnLink'+etdId).property('name','edt');
- if(fix==0){//动态属性隐藏
- //span文字隐藏 select2显示
- d3.select('#spanLink'+etdId).attr('style','display:none');
- }
-
-
- //根据用户选择的name(连线名称) 查询
-
- var linkId;//选中的name的ID 即属性表里的sourceId
- for(var i=0;i<linkType.length;i++){
- if(linkeName==linkType[i].type){
- linkId=linkType[i].id;
- }
- }
- var res=[];
- $('#selectLink'+etdId).select2({
- ajax: {
- url: window.basePath+'/api/cm/findByPro',//sourceType=2 连线
- type:'GET',
- dataType: 'json',
- data: function(){
- return {
- sourceType : 2,
- sourceId: linkId
- };
- },
- results: function (data) {
- res=data;
-
- var bindData = [];
- for(var i = 0;i < data.length;i++){
- bindData.push({'id':data[i].key,'text':data[i].descName});
- }
-
- return {results: bindData};
- }
- },
- createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
-
- }).on("select2-selecting", function(e) {
-
- etdBtnId=this.id.substr('selectLink'.length);
- var result = [];
- for(var i = 0;i < res.length;i++){
- result.push({'id':res[i].key,'text':res[i].descName});
- }
-
- if(result.length==0){//如果没有属性 弹出添加属性框
- //置空
- d3.select("#proType").property('value','');
- d3.select("#proNo").property('value','');
-
- d3.select('#overlay').style('display','block');
- d3.select('#win').style('display','block');
- return;
- }
-
- //console.log ("selecting val="+ e.val+" choice="+ JSON.stringify(e.choice));
- for(var i=0;i<result.length;i++){
- if(e.choice.id==result[i].id && e.choice.text==result[i].text){//用户选择的是已有的属性
- //nodes props
- var props=selected_link.props;
- // 用户选择不能重复
- for(var key in props){
- if(e.val==key && e.val!=etdId){
-
- jAlert("此资产已有此属性,请另外选择!",'提示',function(){
- //绑定默认值
- $('#selectLink'+etdId).select2('data', {id:etdId,text:value});
- });
-
- return;
- }
- }
- //用户选择验证通过 写入到nodes中
- for(var p in props){
- if(p==etdId){
- delete props[etdId];
- selected_link.props[e.choice.id]=e.choice.text;
-
- showLink();//此处不重新加载(shownode) 会有问题
- return;
- }
- }
-
- }else if((i==result.length-1) && (e.choice!=result[i])){//用户新输入的
-
- //置空
- d3.select("#proType").property('value','');
- d3.select("#proNo").property('value','');
-
- d3.select('#overlay').style('display','block');
- d3.select('#win').style('display','block');
- return;
- }
- }
- });;
-
- //绑定默认值
- $('#selectLink'+etdId).select2('data', {id:etdId,text:value});
-
- }else{//确定
- d3.select('#edtBtnLink'+etdId).property('name','edtBtn');
- d3.select('#edtBtnLink'+etdId).attr('style','background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;');
- //数据写入links
- var props=selected_link.props;
- for(var key in props){
- if(key==etdId){
- var k=null;
- var text=d3.select('#inputLink'+etdId).property('value');
- if(k==0){
- k=d3.select('#selectLink'+etdId).property('value');
- }else{
- k=etdId;
- }
-
- delete props[etdId];
- props[k]=text;
- showLink();
- return;
- }
- }
- showLink();
- }
- }
- //节点属性删除按钮点击事件
- function delBtnClk(delId){
- jConfirm('确定删除?', '提示', function(r) {
- if(r){
- //提交的时候只删除数据,不删除属性表数据
- //删除表格
- d3.select('#tr'+delId).selectAll('td').remove('td');
- //删除node属性
- var props=selected_node.props;
- for(var key in props){
- if(delId==key){
- delete props[key];//删除node中元素
- return;
- }
- }
- }
- });
-
- }
- //连线属性删除按钮点击事件
- function delBtnLinkClk(delId){
- jConfirm('确定删除?', '提示', function(r) {
- //提交的时候只删除数据,不删除属性表数据
- if(r){
- //删除表格
- d3.select('#trLink'+delId).selectAll('td').remove('td');
- //删除node属性
- var props=selected_link.props;
- for(var key in props){
- if(delId==key){
- delete props[delId];//删除link中元素
- return;
- }
- }
- }
- });
- }
- //连线名 的选择事件 选择后将 值写入到 selected_node
- d3.select('#linkName').on('change',function(){
- var num=0;
- for(var key in selected_link.props){
- num++;
- break;
- }
- var tType=this;
- if(num>0){
- jConfirm('更换名称选项,动态属性将会被删除,是否继续?', '提示', function(r) {
- if(r){
- selected_link.name=tType.value;
- selected_link.props={};
- showLink();
-
- selected_link['props']={};
- fixPro(tType.value);
- }else{
- tType.value=selected_link.name;
- return;
- }
- });
-
-
- }else{
- selected_link.name=tType.value;
- selected_link['props']={};
- fixPro(tType.value);
- }
- });
- //加载固定属性(节点/连线)
- function fixPro(label){
- if(selected_node){//操作节点
- var labelId=null;
- //根据选择‘节点’的label 查询label的ID
- d3.xhr(window.basePath+"/api/cm/findLabel?sign="+label).send("GET",
- function(error, json) {
- if (!error){
- labelId =JSON.parse(json.responseText).id;
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cm/findByPro",
- data: {sourceType : 1,sourceId: labelId},
- dataType: "json",
- success:function(data){
-
- var keys=[];
- for(var i = 0;i < data.length;i++){
- var entity=data[i];
- if(entity.fix==1){
- var key=entity.key;
- keys.push(key);
- selected_node['props'][key]='';
- }
- }
- //先移除div中原来的标签
- d3.select('#nodeTab').selectAll('tr').remove('tr');
- d3.select('#nodeTab').selectAll('tr')
- .data(keys).enter()
- .append('tr')
- .attr('id',function(d){
- return 'tr'+d;
- })
- .html(function(d){ //undefined
- var str="";
- //新增加行
- var strS="";
- for(var i = 0;i < data.length;i++){
- var entity=data[i];
- if(entity.fix==1 && entity.key==d){//固态属性 并且key相等
-
- if(entity.valueType=="date"){
- strS='<input id="input'+d+'" placeholder="请输入日期" style="width: 151px;" class="laydate-icon" onClick="laydate({istime: true, format: \'YYYY-MM-DD hh:mm:ss\'})" value="'
- +'" disabled>';
- }else{
- strS='<input type="text" id="input'+d+'" value="" disabled/>';
- }
- //类型设置 读取JSON string date int float
- str+='<td style="padding-left: 0px;width:145px;"><span id="span'
- +d+'">'+entity.descName+':</span></td>'+
- '<td style="padding-left: 21px;">'+strS+'</td><td>'+'<input id="edtBtn'+d
- +'" name="edtBtn" type="button" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnClk(\''
- +entity.valueType+'\',\''+d+'\',\''+entity.descName+'\',\''+entity.fix+'\')" />'+' </td>';
- break;
- }
- }
-
- return str;
-
- });
- },
- error:function(data){jAlert("服务器访问异常!",'提示');}
- });
- }
- });
- }else if(selected_link){//操作连线
- var sourceId=null;
- for(var i=0;i<linkType.length;i++){
- if(label==linkType[i].type){
- sourceId=linkType[i].id;
- }
- }
- if(sourceId!=null){
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cm/findByPro",
- data: {sourceType : 2,sourceId: sourceId},
- dataType: "json",
- success:function(data){
-
- var keys=[];
- for(var i = 0;i < data.length;i++){
- var entity=data[i];
- if(entity.fix==1){
- var key=entity.key;
- keys.push(key);
- selected_link['props'][key]='';
- }
- }
- //先移除div中原来的标签
- d3.select('#linkTab').selectAll('tr').remove('tr');
- d3.select('#linkTab').selectAll('tr')
- .data(keys).enter()
- .append('tr')
- .attr('id',function(d){
- return 'tr'+d;
- })
- .html(function(d){ //undefined
- var str="";
- //固态属性
- var strS="";
- for(var i = 0;i < data.length;i++){
- var entity=data[i];
- if(entity.fix==1 && entity.key==d){//固态属性 并且key相等
- if(entity.valueType=="date"){
- strS='<input id="inputLink'+d+'" placeholder="请输入日期" style="width: 151px;" class="laydate-icon" onClick="laydate({istime: true, format: \'YYYY-MM-DD hh:mm:ss\'})" value="" disabled>';
- }else{
- strS='<input type="text" id="inputLink'+d+'" value="" disabled/>';
- }
- //类型设置 读取JSON string date int float
-
- str+='<td style="padding-left: 0px;width:145px;"><span id="spanLink'+d+'">'+entity.descName+':</span></td>'+
- '<td style="padding-left: 21px;">'+strS+'</td><td>'+'<input id="edtBtnLink'+d
- +'" type="button" name="edtBtn" style="background-image:url(css/img/tup/xiug.jpg);width:22px;height:22px;" onclick="edtBtnLinkClk(\''
- +entity.valueType+'\',\''+d+'\',\''+entity.descName+'\',\''+entity.fix+'\')" />'
- +' </td>';
- break;
- }
- }
- return str;
-
- });
- },
- error:function(data){jAlert("服务器访问异常!",'提示');}
- });
- }
- }
-
- }
- //节点'添加属性' 点击事件
- d3.select('#addBtn').on('click',function(){
- var label=d3.select('#nodeLabel').property('value');
-
- if(""!=label){
- addRow();
- }else{
- jAlert('请先选择资产类型!','提示');
- }
-
- });
- //添加属性行
- function addRow(){
- if(selected_node.props){
-
- selected_node.props['']='未定义';
- }else{
-
- selected_node['props']={'':'未定义'};
- }
- //用户写入的记录下来
- updJson();
-
- shownode();
- }
- //刷新时记录用户输入
- function updJson(){
- if(selected_node){
- selected_node.assetNo=d3.select("#nodeAssetNo").property('value');
- selected_node.name=d3.select("#nodeName").property('value');
- selected_node.type=d3.select("#nodeType").property('value');
- selected_node.label=d3.select("#nodeLabel").property('value');
- selected_node.status=d3.select("#nodeStatus").property('value');
- }else if(selected_link){
- selected_link.name=d3.select("#linkName").property('value');
- selected_link.cost=d3.select("#linkCost").property('value');
- }
- }
- //=============弹出层添加属性begin=======================================
- //加载属性类型
- d3.select('#proType').selectAll('option')
- .data(proTypes).enter()
- .append('option')
- .attr('value',function(d){return d.key;})
- .html(function(d){ return d.value;});
- //提交
- d3.select('#addSubBtn').on('click',function(){
- if(selected_node){
- var proType=d3.select("#proType").property('value');
- var proNo=d3.select("#proNo").property('value');
- var desName=d3.select("#select"+etdBtnId).property('value');//正在编辑select2的值
- //插入前先查询节点‘英文编号’的此label 此简称是否存在
- var t=d3.select('#nodeLabel').property('value');
- var labelId="";//选中的资源label的ID
- for(var i=0;i<labels.length;i++){
- if(labels[i].sign==t){
- labelId=labels[i].id;
- break;
- }
- }
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cm/findByPros",
- data: {sourceType : 1,sourceId: labelId,keyname:proNo},
- dataType: "text",
- success:function(data){
- if(data==0){//数据库中没有此属性 允许插入
- //插入操作 返回实体
- $.ajax({
- type: "POST",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cm/dynparam",
- data: JSON.stringify({ descName:desName, key:proNo, sourceId:labelId, sourceType:1, valueType:proType,fix:0}),//JSON.stringify(entity),fix:0动态属性
- dataType: "json",
- success:function(data){
- //插入成功后改变正在编辑的
- var props=selected_node.props;
- for(var key in props){
- if(key==etdBtnId){
- delete props[key];
- props[proNo]=desName;
-
- //层隐藏
- d3.select('#overlay').style('display','none');
- d3.select('#win').style('display','none');
- //更新属性
- initParams();
- restart();
- return;
- }
- }
-
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
-
- }else{
- jAlert("此属性编号已存在,请重新输入编号!",'提示');
- }
- },
- error:function(data){jAlert("服务器访问异常!",'提示');}
- });
- }else if(selected_link){
- var proType=d3.select("#proType").property('value');
- var proNo=d3.select("#proNo").property('value');
- var desName=d3.select("#selectLink"+etdBtnId).property('value');//正在编辑select2的值
-
- //插入前先查询节点‘英文编号’的此label 此简称是否存在
-
- var linkeName=d3.select('#linkName').property('value');
- var linkId;//选中的name的ID 即属性表里的sourceId
- for(var i=0;i<linkType.length;i++){
- if(linkeName==linkType[i].type){
- linkId=linkType[i].id;
- }
- }
- $.ajax({
- type: "GET",
- url: window.basePath+"/api/cm/findByPros",
- data: {sourceType : 2,sourceId: linkId,keyname:proNo},
- dataType: "text",
- success:function(data){
- if(data==0){//数据库中没有此属性 允许插入
- //插入操作 返回实体
- $.ajax({
- type: "POST",
- contentType:"application/json; charset=UTF-8",
- url: window.basePath+"/api/cm/dynparam",
- data: JSON.stringify({ descName:desName, key:proNo, sourceId:linkId, sourceType:2, valueType:proType,fix:0}),//JSON.stringify(entity),
- dataType: "json",
- success:function(data){
- //插入成功后改变正在编辑的
- //params=JSON.parse(json.responseText);
- var props=selected_link.props;
- for(var key in props){
- if(key==etdBtnId){
- delete props[key];
- props[proNo]=desName;
- //层隐藏
- d3.select('#overlay').style('display','none');
- d3.select('#win').style('display','none');
- //更新属性
- initParams();
- return;
- }
- }
- },
- error: function(data){jAlert("服务器请求异常",'提示');}
- });
-
- }else{
- jAlert("此属性编号已存在,请重新输入编号!",'提示');
- }
- },
- error:function(data){jAlert("服务器访问异常!",'提示');}
- });
-
-
- }
- });
- //取消
- d3.select('#addResBtn').on('click',function(){
- d3.select('#overlay').style('display','none');
- d3.select('#win').style('display','none');
- });
- //关闭
- d3.select('#close').on('click',function(){
- d3.select('#overlay').style('display','none');
- d3.select('#win').style('display','none');
- });
- //=================添加属性end==============================================
- //连线'添加属性' 点击事件
- d3.select('#addBtn1').on('click',function(){
- addRow1();
-
- });
- //添加节点
- function addRow1(){
- if(selected_link){
- if(selected_link.props){
- selected_link.props['']='未定义';
- }else{
- selected_link['props']={'':'未定义'};
- }
- updJson();
- showLink();
- }
- }
- function cancel(){
- for(var i=0;i<links.length;i++){//确保用户不提交的时候不出错
- if(links[i].id==-1){
- // delete links[i];
- links.splice(i, 1);
- break;
- }
- }
- };
- //取消1
- d3.select('#addResBtn1').on('click',function(){
- d3.select('#overlay1').style('display','none');
- d3.select('#win1').style('display','none');
- });
- //数据导入
- d3.select("#importData").on("click",function(){
- d3.select('#overlay1').style('display','block');
- d3.select('#win1').style('display','block');
- });
- //上传
- d3.select("#importBtn").on("click",function(){
- var name=d3.select("#importFile").property('value');
- var suffix=name.substring(name.lastIndexOf(".")+1,name.length);
-
- if(suffix!="xls" && suffix!="xlsx"){
- jAlert("请选择EXCEL文件!",'提示');
- return;
- }else{
- var form=$("#myform")[0];
- form.method="POST";
- form.action=window.basePath +"/api/cm/importFile";
- form.submit();
- jAlert("上传成功!",'提示');
- d3.select('#overlay1').style('display','none');
- d3.select('#win1').style('display','none');
- }
- });
- }]);
|