Clear Up
SharpKit Reference

Ext.layout Namespace

Download SDK: SharpKit.ExtJs.zip

Classes

Name Description
ClassList

This class provides a DOM ClassList API to buffer access to an element's class. Instances of this class are created by Ext.layout.ContextItem.getClassList.

ClassListConfig
ClassListEvents
Context

Manages context information during a layout.

Algorithm

This class performs the following jobs:

  • Cache DOM reads to avoid reading the same values repeatedly.
  • Buffer DOM writes and flush them as a block to avoid read/write interleaving.
  • Track layout dependencies so each layout does not have to figure out the source of its dependent values.
  • Intelligently run layouts when the values on which they depend change (a trigger).
  • Allow layouts to avoid processing when required values are unavailable (a block).

Work done during layout falls into either a "read phase" or a "write phase" and it is essential to always be aware of the current phase. Most methods in Layout are called during a read phase: calculate, completeLayout and finalizeLayout. The exceptions to this are beginLayout, beginLayoutCycle and finishedLayout which are called during a write phase. While finishedLayout is called a write phase, it is really intended to be a catch-all for post-processing after a layout run.

In a read phase, it is OK to read the DOM but this should be done using the appropriate ContextItem where possible since that provides a cache to avoid redundant reads. No writes should be made to the DOM in a read phase! Instead, the values should be written to the proper ContextItem for later write-back.

The rules flip-flop in a write phase. The only difference is that ContextItem methods like getStyle will still read the DOM unless the value was previously read. This detail is unknowable from the outside of ContextItem, so read calls to ContextItem should also be avoided in a write phase.

Calculating interdependent layouts requires a certain amount of iteration. In a given cycle, some layouts will contribute results that allow other layouts to proceed. The general flow then is to gather all of the layouts (both component and container) in a component tree and queue them all for processing. The initial queue order is bottom-up and component layout first, then container layout (if applicable) for each component.

This initial step also calls the beginLayout method on all layouts to clear any values from the DOM that might interfere with calculations and measurements. In other words, this is a "write phase" and reads from the DOM should be strictly avoided.

Next the layout enters into its iterations or "cycles". Each cycle consists of calling the calculate method on all layouts in the layoutQueue. These calls are part of a "read phase" and writes to the DOM should be strictly avoided.

Considerations

RULE 1: Respect the read/write cycles. Always use the getProp or getDomProp methods to get calculated values; only use the getStyle method to read styles; use setProp to set DOM values. Some reads will, of course, still go directly to the DOM, but if there is a method in ContextItem to do a certain job, it should be used instead of a lower-level equivalent.

The basic logic flow in calculate consists of gathering values by calling getProp or getDomProp, calculating results and publishing them by calling setProp. It is important to realize that getProp will return undefined if the value is not yet known. But the act of calling the method is enough to track the fact that the calling layout depends (in some way) on this value. In other words, the calling layout is "triggered" by the properties it requests.

RULE 2: Avoid calling getProp unless the value is needed. Gratuitous calls cause inefficiency because the layout will appear to depend on values that it never actually uses. This applies equally to getDomProp and the test-only methods hasProp and hasDomProp.

Because getProp can return undefined, it is often the case that subsequent math will produce NaN's. This is usually not a problem as the NaN's simply propagate along and result in final results that are NaN. Both undefined and NaN are ignored by Ext.layout.ContextItem.setProp, so it is often not necessary to even know that this is happening. It does become important for determining if a layout is not done or if it might lead to publishing an incorrect (but not NaN or undefined) value.

RULE 3: If a layout has not calculated all the values it is required to calculate, it must set done to false before returning from calculate. This value is always true on entry because it is simpler to detect the incomplete state rather than the complete state (especially up and down a class hierarchy).

RULE 4: A layout must never publish an incomplete (wrong) result. Doing so would cause dependent layouts to run their calculations on those wrong values, producing more wrong values and some layouts may even incorrectly flag themselves as done before the correct values are determined and republished. Doing this will poison the calculations.

RULE 5: Each value should only be published by one layout. If multiple layouts attempt to publish the same values, it would be nearly impossible to avoid breaking RULE 4. To help detect this problem, the layout diagnostics will trap on an attempt to set a value from different layouts.

Complex layouts can produce many results as part of their calculations. These values are important for other layouts to proceed and need to be published by the earliest possible call to Ext.layout.Layout.calculate to avoid unnecessary cycles and poor performance. It is also possible, however, for some results to be related in a way such that publishing them may be an all-or-none proposition (typically to avoid breaking RULE 4).

RULE 6: Publish results as soon as they are known to be correct rather than wait for all values to be calculated. Waiting for everything to be complete can lead to deadlock. The key here is not to forget RULE 4 in the process.

Some layouts depend on certain critical values as part of their calculations. For example, HBox depends on width and cannot do anything until the width is known. In these cases, it is best to use block or domBlock and thereby avoid processing the layout until the needed value is available.

RULE 7: Use block or domBlock when values are required to make progress. This will mimize wasted recalculations.

RULE 8: Blocks should only be used when no forward progress can be made. If even one value could still be calculated, a block could result in a deadlock.

Historically, layouts have been invoked directly by component code, sometimes in places like an afterLayout method for a child component. With the flexibility now available to solve complex, iterative issues, such things should be done in a responsible layout (be it component or container).

RULE 9: Use layouts to solve layout issues and don't wait for the layout to finish to perform further layouts. This is especially important now that layouts process entire component trees and not each layout in isolation.

Sequence Diagram

The simplest sequence diagram for a layout run looks roughly like this:

  
Context         Layout 1     Item 1     Layout 2     Item 2
          |               |           |           |           |
          ---->X-------------->X           |           |           |
          run  X---------------|-----------|---------->X           |
          X beginLayout   |           |           |           |
          X               |           |           |           |
          A X-------------->X           |           |           |
          X  calculate    X---------->X           |           |
          X             C X  getProp  |           |           |
          B X               X---------->X           |           |
          X               |  setProp  |           |           |
          X               |           |           |           |
          D X---------------|-----------|---------->X           |
          X  calculate    |           |           X---------->X
          X               |           |           |  setProp  |
          E X               |           |           |           |
          X---------------|-----------|---------->X           |
          X completeLayout|           |         F |           |
          X               |           |           |           |
          G X               |           |           |           |
          H X-------------->X           |           |           |
          X  calculate    X---------->X           |           |
          X             I X  getProp  |           |           |
          X               X---------->X           |           |
          X               |  setProp  |           |           |
          J X-------------->X           |           |           |
          X completeLayout|           |           |           |
          X               |           |           |           |
          K X-------------->X           |           |           |
          X---------------|-----------|---------->X           |
          X finalizeLayout|           |           |           |
          X               |           |           |           |
          L X-------------->X           |           |           |
          X---------------|-----------|---------->X           |
          X finishedLayout|           |           |           |
          X               |           |           |           |
          M X-------------->X           |           |           |
          X---------------|-----------|---------->X           |
          X notifyOwner   |           |           |           |
          N |               |           |           |           |
          -               -           -           -           -
            

Notes:

A. This is a call from the run method to the runCycle method. Each layout in the queue will have its calculate method called.

B. After each calculate method is called the done flag is checked to see if the Layout has completed. If it has completed and that layout object implements a completeLayout method, this layout is queued to receive its call. Otherwise, the layout will be queued again unless there are blocks or triggers that govern its requeueing.

C. The call to getProp is made to the Item and that will be tracked as a trigger (keyed by the name of the property being requested). Changes to this property will cause this layout to be requeued. The call to setProp will place a value in the item and not directly into the DOM.

D. Call the other layouts now in the first cycle (repeat B and C for each layout).

E. After completing a cycle, if progress was made (new properties were written to the context) and if the layoutQueue is not empty, the next cycle is run. If no progress was made or no layouts are ready to run, all buffered values are written to the DOM (a flush).

F. After flushing, any layouts that were marked as done that also have a completeLayout method are called. This can cause them to become no longer done (see invalidate). As with calculate, this is considered a "read phase" and direct DOM writes should be avoided.

G. Flushing and calling any pending completeLayout methods will likely trigger layouts that called getDomProp and unblock layouts that have called domBlock. These variants are used when a layout needs the value to be correct in the DOM and not simply known. If this does not cause at least one layout to enter the queue, we have a layout FAILURE. Otherwise, we continue with the next cycle.

H. Call calculate on any layouts in the queue at the start of this cycle. Just a repeat of B through G.

I. Once the layout has calculated all that it is resposible for, it can leave itself in the done state. This is the value on entry to calculate and must be cleared in that call if the layout has more work to do.

J. Now that all layouts are done, flush any DOM values and completeLayout calls. This can again cause layouts to become not done, and so we will be back on another cycle if that happens.

K. After all layouts are done, call the finalizeLayout method on any layouts that have one. As with completeLayout, this can cause layouts to become no longer done. This is less desirable than using completeLayout because it will cause all finalizeLayout methods to be called again when we think things are all wrapped up.

L. After finishing the last iteration, layouts that have a finishedLayout method will be called. This call will only happen once per run and cannot cause layouts to be run further.

M. After calling finahedLayout, layouts that have a notifyOwner method will be called. This call will only happen once per run and cannot cause layouts to be run further.

N. One last flush to make sure everything has been written to the DOM.

Inter-Layout Collaboration

Many layout problems require collaboration between multiple layouts. In some cases, this is as simple as a component's container layout providing results used by its component layout or vise-versa. A slightly more distant collaboration occurs in a box layout when stretchmax is used: the child item's component layout provides results that are consumed by the ownerCt's box layout to determine the size of the children.

The various forms of interdependence between a container and its children are described by each components' size model.

To facilitate this collaboration, the following pairs of properties are published to the component's ContextItem:

  • width/height: These hold the final size of the component. The layout indicated by the size model is responsible for setting these.
  • contentWidth/contentHeight: These hold size information published by the container layout or from DOM measurement. These describe the content only. These values are used by the component layout to determine the outer width/height when that component is shrink-wrapped. They are also used to determine overflow. All container layouts must publish these values for dimensions that are shrink-wrapped. If a component has raw content (not container items), the componentLayout must publish these values instead.
ContextConfig
ContextEvents
ContextItem

NOTE This is a private utility class for internal use by the framework. Don't rely on its existence.

This class manages state information for a component or element during a layout.

Blocks

A "block" is a required value that is preventing further calculation. When a layout has encountered a situation where it cannot possibly calculate results, it can associate itself with the context item and missing property so that it will not be rescheduled until that property is set.

Blocks are a one-shot registration. Once the property changes, the block is removed.

Be careful with blocks. If any further calculations can be made, a block is not the right choice.

Triggers

Whenever any call to getProp, getDomProp, hasProp or hasDomProp is made, the current layout is automatically registered as being dependent on that property in the appropriate state. Any changes to the property will trigger the layout and it will be queued in the Ext.layout.Context.

Triggers, once added, remain for the entire layout. Any changes to the property will reschedule all unfinished layouts in their trigger set.

ContextItemConfig
ContextItemEvents
Layout

Base Layout class - extended by ComponentLayout and ContainerLayout

LayoutConfig
LayoutEvents
SizeModel

This class describes a size determination strategy or algorithm used by the layout system. There are special instances of this class stored as static properties to avoid needless object instantiation. These instances should be treated as readonly.

  • calculated
  • configured
  • constrainedMax
  • constrainedMin
  • natural
  • shrinkWrap
  • calculatedFromConfigured
  • calculatedFromNatural
  • calculatedFromShrinkWrap

Using one of these instances is simply:

  
var calculated = Ext.layout.SizeModel.calculated;
            
SizeModelConfig
SizeModelEvents
© Copyright 2005-2011 SharpKit. All rights reserved.