70f868bf885a1bb37616cb6099cbbb35236bcb90.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright DHTMLX LTD. http://www.dhtmlx.com You allowed to use this component
  3. * or parts of it under GPL terms To use it on other terms or get Professional
  4. * edition of the component please contact us at sales@dhtmlx.com
  5. */
  6. dhtmlXGridObject.prototype.useCSV = function(path) {
  7. if (!this._csv_loadXML) {
  8. this._csv_loadXML = this.loadXML;
  9. this._csv_loadXMLString = this.loadXMLString;
  10. this.loadXML = this.loadCSVFile;
  11. this.loadXMLString = this.loadCSVFile
  12. }
  13. };
  14. dhtmlXGridObject.prototype.loadCSVFile = function(path, afterCall) {
  15. this.xmlLoader = new dtmlXMLLoaderObject(this._onCSVFileLoad, this);
  16. this.xmlLoader.waitCall = afterCall;
  17. this.xmlLoader.loadXML(path)
  18. };
  19. dhtmlXGridObject.prototype._onCSVFileLoad = function(obj, b, c, d, xml) {
  20. var z = this.xmlDoc.responseText;
  21. obj.loadCSVString(z)
  22. };
  23. dhtmlXGridObject.prototype.enableCSVAutoID = function(mode) {
  24. this._csvAID = convertStringToBoolean(mode)
  25. };
  26. dhtmlXGridObject.prototype.enableCSVHeader = function(mode) {
  27. this._csvHdr = convertStringToBoolean(mode);
  28. if (this._csvHdr)
  29. this.enableCSVAutoID(true)
  30. };
  31. dhtmlXGridObject.prototype.setCSVDelimiter = function(str) {
  32. this._csvDelim = (str || this._csvDelim);
  33. this._csvDelimX = this._csvDelim.charCodeAt(0);
  34. var trans = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
  35. this._csvDelimX = "\\x" + trans[Math.floor(this._csvDelimX / 16)] + ""
  36. + trans[(this._csvDelimX % 16)]
  37. };
  38. dhtmlXGridObject.prototype.loadCSVString = function(str) {
  39. if (!this._csvDelimX)
  40. this.setCSVDelimiter();
  41. var r1 = new RegExp("^([^" + this._csvDelimX + "]+)" + this._csvDelimX);
  42. var r2 = new RegExp("\n([^" + this._csvDelimX + "]+)" + this._csvDelimX,
  43. "g");
  44. var r3 = new RegExp("" + this._csvDelimX + "", "g");
  45. if (this._csvHdr) {
  46. hdr = str.substr(0, str.indexOf("\n"));
  47. str = str.substr(str.indexOf("\n") + 1);
  48. hdr = "<head><beforeInit><call command='setHeader'><param>"
  49. + hdr.replace(new RegExp(this._csvDelimX, "g"), ",")
  50. + "</param></call><call command='init'></call></beforeInit></head>"
  51. };
  52. if (this._csvAID) {
  53. str = str
  54. .replace(r1, "<row><cell><![CDATA[$1]]></cell><cell><![CDATA[");
  55. str = str
  56. .replace(r2,
  57. "]]></cell></row><row id=''><cell><![CDATA[$1]]></cell><cell><![CDATA[")
  58. } else {
  59. str = str.replace(r1, "<row id='$1'><cell><![CDATA[");
  60. str = str.replace(r2, "]]></cell></row><row id='$1'><cell><![CDATA[")
  61. };
  62. str = str.replace(r3, "]]></cell><cell><![CDATA[");
  63. if (str.indexOf("<row") != 0)
  64. str = "<row><cell><![CDATA[" + str;
  65. str = "<?xml version='1.0'?><rows>" + (this._csvHdr ? (hdr + str) : str)
  66. + "]]></cell></row></rows>";
  67. this.xmlLoader = new dtmlXMLLoaderObject(this.doLoadDetails, window, true,
  68. this.no_cashe);
  69. this.xmlLoader.loadXMLString(str)
  70. };
  71. dhtmlXGridObject.prototype.serializeToCSV = function() {
  72. this.editStop();
  73. if (this._mathSerialization)
  74. this._agetm = "getMathValue";
  75. else
  76. this._agetm = "getValue";
  77. var out = "";
  78. if (this._csvHdr) {
  79. var a = [];
  80. var b = this.hdr.rows[1].cells;
  81. for (var i = 0; i < b.length; i++)
  82. if ((!this._srClmn) || (this._srClmn[i]))
  83. a
  84. .push((b[i].firstChild.tagName ? b[i].firstChild : b[i]).innerHTML);
  85. out = a.join(this._csvDelim) + "\n"
  86. };
  87. var i = 0;
  88. var leni = (this._dload)
  89. ? this.rowsBuffer[0].length
  90. : (this.rowsBuffer[0].length + this.rowsCol.length);
  91. for (i; i < leni; i++) {
  92. if (i >= this.rowsCol.length && this.pagingOn) {
  93. var temp = this._serializeRowToCVS(null, i - this.rowsCol.length)
  94. } else {
  95. var r = this.rowsCol[i];
  96. var temp = this._serializeRowToCVS(r, i)
  97. };
  98. out += temp;
  99. if (temp != "")
  100. out += "\n"
  101. };
  102. return out
  103. };
  104. dhtmlXGridObject.prototype._serializeRowToCVS = function(r, i, start, end) {
  105. var out = new Array();
  106. if ((!r) || (r._sRow) || (r._rLoad) || (r.tagName == "row")) {
  107. if (this.rowsBuffer[1][i]) {
  108. var r = this.rowsBuffer[1][i];
  109. if (!this._csvAID)
  110. out[out.length] = r.getAttribute("id");
  111. for (var j = 0; j < r.childNodes.length; j++) {
  112. if (r.childNodes[j].tagName == "cell") {
  113. var c = r.childNodes[j];
  114. out[out.length] = c.firstChild
  115. ? c.firstChild.nodeValue
  116. : ""
  117. }
  118. }
  119. };
  120. return out
  121. };
  122. if (!this._csvAID)
  123. out[out.length] = r.idd;
  124. start = start || 0;
  125. end = end || r.childNodes.length;
  126. var changeFl = false;
  127. for (var jj = start; jj < end; jj++)
  128. if ((!this._srClmn) || (this._srClmn[jj])) {
  129. var cvx = r.childNodes[jj];
  130. var zx = this.cells(r.idd, cvx._cellIndex);
  131. // =======2007/11/26=====add by xiaoxiao while hidden then cancel
  132. // copy data .....
  133. if (zx.cell.style.display == "") { // ��ӵ�
  134. if (zx.cell)
  135. zxVal = zx[this._agetm]();
  136. else
  137. zxVal = "";
  138. if ((this._chAttr) && (zx.wasChanged()))
  139. changeFl = true;
  140. out[out.length] = ((zxVal === null) ? "" : zxVal)
  141. if ((this._ecspn) && (cvx.colSpan)) {
  142. cvx = cvx.colSpan - 1;
  143. for (var u = 0; u < cvx; u++)
  144. out[out.length] = ""
  145. }
  146. }
  147. };
  148. if ((this._onlChAttr) && (!changeFl))
  149. return "";
  150. return out.join(this._csvDelim)
  151. };
  152. dhtmlXGridObject.prototype.toClipBoard = function(val) {
  153. if (window.clipboardData)
  154. window.clipboardData.setData("Text", val);
  155. else
  156. (new Clipboard()).copy(val)
  157. };
  158. dhtmlXGridObject.prototype.fromClipBoard = function() {
  159. if (window.clipboardData)
  160. return window.clipboardData.getData("Text");
  161. else
  162. return (new Clipboard()).paste()
  163. };
  164. dhtmlXGridObject.prototype.cellToClipboard = function(rowId, cellInd) {
  165. if ((!rowId) || (!cellInd)) {
  166. if (!this.selectedRows[0])
  167. return;
  168. rowId = this.selectedRows[0].idd;
  169. cellInd = this.cell._cellIndex
  170. };
  171. var ed = this.cells(rowId, cellInd);
  172. this.toClipBoard(ed.getLabel ? ed.getLabel() : ed.getValue())
  173. };
  174. dhtmlXGridObject.prototype.updateCellFromClipboard = function(rowId, cellInd) {
  175. if ((!rowId) || (!cellInd)) {
  176. if (!this.selectedRows[0])
  177. return;
  178. rowId = this.selectedRows[0].idd;
  179. cellInd = this.cell._cellIndex
  180. };
  181. var ed = this.cells(rowId, cellInd);
  182. ed[ed.setImage ? "setLabel" : "setValue"](this.fromClipBoard())
  183. };
  184. dhtmlXGridObject.prototype.rowToClipboard = function(rowId) {
  185. var out = "";
  186. if (this._mathSerialization)
  187. this._agetm = "getMathValue";
  188. else
  189. this._agetm = "getValue";
  190. if (rowId)
  191. out = this._serializeRowToCVS(this.getRowById(rowId));
  192. else
  193. for (var i = 0; i < this.selectedRows.length; i++) {
  194. if (out)
  195. out += "\n";
  196. out += this._serializeRowToCVS(this.selectedRows[i])
  197. };
  198. this.toClipBoard(out)
  199. };
  200. dhtmlXGridObject.prototype.updateRowFromClipboard = function(rowId) {
  201. var csv = this.fromClipBoard();
  202. if (!csv)
  203. return;
  204. if (rowId)
  205. var r = this.getRowById(rowId);
  206. else
  207. var r = this.selectedRows[0];
  208. if (!r)
  209. return;
  210. csv = csv.split(this._csvDelim);
  211. for (var i = 1; i < csv.length; i++) {
  212. var ed = this.cells3(r, i - 1);
  213. ed[ed.setImage ? "setLabel" : "setValue"](csv[i])
  214. }
  215. };
  216. dhtmlXGridObject.prototype.addRowFromClipboard = function() {
  217. var csv = this.fromClipBoard();
  218. if (!csv)
  219. return;
  220. var z = csv.split("\n");
  221. for (var i = 0; i < z.length; i++)
  222. if (z[i]) {
  223. csv = z[i].split(this._csvDelim);
  224. this.addRow(csv[0], csv.slice(1))
  225. }
  226. };
  227. dhtmlXGridObject.prototype.gridToClipboard = function() {
  228. this.toClipBoard(this.serializeToCSV())
  229. };
  230. dhtmlXGridObject.prototype.gridFromClipboard = function() {
  231. var csv = this.fromClipBoard();
  232. if (!csv)
  233. return;
  234. this.loadCSVString(csv)
  235. };
  236. dhtmlXGridObject.prototype.getXLS = function(path) {
  237. if (!this.xslform) {
  238. this.xslform = document.createElement("FORM");
  239. this.xslform.action = (path || "") + "xls.php";
  240. this.xslform.method = "post";
  241. this.xslform.target = (_isIE ? "_blank" : "");
  242. document.body.appendChild(this.xslform);
  243. var i1 = document.createElement("INPUT");
  244. i1.type = "hidden";
  245. i1.name = "csv";
  246. this.xslform.appendChild(i1);
  247. var i2 = document.createElement("INPUT");
  248. i2.type = "hidden";
  249. i2.name = "csv_header";
  250. this.xslform.appendChild(i2)
  251. };
  252. var cvs = this.serializeToCSV();
  253. this.xslform.childNodes[0].value = cvs;
  254. var cvs_header = [];
  255. var l = this._cCount;
  256. for (var i = 0; i < l; i++) {
  257. cvs_header.push(this.getHeaderCol(i))
  258. };
  259. cvs_header = cvs_header.join(',');
  260. this.xslform.childNodes[1].value = cvs_header;
  261. this.xslform.submit()
  262. };
  263. dhtmlXGridObject.prototype.printView = function(path) {
  264. var html = "<style>TD {font-family:Arial;text-align:center};</style>";
  265. html += "<base href='" + document.location.href + "'></base>";
  266. html += '<table width="100%" border="2px" cellpadding="0" cellspacing="0">';
  267. var row_length = this.rowsCol.length;
  268. var col_length = this._cCount;
  269. var width = this._printWidth();
  270. html += '<tr>';
  271. for (var i = 0; i < col_length; i++) {
  272. if (this._hrrar && this._hrrar[i])
  273. continue;
  274. var colspan = (this.hdr.rows[1].cells[this.hdr.rows[1]._childIndexes
  275. ? this.hdr.rows[1]._childIndexes[parseInt(i)]
  276. : i].colSpan || 1);
  277. for (var j = 1; j < colspan; j++)
  278. width[i] += width[j];
  279. html += '<td width="'
  280. + width[i]
  281. + '%" style="padding-left:2px;padding-right:2px;background-color:lightgray;" colspan="'
  282. + colspan + '">' + this.getHeaderCol(i) + '</td>';
  283. i += colspan - 1
  284. };
  285. html += '</tr>';
  286. for (var i = 2; i < this.hdr.rows.length; i++)
  287. html += "<tr>" + this.hdr.rows[i].innerHTML + "</tr>";
  288. for (var i = 0; i < row_length; i++) {
  289. html += '<tr>';
  290. for (var j = 0; j < col_length; j++) {
  291. if (this._hrrar && this._hrrar[j])
  292. continue;
  293. var c = this.cells(this.rowsCol[i].idd, j);
  294. if (c.getImage)
  295. var value = c.cell.innerHTML;
  296. else
  297. var value = c.getValue();
  298. var color = this.columnColor[j] ? 'background-color:'
  299. + this.columnColor[j] + ';' : '';
  300. var align = this.cellAlign[j] ? 'text-align:' + this.cellAlign[j]
  301. + ';' : '';
  302. html += '<td style="padding-left:2px;padding-right:2px;' + color
  303. + align + '">' + (value === "" ? "&nbsp;" : value)
  304. + '</td>'
  305. };
  306. html += '</tr>'
  307. };
  308. if (this.ftr)
  309. for (var i = 1; i < this.ftr.childNodes[0].rows.length; i++)
  310. html += "<tr>" + this.ftr.rows[i].innerHTML + "</tr>";
  311. html += '</table>';
  312. var d = window.open('', '_blank');
  313. d.document.write(html);
  314. d.document.close()
  315. };
  316. dhtmlXGridObject.prototype._printWidth = function() {
  317. var width = [];
  318. var total_width = 0;
  319. for (var i = 0; i < this._cCount; i++) {
  320. var w = this.getColWidth(i);
  321. width.push(w);
  322. total_width += w
  323. };
  324. var percent_width = [];
  325. var total_percent_width = 0;
  326. for (var i = 0; i < width.length; i++) {
  327. var p = Math.floor((width[i] / total_width) * 100);
  328. total_percent_width += p;
  329. percent_width.push(p)
  330. };
  331. percent_width[percent_width.length - 1] += 100 - total_percent_width;
  332. return percent_width
  333. };
  334. dhtmlXGridObject.prototype.loadObject = function(obj) {
  335. };
  336. dhtmlXGridObject.prototype.loadJSONFile = function(path) {
  337. };
  338. dhtmlXGridObject.prototype.serializeToObject = function() {
  339. };
  340. dhtmlXGridObject.prototype.serializeToJSON = function() {
  341. };
  342. if (!window.clipboardData)
  343. window.clipboardData = {
  344. _make : function() {
  345. var clip = Components.classes['@mozilla.org/widget/clipboard;1']
  346. .createInstance(Components.interfaces.nsIClipboard);
  347. if (!clip)
  348. return null;
  349. var trans = Components.classes['@mozilla.org/widget/transferable;1']
  350. .createInstance(Components.interfaces.nsITransferable);
  351. if (!trans)
  352. return null;
  353. trans.addDataFlavor('text/unicode');
  354. var str = Components.classes["@mozilla.org/supports-string;1"]
  355. .createInstance(Components.interfaces.nsISupportsString);
  356. this._p = [clip, trans, str];
  357. return true
  358. },
  359. setData : function(type, text) {
  360. netscape.security.PrivilegeManager
  361. .enablePrivilege('UniversalXPConnect');
  362. if (!this._make())
  363. return false;
  364. this._p[2].data = text;
  365. this._p[1].setTransferData("text/unicode", this._p[2], text.length
  366. * 2);
  367. var clipid = Components.interfaces.nsIClipboard;
  368. this._p[0].setData(this._p[1], null, clipid.kGlobalClipboard)
  369. },
  370. getData : function(type) {
  371. netscape.security.PrivilegeManager
  372. .enablePrivilege('UniversalXPConnect');
  373. if (!this._make())
  374. return false;
  375. this._p[0].getData(this._p[1], this._p[0].kGlobalClipboard);
  376. var strLength = new Object();
  377. var str = new Object();
  378. this._p[1].getTransferData("text/unicode", str, strLength);
  379. if (str)
  380. str = str.value
  381. .QueryInterface(Components.interfaces.nsISupportsString);
  382. if (str)
  383. return str.data.substring(0, strLength.value / 2);
  384. return ""
  385. }
  386. };
  387. /*
  388. * Copyright DHTMLX LTD. http://www.dhtmlx.com You allowed to use this component
  389. * or parts of it under GPL terms To use it on other terms or get Professional
  390. * edition of the component please contact us at sales@dhtmlx.com
  391. */