dhtmlXGrid_math.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright Scand LLC http://www.scbr.com To use this component please contact
  3. * info@scbr.com to obtain license
  4. */
  5. /**
  6. * @desc: math cells
  7. * @param: cell - cell object
  8. * @type: private
  9. * @edition: Professional
  10. */
  11. function eXcell_math(cell) {
  12. try {
  13. this.cell = cell;
  14. this.grid = this.cell.parentNode.grid;
  15. } catch (er) {
  16. }
  17. /**
  18. * @desc: edit cell
  19. * @type: private
  20. * @edition: Professional
  21. */
  22. this.edit = function() {
  23. this.val = this.getValue();
  24. if ((this.cell._val.indexOf("=") == 0) && (!this.grid._mathEdit))
  25. return false;
  26. this.obj = document.createElement("TEXTAREA");
  27. this.obj.style.width = "100%";
  28. this.obj.style.height = (this.cell.offsetHeight - 4) + "px";
  29. this.obj.style.border = "0px";
  30. this.obj.style.margin = "0px";
  31. this.obj.style.padding = "0px";
  32. this.obj.style.overflow = "hidden";
  33. this.obj.style.fontSize = "12px";
  34. this.obj.style.fontFamily = "Arial";
  35. this.obj.wrap = "soft";
  36. this.obj.style.textAlign = this.cell.align;
  37. this.obj.onclick = function(e) {
  38. (e || event).cancelBubble = true
  39. }
  40. this.obj.value = this.cell._val;
  41. this.cell.innerHTML = "";
  42. this.cell.appendChild(this.obj);
  43. this.obj.onselectstart = function(e) {
  44. if (!e)
  45. e = event;
  46. e.cancelBubble = true;
  47. return true;
  48. };
  49. this.obj.focus()
  50. this.obj.focus()
  51. }
  52. /**
  53. * @desc: get cell value
  54. * @returns: cell value
  55. * @type: private
  56. * @edition: Professional
  57. */
  58. this.getValue = function() {
  59. // this.grid.editStop();
  60. if (this.grid._strangeParams[this.cell._cellIndex]) {
  61. var d = this.cell;
  62. var z = eval("new eXcell_"
  63. + (this.grid._strangeParams[this.cell._cellIndex]) + "(d)");
  64. return z.getValue();
  65. } else
  66. return this.cell.innerHTML;
  67. }
  68. /**
  69. * @desc: set cell value, by using calculations and specified editor
  70. * @param: val - new value
  71. * @type: private
  72. * @edition: Professional
  73. */
  74. this.setValueA = function(val) {
  75. if (this.grid._strangeParams[this.cell._cellIndex]) {
  76. var z = eval("new eXcell_"
  77. + this.grid._strangeParams[this.cell._cellIndex]
  78. + "(this.cell)");
  79. z.setValue(val);
  80. } else
  81. this.cell.innerHTML = this.grid._calcSCL(cell) || "0";
  82. }
  83. /**
  84. * @desc: set cell formula
  85. * @param: val - new value
  86. * @type: private
  87. * @edition: Professional
  88. */
  89. this.setValue = function(val) {
  90. this.cell._val = val;
  91. this.cell._code = this.grid._compileSCL(val, this.cell);
  92. this.setValueA(this.grid._calcSCL(cell));
  93. this.grid._checkSCL(this.cell);
  94. }
  95. /**
  96. * @desc: remove cell's editor
  97. * @type: private
  98. * @edition: Professional
  99. */
  100. this.detach = function() {
  101. if ((this.cell._val.indexOf("=") == 0) && (!this.grid._mathEdit))
  102. return false;
  103. this.setValue(this.obj.value);
  104. return this.val != this.getValue();
  105. }
  106. }
  107. eXcell_math.prototype = new eXcell;
  108. dhtmlXGridCellObject.prototype.setValueA = dhtmlXGridCellObject.prototype.setValue;
  109. eXcell_price.prototype.setValueA = eXcell_price.prototype.setValue;
  110. eXcell_dyn.prototype.setValueA = eXcell_dyn.prototype.setValue;
  111. eXcell_ch.prototype.setValueA = eXcell_ch.prototype.setValue;
  112. eXcell_ra.prototype.setValueA = eXcell_ra.prototype.setValue;
  113. eXcell_cp.prototype.setValueA = eXcell_cp.prototype.setValue;
  114. eXcell_co.prototype.setValueA = eXcell_co.prototype.setValue;
  115. eXcell_edn.prototype.setValueA = eXcell_edn.prototype.setValue;
  116. /**
  117. * @desc: wrapper for set value routines
  118. * @param: val - new value
  119. * @type: private
  120. * @edition: Professional
  121. */
  122. eXcell_math.prototype._NsetValue = function(val) {
  123. this.setValueA(val);
  124. this.grid._checkSCL(this.cell);
  125. }
  126. dhtmlXGridCellObject.prototype.setValue = eXcell_math.prototype._NsetValue;
  127. eXcell_price.prototype.setValue = eXcell_math.prototype._NsetValue;
  128. eXcell_dyn.prototype.setValue = eXcell_math.prototype._NsetValue;
  129. eXcell_ch.prototype.setValue = eXcell_math.prototype._NsetValue;
  130. eXcell_ra.prototype.setValue = eXcell_math.prototype._NsetValue;
  131. eXcell_cp.prototype.setValue = eXcell_math.prototype._NsetValue;
  132. eXcell_co.prototype.setValue = eXcell_math.prototype._NsetValue;
  133. eXcell_edn.prototype.setValue = eXcell_math.prototype._NsetValue;
  134. /**
  135. * @desc: check if math cell require linking
  136. * @param: cell - math cell
  137. * @type: private
  138. * @edition: Professional
  139. */
  140. dhtmlXGridObject.prototype._checkSCL = function(cell) {
  141. if (!this.math_off) {
  142. if (cell._SCL) {
  143. for (var i = 0; i < cell._SCL.length; i++)
  144. if (cell._SCL[i]) {
  145. if (this._strangeParams[cell._SCL[i]._cellIndex]) {
  146. var z = eval("new eXcell_"
  147. + this._strangeParams[cell._SCL[i]._cellIndex]
  148. + "(cell._SCL[i])");
  149. z.setValue(this._calcSCL(cell._SCL[i]));
  150. } else
  151. cell._SCL[i].innerHTML = this._calcSCL(cell._SCL[i]);
  152. this._checkSCL(cell._SCL[i]);
  153. }
  154. }
  155. if (cell.parentNode.parent_id) {
  156. var pRow = this.getRowById(cell.parentNode.parent_id);
  157. if (!pRow)
  158. return;
  159. if (pRow.childNodes[cell._cellIndex]._sumArr) {
  160. if (this._strangeParams[cell._cellIndex]) {
  161. var z = eval("new eXcell_"
  162. + this._strangeParams[cell._cellIndex]
  163. + "(pRow.childNodes[cell._cellIndex])");
  164. z.setValue(this._calcSCL(pRow.childNodes[cell._cellIndex]));
  165. } else
  166. pRow.childNodes[cell._cellIndex].innerHTML = this
  167. ._calcSCL(pRow.childNodes[cell._cellIndex]);
  168. this._checkSCL(pRow.childNodes[cell._cellIndex]);
  169. }
  170. }
  171. } else
  172. this.math_req = true;
  173. }
  174. /**
  175. * @desc: enable/disable rounding while math calculations
  176. * @param: digits - set hom many digits must be rounded, set 0 for disabling
  177. * @type: public
  178. * @edition: Professional
  179. */
  180. dhtmlXGridObject.prototype.setMathRound = function(digits) {
  181. this._roundD = digits;
  182. }
  183. /**
  184. * @desc: enable/disable editing of math cells
  185. * @param: status - true/false
  186. * @type: public
  187. * @edition: Professional
  188. */
  189. dhtmlXGridObject.prototype.enableMathEditing = function(status) {
  190. this._mathEdit = convertStringToBoolean(status);
  191. }
  192. /**
  193. * @desc: enable/disable serialization of math formulas
  194. * @param: status - true/false
  195. * @type: public
  196. * @edition: Professional
  197. */
  198. dhtmlXGridObject.prototype.enableMathSerialization = function(status) {
  199. this._mathSerialization = convertStringToBoolean(status);
  200. }
  201. /**
  202. * @desc: calculate value of math cell
  203. * @param: cell - math cell
  204. * @returns: cell value
  205. * @type: private
  206. * @edition: Professional
  207. */
  208. dhtmlXGridObject.prototype._calcSCL = function(cell) {
  209. if (!cell._code)
  210. return "";
  211. try {
  212. var agrid = this;
  213. if (!this._roundD) {
  214. var z = eval(cell._code);
  215. return z;
  216. } else {
  217. var z = eval(cell._code);
  218. z = z.toString().split(".");
  219. return (z[0] + "." + ((z[1] || "0") + "000000000").substring(0,
  220. this._roundD));
  221. }
  222. } catch (e) {
  223. return ("#SCL");
  224. }
  225. }
  226. dhtmlXGridObject.prototype._countTotal = function(row, cel) {
  227. var a = this.loadedKidsHash.get(row);
  228. if (!a)
  229. return;
  230. var b = 0;
  231. for (var i = 0; i < a.length; i++)
  232. b = b * 1 + this.cells3(a[i], cel).getValue() * 1;
  233. return b;
  234. }
  235. /**
  236. * @desc: compile pseudo code to correct javascript
  237. * @param: code - pseudo code
  238. * @param: cell - math cell
  239. * @returns: valid js code
  240. * @type: private
  241. * @edition: Professional
  242. */
  243. dhtmlXGridObject.prototype._compileSCL = function(code, cell) {
  244. if (!code)
  245. return "";
  246. if (code.indexOf("=") != 0) {
  247. this._reLink(new Array(), cell);
  248. return code;
  249. }
  250. code = code.replace("=", "");
  251. if (code.indexOf("sum") != -1) {
  252. code = code.replace("sum", "(agrid._countTotal('" + cell.parentNode.idd
  253. + "'," + cell._cellIndex + "))");
  254. /*
  255. * if (!cell.parentNode._sumArr) cell.parentNode._sumArr=new Array()
  256. * parentNode._sumArr[cell._cellIndex]=true;
  257. */
  258. cell.parentNode._sumArr = true;
  259. cell._sumArr = true;
  260. return code;
  261. }
  262. if (code.indexOf("[[") != -1) {
  263. var test = /(\[\[([^\,]*)\,([^\]]*)]\])/g;
  264. var agrid = this;
  265. var linked = new Array();
  266. code = code.replace(test, function($0, $1, $2, $3) {
  267. if ($2 == "-")
  268. $2 = cell.parentNode.idd;
  269. if ($2.indexOf("#") == 0)
  270. $2 = agrid.getRowId($2.replace("#", ""));
  271. linked[linked.length] = [$2, $3];
  272. return "(agrid.cells(\"" + $2 + "\"," + $3
  273. + ").getValue()*1)";
  274. });
  275. } else {
  276. var test = /c([0-9]+)/g;
  277. var agrid = this;
  278. var id = cell.parentNode.idd;
  279. var linked = new Array();
  280. code = code.replace(test, function($0, $1, $2, $3) {
  281. linked[linked.length] = [id, $1];
  282. return "(agrid.cells(\"" + id + "\"," + $1
  283. + ").getValue()*1)";
  284. });
  285. }
  286. this._reLink(linked, cell);
  287. return code;
  288. }
  289. /**
  290. * @desc: link math cells to it source cells after incorrect attempt
  291. * @type: private
  292. * @edition: Professional
  293. */
  294. dhtmlXGridObject.prototype._laterLink = function() {
  295. var a = window._SCL_later;
  296. window._SCL_later = new Array();
  297. window._SCL_later_timer = null;
  298. for (var i = 0; i < a.length; i++) {
  299. (a[i][2])._reLink(a[i][0], a[i][1]);
  300. if ((a[i][2])._strangeParams[a[i][1]._cellIndex]) {
  301. var z = eval("new eXcell_"
  302. + (a[i][2])._strangeParams[a[i][1]._cellIndex]
  303. + "(a[i][1])");
  304. z.setValue((a[i][2])._calcSCL(a[i][1]));
  305. } else
  306. a[i][1].innerHTML = (a[i][2])._calcSCL(a[i][1]);
  307. (a[i][2])._checkSCL(a[i][1]);
  308. }
  309. }
  310. /**
  311. * @desc: link math cells to it source cells
  312. * @param: ar - array of nodes for linking
  313. * @param: cell - math cell
  314. * @type: private
  315. * @edition: Professional
  316. */
  317. dhtmlXGridObject.prototype._reLink = function(ar, cell) {
  318. if (cell._alink) {
  319. for (var i = 0; i < cell._alink.length; i++)
  320. (cell._alink[i][0])._SCL[cell._alink[i][1]] = null;
  321. }
  322. cell._alink = new Array();
  323. for (var i = 0; i < ar.length; i++) {
  324. var a = this.getRowById(ar[i][0]);
  325. if (!a) {
  326. if (!window._SCL_later)
  327. window._SCL_later = new Array();
  328. window._SCL_later.grid = this;
  329. window._SCL_later[window._SCL_later.length] = [ar, cell, this];
  330. if (!window._SCL_later_timer)
  331. window._SCL_later_timer = window.setTimeout(this._laterLink,
  332. 300);
  333. return 0;
  334. }
  335. var b = a.childNodes[ar[i][1]];
  336. if (!b._SCL)
  337. b._SCL = new Array();
  338. cell._alink[i] = [b, b._SCL.length];
  339. b._SCL[b._SCL.length] = cell;
  340. }
  341. }
  342. if (_isKHTML) {
  343. // replace callback support for safari.
  344. (function() {
  345. var default_replace = String.prototype.replace;
  346. String.prototype.replace = function(search, replace) {
  347. // replace is not function
  348. if (typeof replace != "function") {
  349. return default_replace.apply(this, arguments)
  350. }
  351. var str = "" + this;
  352. var callback = replace;
  353. // search string is not RegExp
  354. if (!(search instanceof RegExp)) {
  355. var idx = str.indexOf(search);
  356. return (idx == -1 ? str : default_replace.apply(str, [search,
  357. callback(search, idx, str)]))
  358. }
  359. var reg = search;
  360. var result = [];
  361. var lastidx = reg.lastIndex;
  362. var re;
  363. while ((re = reg.exec(str)) != null) {
  364. var idx = re.index;
  365. var args = re.concat(idx, str);
  366. result.push(str.slice(lastidx, idx), callback.apply(null, args)
  367. .toString());
  368. if (!reg.global) {
  369. lastidx += RegExp.lastMatch.length;
  370. break
  371. } else {
  372. lastidx = reg.lastIndex;
  373. }
  374. }
  375. result.push(str.slice(lastidx));
  376. return result.join("")
  377. }
  378. })();
  379. }