DataObjects/Core_notify.php
[Pman.Core] / Pman.Gnumeric.js
index 8b73797..c5ef588 100644 (file)
@@ -58,7 +58,7 @@ Pman.Gnumeric = function (cfg)
             * Fires when source document has been loaded
             * @param {Pman.Gnumerci} this
             */
-           "load" : true
+           'load' : true
     }); 
     
     Roo.util.Observable.call(this,cfg);
@@ -126,7 +126,10 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
      * @type {Object} rowInfo - list of row sizes
      */
     rowInfo : false,
-    
+     /**
+     * @type {Object} rowInfoDom - dom elements with sizes
+     */
+    rowInfoDom : false,
     /**
      * @type {Number} cmax - maximum number of columns
      */
@@ -163,7 +166,9 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         this.sheet = false;
         this.grid = false;
         this.colInfo = false;
+        this.colInfoDom = false;
         this.rowInfo = false;
+        this.rowInfoDom = false;
         this.cmax = false;
         this.rmax = false;
         
@@ -340,10 +345,12 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         ci = this.sheet.getElementsByTagNameNS('*','RowInfo');
         
         this.rowInfo = {};
+        this.rowInfoDom = {};
         Roo.each(ci, function(c) {
             var count = c.getAttribute('Count') || 1;
             var s =  c.getAttribute('No')*1;
             for(var i =0; i < count; i++) {
+                _t.rowInfoDom[s+i] = c;
                 _t.rowInfo[s+i] = Math.floor(c.getAttribute('Unit')*1);
             }
         });
@@ -525,16 +532,16 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         //Roo.log( cs.r+ ',' + cs.c + ' = '+ v);
         // need to generate clell if it doe
         if (typeof(this.grid[cs.r]) == 'undefined') {
-            Roo.log('no row:' + cell);
+            //Roo.log('no row:' + cell);
             this.grid[cs.r] = []; // create a row..
             //return;
         }
         if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
-            Roo.log('cell not defined:' + cell);
+            //Roo.log('cell not defined:' + cell);
             this.createCell(cs.r,cs.c);
         }
         if (typeof(this.grid[cs.r][cs.c].dom) == 'undefined') {
-            Roo.log('no default content for cell:' + cell);
+            ///Roo.log('no default content for cell:' + cell);
             this.createCell(cs.r,cs.c);
             //return;
         }
@@ -700,6 +707,60 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         }
             
     },
+    
+    readTableData : function(table)
+    {
+        // read the first row.
+        var tds = Roo.get(table).select('tr').item(0).select('td');
+        var maxnc = 0;
+        
+        Roo.get(table).select('tr').each(function(trs) {
+            var nc = 0;
+           
+            trs.select('td').each(function(td) {
+                var cs = td.dom.getAttribute('colspan');
+                cs = cs ? cs * 1 : 1;
+                nc += cs;
+            });
+            maxnc = Math.max(nc, maxnc);
+        });
+        
+        var tr = document.createElement('tr');
+        table.appendChild(tr);
+        var ar = {};
+        for (i =0; i < maxnc; i++) {
+            ar[i] = document.createElement('td');
+            tr.appendChild(ar[i]);
+        }
+        // find the left.
+        var ret = { cols : maxnc, pos : {} };
+        for (i =0; i < maxnc; i++) {
+            ret.pos[ Roo.get(ar[i]).getLeft()] =i;
+        }
+        ret.near = function(p) {
+            // which one is nearest..
+            
+            if (this.pos[p]) {
+                return this.pos[p];
+            }
+            var prox = 100000;
+            var match = 0;
+            for(var i in this.pos) {
+                var dis = Math.abs(p-i);
+                if (dis < prox) {
+                    prox = dis;
+                    match = this.pos[i];
+                }
+            }
+            return match;
+            
+        }
+        table.removeChild(tr);
+        return ret;
+    },
+    
+     
+   
      
     /**
      * importTable: 
@@ -719,93 +780,77 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         xoff = xoff || 0;
         yoff = yoff || 0;
         
+        
+        var table_data = this.readTableData(datagrid);
+        
+        // oroginally this cleaned line breaks, but we acutally need them..
         var cleanHTML = function (str) {
             
-             var ret = str;
-            ret = ret.replace(/&nbsp;/g,'.');
-            ret = ret.replace(/\n/g,'.');
-            ret = ret.replace(/\r/g,'.');
+            var ret = str;
+            ret = ret.replace(/&nbsp;/g,' ');
+           // ret = ret.replace(/\n/g,'.');
+          //  ret = ret.replace(/\r/g,'.');
             var i;
-            while (-1 != (i = ret.indexOf(unescape('%A0')))) {
-                ret = ret.substring(0,i) + ' ' + ret.substring(i+1,str.length);
-            }
+             
             return ret;
         };
 
         
         // <cell col="A" row="1">Test< / cell>
         // <cell col="B" row="2" type="Number" format="test1">30< / cell>
-        var rowOffsets = [];
+        var rowOffsets = {};
         var rows = datagrid.getElementsByTagName('tr');
         //alert(rows.length);
         
+        
         for(var row=0;row<rows.length;row++) {
-            //var style = document.defaultView.getComputedStyle(rows[row], "");
             
-            //if (rows[row].getAttribute('xls:height')) {
-            //    this.setRowHeight(row+y_offset, 0 + rows[row].getAttribute('xls:height'));
-            //} else {
-            //    this.setRowHeight(row+y_offset, 0 + style.height.replace(/[^0-9.]+/g,''));
-           // }
+            // let's see what affect this has..
+            // it might mess things up..
             
-            var coloffset = 0;
-           // if (rowOffsets[row]) {
-           //     coloffset += rowOffsets[row];
-           // }
+            if (rows[row].getAttribute('xls:height')) {
+                this.setRowHeight(row + yoff +1, 1* rows[row].getAttribute('xls:height'));
+            } else {
+                this.setRowHeight( row + yoff +1, Roo.get(rows[row]).getHeight());
+            }
+            
+         
             var cols = rows[row].getElementsByTagName('td');
             
             
             for(var col=0;col < cols.length; col++) {
                 
                 
-                //var colat = col + coloffset;
-                coloffsetadd = 0;
-                if (cols[col].getAttribute('colspan') && (cols[col].getAttribute('colspan') > 1)) {
+               
+                
+                var colspan = cols[col].getAttribute('colspan');
+                colspan  = colspan ? colspan *1 : 1;
+                
+                var rowspan = cols[col].getAttribute('rowspan');
+                rowspan = rowspan ? rowspan * 1 : 1;
+                
+                var realcol = table_data.near( Roo.get(cols[col]).getLeft() );
+                
+                
+                
+                if (colspan > 1 || rowspan > 1) {
                     
-                   //row + yoff, c : col + xoff + coloffset
+                    // getting thisese right is tricky..
                     this.mergeRegion(
-                        col + xoff + coloffset,
-                        row + yoff,
-                        col + xoff + coloffset + (cols[col].getAttribute('colspan') - 1), 
-                        row + yoff /*+ (
-                                (cols[col].getAttribute('rowspan') > 1) ?
-                                    (cols[col].getAttribute('rowspan') - 1) : 0
-                                )*/
+                        realcol + xoff,
+                        row + yoff +1,
+                        realcol+ xoff + (colspan -1),
+                        row + yoff + rowspan 
                     );
-                    coloffsetadd  = (cols[col].getAttribute('colspan')*1) - 1;
                     
-                }
-                /*
-                if (cols[col].getAttribute('rowspan') && (cols[col].getAttribute('rowspan') > 1)) {
-                    // this should really do a merge, but it's pretty damn complex...
-                    //this.mergeRegion(colat,row +y_offset,colat + (cols[col].getAttribute('colspan') - 1), row+y_offset);
-                    var rroff = cols[col].getAttribute('colspan')  ? (cols[col].getAttribute('colspan') -0): 1;
-                    var rr = 0;
-                    for (rr = 0; rr < cols[col].getAttribute('rowspan');rr++) {
-                        rowOffsets[rr + row] = col + rroff;
-                    }
-                    
-                }
-                 */
-               
-                /*
-                var style = this.newStyle();
-                if (style.setFrom(cols[col])) {
-                    style.add(
-                        colat+x_offset,
-                        row+y_offset,
-                        
-                        colat+x_offset + ((cols[col].getAttribute('colspan') > 1) ?
-                                    (cols[col].getAttribute('colspan') - 1) : 0),
-                        row+y_offset  + ((cols[col].getAttribute('rowspan') > 1) ?
-                                    (cols[col].getAttribute('rowspan') - 1) : 0) 
-                    );
                 }
                 
-                 */
                 // skip blank cells
+                // set the style first..
+                this.parseHtmlStyle( cols[col], row + yoff, realcol + xoff   , colspan, rowspan);
+                
                 if (!cols[col].childNodes.length) {
-                    coloffset += coloffsetadd;
+                     
                     continue;
                 }
                 
@@ -844,28 +889,13 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
                        
                         break;
                 }
-                /*
-                if (cols[col].getAttribute('xls:src')) {
-                    //alert(cols[col].childNodes[0].width);
-                    if (this.writeImage(
-                        row+y_offset, 
-                        colat+x_offset+coloffset, 
-                        cols[col].getAttribute('xls:src'), 
-                        cols[col].childNodes[0].width, 
-                        cols[col].childNodes[0].height
-                        )) {
-                       
-                    }
-                    continue;
-                }
-                */
-                 
+               
                 if (!cols[col].childNodes[0].nodeValue) {
-                    coloffset += coloffsetadd;
+                   
                     continue;
                 }
                 if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
-                    coloffset += coloffsetadd;
+                  
                     continue;
                 }
                 // strip me.!
@@ -888,14 +918,9 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
                     vt = '';
                     cell_value_text = s.replace(/#row#/g,(row + yoff + 1));
                 }
-                this.set({ r: row + yoff, c : col + xoff + coloffset }, cell_value_text, vt, vf);
+                this.set({ r: row + yoff, c : realcol + xoff }, cell_value_text, vt, vf);
                  
-                this.parseHtmlStyle( cols[col], row + yoff, col + xoff + coloffset , coloffsetadd);
-                 
-                coloffset += coloffsetadd;
-                
-                
-                
+                  
                 
                 
                 
@@ -907,14 +932,15 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
     
     
     
-    parseHtmlStyle : function(dom, row, col, cspan) {
+    parseHtmlStyle : function(dom, row, col, colspan, rowspan) {
         
         function toCol (rgb) {
-            var ar = rgb.replace(/rgb\(/, '').replace(/\)/, '').split(',');
+            
+            var ar = rgb.replace(/rgb[a]?\(/, '').replace(/\)/, '').replace(/ /, '').split(',');
             var rcs = [];
+            ar = ar.slice(0,3);
             Roo.each(ar, function(c) { 
-                var hex = (c*256).toString(16);
-                rcs.push( ('0000' + hex).substring(  hex.length)); 
+                rcs.push((c*c).toString(16)) ;   
             });
             return rcs.join(':');
             
@@ -931,10 +957,15 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             
             'color': function(ent,v) { 
                 ent['Fore'] = toCol(v);
+                // this is a bit dumb.. we assume that if it's not black text, then it's shaded..
+                if (ent['Fore'] != '0:0:0') {
+                    ent['Shade'] = 1;
+                }
+                
             },
             'background-color' : function(ent,v) { 
                 ent['Back'] = toCol(v);
-                
+                 
             }
             
         }
@@ -962,22 +993,31 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             }
             map[k](ent,val);
         }
+        // special flags..
+        if (el.dom.getAttribute('xls:wraptext')) {
+            ent.WrapText = 1;
+        }
+        if (el.dom.getAttribute('xls:valign')) {
+            ent.VAlign= 1;
+        }
+        if (el.dom.getAttribute('xls:halign')) {
+            ent.HAlign= 1;
+        }
         // fonts..
         var fmap = {
             
            
             'font-size' : function(ent,v) { 
-                ent['FontUnit'] = v.replace(/px/, '');
+                ent['Unit'] = v.replace(/px/, '');
             },
             'font-weight' : function(ent,v) { 
                 if (v != 'bold') return;
-                ent['FontBold'] = 1;
+                ent['Bold'] = 1;
+            },
+            'font-style' : function(ent,v) { 
+                if (v != 'italic') return;
+                ent['Italic'] = 1;
             } 
-            //FontItalic : function(ent,v) { 
-            //    if (v*0 < 1) return;
-            //    //ent['font-weight'] = 'bold';
-            //},
-            
         }
        
         var fent = {
@@ -993,10 +1033,12 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             if (!val || !val.length) {
                continue;
             }
-            fmap[k](ent,val);
+            fmap[k](fent,val);
         }
         var font = el.getStyle('font-family') || 'Sans';
-        
+        if (font.split(',').length > 1) {
+            font = font.split(',')[1].replace(/\s+/, '');
+        }
         
         
         /// -- now create elements..
@@ -1006,17 +1048,19 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         //<gnm:StyleRegion startCol="0" startRow="0" endCol="255" endRow="65535"
         var sr = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleRegion');
         objs.appendChild(sr);
+        objs.appendChild(this.doc.createTextNode("\n"));// add a line break..
+
         sr.setAttribute('startCol', col);
-        sr.setAttribute('endCol', col+coloffsetadd);
+        sr.setAttribute('endCol', col+ colspan-1);
         sr.setAttribute('startRow', row);
-        sr.setAttribute('endRow', row);
+        sr.setAttribute('endRow', row + rowspan -1);
         
         
         var st = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Style');
         sr.appendChild(st);
         // do we need some defaults..
         for(var k in ent) {
-            Roo.log(k);
+            //Roo.log(k);
             st.setAttribute(k, ent[k]);
         }
         
@@ -1024,7 +1068,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         st.appendChild(fo);
         // do we need some defaults..
         for(var k in fent) {
-            fo.setAttribute(k, ent[k]);
+            fo.setAttribute(k, fent[k]);
         }
         fo.textContent  = font;
         
@@ -1032,13 +1076,13 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         // borders..
         Roo.each(['top','left','bottom','right'], function(p) {
             var w = el.getStyle('border-' + p + '-width').replace(/px/, '');
-            if (!w.length) {
+            if (!w || !w.length || (w*1) < 1) {
                 return;
             }
             if (!sb) {
                 sb= this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:StyleBorder');
             }
-            var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(2));
+            var be = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:' + p[0].toUpperCase() + p.substring(1));
             be.setAttribute('Style', '1');
             be.setAttribute('Color', '0:0:0'); // fixme..
             sb.appendChild(be);
@@ -1047,7 +1091,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         // start adding them all together..
         
         if (sb) {
-            st.appendChild(sb)
+            st.appendChild(sb);
         }
         
         
@@ -1106,7 +1150,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         // step 1 - work out how many columns it will span..
         // lets hope the spreadsheet is big enought..
         var colwidth = 0;
-        var endcol=col
+        var endcol=col;
         for ( endcol=col;endcol <100; endcol++) {
             if (!this.colInfo[endcol]) {
                 this.colInfo[endcol] = 100; // eak fudge
@@ -1170,7 +1214,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
        //     return;
         //}
         
-        cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2)
+        cell.textContent = this.RCtoCell(row1,col1) + ':' + this.RCtoCell(row2,col2);
         
         //var merges = this.gnumeric.getElementsByTagNameNS('*','MergedRegions');
         var merges = this.sheet.getElementsByTagNameNS('*','MergedRegions');
@@ -1180,7 +1224,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             this.sheet.insertBefore(merges,sl);
         } else {
             merges = merges[0];
-           }
+        }
         merges.appendChild(cell);
     
     },
@@ -1197,6 +1241,14 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         //<gmr:Rows DefaultSizePts="12.75">
         //   <gmr:RowInfo No="2" Unit="38.25" MarginA="0" MarginB="0" HardSize="1"/>
     //  < /gmr:Rows>
+        
+        // this doesnt handle row ranges very well.. - with 'count in them..'
+        
+        if (this.rowInfoDom[r]) {
+            this.rowInfoDom[r].setAttribute('Unit', height);
+            return;
+        }
+    
         var rows = this.sheet.getElementsByTagNameNS('*','Rows')[0]; // assume this exists..
         var ri = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd','gnm:RowInfo');
         // assume we have no rows..
@@ -1206,6 +1258,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         ri.setAttribute('MarginB', 0);
         ri.setAttribute('HardSize', 1);
         rows.appendChild(ri);
+        this.rowInfoDom[r] = ri;
     },
      
     /**
@@ -1234,7 +1287,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             sheetnames = this.doc.getElementsByTagNameNS('*','Sheet');
             sheetnames[0].parentNode.appendChild(sheetnames[sheetnames.length-1].cloneNode(true));
             var sn = this.doc.getElementsByTagNameNS('*','Sheet')[sheet];
-            var cls = sn.getElementsByTagNameNS('*','Cells')[0]
+            var cls = sn.getElementsByTagNameNS('*','Cells')[0];
             while (cls.childNodes.length) {
                 cls.removeChild(cls.firstChild);
             }
@@ -1346,6 +1399,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         var ser = new XMLSerializer();
         var x = new Pman.Download({
             method: 'POST',
+            timeout : 120000, // quite a long wait.. 2 minutes.
             params : {
                xml : ser.serializeToString(this.doc),
                format : 'xls', //xml