bb6207286c10c13856addef81dede60f193e4fd4.svn-base 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright Scand LLC http://www.scbr.com To use this component please contact
  3. * info@scbr.com to obtain license
  4. */
  5. document.write("<link rel='stylesheet' type='text/css' href='" + _css_prefix
  6. + "dhtmlXGrid_calck.css'></link>");
  7. // simple text editor
  8. function eXcell_calck(cell) {
  9. try {
  10. this.cell = cell;
  11. this.grid = this.cell.parentNode.grid;
  12. } catch (er) {
  13. }
  14. this.edit = function() {
  15. this.val = this.getValue();
  16. var arPos = this.grid.getPosition(this.cell);
  17. this.obj = new calcX(arPos[0] - this.grid.objBox.scrollLeft, arPos[1]
  18. + this.cell.offsetHeight - this.grid.objBox.scrollTop,
  19. this, this.val);
  20. }
  21. this.getValue = function() {
  22. // this.grid.editStop();
  23. return this.grid._aplNFb(this.cell.innerHTML.toString()._dhx_trim(),
  24. this.cell._cellIndex);
  25. }
  26. this.detach = function(x, val) {
  27. if (x)
  28. this.setValue(val);
  29. else if (this.obj)
  30. this.obj.removeSelf();
  31. this.obj = null;
  32. return this.val != this.getValue();
  33. }
  34. }
  35. eXcell_calck.prototype = new eXcell;
  36. eXcell_calck.prototype.setValue = function(val) {
  37. if (!val || val.toString()._dhx_trim() == "")
  38. val = "0"
  39. this.cell.innerHTML = this.grid._aplNF(val, this.cell._cellIndex);
  40. }
  41. function calcX(left, top, onReturnSub, val) {
  42. this.top = top || 0;
  43. this.left = left || 0;
  44. this.onReturnSub = onReturnSub || null;
  45. this.operandA = 0;
  46. this.operandB = 0;
  47. this.operatorA = "";
  48. this.state = 0;
  49. this.dotState = 0;
  50. this.calckGo = function() {
  51. return (eval(this.operandA + "*1" + this.operatorA + this.operandB
  52. + "*1"));
  53. };
  54. this.isNumeric = function(str) {
  55. return ((str.search(/[^1234567890]/gi) == -1) ? (true) : (false));
  56. };
  57. this.isOperation = function(str) {
  58. return ((str.search(/[^\+\*\-\/]/gi) == -1) ? (true) : (false));
  59. }
  60. this.onCalcKey = function() {
  61. that = this.calk;
  62. var z = this.innerHTML;
  63. var rZone = that.inputZone;
  64. if (((that.state == 0) || (that.state == 2)) && (that.isNumeric(z)))
  65. if (rZone.value != "0")
  66. rZone.value += z;
  67. else
  68. rZone.value = z;
  69. if ((((that.state == 0) || (that.state == 2)) && (z == '.'))
  70. && (that.dotState == 0)) {
  71. that.dotState = 1;
  72. rZone.value += z;
  73. }
  74. if ((z == "C")) {
  75. rZone.value = 0;
  76. that.dotState = 0;
  77. that.state = 0;
  78. }
  79. if ((that.state == 0) && (that.isOperation(z))) {
  80. that.operatorA = z;
  81. that.operandA = rZone.value;
  82. that.state = 1;
  83. }
  84. if ((that.state == 2) && (that.isOperation(z))) {
  85. that.operandB = rZone.value;
  86. rZone.value = that.calckGo();
  87. that.operatorA = z;
  88. that.operandA = rZone.value;
  89. that.state = 1;
  90. }
  91. if ((that.state == 2) && (z == "=")) {
  92. that.operandB = rZone.value;
  93. rZone.value = that.calckGo();
  94. that.operatorA = z;
  95. that.operandA = rZone.value;
  96. that.state = 3;
  97. }
  98. if ((that.state == 1) && (that.isNumeric(z))) {
  99. rZone.value = z;
  100. that.state = 2;
  101. that.dotState = 0
  102. }
  103. if ((that.state == 3) && (that.isNumeric(z))) {
  104. rZone.value = z;
  105. that.state = 0;
  106. }
  107. if ((that.state == 3) && (that.isOperation(z))) {
  108. that.operatorA = z;
  109. that.operandA = rZone.value;
  110. that.state = 1;
  111. }
  112. if (z == "e") {
  113. rZone.value = Math.E;
  114. if (that.state == 1)
  115. that.state = 2;
  116. that.dotState = 0
  117. }
  118. if (z == "p") {
  119. rZone.value = Math.PI;
  120. if (that.state == 1)
  121. that.state = 2;
  122. that.dotState = 0
  123. }
  124. if (z == "Off")
  125. that.topNod.removeNode(true);
  126. }
  127. this.sendResult = function() {
  128. that = this.calk;
  129. if (that.state == 2) {
  130. var rZone = that.inputZone;
  131. that.operandB = rZone.value;
  132. rZone.value = that.calckGo();
  133. that.operatorA = z;
  134. that.operandA = rZone.value;
  135. that.state = 3;
  136. }
  137. var z = that.inputZone.value;
  138. that.topNod.parentNode.removeChild(that.topNod);
  139. that.onReturnSub.detach(1, z);
  140. };
  141. this.removeSelf = function() {
  142. if (this.topNod.parentNode)
  143. this.topNod.parentNode.removeChild(this.topNod);
  144. }
  145. this.keyDown = function() {
  146. this.className = "calcPressed";
  147. };
  148. this.keyUp = function() {
  149. this.className = "calcButton";
  150. };
  151. this.init_table = function() {
  152. var table = this.topNod.childNodes[0];
  153. if ((!table) || (table.tagName != "TABLE"))
  154. return;
  155. for (i = 1; i < table.childNodes[0].childNodes.length; i++)
  156. for (j = 0; j < table.childNodes[0].childNodes[i].childNodes.length; j++) {
  157. table.childNodes[0].childNodes[i].childNodes[j].onclick = this.onCalcKey;
  158. table.childNodes[0].childNodes[i].childNodes[j].onmousedown = this.keyDown;
  159. table.childNodes[0].childNodes[i].childNodes[j].onmouseout = this.keyUp;
  160. table.childNodes[0].childNodes[i].childNodes[j].onmouseup = this.keyUp;
  161. table.childNodes[0].childNodes[i].childNodes[j].calk = this;
  162. }
  163. this.inputZone = this.topNod.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0];
  164. if (this.onReturnSub) {
  165. this.topNod.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].onclick = this.sendResult;
  166. this.topNod.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].calk = this;
  167. } else
  168. this.topNod.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].innerHTML = "";
  169. }
  170. this.drawSelf = function() {
  171. var div = document.createElement("div");
  172. div.className = "calcTable";
  173. div.style.position = "absolute";
  174. div.style.top = this.top + "px";
  175. div.style.left = this.left + "px";
  176. div.innerHTML = "<table cellspacing='0' id='calc_01' class='calcTable'><tr><td colspan='4'><table cellpadding='1' cellspacing='0' width='100%'><tr><td width='100%'><input style='width:100%' class='calcInput' value='0' align='right' readonly='true' style='text-align:right'></td><td class='calkSubmit'>=</td></tr></table></td></tr><tr><td class='calcButton' width='25%'>Off</td><td class='calcButton' width='25%'>p</td><td class='calcButton' width='25%'>e</td><td class='calcButton' width='25%'>/</td></tr><tr><td class='calcButton'>7</td><td class='calcButton'>8</td><td class='calcButton'>9</td><td class='calcButton'>*</td></tr><tr><td class='calcButton'>4</td><td class='calcButton'>5</td><td class='calcButton'>6</td><td class='calcButton'>+</td></tr><tr><td class='calcButton'>1</td><td class='calcButton'>2</td><td class='calcButton'>3</td><td class='calcButton'>-</td></tr><tr><td class='calcButton'>0</td><td class='calcButton'>.</td><td class='calcButton'>C</td><td class='calcButton'>=</td></tr></table>";
  177. document.body.appendChild(div);
  178. this.topNod = div;
  179. }
  180. this.drawSelf();
  181. this.init_table();
  182. if (val) {
  183. var rZone = this.inputZone;
  184. rZone.value = val * 1;
  185. this.operandA = val * 1;
  186. this.state = 1;
  187. }
  188. return this;
  189. };