Summary of the invention
The objective of the invention is to provides a kind of Web application interface dynamic adaptation method towards heterogeneous terminals to the problems referred to above, can be loyal interfacial structure and the performance of reflection time of running, guarantee adaptive correct in interface.
For realizing above-mentioned purpose, the present invention adopts following technical scheme:
A kind of Web application interface dynamic adaptation method towards heterogeneous terminals, its step comprises:
1) loads the Web application interface through browser and play up;
2) obtain said Web application interface after playing up at the dom tree of the time of running;
3) according to the type and the dimension information of all subtrees in the said dom tree, said Web application interface is cut apart the sub-interface that generates some adaptation current screens;
4) variation of the said dom tree of monitoring heavily is divided into the interface that changes the sub-interface that adapts to said current screen;
5) the sub-interface after demonstration is cut apart in said current screen.
Further, said current screen only shows a sub-interface at any one time, hides other sub-interface.
Further, through in a sub-interface, operating, switch to other sub-interface.
Further, detect the variation at other the sub-interface that when the user operates, causes in a sub-interface, and remind the user to switch to vicissitudinous sub-interface, or automatically switch to vicissitudinous sub-interface.
Further, the variation of said dom tree comprises the additions and deletions of element in variation and the DOM subtree of element content in the dom tree; When keeping watch on the dom tree variation, regularly the eigenwert at the sub-interface of current time and the eigenwert at last sub-interface of a moment are compared, whether change to judge the interface.
Further, the type of said subtree comprises: indivisible subtree, can ignore subtree and divisible subtree.
Further, according in the following attribute of DOM subtree and root element thereof one or more said Web application interface is cut apart: length and width, overlay area, subtree full-size, subtree are overlapping.
Further, when collecting the subtree size, begin recursively to calculate the size of its all daughter elements and the side-play amount of relative root node thereof, and then calculate the size of stalk tree actual displayed from root element.
Further, carry out the pattern adjustment according to the control enclosure type antithetical phrase interface of DOM element.
The present invention is supported in the interface of cutting apart dynamic generation the time of running, and after the interface is upgraded, also dynamically heavily cuts apart.Because it cuts apart the time of running document object model tree of behavior based on the Web interface, but not based on traditional static html page, so can loyalty reflect interfacial structure and the performance of the time of running, thus the correct of interface adaptation method guaranteed.The present invention can make the Web interface for the design of large scale screen equipments such as PC be able to show with the mode that is fit at small-size screen equipment.
Embodiment
For ease of understanding and embodiment of the present invention, at first introduce and the closely-related DOM Document Object Model of the present invention below, specify technical scheme of the present invention then, and concrete application example is provided.
DOM Document Object Model (DOM, Document Object Model) is a structured document visit standard of W3C definition, and a kind of standard A PI (Application Programming Interface) of access structure document is provided.The structured document that this specification handles is tree-shaped, that is: 1) each document has unique root element; 2) each element comprises the plurality of sub element in the document; 3) except that root element, each unit have and unique father's element is only arranged; 4) each element content and certain attributes.The DOM regulation and stipulation API of each element in one group of traversal and the operation tree structure, also stipulated to visit the content of each element and the API of attribute simultaneously.
Html document is a kind of structured document of the DOM of meeting standard, can use the DOMAPI visit.Each label (Tag) in the html document is an element in the DOM standard, and its root element is the html label, has a large amount of attributes in each element, and wherein a part of attribute is the attribute of the existing mode of definite list of elements of stipulating in the CSS standard.The pairing tree structure of structured document that to follow the DOM standard traditionally is called dom tree, and the present invention continues to use this habitual saying.
Web uses can comprise a plurality of interfaces, and each interface is the plain set of a html element, and these elements constitute a HTMLDOM subtree.When the Web interface need be current, browser can be played up it.Play up each element of accomplishing in the rear interface and all possess certain width and height.Defined the BOX Model (Box Model) of DOM element in the CSS standard of W3C.The width and the height of element after BOX Model has been stipulated to play up and accomplished.
According to the definition of BOX Model, each DOM element shows as a rectangle, and this rectangle is made up of four parts, shown in Fig. 1, specifies as follows:
1) content (Content): the actual content of element constitutes a rectangle, plays up and can use after the completion DOM API to obtain the width (width) of this rectangle and highly (height);
2) inset spacing (Padding): the content rectangle of element and the spacing of frame, wherein the inset spacing of four direction maybe be different up and down.Background colour that defines on the element or background picture also can appear at the inset spacing part;
3) frame (Border): the frame of element, frame can be provided with a width, and the width of the frame of four direction maybe be different;
4) outer back gauge (Margin): outside the element frame, the spacing of element and other elements.The outer back gauge of four direction also possibly be different.Outer back gauge is transparent, and background colour that defines on the element or background picture can not appear at outer back gauge part.
Therefore a physical size of playing up element in the back dom tree that finishes is:
The element width:
Width
elem=Width
content+Padding
left+Padding
right+Border
left+Border
right+Margin
left+Margin
right (1)
The element height:
Height
elem=Height
content+Padding
top+Padding
bottom+Border
top+Border
bottom+Margin
top+Margin
bottom (2)
Wherein, Width
ElemExpression element width, Height
ElemExpression element width, Width
ContentThe width of expression content, Height
ContentThe height of expression content, Padding
Left, Padding
Right, Padding
Top, Padding
BottomThe inset spacing of representing the left side, the right, bottom and upper segment respectively, Border
Left, Border
Right, Border
Top, Border
BottomThe border width of representing the left side, the right, bottom and upper segment respectively; Margin
Left, Margin
Right, Margin
Top, Margin
BottomThe outer back gauge of the difference left side, the right, bottom and upper segment.
When the group interface is made up of an element, certain and this element of the size at this sub-interface measure-alike.Yet during DOM subtree of group interface correspondence, situation is more complicated.Because there is multiple different locator meams in the relative father's element of CSS regulation and stipulation one child elements, and is as shown in Figure 2.
Can find out when element adopts relatively, during absolute or ralocatable mode location, its BOX Model possibly exceed the border of his father's element BOX Model.Therefore, the size at an interface need be calculated the outer back gauge coordinate of all elements in its corresponding dom tree, calculates the difference between maximal value and the minimum value, and concrete computing formula is following:
The left side in the maximal value of the rightmargin coordinate in interface width=all daughter elements-all daughter elements is expressed as apart from the minimum value of coordinate:
Width
view=Max(Right
subelem)-Min(Left
subelem) (3)
The minimum value of the bottom margin coordinate in the maximal value of the top margin coordinate in interfacial level=all daughter elements-all daughter elements is expressed as:
Height
view=Max(Bottom
subelem)-Min(Top
subelem) (4)
According to the interface dimensions that calculates, can provide the definition of the adaptive screen in interface.
The current common more complicated in Web interface, an interface possibly be made up of a plurality of zones, and each zone shows a part of content in the complete interface respectively, and is as shown in Figure 3.Therefore in order to make a bigger less screen of interface fit size; Can the interface be divided into the sub-interface of a plurality of adaptation screens according to zones of different; Only show a sub-interface at synchronization; Provide navigation to make the user be able to switch between the different sub interface according to the result of cutting apart simultaneously, this process is called " based on the interface dynamic adaptation of cutting apart ".When subclass that the element set that constitutes an interface is gathered for another interface element, claim that the former is the latter's sub-interface.The alleged interface based on cutting apart of the present invention is adaptive, is that the interface with an incompatibility screen size is decomposed into a series of sub-interfaces, wherein each adaptive screen in sub-interface.
Yet cutting apart, unsuitable interface possibly make the user can't from a plurality of sub-interfaces, obtain the information identical with original interface.An effective interface is cut apart must satisfy following character:
(a) completeness:
Each element in the original interface appears in the sub-interface at least.This guarantees in the cutting procedure of interface, not have information to be omitted.
(b) exclusiveness:
Element in each sub-interface can not appear in other sub-interfaces.This guarantees that information can not duplicate in the cutting procedure of interface.
(c) cohesion:
The element of expressing relevant information is positioned at same sub-interface.The good interface of cohesion is cut apart make the user when accomplishing a task, and the number of times that switches sub-interface is the least possible.This guarantees ease for use and availability after the interface is cut apart.
As previously mentioned, the HTML normalized definition semanteme of each element in the HTML dom tree, container of div element representation for example, h1 element representation one-level title, p element representation passage.The semantic label that the W3C suggestion uses HTML to provide is described the structured content at Web interface, and the level of nesting relation between a this structured content content and content does not relate to element and how to show.The performance information of element uses the CSS specification description of being responsible for performance.Follow the interface of this suggestion, its content structure and its form of expression are separated from each other, and can revise the interior perhaps performance at interface relatively independently, guarantee the maintainability at interface.Therefore the mode standard when becoming interface development is adopted in this suggestion for industrial community.Preceding 50 website homepage of Alexa.com rank all shows the interface model that separates with the structure of following the W3C suggestion at present.
On the other hand; Through analysis to preceding 50 website homepages of Alexa.com; Find this interface that the structure performance separates of following, the sub-interface and the DOM subtree of gathering in it have good corresponding relationship, i.e. the sub-preferably interface of cohesion; Its whole elements are included in the DOM subtree, the situation that element in the sub-interface adheres to a plurality of DOM subtrees separately can not occur.As shown in Figure 4, constitute in several complete interfaces some and gather sub-interface, the corresponding just some subtrees that constitute dom tree, the upper right corner be the html code of correspondence among Fig. 4, dom tree generates according to this code.
The analysis of comprehensive front, the present invention proposes a kind of Web application interface dynamic adaptation method based on the dom tree time of running.Fig. 5 is the flow chart of steps of the inventive method.At first load the Web application interface and play up through browser.Load and accomplish after the interface plays up when Web uses, obtain the dom tree that the interface should moment correspondence and also analyze.Generate the sub-interface that some screens adapt to if interface incompatibility current screen, the inventive method can be cut apart dom tree automatically, and guarantee only to show at any one time to hide other interfaces in a sub-interface.Afterwards, the inventive method can be monitored dom tree and changed, and comprises the additions and deletions of element in atomic element content change in the dom tree and the DOM subtree.When changing generation, the inventive method can heavily be cut apart changing part, guarantees that the interface of dynamic change can adapt to current screen all the time.
In addition, because the interface is split into a plurality of sub-interfaces, all there is big quantum in arbitrary moment at the interface in hidden state.Therefore the present invention can further be provided at the method for switching between a plurality of interfaces for the user.On the other hand, because the operation of user in a certain sub-interface possibly cause other sub-interfaces to change, so further this variation of detection of the present invention, remind the user to switch to vicissitudinous sub-interface, or automatically switch to vicissitudinous sub-interface.More than two processes constitute navigation feature jointly.
Specify the implementation process of technical scheme of the present invention below.
1. dom tree is analyzed
For cutting apart, at first need analyze dom tree.The subtree that in the HTML at Web interface dom tree, has number of different types, though wherein some subtree comprises a plurality of elements and even many stalks tree, it logically but is an atomic element, this type subtree is indivisible in the procedure of adaptation.
For example: the select element in the corresponding dom tree of the drop-down menu in interface, this element comprises a plurality of option elements and represents the different options in the menu, however logically select should be used as an atomic element and treats and can not be cut apart.Also exist some elements in the interface, to show in this external dom tree, these elements are that the subtree that root node constitutes need not participate in the adaptive process in interface.
The present invention is divided three classes the subtree of interface dom tree, and is as shown in table 2:
Table 2.DOM subtree type
When the Web interface be read and dynamically play up finish after, collect the dimension information of all subtrees in all dom trees.These information will be used to calculate and how dom tree cut apart.But in actual the playing up, because the difference of Elements C SS layout type, the wide high scope of dom tree root node may not cover whole daughter elements.Simple dom tree as shown in Figure 6, element 3 is the daughter element of root element 1, but because element 3 has used the layout method that floats to the right, so it highly can " not strut " height of element 1.This makes the height of element 3 surpass the height of root element 1.
In order to handle the situation that this daughter element size " is overflowed ", when collecting the subtree size, can not simply write down the wide height of subtree root element.And need begin recursively to calculate the size of its all daughter elements and the side-play amount of relative root node thereof from root element, utilize these information calculations to go out the size of stalk tree actual displayed again.This result of calculation will be cached in the root element of each stalk tree, through reducing the performance that double counting improves adaptation method.
The algorithm that the dom tree analytic process provides referring to table 3.For for simplicity, in the algorithm false code that " height " and " width " is unified by " size " expression, need write down the wide, high of subtree respectively in the reality.In addition, algorithm also need write down the type information of root element except that the size of record subtree, with the type of identification subtree.These information also can be accomplished in the recursive procedure of algorithm.
Table 3.DOM tree analytical algorithm
2. the interface is cut apart
After type that calculates each DOM subtree and size, further need which DOM subtree of decision to be cut apart and how to cut apart.In carrying out this process, the inventive method can mainly be considered the following attribute of DOM subtree and root element thereof:
1) length and width: carrying out the interface when adaptive, width be the main factor that is considered highly.Under square one, can pay the utmost attention to the adaptation of width, this is because the user adapts to the browsing mode of vertical scrolling more, and the mobile phone browser of current main-stream also can be supported the browsing mode of vertical scrolling well.When considering that width adapts to; Another problem that should be noted that is that current a lot of mobile phone has disposed gravity sensing equipment; When the direction of holding mobile phone as the user changes (having level to change into vertical or inverse variation), promptly the width of mobile phone with highly understand and possibly exchange.Screen size with a mobile phone can change this meaning in the time of running, so in consideration must be included the direction of screen and possible variation when adaptive in the interface.
2) overlay area: the overlay area is another mode of describing DOM subtree size.Use the overlay area as the foundation of cutting apart, can avoid the use of the problem of the wide mobile phone placement direction that exists when high.But thisly ignore wide height and may not obtain best cutting apart.
3) subtree full-size: in the Web interface, have tabulation miscellaneous.Tabulation comprises a plurality of list items, therefore has bigger size.Yet these tabulations are indivisible subtrees in logic.One is avoided the method that unnecessary dom tree is cut apart is to include the full-size of all subtrees of dom tree in consideration.When a very big dom tree of size comprises small-sized in a large number subtree, can infer that this dom tree is certain tabulation, and need not cut apart it.This scheme also can avoid being partitioned into a large amount of sub-interfaces too in small, broken bits, causes " adaptation " but disabled interface splitting scheme.
4) subtree is overlapping: possibly there is overlapped zone in the corresponding sub-interface of subtree that is subordinate to same dom tree.For the unusual complex Web member in some interfaces, Google Map for example, the subtree in the dom tree exists overlapping in a large number.Cut apart overlapped DOM subtree and can't make the adaptive screen in interface, possibly cause the entanglement at interface on the contrary.For fear of this problem, must calculate overlapped zone between the subtree.
Through analyzing above attribute, interface of the present invention adaptation method can be divided into the plurality of sub interface with the interface.
Table 4 has provided the false code that adaptive algorithm is cut apart at the interface.Wherein setAsPartitionUnit (S) function representative is that the dom tree of root is set to a character boundary face after cutting apart with node in the S set.Whether the dom tree set that shouldBePartitioned (S) function then returns in the S set should be by further division, and this function has been considered above four kinds of attributes.
Adaptive algorithm is cut apart at table 4. interface
After cutting apart completion, the display properties " display " of CSS normalized definition in the corresponding DOM subtree root element of sub-interface is set, with a sub-interface display or thoroughly hiding.The inventive method guarantees in a sub-interface display, hides other sub-interfaces.In addition, after dom tree was split into some subtrees, public father's element of a plurality of subtrees was not comprised in any subtree.Yet these elements possibly comprise some pattern effects, background color for example, and these pattern effects possibly cause interference to the current sub-interface that needs to show equally, therefore also need these elements be hidden.Fig. 7 is sub-showing interface and an example of corresponding dom tree thereof after cutting apart.
3. heavily cut apart
To the characteristics of Web interface in the dynamic change time of running; The operation of the interpolation of the variation of DOM element content, element or deletion in the inventive method meeting monitoring interface (DOMAPI does not comprise the operation of moving the DOM element, so move operation is actual in the interpolation completion again of deletion earlier).Occur in a sub-interface internal after cutting apart if once add deletion action, then need heavily not cut apart, because the completeness of current splitting scheme and exclusiveness are not destroyed to whole interface.If the opposite operation of adding deletion DOM element is not in any sub-interface internal, then these are operating as the interface modification of " sub-interface rank ".When this modification takes place, just must calculate again and cut apart, be able to adaptive screen to guarantee amended interface to whole interface.
Being positioned at the DOM number of elements that the outside DOM element additions and deletions operation in sub-interface must cause not belonging to any sub-interface changes.Through keeping watch on the quantity of this type DOM element, can monitor the time that sub-interface rank retouching operation takes place effectively, and heavily cut apart in good time.
4. the pattern adjustment is carried out at the antithetical phrase interface
Sub-interface dimensions after cutting apart usually can not complete just adaptive screen.The size of some indivisible subtree (for example picture) possibly surpass the screen scope.As indicated above in addition, avoid some too in small, broken bits cutting apart for guaranteeing the interface availability, the size of some divisible subtrees also maybe be greater than the size of screen.
Therefore, the interface need the antithetical phrase interface to carry out the pattern adjustment after cutting apart completion.According to analyzing before, sub-interfacial level exceeds the screen scope can not cause too big problem, so width adjustment is mainly considered in the pattern adjustment.
The CSS normalized definition visual formats model of DOM element (Visual Formatting Model).Stipulate that wherein a DOM element possibly generate two kinds of control enclosurees (Controlling Box): block box (Block Box) and row inner box (Inline Box).Two kinds of different control enclosurees cause the different methods that DOM element width is set.The difference of control enclosure is as shown in table 5.
Table 5.DOM element control enclosure is described
Therefore when carrying out the pattern adjustment of sub-interface, must consider the control enclosure type of the DOM subtree root element that sub-interface is corresponding.The width of root element can directly be set for block element.For the row interior element, must find its first be not father's element of row interior element, the width of this element is set; The display attribute of capable interior element perhaps at first is set, and is block element with this element transformation, and its width is set again.In addition, the outer back gauge of root element can also be set, make that existence is necessarily stayed white between sub-interface and the screen frame; The adjustment font size increases the size of small-size screen Chinese words, to improve the overall usability at sub-interface.
After cutting apart completion, the inventive method can be according to the sub-interface tabulation after cutting apart, and generation supplies the user to switch the interface at sub-interface automatically.When the user selects certain sub-interface in the interface, show this sub-interface, hide other sub-interfaces simultaneously.
Then, the inventive method can be kept watch on the variation of each sub-interface internal.When the sub-interface content of hiding changed, dynamic navigation was realized at the sub-interface that needs the instantly prompting user and even automatically switch and show.In order to keep watch on the variation of sub-interface internal, the inventive method can be kept watch on the innerHTML property value of each sub-interface root element.In addition, other DOM element additions and deletions of sub-interface level also need take in dynamic navigation.
When keeping watch on the dom tree variation, write down the eigenwert at last sub-interface of a moment, and regularly the eigenwert and a last moment of current time were compared, whether change to judge the interface.Need the sub-interface feature value of supervision as shown in table 6.It should be noted that when the interface dom tree comprises than the multielement more complicated supervisory work possibly cause big expense.
Table 6. interface monitoring features value
Eigenwert |
Describe |
Root element innerHTML property value |
Judge whether that sub-interface internal changes |
DOM subtree number of elements |
Judging whether to take place sub-interface rank changes |
Provide the example that the interface of Ctrip.com (taking journey) homepage is cut apart below.
Fig. 8 (a) need be cut apart it to adapt to the mobile phone screen of 320x480 for taking the effect of journey PC edition owner page or leaf.Adopt the inventive method that the dom tree that meets the following conditions is cut apart:
1) width and area be all above the width of screen and 1.5 times of area, or width is less than 1.5 times of screen width but area surpasses 4 times of screen;
2) size at maximum sub-interface is greater than 20% of screen size;
3) width and highly all above the width of screen and height.
Through over-segmentation, the interface of member has been divided into 6 sub-interfaces, and numbering has marked each the sub-interface after cutting apart among Fig. 8 (a).In addition, the width of also having reseted sub-interface overflows to avoid level.Actual display effect in mobile phone end is shown in Fig. 8 (b).Can find out that the inventive method has realized that preferably the interface cuts apart.
Above embodiment is only in order to technical scheme of the present invention to be described but not limit it; Those of ordinary skill in the art can make amendment or is equal to replacement technical scheme of the present invention; And not breaking away from the spirit and scope of the present invention, protection scope of the present invention should be as the criterion so that claims are said.