source: gs3-extensions/html-to-expeditee/trunk/src/src/js/html-to-expeditee.js@ 25057

Last change on this file since 25057 was 25057, checked in by davidb, 12 years ago

Next round of improvements to cross-walking Greenstone web pages to Expeditee frames

  • Property svn:executable set to *
File size: 4.8 KB
Line 
1//javascript:
2var expSyntaxArray = [];
3
4Element.prototype.getElementWidth = function() {
5 if (typeof this.clip !== "undefined") {
6 return this.clip.width;
7 } else {
8 if (this.style.pixelWidth) {
9 return this.style.pixelWidth;
10 } else {
11 return this.offsetWidth;
12 }
13 }
14};
15
16Element.prototype.getElementHeight = function() {
17 if (typeof this.clip !== "undefined") {
18 return this.clip.height;
19 } else {
20 if (this.style.pixelHeight) {
21 return this.style.pixelHeight;
22 } else {
23 return this.offsetHeight;
24 }
25 }
26};
27
28function getElementWidth(elem)
29{
30 if (typeof elem.clip !== "undefined") {
31 return elem.clip.width;
32 } else {
33 if (elem.style && elem.style.pixelWidth) {
34 return elem.style.pixelWidth;
35 } else {
36 return elem.offsetWidth;
37 }
38 }
39}
40
41function getElementHeight(elem)
42{
43 if (typeof elem.clip !== "undefined") {
44 return elem.clip.height;
45 } else {
46 if (elem.style && elem.style.pixelHeight) {
47 return elem.style.pixelHeight;
48 } else {
49 return elem.offsetHeight;
50 }
51 }
52}
53
54function hyphenToInitialCapReplacer(str, p1, offset, s)
55{
56 return p1.toUpperCase();
57}
58
59function getStyle(node,style_prop)
60{
61 var prop_val;
62 if (node.currentStyle) {
63 /* IE */
64 style_prop = strCssRule.replace(/\-(\w)/g, hyphenToInitialCapReplacer);
65 prop_val = node.currentStyle[style_prop];
66 }
67 else if (window.getComputedStyle) {
68 /* Firefox */
69 var computed_style = document.defaultView.getComputedStyle(node,null)
70 prop_val = computed_style.getPropertyValue(style_prop);
71 }
72 else {
73 /* try for inline value */
74 prop_val = el.style[style_prop];
75 }
76
77 return prop_val;
78
79}
80
81function getElementPosition(elem)
82{
83 var origElem = elem;
84
85 var xl_ = 0;
86 var yt_ = 0;
87
88 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
89 xl_ += elem.offsetLeft - elem.scrollLeft;
90 yt_ += elem.offsetTop - elem.scrollTop;
91 elem = elem.offsetParent;
92 }
93
94 var xr_ = xl_ + getElementWidth(origElem);
95 var yb_ = yt_ + getElementHeight(origElem);
96
97 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
98}
99
100
101function htmlToExpeditee(node)
102{
103 var nodePos = getElementPosition(node);
104 var pxl = nodePos.xl;
105 var pyt = nodePos.yt;
106 var pxr = nodePos.xr;
107 var pyb = nodePos.yb;
108
109 return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
110}
111
112
113function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
114{
115 depth = (depth) ? depth : 0;
116
117 var jsonNode = null;
118
119 if (node.nodeType == 3) { /* text node */
120 var text = node.nodeValue;
121
122 if (text.match(/\S/)) {
123 jsonNode = {};
124
125 jsonNode.type = "text";
126 jsonNode.text = node.nodeValue;
127
128 jsonNode.xl = pxl;
129 jsonNode.yt = pyt;
130 jsonNode.xr = pxr;
131 jsonNode.yb = pyb;
132
133 var parent = node.parentNode;
134 if (parent != null) {
135 var data = parent.attributes["data"];
136
137 if (data != null) {
138 jsonNode.data = data.value;
139 }
140 }
141
142 }
143 }
144 else if (node.nodeType == 1) { /* element */
145
146 /* need to handle: img, a, li */
147 /* need to dig out: text size, l/r/justified, font-face, type, colour */
148
149 if (getStyle(node,"visibility").match("hidden")
150 || getStyle(node,"display").match("none")) {
151
152 return null;
153 }
154
155 var elemName = node.nodeName.toLowerCase();
156
157 if (!elemName.match(/script/)) {
158
159 var nodePos = getElementPosition(node);
160 var xl = nodePos.xl;
161 var xr = nodePos.xr;
162 var yt = nodePos.yt;
163 var yb = nodePos.yb;
164
165 jsonNode = {};
166 jsonNode.type = "rect";
167 jsonNode.elem = node.nodeName;
168
169 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
170 jsonNode.rect = rect;
171
172 if (elemName.match("img")) {
173 jsonNode.img = node.src;
174 }
175
176 var style = {};
177
178 style["font-family"] = getStyle(node,"font-family");
179 style["font-size"] = getStyle(node,"font-size");
180 style["background-color"] = getStyle(node,"background-color");
181
182 // console.log("background color = " + style["background-color"]);
183
184 // console.log("font size = " + style["font-size"]);
185
186 jsonNode.style = style
187
188 var jsonChildNodes = [];
189
190 var cnode = node.firstChild;
191 while (cnode != null) {
192
193 var jsonChildNode
194 = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
195
196 if (jsonChildNode != null) {
197 jsonChildNodes.push(jsonChildNode);
198 }
199
200 cnode = cnode.nextSibling;
201 }
202
203 jsonNode.childNodes = jsonChildNodes;
204 }
205
206 }
207 /* currently do nothing for the other node types */
208
209 return jsonNode;
210}
211
212
213//var body = document.body;
214//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.