DataObjects/Core_company_type.php
[Pman.Core] / Pman.Gnumeric.js
index ccc4d97..60e1ab2 100644 (file)
@@ -64,6 +64,8 @@ Pman.Gnumeric = function (cfg)
     Roo.util.Observable.call(this,cfg);
     
     this.defaultCell = {
+        c : 0,
+        r : 0,
         valueType : 0,
         valueFormat : '',
         value : '',
@@ -89,10 +91,10 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
      */
     data : false,
      /**
-     * @cfg {String} downloadUrl where GnumerictoExcel.php is...
+     * @cfg {String} downloadURL where GnumerictoExcel.php is...
      */
      
-    downloadUrl : false,
+    downloadURL : false,
     
     /**
      * @type {XmlDocument} doc the gnumeric xml document
@@ -103,6 +105,11 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
      * @type {XmlNode} sheet the 'Sheet' element 
      */
     sheet : false,
+    
+    /**
+     * @type {XmlNode} sheet the 'Cells' element 
+     */    
+    cellholder : false,
     /**
      * @type {Object} grid the map[row][col] = cellData 
      */
@@ -124,22 +131,49 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
      * @type {Object} rmax - maximum number of rows
      */
     rmax : false,
+       /**
+     * @type {String} stylesheetID id of stylesheet created to render spreadsheat
+     */
+    stylesheetID : false,
+    /**
+     * @type {Number} rowOffset - used by table importer to enable multiple tables to be improted
+     */
+    
+    rowOffset : 0,
+    
     /**
      * load:
      * run the connection, parse document and fire load event..
+     * can be run multiple times with new data..
      * 
     */
     
     load : function(url)
     {
-        url = url || this.url;
-        if (!url) {
+        this.url = url || this.url;
+        if (!this.url) {
             return;
         }
+        // reset stufff..
+        this.doc = false;
+        this.sheet = false;
+        this.grid = false;
+        this.colInfo = false;
+        this.rowInfo = false;
+        this.cmax = false;
+        this.rmax = false;
+        
+        if (this.stylesheetID) {
+            
+            Roo.util.CSS.removeStyleSheet(this.stylesheetID);
+            this.stylesheetID = false;
+            
+        }
+        
         _t = this;
         var c = new Roo.data.Connection();
         c.request({
-            url: url,
+            url: this.url,
             method:  'GET',
             success : function(resp, opts) {
                 _t.response = resp;
@@ -161,6 +195,8 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
     },
     
     
+     
+    
     
     /**
      * toRC:
@@ -210,6 +246,9 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         this.cmax = 1;
         
         this.sheet = _t.doc.getElementsByTagNameNS('*','Sheet')[0];
+        
+        
+        this.cellholder = this.sheet.getElementsByTagNameNS('*','Cells')[0];
         var cells = this.sheet.getElementsByTagNameNS('*','Cell');
 
         
@@ -231,7 +270,9 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
                 valueType : vt,
                 valueFormat : vf,
                 value : val,
-                dom: c
+                dom: c,
+                r: row,
+                c: col
             }, _t.defaultCell);
         });
        
@@ -253,7 +294,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             var rc = _t.rangeToRC(c.textContent);
             //Roo.log(JSON.stringify(rc))
             if (typeof(_t.grid[rc[0].r][rc[0].c]) == 'undefined') {
-                _t.grid[rc[0].r][rc[0].c] =  Roo.apply({}, _t.defaultCell);
+                _t.grid[rc[0].r][rc[0].c] =  Roo.applyIf({ r : rc[0].r, c : rc[0].c }, _t.defaultCell);
             }
                 
             _t.grid[rc[0].r][rc[0].c].colspan = (rc[1].c - rc[0].c) + 1;
@@ -312,13 +353,18 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
                    continue;
                 }
                 for (var c = s.c; c < s.c1;c++) {
-                   if (c > _t.cmax) continue;
+                    if (c > _t.cmax) continue;
     
-                    if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.apply({}, _t.defaultCell);
+                    if (typeof(_t.grid[r][c]) == 'undefined') _t.grid[r][c] = Roo.applyIf({ r: r , c : c }, _t.defaultCell);
                     var g=_t.grid[r][c];
-                    if (typeof(g.cls) =='undefined') g.cls = [];
+                    if (typeof(g.cls) =='undefined') {
+                        g.cls = [];
+                        g.styles = [];
+                    }
                     if (g.cls.indexOf(s.name)  > -1) continue;
                     g.cls.push(s.name);
+                    g.styles.push(s.dom);
+                    
                 }
             }
         });
@@ -399,9 +445,10 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
                 r : sr.getAttribute('startRow')*1,
                 c1 : (sr.getAttribute('endCol')*1) +1,
                 r1 : (sr.getAttribute('endRow')*1) +1,
-                style : {},
-                gstyle : {},
-                name : sid +'-gstyle-' + n
+                style : {},  // key val of style for HTML..
+                gstyle : {}, // key val of attributes used..
+                name : sid +'-gstyle-' + n,
+                dom : sr
                 
             };
     
@@ -433,7 +480,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
         
         this.styles = styles;
         
-        
+        this.stylesheetID = sid;
         Roo.util.CSS.createStyleSheet(css, sid);
     },
 
@@ -441,25 +488,150 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
     /**
      * set: 
      * Set the value of a cell..
-     * @param {String} cell name of cell, eg. C10
+     * @param {String} cell name of cell, eg. C10 or { c: 1, r :1 }
+         
      * @param {Value} value to put in cell..
+     * @param {ValueType} type of value
+     * @param {ValueFormat} value format of cell
      * 
      * Cells should exist at present, we do not make them up...
      */
      
     
-    set : function(cell, v) {
+    set : function(cell, v, vt, vf) {
         
         var cs= typeof(cell) == 'string' ? this.toRC(cell) : cell;
-        Roo.log(    this.grid[cs.r][cs.c]);
-        this.grid[cs.r][cs.c].value=  v;
+        //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);
+            this.grid[cs.r] = []; // create a row..
+            //return;
+        }
+        if (typeof(this.grid[cs.r][cs.c]) == 'undefined') {
+            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);
-            return;
+            this.createCell(cs.r,cs.c);
+            //return;
         }
+        this.grid[cs.r][cs.c].value=  v;
         this.grid[cs.r][cs.c].dom.textContent=  v;
+        if (typeof(vt != 'undefined') && vt) {
+            this.grid[cs.r][cs.c].valueType = vt;
+            this.grid[cs.r][cs.c].dom.setAttribute('ValueType', vt);
+        }
+        if (typeof(vf != 'undefined') && vf) {
+            this.grid[cs.r][cs.c].valueFormat = vf;
+            this.grid[cs.r][cs.c].dom.setAttribute('ValueFormat', vf);
+        }
+        
     },
+    
+    
+    copyRow : function(src, dest) {
+        if (dest == src) {
+            return;
+        }
+       // Roo.log('create Row' + dest);
+        if (typeof(this.grid[dest]) == 'undefined') {
+            this.grid[dest] = {}
+        }
+        
+           
+        for (var c = 0; c < this.cmax; c++) {
+
+            this.copyCell({ r: src, c: c } , { r: dest, c: c});
+            
+        }
+        this.rmax = Math.max(this.rmax, dest +1);
+        
+    },
+    
+    createCell: function(r,c)
+    {
+        //<gnm:Cell Row="6" Col="5" ValueType="60">Updated</gnm:Cell>    
+        var nc = this.doc.createElementNS('http://www.gnumeric.org/v10.dtd', 'gnm:Cell');
+        this.cellholder.appendChild(nc);
+        var lb = this.doc.createTextNode("\n");// add a line break..
+        this.cellholder.appendChild(lb);
+        
+        nc.setAttribute('Row', new String(r));
+        nc.setAttribute('Col', new String(c));
+        nc.setAttribute('ValueType', '60');
+        nc.textContent = '';
+        
+        this.grid[r][c] = Roo.applyIf({
+            valueType : '60',
+            valueFormat : '',
+            value : '',
+            dom: nc,
+            r: r,
+            c: c
+            }, _t.defaultCell);
+        
+        return nc;
+
+    },
+    
+    
+    copyCell : function(src, dest)
+    {
+        var old = this.grid[src.r][src.c];
+        // is it an alias...
+        if ((old.c != src.c)  || (old.r != src.r)) {
+            // only really works on horizonatal merges..
+            
+            this.grid[dest.r][dest.c] = this.grid[desc.r][old.c]; // let's hope it exists.
+            return;
+        }
+        
+        
+        var nc = Roo.apply({}, this.grid[src.r][src.c]);
+        
+        nc.value = '';
+        if (typeof(old.dom) == 'undefined') {
+            Roo.log("No cell to copy for " + Roo.encode(src));
+            return;
+        }
+        this.grid[dest.r][dest.c] = nc;
+        nc.dom = old.dom.cloneNode(true);
+        nc.dom.setAttribute('Row', dest.r);
+        nc.dom.setAttribute('Cell', dest.c);
+        nc.dom.textContent = '';
+        old.dom.parentNode.appendChild(nc.dom);
+        if (!old.styles || !old.styles.length) {
+            return;
+        }
+        //Roo.log("DEST");
+        //Roo.log(dest);
+        //Roo.log("STYLES");
+        //  .styles...
+        Roo.each(old.styles, function(s) {
+            // try and extend existing styles..
+            var er = s.getAttribute('endRow') * 1;
+            var ec = s.getAttribute('endCol') * 1;
+            //Roo.log(s);
+            if (dest.r == er) {
+                s.setAttribute('endRow', dest.r + 1);
+            }
+            if (dest.c == ec) {
+                s.setAttribute('endCol', dest.c + 1);
+            }
+            /*var ns = s.cloneNode(true);
+            s.parentNode.appendChild(ns);
+            ns.setAttribute('startCol', dest.c);
+            ns.setAttribute('startRow', dest.r);
+            ns.setAttribute('endCol', dest.c + 1);
+            ns.setAttribute('endRow', dest.r +1);
+            */
+        });
+        
+    },
+    
+    
     /**
      * applyData: 
      * Set the value of a cell..
@@ -498,8 +670,197 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             }
         }
             
+    },
+     // now the rows..
+    importTable : function (datagrid, xoff,yoff)
+    {
+        if (!datagrid) {
+            Roo.log("Error table not found!?");
+            return;
+        }
+        xoff = xoff || 0;
+        yoff = yoff || 0;
+        
+        var cleanHTML = function (str) {
+            
+             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 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,''));
+           // }
+            
+            //var coloffset = 0;
+           // if (rowOffsets[row]) {
+           //     coloffset += rowOffsets[row];
+           // }
+            var cols = rows[row].getElementsByTagName('td');
+            
+            
+            for(var col=0;col < cols.length; col++) {
+                
+                
+                //var colat = col + coloffset;
+                /*
+                if (cols[col].getAttribute('colspan') && (cols[col].getAttribute('colspan') > 1)) {
+                    
+                    
+                    this.mergeRegion(
+                        colat,
+                        row +y_offset,
+                        colat + (cols[col].getAttribute('colspan') - 1), 
+                        row+y_offset + (
+                                (cols[col].getAttribute('rowspan') > 1) ?
+                                    (cols[col].getAttribute('rowspan') - 1) : 0
+                                )
+                    );
+                    
+                    
+                    
+                    coloffset += (cols[col].getAttribute('colspan') - 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
+                if (!cols[col].childNodes.length) {
+                    continue;
+                }
+                
+                
+                
+                
+                var vt = '60';
+                var vf = false;
+                
+                switch(cols[col].getAttribute('xls:type')) {
+                    case 'int':
+                        vt = 30; // int!!!!
+                        break;
+                        
+                    case 'float':
+                        vt = 40; // float!!!!
+                        if (cols[col].getAttribute('xls:floatformat')) {
+                                vf = cols[col].getAttribute('xls:floatformat');
+                        }
+                        break;
+                        
+                    case 'date':
+                        vt = 30;
+                        //ValueFormat="d/m/yyyy" 38635  
+                        var vf = 'd/m/yyy';
+                        if (cols[col].getAttribute('xls:dateformat')) {
+                            vf= cols[col].getAttribute('xls:dateformat');
+                        }
+                        
+                       
+                        
+                        break;
+                    
+                    default:
+                       
+                        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) {
+                    continue;
+                }
+                if (!cols[col].childNodes[0].nodeValue.replace(/^\s*|\s*$/g,"").length) {
+                    continue;
+                }
+                // strip me.!
+                var cell_value_text = cleanHTML(cols[col].childNodes[0].nodeValue);
+       
+                if (cols[col].getAttribute('xls:percent')) {
+                    cell_value_text = '' + ((cell_value_text * 1) / 100);
+                }
+
+                if (cell_value_text.length && (vt == 30)) {
+                    var bits = cell_value_text.split(/-/);
+                    var cur = new Date(bits[0],bits[1]-1,bits[2]);
+                    cell_value_text = '' + Math.round((cur.getTime() - Date.UTC(1899,11,30)) / (24 * 60 * 60 * 1000));
+                }
+
+                
+                
+                if (cols[col].getAttribute('xls:formula')) {
+                    var s = cols[col].getAttribute('xls:formula');
+                    cell.removeAttribute('ValueType');
+                    cell_value_text = s.replace(/#row#/g,(row + y_offset + 1));
+                }
+                this.set({ r: row + yoff, c : col + xoff }, cell_value_text, vt, vf);
+                
+                
+                
+                
+                
+            }
+        }
+        this.rowOffset += rows.length;
+        
     },
     
+
     
      /**
      * toHTML: 
@@ -525,6 +886,7 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
             out += '<tr style="height:'+this.rowInfo[r]+'px;">';
             for (var c = 0; c < this.cmax;c++) {
                 var g = (typeof(grid[r][c]) == 'undefined') ? defaultCell  : grid[r][c];
+                
                 if (typeof(g.cls) =='undefined') g.cls = [];
                 var w= calcWidth(c,g.colspan);
                 out+=String.format('<td colspan="{0}" rowspan="{1}"  class="{4}"><div style="{3}">{2}</div></td>', 
@@ -557,18 +919,22 @@ Roo.extend(Pman.Gnumeric, Roo.util.Observable, {
     {
         name = name || "Missing_download_filename";
         
+        if (this.downloadURL && this.downloadURL.charAt(this.downloadURL .length-1) != '/') {
+            this.downloadURL += '/';
+        }
+        
         var ser = new XMLSerializer();
         var x = new Pman.Download({
             method: 'POST',
             params : {
                xml : ser.serializeToString(this.doc),
-               format : 'xml',
+               format : 'xls', //xml
                debug : 0
                
             },
-            url : ((this.downloadUrl || (baseURL + '/GnumericToExcel/')) + name + '.xls',
+            url : (this.downloadURL || (baseURL + '/GnumericToExcel/')) + name + '.xls',
             success : function() {
-                Roo.MessageBox("Alert", "File should have downloaded now");
+                Roo.MessageBox.alert("Alert", "File should have downloaded now");
                 if (callback) {
                     callback();
                 }