標籤:
概述
資料元素是資料模型的基本要素,用於描述圖形網元,業務網元,或者純資料。TWaver HTML5中所有資料元素都繼承自twaver.Data。為不同功能的需求,預定義了三類資料類型:twaver.Element,twaver.Alarm,twaver.Layer,分別用來描述拓撲的網元,警示和圖層。其中拓撲網元擴充定義了十幾種網元類型,用以描述豐富的拓撲網元特性,其中最常用的幾類拓撲網元包括:Node、Link、Group、SubNetwork、Grid等,TWaver中網元的繼承關係如,本章將詳細介紹這些網元以及其他資料類型的特性,使用以及擴充應用。
基本資料元素(Data)
twaver.Data是TWaver HTML5中最基本的資料元素,預設定義了id,name,name2,icon,toolTip,parent,children等基本屬性,支援事件派發和監聽,由它擴充出來還有twaver.Element,twaver.Alarm,twaver.Layer等資料類型。
事件派發與監聽
twaver.Data繼承於twaver.PropertyChangeDispatcher類,內部包含一個twaver.EventDispatcher執行個體對象,這使得Data具有派發事件和監聽事件的功能,可以通過調用下面的方法派發事件或者添加監聽器:
//派發屬性變化事件firePropertyChange:function(property,oldValue,newValue)//添加屬性變化監聽器addPropertyChangeListener:function(listener,scope,ahead)//刪除屬性變化監聽器removePropertyChangeListener:function(listener)//屬性發生變化的處理方法onPropertyChanged:function(listener)
基本屬性
定義了一些基本屬性,包括id,name,name2,icon,toolTip等。
/** *id:網元的id,如果未設定,TWaver內部會分配唯一的Id。 */twaver.Data:function(id) getId:function()setName:function(value)getName:function()setName2:function(value)getName2:function()setIcon:function(value)getIcon:function()setToolTip:function(value)getToolTip:function()
Note:name2為新增加的屬性,增大了label設定的靈活性。
如果需要設定其他屬性,可以通過setClient()/getClient()方法設定自訂的屬性(包括對象)。
//設定Client屬性;setClient = function(clientProp,newValue)//擷取Client屬性getClient = function(clientProp)
Note:setClient()存放資料類似於Java中的HashMap。
樣本:
var box = new twaver.ElementBox(); var network = new twaver.vector.Network(box); function init() { initNetwork(); initDataBox(); } function initNetwork() { var view = network.getView(); document.body.appendChild(view); network.adjustBounds({x: 0, y: 0, width: 1300, height: 600}); network.getToolTip = function (element) { var Id = element.getId(); var name = element.getName(); var name2 = element.getName2(); var icon = element.getIcon(); var clientProperty = element.getClient(‘clientProperty‘); return ‘Id:‘ + Id + ‘ ‘ + ‘name:‘ + name + ‘ ‘ + ‘name2:‘ + name2+ ‘ ‘ + ‘icon:‘ + icon; } twaver.Styles.setStyle(‘label.color‘,‘#ec6c00‘); twaver.Styles.setStyle(‘label2.color‘,‘#57ab9a‘); twaver.Styles.setStyle(‘select.color‘,‘#ef8200‘); } function initDataBox() { var node = new twaver.Node({ name: ‘from‘, name2: ‘from2‘, location: { x: 300, y: 200 } }); box.add(node); var node2 = new twaver.Node({ name: ‘to‘, name2: ‘to2‘, location: { x: 500, y: 250 } }); box.add(node2); var link = new twaver.Link(node, node2); link.setName(‘Hello TWaver!‘); link.setName2(‘Hello Vector!‘); link.setClient(‘clientProperty‘,node); box.add(link); }
Note:從樣本中我們學會了什嗎?
- 統一設定樣式,如:twaver.Styles.setStyle(‘label.color’,’#ec6c00′)
- 自訂toolTip的提示:重寫network.getToolTip方法
- Client屬性的使用
- TWaver內部內建一些顏色,twaver.Colors.***
在這我們特別列舉出icon的布局問題,icon作為網元的附件可以圍繞在網元的周圍,呈現一些特殊資訊。TWaver支援多組icon同時存在,且擺放的位置可以分別設定。下面列舉使用方法:
function registerImage(){ registerNormalImage(‘./images/list_view.png‘,‘list_view‘); registerNormalImage(‘./images/settings1.png‘,‘setting1‘); registerNormalImage(‘./images/ic_mech_wheel.png‘,‘wheel‘); registerNormalImage(‘./images/ic_search_archive.png‘,‘archive‘); registerNormalImage(‘./images/super_mono.png‘,‘mono‘); registerNormalImage(‘./images/twitter_04.png‘,‘twitter‘);}function registerNormalImage(url, name) { var image = new Image(); image.src = url; image.onload = function() { twaver.Util.registerImage(name, image, image.width, image.height); image.onload = null; network.invalidateElementUIs(); };}function initNode() { var node2 = new twaver.Node("node2"); node2.setLocation(450, 205); box.add(node2); node2.setName(‘name1‘); node2.setName2(‘name2‘); node2.setSize(300,300); node2.setStyle(‘icons.names‘, [["mono","wheel",‘archive‘],["wheel",‘archive‘,‘mono‘],["archive",‘mono‘],["mono",‘wheel‘]]); node2.setStyle(‘icons.position‘, [‘topleft.topleft‘,‘topright.topright‘,‘bottomleft.bottomleft‘, ‘bottomright.bottomright‘]); node2.s(‘icons.orientation‘,[‘top‘,‘left‘,‘right‘,‘bottom‘]);}
其他功能函數
此外,Data中還定義了其他功能函數
//擷取所有子網元getChildren:function()getChildSize:function()//擷取符合macthFunction的所有childs組成的ListtoChildren:function(macthFunction,scope)addChild:function(child,index)removeChild:function(child)getChildAt:function(index)clearChildren:function()getParent:function()setParent:function(parent)hasChildren:function()isRelatedTo:function(data)isParentOf:function(data)isDescendantOf:function(data)
//toChildren:function(macthFunction,scope)的使用方法parent.toChildren(function(e){ return e.getName() === ‘vector‘;});
var box = new twaver.ElementBox(); var network = new twaver.vector.Network(box); function init() { initNetwork(); registerImage(); initDataBox(); } function initNetwork() { var view = network.getView(); document.body.appendChild(view); network.adjustBounds({x: 0, y: 0, width: 1300, height: 600}); twaver.Styles.setStyle(‘select.color‘, ‘#57ab9a‘); }function registerImage() { //register shadow twaver.Util.registerImage(‘shadow‘, { w: 37, h: 29, shadowOffsetX: 0, shadowOffsetY: 0, shadowBlur: 5, shadowColor: ‘#ec6c00‘, v: [ { shape: ‘vector‘, name: ‘node_image‘, x: 0, y: 0 } ] }); }function initDataBox() { var parent = new twaver.Group({ name: ‘parent‘, location: {x: 300, y: 400 }, }); box.add(parent); var node1 = new twaver.Node({ name: ‘Jeff.fu‘, location: { x: 200, y: 200 } }); node1.setClient(‘vector‘, true); box.add(node1); var node2 = new twaver.Node({ name: ‘alex.dong‘, location: { x: 500, y: 350 } }); node2.setClient(‘vector‘, true); box.add(node2); var node3 = new twaver.Node({ name: ‘paul.peng‘, location: { x: 200, y: 350 } }); box.add(node3); var node4 = new twaver.Node({ name: ‘vic.cheng‘, location: { x: 500, y: 200 } }); box.add(node4); var link = new twaver.Link(node1, node2); link.setName(‘link1‘); link.setStyle(‘label.position‘,‘topleft.topleft‘); box.add(link); var link2 = new twaver.Link(node3, node4); link2.setName(‘link2‘); box.add(link2); parent.addChild(node1); parent.addChild(node2); parent.addChild(node3); parent.addChild(node4); matchFunction = function (e) { if (e.getClient(‘vector‘)) { return true; } } var childrenMatch = parent.toChildren(matchFunction); childrenMatch.forEach(function (element) { element.setImage(‘shadow‘); element.setName(‘child_vector‘); }); }
Note:上述樣本我們學會了什嗎?
- toChildren()方法的使用
- shadow陰影的設定
TWaver HTML5 (2D)----資料元素