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

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

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

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/ 提示

相关推荐

Linux集群自动化监控系统Zabbix集群搭建到实战

自动化监控系统Cacti特点:将监控到的数据,绘制成各种图形基于SNMP协议(网络管理协议)的监控软件,强大的绘图能力Nagios特点:状态检查和报警机制(例如:内存不足或CPU负载高时,及时的...

快速掌握Kafka系列《三》配置项总结

往期系列文章:1.快速掌握Kafka系列《一》基本概念入门2.快速掌握Kafka系列《二》常用操作命令汇总目录一、前言二、broker配置2.1三个基本配置2.2其它配置2.3...

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

2.2.2GeneralJavaScriptDevelopment常规JavaScript开发2.2.2.1JavaScriptObfuscation/JavaScript混淆[翻...

英特尔 i9-12900KS 最新爆料:基础功耗 150W,790 美元

IT之家2月14日消息,据爆料者@momomo_us的消息,现在已有海外经销商列出了i9-12900KS的商品信息。i9-12900KS的产品代码为BX8071512900KS,基...

Spring Boot集成OAuth2:实现安全认证与授权的详细指南

SpringBoot集成OAuth2:实现安全认证与授权的详细指南引言在当今数字化时代,Web应用的安全认证和授权至关重要。OAuth2作为一种广泛应用的开放标准协议,为第三方应用提供了安全、便捷的...

DNF人造神团本男气功加点攻略(dnf男气功用什么神话)

SP方面:加点从下往上点起,大技能全部点满,剩余sp在雷霆踏和念雷轰之间根据个人喜好二选一。加点代码:eJwNzTEKglAAx+Hf35D0pU8bImxpkSgHt47QFNRSi2cIkkJ...

Python连接Mysql数据库的几种方式以及问题排查方法

一、使用pymysql连接Mysql数据库连接示例:conn=pymysql.connect(host=host,user=user,password=passwd,db=db,port=int(...

37【源码】数据可视化:基于 Echarts + Python 动态实时大屏

效果图展示1.动态效果演示2.静态切片效果图一、确定需求方案1.确定产品上线部署的屏幕LED分辨率本案例基于16:9屏宽比,F11全屏显示。2.部署方式浏览器打开播放,Chrome浏览器、360浏览...

36【源码】数据可视化:基于 Echarts + Python 动态实时大屏

效果图展示动态效果演示2.静态切片效果图一、确定需求方案1.确定产品上线部署的屏幕LED分辨率本案例于16:9屏宽比,F11全屏显示。2.部署方式浏览器打开播放,Chrome浏览器、360浏览器等。...

Jsp Servlet Mysql实现的在线商城项目源码附带视频指导运行教程

今天给大家演示一款由jspservletMySQL实现的在线商城系统,系统项目源码在【猿来入此】获取!本系统实现了管理员管理用户、商品(商品分类)、订单、留言、新闻等功能,前台会员注册登录,查看商...

MySQL大数据表处理策略,原来一直都用错了……

场景当我们业务数据库表中的数据越来越多,如果你也和我遇到了以下类似场景,那让我们一起来解决这个问题。数据的插入,查询时长较长后续业务需求的扩展,在表中新增字段,影响较大表中的数据并不是所有的都为有效数...

基于SpringBoot 的CMS系统,拿去开发企业官网真香(附源码)

前言推荐这个项目是因为使用手册部署手册非常完善,项目也有开发教程视频对小白非常贴心,接私活可以直接拿去二开非常舒服开源说明系统100%开源模块化开发模式,铭飞所开发的模块都发布到了maven中央库。可...

「Qt入门第22篇」 数据库(二)编译MySQL数据库驱动

导语在上一节的末尾我们已经看到,现在可用的数据库驱动只有两类3种,那么怎样使用其他的数据库呢?在Qt中,我们需要自己编译其他数据库驱动的源码,然后当做插件来使用。下面就以现在比较流行的MySQL数据库...

基于SpringBoot从0到1编写一个图书管理系统(附源码)

项目源码地址:https://muzidong.com/productDetail/8ff44c71db6b4b6aa30c71e646b1c557需求分析基于SSM+MySql+LayUI...

Jsp+Ssm+Mysql实现的投票管理系统源码附带视频指导配置运行教程

今天给大家演示的是一款由jsp+ssm框架+mysql实现的投票管理系统,系统分为前端和后台管理模块,系统项目源码在【猿来入此】获取!前端用户可以登录注册、查看投票信息,登录后可以进行投票,也可以查看...