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

Last change on this file since 26767 was 26767, checked in by davidb, 11 years ago

Work done on linking collection browsing frames as well as linking to matching collection item frames.

  • Property svn:executable set to *
File size: 7.4 KB
Line 
1//javascript:
2var expSyntaxArray = [];
3
4var font_family;
5var font_size;
6var font_color;
7var font_weight;
8var font_style;
9
10var compute_font = false;
11var compute_width = false;
12
13Element.prototype.getElementWidth = function() {
14 if (typeof this.clip !== "undefined") {
15 return this.clip.width;
16 } else {
17 if (this.style.pixelWidth) {
18 return this.style.pixelWidth;
19 } else {
20 return this.offsetWidth;
21 }
22 }
23};
24
25Element.prototype.getElementHeight = function() {
26 if (typeof this.clip !== "undefined") {
27 return this.clip.height;
28 } else {
29 if (this.style.pixelHeight) {
30 return this.style.pixelHeight;
31 } else {
32 return this.offsetHeight;
33 }
34 }
35};
36
37function getElementWidth(elem)
38{
39 if (typeof elem.clip !== "undefined") {
40 return elem.clip.width;
41 } else {
42 if (elem.style && elem.style.pixelWidth) {
43 return elem.style.pixelWidth;
44 } else {
45 return elem.offsetWidth;
46 }
47 }
48}
49
50function getElementHeight(elem)
51{
52 if (typeof elem.clip !== "undefined") {
53 return elem.clip.height;
54 } else {
55 if (elem.style && elem.style.pixelHeight) {
56 return elem.style.pixelHeight;
57 } else {
58 return elem.offsetHeight;
59 }
60 }
61}
62
63function hyphenToInitialCapReplacer(str, p1, offset, s)
64{
65 return p1.toUpperCase();
66}
67
68function getStyle(node,style_prop)
69{
70 var prop_val;
71 if (node.currentStyle) {
72 /* IE */
73 style_prop = strCssRule.replace(/\-(\w)/g, hyphenToInitialCapReplacer);
74 prop_val = node.currentStyle[style_prop];
75 }
76 else if (window.getComputedStyle) {
77 /* Firefox */
78 var computed_style = document.defaultView.getComputedStyle(node,null)
79 prop_val = computed_style.getPropertyValue(style_prop);
80 }
81 else {
82 /* try for inline value */
83 prop_val = el.style[style_prop];
84 }
85
86 return prop_val;
87
88}
89
90function getElementPosition(elem)
91{
92 var origElem = elem;
93
94 var xl_ = 0;
95 var yt_ = 0;
96
97 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
98 xl_ += elem.offsetLeft - elem.scrollLeft;
99 yt_ += elem.offsetTop - elem.scrollTop;
100 elem = elem.offsetParent;
101 }
102
103 var xr_ = xl_ + getElementWidth(origElem);
104 var yb_ = yt_ + getElementHeight(origElem);
105
106 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
107}
108
109
110function htmlToExpeditee(node,compFont,compWidth)
111{
112 compute_font = compFont;
113 compute_width = compWidth;
114
115 var nodePos = getElementPosition(node);
116 var parentStyle = node.getAttribute('style');
117
118 //Use these as default values if font info can't be found for text items.
119 //and only if the user wants the font computed (otherwise use Expeditee defaults).
120 if(compute_font){
121 font_family = getStyle(node,'font-family');
122 font_size = getStyle(node,'font-size');
123 font_color = getStyle(node,'color');
124 }
125
126 var pxl = nodePos.xl;
127 var pyt = nodePos.yt;
128 var pxr = nodePos.xr;
129 var pyb = nodePos.yb;
130
131 return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
132}
133
134
135function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
136{
137 depth = (depth) ? depth : 0;
138
139 var jsonNode = null;
140
141
142 if (node.nodeType == 3) { /* text node */
143
144 var text = node.nodeValue;
145
146 if (text.match(/\S/)) {
147 jsonNode = {};
148
149 jsonNode.type = "text";
150 jsonNode.text = node.nodeValue;
151
152 jsonNode.xl = pxl;
153 jsonNode.yt = pyt;
154 jsonNode.xr = pxr;
155 jsonNode.yb = pyb;
156
157 var parent = node.parentNode;
158
159 if (parent != null) {
160
161 //set a frame link attribute
162 var link = parent.attributes["link"];
163
164 if(link !== null && link !== undefined){
165 console.log(link.value);
166 jsonNode.link = link.value;
167 }
168
169 if(compute_font){
170 //obtain font info for text nodes from their parent nodes.
171 var new_font_family = getStyle(parent,"font-family");
172 var new_font_size = getStyle(parent,"font-size");
173 var new_font_color = getStyle(parent,"color");
174
175 var new_font_weight = getStyle(parent,"font-weight");
176 var new_font_style = getStyle(parent,"font-style");
177
178 if(new_font_family !== undefined && new_font_family !== null)
179 font_family = new_font_family;
180
181 if(new_font_size !== undefined && new_font_size !== null)
182 font_size = new_font_size;
183
184 if(new_font_color !== undefined && new_font_color !== null)
185 font_color = new_font_color;
186
187 //TODO: if parent node is a <b> element, then set style["font-weight"] to be "bold".
188 if(new_font_weight !== undefined && new_font_weight !== null)
189 font_weight = new_font_weight;
190
191 //TODO: if parent node is an <i> element, then set style["font-style"] to be "italic".
192 if(new_font_style !== undefined && new_font_style !== null)
193 font_style = new_font_style;
194
195 var style = {};
196 style["font-family"] = font_family;
197 style["font-size"] = font_size;
198 style["color"] = font_color;
199 style["font-weight"] = font_weight;
200 style["font-style"] = font_style;
201
202 jsonNode.style = style;
203 }
204
205 if(compute_width){
206 var parentPos = getElementPosition(parent);
207 var parentWidth = parentPos.xr - parentPos.xl;
208
209 jsonNode.width = parentWidth;
210 }
211
212 var data = parent.attributes["data"];
213
214 if (data != null) {
215 jsonNode.data = data.value;
216 }
217
218
219 }
220 }
221
222
223
224 }
225 else if (node.nodeType == 1) { /* element */
226
227 /* need to handle: img, a, li */
228 /* need to dig out: text size, l/r/justified, font-face, type, colour */
229
230 if (getStyle(node,"visibility").match("hidden")
231 || getStyle(node,"display").match("none")) {
232
233 return null;
234 }
235
236 var elemName = node.nodeName.toLowerCase();
237
238 if (!elemName.match(/script/)) {
239
240 var nodePos = getElementPosition(node);
241 var xl = nodePos.xl;
242 var xr = nodePos.xr;
243 var yt = nodePos.yt;
244 var yb = nodePos.yb;
245
246 jsonNode = {};
247 jsonNode.type = "rect";
248 jsonNode.elem = node.nodeName;
249
250 if(node.getAttribute("rect") !== undefined){
251
252 /**This is to ensure rectangles aren't drawn around "non-rectangular" items such
253 as text and empty elements such as <br/>**/
254 if(node.getAttribute("rect") === "norect"){
255 jsonNode.type = "norect";
256 }
257 }
258
259 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
260 jsonNode.rect = rect;
261
262 //TODO: Set link attribute for images.
263 if (elemName.match("img")) {
264
265 var getLink = node.getAttribute("link");
266 if(getLink !== null && getLink !== undefined){
267 jsonNode.link = getLink;
268 }
269
270 jsonNode.img = node.src;
271
272
273 }
274
275 var style = {};
276
277 style["background-color"] = getStyle(node,"background-color");
278
279 jsonNode.style = style;
280
281 var attr = node.attributes["attr"];
282
283 if (attr != null) {
284 jsonNode.attr = attr.value;
285 }
286
287 var jsonChildNodes = [];
288
289 var cnode = node.firstChild;
290 while (cnode != null) {
291
292 var jsonChildNode
293 = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
294
295 if (jsonChildNode != null) {
296 jsonChildNodes.push(jsonChildNode);
297 }
298
299 cnode = cnode.nextSibling;
300 }
301
302 jsonNode.childNodes = jsonChildNodes;
303 }
304
305 }
306 /* currently do nothing for the other node types */
307
308 return jsonNode;
309}
310
311
312//var body = document.body;
313//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.