📄 liucheng2.js
字号:
// JavaScript Document
//画图type=1节点 type=2路由 type=3控制
var gSoft = {
$:function(id){
if(typeof id=="string"||typeof id=="number"){
return document.getElementById(id.toString());
} else if(typeof id=="object"){
return id;
}
return null;
},
method:{
getUniqueId:function(i){
if(typeof gSoft.method.getUniqueId.identity=="undefined"){
gSoft.method.getUniqueId.identity = 0;
//生成id的时候要>页面的上元素的最大id
}
if(typeof i=="undefined"){
return ++gSoft.method.getUniqueId.identity;
} else {
if(gSoft.method.getUniqueId.identity<i){
gSoft.method.getUniqueId.identity = i;
}
return i;
}
},
getUniquezIndex:function(i){
if(typeof gSoft.method.getUniquezIndex.identity=="undefined"){
gSoft.method.getUniquezIndex.identity=i||1000;
}
return ++gSoft.method.getUniquezIndex.identity;
}
}
};
gSoft.flow = function(data){
this.data = data || [];
this.sharps = [];
};
gSoft.flow.prototype = {
getContainer:function(){
return gSoft.$("sharpArea");
},
getType:function(type){
var types = [];
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.type==type){
types.push(o);
}
}
return types;
},
getNodes:function(){
return this.getType(1);
},
getRoutes:function(){
return this.getType(2);
},
getControls:function(){
return this.getType(3);
},
getTypeById:function(id){
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.id===id){
return o;
}
}
return null;
},
//根据开始节点和终止节点得到某条路由
getRouteByNodes:function(firstNode,endNode){
if(firstNode&&endNode){
var firstNodeId = firstNode.id;
var endNodeId = endNode.id;
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.from==firstNodeId&&o.to==endNodeId){
return o;
}
}
return null;
}
return null;
},
//根据路由得到开始节点和终止节点
getNodesByRoute:function(route){
if(!route) return null;
return [this.getTypeById(route.from),this.getTypeById(route.to)];
},
//根据id得到对象
getSharpById:function(id){
for(var i=0,l=this.sharps.length;i<l;i++){
var sharp = this.sharps[i];
if(sharp.id==id){
return sharp;
}
}
return null;
},
//根据开始节点和终止节点得到路由的起始位置和结束位置
getRoutePointsByNodes:function(sharp1,sharp2){
if(sharp1&&sharp2){
return {
from:{x:sharp1.getLeft()+sharp1.getWidth()/2,y:sharp1.getTop()+sharp1.getHeight()},
to:{x:sharp2.getLeft()+sharp2.getWidth()/2,y:sharp2.getTop()}
}
} else if(sharp1){
return {
from:{x:sharp1.getLeft()+sharp1.getWidth()/2,y:sharp1.getTop()+sharp1.getHeight()},
to:{}
}
} else if(sharp2){
return {
from:{},
to:{x:sharp2.getLeft()+sharp2.getWidth()/2,y:sharp2.getTop()}
}
}
},
//根据路由得到控制的位置
getControlPointsByRoute:function(sharp,size){
return {
left:(sharp.from.x+sharp.to.x-size.width)/2,
top:(sharp.from.y+sharp.to.y-size.height)/2
}
},
//根据控制得到路由
getRouteByControl:function(control){
if(!control) return null;
return this.getTypeById(control.route);
},
//根据路由得到控制
getControlByRoute:function(route){
if(!route) return null;
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.type==3&&o.route==route.id){
return o;
}
}
return null;
},
getRoutesByNodeId:function(id){
var routes = {from:[],to:[]};
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.type==2){
if(o.from==id){
var sharp = this.getSharpById(o.id);
routes.from.push({obj:o,sharp:sharp});
} else if(o.to==id){
var sharp = this.getSharpById(o.id);
routes.to.push({obj:o,sharp:sharp});
}
}
}
return routes;
},
getControlSharpByRouteId:function(id){
for(var i=0,l=this.data.length;i<l;i++){
var o = this.data[i];
if(!o) continue;
if(o.type==3&&o.route==id){
return this.getSharpById(o.id);
}
}
return null;
},
drawNodes:function(){
var nodes = this.getNodes();
if(nodes){
for(var i=0,l=nodes.length;i<l;i++){
var node = nodes[i];
if(!node) continue;
var sharp = new gSoft.flow.roundRect(node);
if(sharp){
this.sharps.push(sharp);
sharp.draw();
}
}
}
},
drawRoutes:function(){
var routes = this.getRoutes();
if(routes){
for(var i=0,l=routes.length;i<l;i++){
var route = routes[i];
if(!route) continue;
var nodes = this.getNodesByRoute(route);
if(nodes&&nodes.length==2){
var sharp1 = this.getSharpById(nodes[0].id);
var sharp2 = this.getSharpById(nodes[1].id);
var points = this.getRoutePointsByNodes(sharp1,sharp2);
var sharp = new gSoft.flow.line({
id:route.id,
from:points.from,
to:points.to
});
if(sharp){
this.sharps.push(sharp);
sharp.draw();
}
}
}
}
},
drawControls:function(){
var controls = this.getControls();
if(controls){
for(var i=0,l=controls.length;i<l;i++){
var control = controls[i];
if(!control) continue;
var route = this.getRouteByControl(control);
if(route){
var sharp = null;
var line = this.getSharpById(route.id);
if(line){
var size = {width:10,height:10};
var position = this.getControlPointsByRoute(line,size);
sharp = new gSoft.flow.oval({
id:control.id,
left:position.left,
top:position.top,
width:size.width,
height:size.height,
isDrag:false
});
}
if(sharp){
this.sharps.push(sharp);
sharp.draw();
}
}
}
}
},
draw:function(){
//生成节点
this.drawNodes();
//生成路由
this.drawRoutes();
//生成控制
this.drawControls();
//this.getContainer().innerHTML = this.sharps.join("\r\n");
gSoft.$("tt").value = this.sharps.join("\r\n");
}
};
gSoft.flow.sharp = function(config){
this.config = config || {
fill:{
filled:"true",
color:"#000000"
},
style:{
position:"",
zIndex:gSoft.method.getUniquezIndex(),
left:0,
top:0,
width:0,
height:0
},
stroke:{
color:"#000000",
weight:"1",
dashstyle:"solid",
startArrow:"",
endArrow:""
},
shadow:null,
textbox:null,
path:null
};
this.namespace = this.config.namespace || "v";
this.tag = this.config.tag || "sharp";
//this.id = gSoft.method.getUniqueId(this.config.id);
/*
this.width = this.config.style.width;
this.height = this.config.style.height;
this.left = this.config.style.left;
this.top = this.config.style.top;
*/
var self = this;
this.elements = [];
this.isDrag = true;
}
gSoft.flow.sharp.prototype = {
toString:function(){
return this.getOuterTag();
},
get:function(){
return this;
},
getContainer:function(){
return gSoft.$("sharpArea");
},
getElement:function(){
return gSoft.$(this.id);
},
draw:function(){
this.elements.push(this);
var container = this.getContainer();
if(container){
var parent = document.createElement(this.getTag().join(""));
container.appendChild(parent);
var children = this.getChildrenTag();
for(var i=0,l=children.length;i<l;i++){
var tag = children[i];
if(/^\s*(<[^>]+\s*>)(\S*)(<[^>]+\s*>)\s*$/gi.test(tag)){
var text = RegExp.$2;//alert([RegExp.$1,RegExp.$2,RegExp.$3]);
if(text.replace(/^\s+|\s+$/gi,"")!=""){
//处理文本节点
tag = RegExp.$1+RegExp.$3;
var x = document.createElement(tag)
parent.appendChild(x);
var textNode = document.createTextNode(text);
x.appendChild(textNode);
} else {
parent.appendChild(document.createElement(tag));
}
}
}
gSoft.$("tt").value = this;
this.addDragEvent();
}
},
defaultLocation:{left:0,top:0},
link:function(name,value,separate,sign,sign2){
if(/^\s*$/g.test(value)) return "";
separate = separate || "=";
sign = typeof(sign)=='undefined'?"":sign;
sign2 = typeof(sign2)=='undefined'?"\"":sign2;
return name+separate+sign2+value+sign2+sign;
},
linkNS:function(name,value){
return this.link(name,value,":","","");
},
linkAttr:function(name,value){
return this.link(name,value,"=","","\"");
},
linkStyle:function(name,value){
return this.link(name,value,":",";","");
},
getStyle:function(){
var s="";
for(var p in this.config.style){
s+=this.linkStyle(p,this.config.style[p])+" ";
}
return s;
},
setStyle:function(style){
this.config.style = style;
},
setStroke:function(stroke){
this.config.stroke = stroke;
},
setFill:function(fill){
this.config.fill = fill;
},
setShadow:function(shadow){
this.config.shadow = shadow;
},
setTextBox:function(textbox){
this.config.textbox = textbox;
},
setPath:function(path){
this.config.path = path;
},
getChildTag:function(name,value){
var s="";
if(value){
s+="<"+this.linkNS(this.namespace,name)+" ";
for(var p in value){
s+=this.linkAttr(p,value[p])+" ";
}
s+="></"+this.linkNS(this.namespace,name)+">";
}
return s;
},
getFillTag:function(){
return this.getChildTag("fill",this.config.fill);
},
getShadowTag:function(){
return this.getChildTag("shadow",this.config.shadow);
},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -