My First UI Widget
Although I’ve been programming in Java for well over a decade, JavaScript was actually the first programming language I ever learned. C was a required course in college, but I can’t honestly say I had mastered it at that point. In the Web 2.0 era, the most popular development tools were the “Big Three” – and Dreamweaver‘s WYSIWYG editor made it possible for even beginners to build a working web application. I was one of them.
The Secret Manual
When I first started learning web development, page layouts were mostly done through Dreamweaver‘s visual designer. If you wanted dynamic effects, you had to find someone else’s JavaScript snippets online. As a self-taught non-CS-major, I could roughly follow the logic, but had no idea where the properties and methods used in those scripts came from. How was I supposed to know what else was available? This question plagued me for a long time – until one day, a friend gave me an electronic document in CHM format: “The HTML Programming Guide.”
That guide was like a martial-arts manual to me. For the first time, I learned that something called an API specification existed, and this document laid out every API definition in HTML along with usage examples. From it, I learned how to manipulate the HTML DOM to create dynamic effects, how to handle events for interactive behavior, and the hottest web technology of the time – AJAX (Asynchronous JavaScript and XML). Rumor had it that knowing AJAX alone could land you a decent web development job back then.
The Tree Menu
Going from copying other people’s code to independently implementing dynamic effects was a major milestone for me. One day, while logging into my university’s intranet course system, I noticed the tree-structured menu on the page and found it fascinating. Could I build one myself? I went straight back to the dorm and got to work. After some research, I realized that creating a static tree structure visually wasn’t hard – you could nest HTML <DIV> or <UL> elements with a bit of CSS. The real question was: how do you dynamically create a tree menu with JavaScript, allowing nodes to be added and removed on the fly?
Since HTML only has DOM Elements and no built-in concept of a tree widget, implementing one requires assembling multiple DOM elements. A tree widget is less about visual tree structure and more about logical tree structure. From the DOM‘s perspective, the relationships between DOM nodes don’t map directly to tree widget node relationships. For example, parent-child nodes in the tree widget can’t simply be represented by nesting one DOM element inside another, because each tree node has a title, an icon, a state indicator (expanded or collapsed), child nodes, and more. A single DOM element can’t express all that information – multiple elements are needed, as shown below:
1 | +------------------------------ Node Container ----------------------------+ |
To implement a tree widget, we need to separate the visual structure from the logical structure and use the logical structure to organize the visual one. The visual structure consists of HTML DOM elements; the logical structure is the object-oriented concept of a Class. For example, we can use TreeView to represent the whole tree widget and TreeNode for each node, where every node has a parent and a list of children:
TreeNode
1 | function TreeNode(label, parent) { |
TreeView
1 | function TreeView(root) { |
Now we can dynamically build a tree widget with code:
1 | function createTreeView() { |
The Origin of Architecture
Through simple abstraction and encapsulation, a reusable tree widget becomes straightforward to implement. At the time, I felt that mastering object-oriented programming and abstraction meant mastering the essence of architecture design. In reality, it was only the beginning. For complex systems, we need higher levels of abstraction – which is exactly why architectural patterns like MVC, MVP, and MVVM exist.
- Blog Link: https://johnsonlee.io/2021/12/19/my-first-ui-widget.en/
- Copyright Declaration: 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
