Page Rearrangement - Ajax Patterns

Page Rearrangement

From Ajax Patterns

Evidence: 3/3

Tags: Add Adjust Change Delete DOM Move Rearrange Restructure Remove


Contents

Ajax/Javascript Programming and Usability in "Ajax Design Patterns" Book

In A Blink

Diagram: Portlets/Portal


Goal Story

Doc is looking at an X-Ray image in an online diagnostics system. He mouse-clicks on the bottom-right region to reveal the time and date of the X-Ray. He then decides to annotate the image, so double-clicks on a critical point to pop up an editable "sticky note".


Problem

How can you dynamically restructure the page?


Forces

  • As the user's task and context changes, Ajax applications need to present different information.
  • Changes to page structure should be as smooth as possible.
  • Refreshing the page breaks continuity and also clears Javascript state.
  • Refreshing the page only allows for a discrete transition from one appearance to another - it's not possible to gradually fade or move an item.


Solution

Add, remove, move, and overlay elements by manipulating the DOM. The DOM is a hierarchical structure that defines how elements relate to one another. By adjusting the DOM structure, you can adjust where elements appear on the page. There are also critical CSS styles that affect page structure - you can affect these by manipulating DOM elements' style attributes.

Note: An online demo illustrates most of the code concepts throughout this Solution and the code snippets loosely follow from the demo.

Adding is normally achieved by introducing a new element as a child of an existing container element, such as body or div. For example, you can create a new div element and add it to a parent div.

 var message = document.createElement("span");
 $("container").appendChild(message);

Another way to insert an element is to append to the innerHTML property of the container element.

 $("container").innerHTML += "";

The opposite action is removing. By removing an element from the DOM, you take it off the display.

 $("container").removeChild($("message"));

As a variant of adding and removing, you can keep an element on the page, but toggle its visibility using CSS. There are two alternatives here. The visibility and display. The former will always preserve layout while the latter will cause the element to squeeze in and out when it's shown or hidden. So with visibility, it's like the element's still there but wearing invisible paint; whereas. This makes layout easy, because everything stays where it is, but it can be ugly to have a big patch of whitespace on the page. With the display style, it's more like the element's shoved into a temporary store to make room for other elements, which adds more layout complexity but is often more appropriate from a visual perspective. For visibility, the style property is switched between visible and hidden.

   $("message").style.visibility = "visible"; // Now you see me
   $("message").style.visibility = "hidden";  // Now you don't

display is actually used for more than just showing and hiding an element - it also defines how an element appears when it is visible. It's beyond the scope of these patterns to cover the options in detail, but the main options are block (the default for div elements) and inline (the default for span) elements. To show and hide using display, you switch its value between none and one of these values.

   $("message").style.display = "block"; // Now you see me, with block layout
   $("message").style.display = "none";  // Now you don't

You can move an element around in a couple of ways. Firstly, you can remove it from one place in the DOM and add it to another.

   container.removeChild(message);
   extraContainer.appendChild(message);

Second, you can adjust CSS properties. The most direct styles are left, right, top, and bottom, which define the co-ordinates of the element in question.

   message.style.top = "150px";
   message.style.left = "50px";

But what is the (0,0) point these co-ordinates are relative to? The precise meaning of these styles is modulated by the positioning element.

  • If static, the co-ordinates have no effect - the element is positioned according to standard CSS layout rules.
  • If relative, the co-ordinates are relative to the position it would normally be positioned under standard CSS layout rules - they suggest how far its displaced.
  • If absolute, the co-ordinates are relative to the top-left of the entire document.
  • If fixed, the co-ordinates are relative to the top-left of the browser viewport (the portion of the browser window that shows the page). Even if you scroll the document, the element will stick right on the same point on the screen as before.

Positioning is set as a standard CSS style.

   message.style.positioning = "150px";

Finally, you can also overlay elements. An HTML document has "2.5" dimensions, which is to say that it has a limited notion of depth in which elements are able to overlap each other. The critical CSS style here is zIndex, which signifies an element's depth. When two elements occupy the same portion of the page, the element which will appear in front is that with the higher zIndex. The zIndex is not a real depth, because all that matters is the relative ordering of zIndex values. Against a zIndex of zero, it makes no difference whether an element's zIndex is 1, 10, or 100. The default value is 0 and a zIndex can take on any positive or negative value.

   message.style.zIndex = -100; // Behind of all elements with default zIndex
   message.style.zIndex = 100;  // In front of all elements with default zIndex


Decisions

Which positioning style to use?

A single application can combine different types of positioning. In most cases, static - the default positioning - suffices. Non-static positioning is most commonly used with more free-floating elements, such as Sprites or elements suitable for Drag-And-Drop. For non-static positioning, relative positioning tends to be the most useful, because it allows you to move the element around within a defined container.

How will you protect against memory leaks?

Continuously adding and removing elements leads to the risk of memory leaks. Javascript is supposed to perform garbage collection, automatically removing variables that are no longer referenced. Some general guidelines:

  • Avoid global variables where possible. Local variables go out of scope, so if a local variable points to a deleted element, the element will disappear. But if a global variable points to such an element, it will stick around.
  • Explicitly nullify references. Where you're sure a variable will no longer need to use the value it references, set the variable to null.
  • Avoid or destroy circular references You can sometimes end up with a circular structure that no-one's using anymore, but sticks in memory because garbage collection isn't smart enough to remove it (it concludes each object is still relevant because at least one reference exists). This can happen, for instance, when an object's event handler refers back to the object (see "Javascript Closures" for more details).
  • Test, test, test It may not be fun, but you need to stress test your application under different browser environments, monitoring memory usage to ensure it's stable.

web monitor


Real-World Examples

Tadalist

Ta-da List (Screencast available) allows users to manage TODO items. Each TODO item is a phrase like "Finish homework" and the user can add, remove, and rearrange items.

Super Maryo World

Super Maryo World is an outright video game, a clone of the classic Super Mario Bros game implemented with standard Ajax technologies. The manipulation of game characters and fixtures illustrates how elements can rapidly be added, removed, and moved around.

Kiko

Kiko is a visual Ajax calendar application. You can add and remove appointments, drag them around to change the time, and stretch them out to increase duration.


Code Examples

Basic Wiki Demo

The Basic Wiki Demo periodically polls for a fresh list of messages to display. Each time the list is retrieved, it removes all existing messages and adds the new list. The containing element is called "messages" and removing all messages runs a loop across each of its children.

 while ($("messages").hasChildNodes()) {
   $("messages").removeChild($("messages").firstChild);
 }

Each message is used to create a new textarea element (among other things), which is then added to a new div, which in turn is added to the messages container.

 for (var i=0; i<wikiMessages.length; i++) {
   var messageArea = document.createElement("textarea");
   ...
   messageDiv = document.createElement("div");
   ...
   messageDiv.appendChild(messageArea);
   ...
   $("messages").appendChild(messageDiv);
   ...
 }


Alternatives


Related Patterns

The Display Morphing pattern addresses an element's appearance; this pattern talks about elements' location, visibility, and "height above the page" (z-index). Together, the two patterns cover any


Visual Metaphor

Page rearrangement is like adding, removing, and rearranging Post-It notes on a whiteboard.


Want to Know More?

网站推广|google排名|google优化|google左侧排名|google排名网|google排名推广 SEO排名|SEO左侧排名|SEO排名网 google排名服务 关键字排名 关键字优化 关键字排名优化 google关键字排名 google排名优化 google排名 google排名推广 网站优化 网站优化排名 网站优化服务 实验台 加拿大移民 南京搬家 通风柜 实验台 实验台|中央实验台|工作台|天平台|通风柜|实验室家具| 关于我们 新闻中心 产品展示 工程案例 网站地图 联系我们 中央实验台 中央实验台 单面实验台 钢木转角台 中央水斗台 单面水斗台 转角水斗台 实验室 中央实验台套 中央实验台 单边实验台 铝木实验台 中央实验台 转角水斗台 单面水斗台 中央水斗台 单面实验台 天平台 钢木结构通风柜 通风柜 全钢简易通风柜 板式结构通风柜 钢木结构通风柜 实验台中央实验台|实验室家具 通风柜实验通风柜 器皿柜 药品柜 毒品安全柜 毒品柜 气瓶柜 钢瓶柜 全钢试剂架 试剂架 钢木试剂架 更衣柜 物理实验桌 工作凳 滴定车 单面滴定架 万向抽气罩 PP水杯 PP水槽 落地式冲淋洗眼器 三口化验龙头 水龙头 风机 管道式风机 双开立式气咀 三开立式气咀 四开立式供气咀 单开立式气咀 单开壁式气咀 google排名 google推广 加拿大投资移民 南京搬家 实验台 实验台 通风柜 实验家具 天平台 药品柜 实验工作台 实验室设备 货架 南京网站制作 实验台 通风柜 实验设备 实验室家具 货架 南京网站制作 实验室家具|实验设备|通风柜|实验台 公司简介 企业资质 新闻中心 产品中心 服务中心 工程案例 联系我们 实验台|中央实验台|边台 通风柜 实验室台柜,药品柜,天平台,试剂架,中央试剂架,更衣柜 实验室配件 未来几年间我国仪器仪表发展方向 超净工作台的选购、使用及维护 通风柜的功能与安全性 通风柜的正确使用 我国重点铸造大型干燥设备“中国芯” 通风柜技术标准 实验室经济”助力企业从容不迫御“寒流” 化学实验室的灭火常识 网站地图 全木实验边台 钢木结构中央实验台 铝木中央实验台 钢结构中央实验台 中央实验台 全木实验边台 钢木结构边台 铝木实验边台 实验边台 全木通风柜 钢木结构通风柜 落地式通风柜 全钢通风柜 通风柜 化学药品柜 实验室药品柜 药品柜 钢木天平台 全木天平台 毒品柜 更衣柜 更鞋柜 中央水斗台 气瓶柜 器皿柜 防静电工作台 吊柜 全钢气瓶柜 万向抽气罩 抽气罩 通风机 单联气体考麦 双联气体考麦 三联气体考麦 化验水槽 超净化工作台 洗涤台 实验坐椅 双联化验水咀 试管插板