百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 文章教程 > 正文

7.在 JavaScript 中扩展 mxGraph(javascript扩展代码)

yund56 2025-07-17 00:03 3 浏览

2.2.1.3 Extending mxGraph in JavaScript/ 在 JavaScript 中扩展 mxGraph

[翻译]
在 JavaScript 中,有多种方式将面向对象范式映射到语言结构。mxGraph 在整个项目中使用了一种特定的方案,遵循以下隐式规则:

  • 不更改内置原型。
  • 不尝试限制 JavaScript 语言的功能。

[原文]
In JavaScript, there are various ways of mapping the Object Oriented paradigm to language constructs. mxGraph uses a particular scheme throughout the project, with the following implicit rules:

  • Do not change the built-in prototypes
  • Do not try to limit the power of the JavaScript language.

  • mapping /'maep/ 映射
  • paradigm /'paerdam/ 范式
  • constructs /'knstrkts/ 结构
  • implicit /m'plst/ 隐式的
  • prototypes /'prottaps/ 原型

[翻译]
mxGraph 中有两种类型的“类”:类和单例(只有一个类实例存在)。单例映射到全局对象,其中变量名称与类名称相同。例如,mxConstants 是一个对象,所有常量定义为对象字段。普通类映射到构造函数和定义实例字段和方法的原型。例如,mxEditor 是一个函数,mxEditor.prototype 是 mxEditor 函数创建的对象的原型。mx 前缀是 mxGraph 包中所有类的命名约定,以避免与全局命名空间中的其他对象冲突。

[原文]
There are two types of “classes” in mxGraph; classes and singletons (where only one instance of the class exists). Singletons are mapped to global objects where the variable name is the same as the class name. For example, mxConstants is an object with all the constants defined as object fields. Normal classes are mapped to a constructor function and a prototype which defines the instance fields and methods. For example, mxEditor is a function and mxEditor.prototype is the prototype for the object that the mxEditor function creates. The mx prefix is a convention that is used for all classes in the mxGraph package to avoid conflicts with other objects in the global namespace.

  • singletons /'sɡltnz/ 单例
  • mapped /maept/ 映射
  • constants /'knstnts/ 常量
  • prototype /'prottap/ 原型
  • conflicts /'knflkts/ 冲突

[翻译]
对于子类化,超类必须提供一个无参数或能处理无参数调用的构造函数。此外,在扩展原型后必须重新定义特殊的构造函数字段。例如,mxEditor 的超类是 mxEventSource。这在 JavaScript 中通过首先“继承”超类的所有字段和方法来表示,例如通过将原型分配给超类的一个实例:

[原文]
For subclassing, the superclass must provide a constructor that is either parameterless or handles an invocation with no arguments. Furthermore, the special constructor field must be redefined after extending the prototype. For example, the superclass of mxEditor is mxEventSource. This is represented in JavaScript by first “inheriting” all fields and methods from the superclass by assigning the prototype to an instance of the superclass, eg.

  • subclassing /'sbklaes/ 子类化
  • superclass /'suprklaes/ 超类
  • constructor /kn'strktr/ 构造函数
  • invocation /nv'ken/ 调用
  • inheriting /n'hert/ 继承
mxEditor.prototype = new mxEventSource()

[翻译]并使用以下方式重新定义构造函数字段:

[原文] and redefining the constructor field using:

mxEditor.prototype.constructor = mxEditor

[翻译]
后者规则的应用是为了通过 mxUtils.getFunctionName(obj.constructor) 获取对象的构造函数名称来检索对象的类型。

[原文]
The latter rule is applied so that the type of an object can be retrieved via the name of it’s constructor using mxUtils.getFunctionName(obj.constructor).

  • retrieved /r'trivd/ 检索
  • constructor /kn'strktr/ 构造函数

Constructor/构造函数

[翻译]
在 mxGraph 中进行子类化时,应应用相同的机制。例如,要对 mxGraph 类进行子类化,首先必须为新类定义一个构造函数。构造函数使用 mxGraph 函数对象上的 call 方法调用超构造函数,明确传递所有参数:

[原文]
For subclassing in mxGraph, the same mechanism should be applied. For example, for subclassing the mxGraph class, first a constructor must be defined for the new class. The constructor calls the super constructor with any arguments that it may have using the call function on the mxGraph function object, passing along explicitly each argument:

  • mechanism /'meknzm/ 机制
  • constructor /kn'strktr/ 构造函数
  • super /'supr/ 超
  • arguments /'ɑrɡjmnts/ 参数
  • explicitly /k'splstli/ 明确地
function MyGraph(container)  
{  
mxGraph.call(this, container);  
}  

[翻译] MyGraph 的原型继承自 mxGraph,具体如下。通常,在扩展超类之后,会重新定义构造函数:

[原文] The prototype of MyGraph inherits from mxGraph as follows. As usual, the constructor is redefined after extending the superclass:

MyGraph.prototype = new mxGraph();  
MyGraph.prototype.constructor = MyGraph;

[翻译]
您可能希望在上述代码后定义与该类关联的编解码器(参见手册的 I/O 部分)。此代码将在类加载时执行,确保使用相同的编解码器对 mxGraph 和 MyGraph 的实例进行编码。

[原文]
You may want to define the codec associated for the class after the above code (see I/O section of manual). This code will be executed at class loading time and makes sure the same codec is used to encode instances of mxGraph and MyGraph.

  • codec /'kodek/ 编解码器
  • associated /'sosietd/ 关联的
  • executed /'ekskjutd/ 执行
  • encode /n'kod/ 编码
  • instances /'nstnsz/ 实例
var codec = mxCodecRegistry.getCodec(mxGraph);  
codec.template = new MyGraph();  
mxCodecRegistry.register(codec);

Functions/函数

[翻译]
在 MyGraph 的原型中,可以按以下方式扩展 mxGraph 的函数:

[原文]
In the prototype for MyGraph, functions of mxGraph can be extended as follows.

  • prototype /'prottap/ 原型
  • extended /k'stendd/ 扩展
MyGraph.prototype.isSelectable = function(cell)  
{  
var selectable = mxGraph.prototype.isSelectable.apply(this, arguments);  
var geo = this.model.getGeometry(cell);  
return selectable &&(geo == null || !geo.relative);  
}  

[翻译]
第一行的超类调用是可选的。它使用 mxGraph 原型的 isSelectable 函数对象上的 apply 函数完成,使用特殊的 this 和 arguments 变量作为参数。只有在超类中函数未被替换的情况下,调用超类函数才可能,如下所示,这是 JavaScript 中“子类化”的另一种方式。

[原文]
The supercall in the first line is optional. It is done using the apply function on the isSelectable function object of the mxGraph prototype, using the special this and arguments variables as parameters. Calls to the superclass function are only possible if the function is not replaced in the superclass as follows, which is another way of “subclassing” in JavaScript.

  • supercall /'suprkl/ 超类调用
  • optional /'ɑpnl/ 可选的
  • apply /'pla/ 应用
  • replaced /r'plest/ 替换
  • subclassing /'sbklaes/ 子类化
mxGraph.prototype.isSelectable = function(cell)  
{  
var geo = this.model.getGeometry(cell);  
return selectable && (geo == null || !geo.relative);  
}  

[翻译]
上述方案在需要完全替换函数定义时很有用。

[原文]
The above scheme is useful if a function definition needs to be replaced completely.

  • scheme /skim/ 方案
  • definition /def'nn/ 定义
  • replaced /r'plest/ 替换

[翻译]
为了向子类添加新函数和字段,使用以下代码。下面的示例添加了一个新函数以返回图模型的 XML 表示:

[原文]
In order to add new functions and fields to the subclass, the following code is used. The example below adds a new function to return the XML representation of the graph model:

  • subclass /'sbklaes/ 子类
  • fields /fildz/ 字段
  • representation /reprzen'ten/ 表示
MyGraph.prototype.getXml = function()  
{  
var enc = new mxCodec();  
return enc.encode(this.getModel());  
}  

Fields/字段

[翻译]
同样,声明和定义新字段如下:

[原文]
Likewise, a new field is declared and defined as follows:

  • declared /d'klerd/ 声明
  • defined /d'fand/ 定义
MyGraph.prototype.myField = ‘Hello, World!’;

[翻译]
请注意,分配给 myField 的值只创建一次,即 MyGraph 的所有实例共享相同的值。如果需要特定于实例的值,则必须在构造函数中定义该字段。例如:

[原文]
Note that the value assigned to myField is created only once, that is, all instances of MyGraph share the same value. If you require instance-specific values, then the field must be defined in the constructor instead. For example:

  • assigned /'sand/ 分配
  • instances /'nstnsz/ 实例
  • constructor /kn'strktr/ 构造函数
function MyGraph(container)  
{  
mxGraph.call(this, container);  
this.myField = [];  
}  

[翻译]
最后,使用以下代码创建 MyGraph 的新实例,其中 container 是作为图视图容器的 DOM 节点:

[原文]
Finally, a new instance of MyGraph is created using the following code, where container is a DOM node that acts as a container for the graph view:

  • instance /'nstns/ 实例
  • DOM /di o 'em/ 文档对象模型
  • container /kn'tenr/ 容器
var graph = new MyGraph(container);

相关推荐

柚墨个人简历Word模板分享(柚墨ppt)

1、棕色商务风餐饮店长个人通用简历:http://www.yomoer.cn/template/detail/7801.html2、棕色商务风猎头个人通用简历:http://www.yomoer.cn...

纯粹的 Prompt 优化:输出HTML一步到位, 简历诊断 AI 小程序揭秘

最近我琢磨着,能不能搞一个超便捷的简历诊断工具,用户只需上传简历和岗位要求截图,AI就能一步到位生成HTML报告,这想法是不是听着就带劲?现在,我把这整个过程拆解出来,跟大家分享一下,保证干货满...

代码式动态录入生成个人简历页面html页面前端源码

大家好,今天给大家介绍一款,代码式动态录入生成个人简历页面html页面前端源码(图1)。送给大家哦,获取方式在本文末尾。整个页面分为两个部分,左右布局,左边动态输入一个代码编辑器,根据输入的代码动态变...

Web前端开发,HTML超链接标签,不懂的可以学习一下

一、什么是HTML的超链接大家平时浏览的网页中都可以找到链接。点击链接就可以从一个页面跳转到另一个页面。HTML超链接可以是一个字,一个词,或者一组词,也可以是一幅图像。可以点击这些内容来跳转到新的文...

HTML-列表标签(双标签) 208(html项目列表标签)

列表标签有3种:1)dl,dd与dt(定义列表)2)ol与li(有序列表)3)ul与li(无序列表)前两者一般不怎么用,网页中运用最多的是第三种1)<dl><dd>与<...

html开发笔记06- 字体标签和文字标签

1、字体标签:用于在页面中加粗、倾斜、加下划线等操作(不推荐)。字体标签分为:<b>加粗</b>,<i>倾斜</i>,<u>下划线&...

Web前端:HTML的10大重要用途(web前端开发html总结)

  HTML是最流行的Web前端开发技术之一,它是一种用于创建网页和Web应用程序的标记语言。HTML与CSS和JavaScript结合使用以创建有吸引力且响应迅速的前端网页。  HTML提供了...

晨间解析!HTML canvas 标签面试题,绘图知识轻松掌握

当清晨的阳光如笔尖般轻轻划过窗台,泡一杯温润的金骏眉,坐在桌前翻开这篇文章——此刻的学习,就像在网页的“电子画布”上从容调色,让HTML中<canvas>标签的知识点如晨光般细...

想学 HTML,不知从何入手?看完这篇文章你就知道了

很多人都说HTML是一门很简单的语言,看看书,看看视频就能读懂。但是,如果你完全没有接触过,就想通过看一遍教程,背背标签,想要完全了解HTML,真的有点太天真了。HTML中文“超文本标记语言”,英文名...

B端设计|页面标签的认识和实操应用

既然浏览器中可以使用页面标签,那为什么在项目中还需要使用这个组件和交互框架的形式呢?本文IE等浏览器界面为例,介绍了页面标签在产品设计中的作用,以及它们在不同使用场景当中的效果。希望能帮助你对页面标签...

视频号超链接怎么设置能成功?核心要点:标签代码格式

视频号超链接怎么设置能成功?坦白来讲,成功背后的方法很简单,唯一需要注意的点,就是<a>标签代码背后对应的格式。前两天一朋友跟我说:为什么视频号超链接设置完以后,在用户端看测试效果时,显示...

那些容易被你忽略的HTML重要属性,你知道几个?

前言在前端开发编写html文件的时候,我们可能会很熟练的写出常见的html元素,但是如果问到某些元素的差别时,大家不一定能说的出来,今天就给大家总结一下那些很常见但容易混淆的属性。html与csscs...

「测试开发全栈-HTML」(18) label标签的使用

说完了标签属性后,接着说下HTML中最后的标签--label<label>标签为input元素定义标注,用于绑定一个表单元素,当点击一个<label>标签内的文本时,浏览器就会...

Markdown 各种标签说明介绍(markdownhere)

介绍主要介绍各种Markdown的标签。汇总一下自己在Markdown中编辑中的常用标签。本篇内容的标签,全部是在Typora的环境下自测使用过。1.基础标签主要介绍Markdown中的一些基础标签,...

10 个罕见的 HTML 标签,几乎无人使用 - 、等等

HTML的内容远不止<div>、<a>和<p>。如此多更复杂、更强大的标签,你可能从未使用过。具有从交互式图像到复杂的UI组件的有趣功能。1.<...