百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

8.mxGraph 命名空间与 Hello World 示例实践.md

moboyou 2025-07-21 17:26 12 浏览

2.2.2 General JavaScript Development 常规 JavaScript 开发

2.2.2.1 JavaScript Obfuscation/ JavaScript 混淆

[翻译]
默认情况下,当您将 JavaScript 交付到浏览器客户端时,您将整个 JavaScript 源代码交付给客户端。然后,该 JavaScript 在浏览器中被解释和运行。在客户端运行时,无法对 JavaScript 进行任何程度的加密,因为 JavaScript 源代码必须被 JavaScript 解释器理解,而解释型语言没有二进制中间形式。

[原文]
By default, when you deliver JavaScript to a browser client, you deliver the entire source to that JavaScript. That JavaScript is then interpreted and run on the browser. It is not possible to encrypt the JavaScript to any extent on the client at the point it is run, since the JavaScript source must be understood by the JavaScript interpretor and interpreted languages do not have a binary intermediate form.

  • deliver /d'lvr/ 交付
  • interpreted /n'trprtd/ 解释的
  • encrypt /n'krpt/ 加密
  • interpretor /n'trprtr/ 解释器
  • binary /'banri/ 二进制的

[翻译]
可以在传输中加密 JavaScript,并在客户端解密后运行,但客户端仍然可以在解密后访问源代码。

[原文]
It would be possible to encrypt the JavaScript in transmission and have it decrypted and run on the client, but the client would still be able to access the source after decryption.

  • transmission /traens'mn/ 传输
  • decrypted /di'krptd/ 解密的
  • access /'aekses/ 访问

[翻译]
我们不进行混淆,因为方法名称构成了公共 API,并且输入/输出需要在通信两端理解混淆。

[原文]
We do not obfuscate because the method names form a public API and I/O would need to understand the obfuscation at both communication ends.

  • obfuscate /'ɑbfsket/ 混淆
  • public /'pblk/ 公共的
  • API /e pi 'a/ 应用程序编程接口
  • communication /kmjun'ken/ 通信

2.2.2.2 Namespaces 命名空间

[翻译]
标准 JavaScript 中不存在命名空间的概念,因此在创建新类名称时要格外小心。在 mxGraph 中,所有类都以“mx-”前缀开头,以避免意外冲突或覆盖原型。

[原文]
The concept of namespaces does not exist in standised JavaScript, so take great care when creating new class names. In mxGraph, all of the classes begin with the prefix “mx-”, to avoid clashes or overriding prototypes unintentionally.

  • namespaces /'nemspesz/ 命名空间
  • standardised /'staendrdazd/ 标准化的
  • prefix /'prifks/ 前缀
  • clashes /klaez/ 冲突
  • overriding /ovr'rad/ 覆盖

2.3 Hello World! 你好,世界!

[翻译]
mxGraph 的“你好,世界!”是一个简单的客户端示例,显示两个带标签“Hello”和“World!”的连接顶点。该示例展示了以下内容:

  • 创建一个链接 mxGraph 客户端 JavaScript 的 HTML 页面,
  • 创建一个放置 mxGraph 的容器,
  • 向该图添加所需的单元。

[原文]
Hello World in mxGraph consists of a simple client-side example that displays two connected vertices with the labels “Hello” and “World!”. The example demonstrates the following things:

  • Creating an HTML page that links the mxGraph client JavaScript,
  • Creating a container to place the mxGraph into,
  • Adds the required cells to that graph.

  • consists /kn'ssts/ 包含
  • client-side /'klant sad/ 客户端
  • vertices /'vrtsiz/ 顶点
  • labels /'leblz/ 标签
  • container /kn'tenr/ 容器

[翻译]
示例的源代码 helloworld.html 可以在 mxGraph 的评估版和完整版的示例目录中找到。HTML 源代码包含两个主要部分:头部和主体。这些部分包含以下主要元素,您可以将其视为构建基本 mxGraph 应用程序的模板:

[原文]
The source code for the example, helloworld.html, can be found below and in the examples directory of both the evaluation and full versions of mxGraph. The HTML source contains two main sections, the head and the body. These contain the following main elements that you can consider a template for building a basic mxGraph application:

  • source /srs/ 源代码
  • evaluation /vaelju'en/ 评估
  • sections /'seknz/ 部分
  • head /hed/ 头部
  • template /'templt/ 模板

[翻译]

  • mxBasePath:这是一个 JavaScript 变量,定义了 css、images、resources 和 js 目录所在的目录。它是 JavaScript 代码,需要放在 script 标签内。此变量必须在加载 mxClient.js 之前定义,且不应以斜杠结尾。
  • mxClient.js:这是 mxGraph 库的路径。如果 HTML 文件在本地执行,路径可能是本地计算机或公共互联网路径。如果 HTML 页面从 Web 服务器下载,路径通常是公共互联网路径。
  • 容器创建:在代码底部,在 body 元素中,定义了网页加载时调用的函数(onload 的值)。它传入一个 div 容器作为参数,该容器定义在下方。此 div 是 mxGraph 组件将放置的容器。在本例中,应用了网格背景,常用于图表应用程序。在创建容器时,除背景和容器宽度高度外,未描述图的其他视觉部分。 注意:如果您不希望出现滚动条,应始终使用 overflow:hidden 样式。

[原文]

  • mxBasePath: This is a JavaScript variable that defines the directory within which the css, images, resources and js directories are expected to be found. It is JavaScript code and needs to be placed with in a script tag. This must come before the line loading mxClient.js and should not have a trailing slash.
  • mxClient.js: This is the path to mxGraph library. If the HTML file is executed locally, the path might be local to the computer or a public Internet path. If the html page were downloaded from a web server, the path would generally be a public Internet path.
  • Creation of the container: At the bottom of the code, in the body element, the function that is called on loading the web page is defined (the value of onload). It passes in a div container as a parameter, that is defined underneath. This div is the container the mxGraph component will be placed within. In this example a grid background is applied, as commonly used in diagramming applications. No other part of the graph visuals are described at container creation, other than the background and the container width and height.
    Note that the overflow:hidden style should always be used if you want no scrollbars to appear.

  • variable /'vaeribl/ 变量
  • directory /d'rektri/ 目录
  • script /skrpt/ 脚本
  • trailing /'trel/ 末尾的
  • scrollbars /'skrolbɑrz/ 滚动条

[翻译]

  • 入口函数:文件的主要代码是页面加载时执行的入口方法,在本例中是 JavaScript 代码,必须位于 JavaScript 脚本元素内。任何 mxGraph 应用程序的前几行应检查浏览器是否受支持,如果不支持则适当退出。如果浏览器受支持,则在 div 容器中创建 mxGraph,并在 begin/end 更新调用之间向图添加三个单元。

[原文]

  • The entry function: The main code of the file is the entry method executed on page load in this case. This is JavaScript code and must be within a JavaScript script element. The first lines of any mxGraph application should be to check the browser is supported and exit appropriately if not. If the browser is supported, a mxGraph is created within the div container and three cells are added to the graph between the begin/end update calls.

  • entry /'entri/ 入口
  • executed /'ekskjutd/ 执行
  • appropriately /'propritli/ 适当地
  • supported /s'prtd/ 支持的
  • cells /selz/ 单元

mxGraph 你好世界示例 / The mxGraph HelloWorld example

<html>  
<head>  
   <title>Hello, World! example for mxGraph</title>  

   <!-- Sets the basepath for the library if not in same directory -->  
   <script type="text/javascript">  
      mxBasePath = '../src';  
   </script>  

   <!-- Loads and initializes the library -->  
   <script type="text/javascript" src="../src/js/mxClient.js"></script>  

   <!-- Example code -->  
   <script type="text/javascript">  
      // Program starts here. Creates a sample graph in the  
      // DOM node with the specified ID. This function is invoked  
      // from the onLoad event handler of the document (see below).  
      function main(container)  
      {  
         // Checks if the browser is supported  
         if (!mxClient.isBrowserSupported())  
         {  
            mxUtils.error('Browser is not supported!', 200, false);  
         }  
         else  
         {  
            // Creates the graph inside the given container  
            var graph = new mxGraph(container);  

            // Enables rubberband selection  
            new mxRubberband(graph);  

            // Gets the default parent for inserting new cells. This  
            // is normally the first child of the root (ie. layer 0).  
            var parent = graph.getDefaultParent();  

            // Adds cells to the model in a single step  
            graph.getModel().beginUpdate();  
            try  
            {  
               var v1 = graph.insertVertex(parent, null,  
                        'Hello,', 20, 20, 80, 30);  
               var v2 = graph.insertVertex(parent, null,  
                        'World!', 200, 150, 80, 30);  
               var e1 = graph.insertEdge(parent, null, '', v1, v2);  
            }  
            finally  
            {  
               // Updates the display  
               graph.getModel().endUpdate();  
            }  
         }  
      };  
   </script>  
</head>  

<!-- Page passes the container for the graph to the program -->  
<body onload="main(document.getElementById('graphContainer'))">  

   <!-- Creates a container for the graph with a grid wallpaper -->  
   <div id="graphContainer"  
      style="overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif')">  
   </div>  
</body>  
</html>  

[翻译]
本练习中需要注意的重要概念包括:

  • mxClient.js 是一个将 mxGraph 所有 JavaScript 源代码合并的 JavaScript 文件。从 Web 服务器下载时,将所有 JavaScript 作为一个文件获取比多个单独文件快得多,因为每个文件都需要请求/确认的开销。通常速度提升至少为 2 倍,尽管这取决于服务器与一个客户端保持并行套接字的能力。
  • JavaScript 代码及其依赖项都放置在 head 元素中。
  • Internet Explorer 默认启用了安全选项,当尝试从本地文件系统运行 JavaScript 时会提示用户。此选项可在选项菜单中禁用,但请注意,从本地文件系统运行不是 mxGraph 的部署场景,仅在开发期间可能发生。
  • 您的应用程序可以编写并链接到应用程序中,可以在 HTML 文件内,也可以在单独的 JavaScript 源代码中,通过类似于示例中 mxClient.js 文件的方式链接到 HTML 中。

[原文]
Important concepts to note in this exercise are:

  • mxClient.js is a JavaScript file combining all of the JavaScript source code of mxGraph. When downloading from a web server, obtaining all the JavaScript as one file is much faster than as lots of separate files, due to the overhead of the requests/acknowledgements required for each file. The speed increase is usually at least x2, although it varies with the capacity of the server to have parallel sockets open with one client.
  • The JavaScript code and its dependencies are all placed within the head element.
  • Internet Explorer has, by default, security options enabled that cause a user prompt when attempting to run JavaScript from the local file system. This can be disabled in the options menu, but note that running from the local file system is not a deployment scenario of mxGraph, this would only happen during development.
  • Your application can be written and linked into the application either within the HTML file, or in separate JavaScript source code that is linked into the html in the way the mxClient.js file is in the example.

  • combining /km'ban/ 合并
  • overhead /'ovrhed/ 开销
  • acknowledgements /k'nɑldmnts/ 确认
  • sockets /'sɑkts/ 套接字
  • prompt /prɑmpt/ 提示

相关推荐

python新手学习常见数据类型——数字

Python支持三种不同的数值类型:整型(int)、浮点型(float)、复数(complex)创建数字:a=1b=2.7c=8+4j删除数字:a=1b=2.7c=8+4...

只用一个套路公式,给 Excel 中一列人员设置随机出场顺序

很多同学会觉得Excel单个案例讲解有些碎片化,初学者未必能完全理解和掌握。不少同学都希望有一套完整的图文教学,从最基础的概念开始,一步步由简入繁、从入门到精通,系统化地讲解Excel的各个知...

Excel神技 TIME函数:3秒搞定时间拼接!职场人必学的效率秘籍

你是否经常需要在Excel中手动输入时间,或者从不同单元格拼接时、分、秒?今天我要揭秘一个超实用的Excel函数——TIME函数,它能让你3秒内生成标准时间格式,彻底告别繁琐操作!一、TIME函数基础...

销售算错数被批?97 Excel 数字函数救场,3 步搞定复杂计算

销售部小张被老板当着全部门骂。上季度销售额汇总,他把38652.78算成36852.78,差了1800块。财务对账时发现,整个部门的提成表都得重算。"连个数都算不对,还做什么销售?&...

如何使用Minitab 1分钟生成所需要的SPC数据

打开Minitab,“计算”-“随机数据”-“正太”,因为不好截图,使用的是拍照记录的方式.再要生产的行数中,填写125,可以按照要求,有些客户要求的是100个数据,就可以填写100...

验证码,除了 12306,我还没有服过谁

为了防止暴力注册或爬虫爬取等机器请求,需要验证操作者是人还是机器,便有了验证码这个设计。本文作者主要介绍了如何使用Axure来设计一个动态的图形验证码,一起来学习一下吧。在软件设计中,为了防止暴力...

零基础也能学会的9个Excel函数,小白进阶必备

今天给大家分享一些常用的函数公式,可以有效地解决Excel中办公所需,0基础也可以轻松学会。建议收藏,在需要的时候可以直接套用函数。1、计算排名根据总和,计算学生成绩排名。函数公式=RANK(E2,$...

[office] excel表格数值如何设置_excel表格怎样设置数值

excel表格数值如何设置  因为电子表格应用程序是用来处理数值数据的,所以数值格式可能是工作表中最关键的部分,格式化数值数据的方式由用户决定,但在每个工作簿的工作表之间应使用一致的处理数字的方法。...

Excel最常用的5个函数!会用最后一个才是高手

是不是在处理Excel数据时,面对繁琐的操作烦恼不已?手动操作不仅耗时费力,还容易出错。别担心,表姐这就为你揭秘Excel中几个超实用的函数,让数据处理变得轻松高效!表姐整理了552页《Office从...

新手必会的53个Excel函数_惊呆小伙伴的全套excel函数技能

(新手入门+进阶+新函数)一、新手入门级(24个)1、Sum函数:求和=Sum(区域)2、Average函数:求平均值=Average(区域)3、Count函数:数字个数=Count(区域)4、Cou...

打工人私藏的4个Excel函数秘籍,效率提升3.7%

小伙伴们好啊,今天咱们分享几个常用函数公式的典型应用。合并内容如下图,希望将B列的姓名,按照不同部门合并到一个单元格里。=TEXTJOIN(",",1,IF(A$2:A$15=D2,B...

Excel偷偷更新的8个函数!原来高手都在用这些隐藏技能

领导突然要销售数据,你手忙脚乱筛选到眼花...同事3分钟搞定的报表,你折腾半小时还在填充公式...明明用了VLOOKUP,却总显示#N/A错误...别慌!今天教你的8个动态数组函数,就像给Excel装...

Excel表格随机函数怎么用?讲解三种随机函数在不同场景的应用

excel随机函数,其特点是能够生成一组随机数字,根据不同需求,还能批量生成小数位和整数,及指定行数和列数,或指定区间范围内的数字。这里根据需求,作者设置了三个问题,第1个是随机生成0至1之间的数字...

单纯随机抽样该如何进行?_单纯随机抽样的适用范围及注意事项

在数据分析中,抽样是指从全部数据中选择部分数据进行分析,以发掘更大规模数据集中的有用信息。在收集数据过程中,绝大多数情况下,并不采取普查的方式获取总体中所有样本的数据信息,而是以各类抽样方法抽取其中若...

随机函数在Excel中的应用_随机函数在excel中的应用实例

【分享成果,随喜正能量】职场,如果你没有价值,那么你随时可能被取代;如果你的价值不如别人,那么社会也不会惯你,你将被无情地淘汰掉。不管什么时候,你一定要学会构建自己的价值。每个人都应该思考这个问题:我...