not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
13 KiB

  1. /*jshint multistr: true */
  2. /* This is the calculate activity file, providing methods related to events*/
  3. function getWindowHeight() {
  4. var windowHeight = 0;
  5. if (typeof(window.innerHeight) == 'number') {
  6. windowHeight = window.innerHeight;
  7. } else {
  8. if (document.documentElement && document.documentElement.clientHeight) {
  9. windowHeight = document.documentElement.clientHeight;
  10. } else {
  11. if (document.body && document.body.clientHeight) {
  12. windowHeight = document.body.clientHeight;
  13. }
  14. }
  15. }
  16. return windowHeight;
  17. }
  18. function getWindowWidth() {
  19. var windowWidth = 0;
  20. if (typeof(window.innerWidth) == 'number') {
  21. windowWidth = window.innerWidth;
  22. } else {
  23. if (document.documentElement && document.documentElement.clientWidth) {
  24. windowWidth = document.documentElement.clientWidth;
  25. } else {
  26. if (document.body && document.body.clientWidth) {
  27. windowWidth = document.body.clientWidth;
  28. }
  29. }
  30. }
  31. return windowWidth;
  32. }
  33. //This function allows us to get the location of the caret inside the calc input
  34. function doGetCaretPosition(oField) {
  35. var iCaretPos = 0;
  36. if (document.selection) {
  37. oField.focus();
  38. var oSel = document.selection.createRange();
  39. oSel.moveStart('character', -oField.value.length);
  40. iCaretPos = oSel.text.length;
  41. } else if (oField.selectionStart || oField.selectionStart == '0')
  42. iCaretPos = oField.selectionStart;
  43. return (iCaretPos);
  44. }
  45. //We will remove one character from the calc input
  46. //If on desktop, we remove a character from the carret position
  47. function removeCharacter() {
  48. if (CalculateApp.data.isMobile) {
  49. CalculateApp.elements.calcInput.value = CalculateApp.elements.calcInput.value.substring(0, CalculateApp.elements.calcInput.value.length - 1);
  50. } else {
  51. var caretPosition = doGetCaretPosition(CalculateApp.elements.calcInput);
  52. if (caretPosition === CalculateApp.elements.calcInput.value.length) {
  53. caretPosition = caretPosition - 1;
  54. }
  55. if (caretPosition < 0) {
  56. caretPosition = 0;
  57. }
  58. var currentValue = CalculateApp.elements.calcInput.value;
  59. CalculateApp.elements.calcInput.value = currentValue.slice(0, caretPosition) + currentValue.slice(caretPosition + 1);
  60. CalculateApp.elements.calcInput.setSelectionRange(caretPosition, caretPosition);
  61. }
  62. CalculateApp.focus();
  63. }
  64. //We will add test inside the calc input
  65. //If on desktop, we will add the text from the carret position
  66. function addTextInsideInput(value) {
  67. if (CalculateApp.data.isMobile) {
  68. CalculateApp.elements.calcInput.value = CalculateApp.elements.calcInput.value + value;
  69. } else {
  70. var caretPosition = doGetCaretPosition(CalculateApp.elements.calcInput);
  71. var currentValue = CalculateApp.elements.calcInput.value;
  72. CalculateApp.elements.calcInput.value = currentValue.slice(0, caretPosition) + value + currentValue.slice(caretPosition);
  73. CalculateApp.elements.calcInput.setSelectionRange(caretPosition + value.length, caretPosition + value.length);
  74. }
  75. CalculateApp.focus();
  76. }
  77. /* Callback from the calc buttons */
  78. function calcButtonClick(button) {
  79. if (CalculateApp.data.calculationDone && !isNaN(button.target.value)) {
  80. CalculateApp.elements.calcInput.value = "";
  81. }
  82. CalculateApp.data.calculationDone = false;
  83. addTextInsideInput(button.target.value);
  84. }
  85. /* Trigger calculation when the key is enter inside the calc input */
  86. function calcInputKeyUp(event) {
  87. if (event.which == 13) {
  88. calcEqualClick();
  89. }
  90. }
  91. /* Generation of the error box */
  92. function getErrorTemplate() {
  93. return "<div class='result' style='overflow:hidden; margin-top:3px;padding:5px; font-size: 140%; color:#fff; border:5px solid " + CalculateApp.data.buddyColor.stroke + "; background-color:" + CalculateApp.data.buddyColor.fill + "; '> \
  94. {{#label}}<span>{{ label }}</span> <br/> {{/label}}\
  95. <span>{{ calculation }}</span> <br/> \
  96. <b style='float:right;'>{{error}}</b> \
  97. <div style='clear:both;'></div> \
  98. </div>";
  99. }
  100. /* Generation of the result box */
  101. function getResultTemplate() {
  102. return "<div class='result' style='overflow:hidden; margin-top:3px;padding:5px; font-size: 140%; color:#fff; border:5px solid " + CalculateApp.data.buddyColor.stroke + "; background-color:" + CalculateApp.data.buddyColor.fill + "; '> \
  103. {{#label}}<span>{{ label }}</span> <br/> {{/label}}\
  104. <span>{{ calculation }}</span> <br/> \
  105. <b style='float:right;'>{{result}}</b> \
  106. <div style='clear:both;'></div> \
  107. </div>";
  108. }
  109. /* Generation of the result box */
  110. function getGraphTemplate() {
  111. return "<div class='result' style='overflow:hidden; margin-top:3px;padding:5px; font-size: 140%; color:#fff; border:5px solid " + CalculateApp.data.buddyColor.stroke + "; background-color:" + CalculateApp.data.buddyColor.fill + "; '> \
  112. {{#label}}<span>{{ label }}</span> <br/> {{/label}}\
  113. <span>{{ calculation }}</span> <br/> \
  114. <button value='{{calculation}}' style='background:none; border-radius:0px; float:right; border:0px; margin:3px; width:55px; height:55px; background-image: url(icons/plot.svg)'></button>\
  115. <div style='clear:both;'></div> \
  116. </div>";
  117. }
  118. /* We handle a graph request */
  119. function launchGraph(calcInputValue, labelValue) {
  120. var calculation = {
  121. calculation: calcInputValue,
  122. graph: true,
  123. };
  124. if (labelValue) {
  125. calculation.label = labelValue;
  126. }
  127. calcInputValue = CalculateApp.tryPatchGraphEquation(calcInputValue);
  128. try {
  129. var result = CalculateApp.evalFunctionAtZero(calcInputValue);
  130. if (result.value) {
  131. result = result.value;
  132. }
  133. calculation.result = result;
  134. } catch (e) {
  135. calculation.error = e.message;
  136. }
  137. CalculateApp.storeCalculation(calculation);
  138. CalculateApp.persistCalculations();
  139. CalculateApp.displayCalculation(calculation);
  140. if (!calculation.error) {
  141. CalculateApp.graph(calculation.calculation);
  142. }
  143. }
  144. /* We handle a calculation request */
  145. function launchCalculation(calcInputValue, labelValue) {
  146. var calculation = {
  147. calculation: calcInputValue,
  148. graph: false
  149. };
  150. if (labelValue) {
  151. calculation.label = labelValue;
  152. }
  153. calcInputValue = CalculateApp.tryPatchEquation(calcInputValue);
  154. try {
  155. var result = CalculateApp.eval(calcInputValue);
  156. calculation.result = result.result;
  157. CalculateApp.elements.calcInput.value = result.resultBase10;
  158. } catch (e) {
  159. calculation.error = e.message;
  160. }
  161. CalculateApp.storeCalculation(calculation);
  162. CalculateApp.persistCalculations();
  163. CalculateApp.displayCalculation(calculation);
  164. }
  165. /* Handling of calc equal button click */
  166. function calcEqualClick() {
  167. if (CalculateApp.elements.calcInput.value === undefined || CalculateApp.elements.calcInput.value.length === 0) {
  168. return;
  169. }
  170. var labelValue = CalculateApp.elements.labelInput.value;
  171. var calcInputValue = CalculateApp.elements.calcInput.value;
  172. calcInputValue = calcInputValue.replace(/ /g, '');
  173. CalculateApp.elements.labelInput.value = "";
  174. CalculateApp.elements.calcInput.value = "";
  175. if (CalculateApp.isGraph(calcInputValue, labelValue)) {
  176. launchGraph(calcInputValue, labelValue);
  177. } else {
  178. launchCalculation(calcInputValue, labelValue);
  179. }
  180. CalculateApp.data.calculationDone = true
  181. CalculateApp.focus();
  182. }
  183. /* Handling of the calc remove one char button click */
  184. function calcDelClick() {
  185. removeCharacter();
  186. CalculateApp.focus();
  187. }
  188. /* Handling of the calc clear all button click */
  189. function calcClearClick() {
  190. CalculateApp.elements.calcInput.value = "";
  191. CalculateApp.focus();
  192. }
  193. /* Handling of the base palette button click */
  194. function onbaseClick() {
  195. switch (CalculateApp.data.outputBase) {
  196. case 10:
  197. CalculateApp.data.outputBase = 2;
  198. CalculateApp.elements.basePaletteDiv.style.backgroundImage = "url(icons/base-2.svg)";
  199. break;
  200. case 2:
  201. CalculateApp.data.outputBase = 8;
  202. CalculateApp.elements.basePaletteDiv.style.backgroundImage = "url(icons/base-8.svg)";
  203. break;
  204. case 8:
  205. CalculateApp.data.outputBase = 16;
  206. CalculateApp.elements.basePaletteDiv.style.backgroundImage = "url(icons/base-16.svg)";
  207. break;
  208. case 16:
  209. CalculateApp.data.outputBase = 10;
  210. CalculateApp.elements.basePaletteDiv.style.backgroundImage = "url(icons/base-10.svg)";
  211. break;
  212. }
  213. }
  214. /* Handling of the digits palette button click */
  215. function onOutputDigitsClick() {
  216. switch (CalculateApp.data.outputDigits) {
  217. case 6:
  218. CalculateApp.data.outputDigits = 9;
  219. CalculateApp.elements.outputDigitsPalette.style.backgroundImage = "url(icons/digits-9.svg)";
  220. break;
  221. case 9:
  222. CalculateApp.data.outputDigits = 12;
  223. CalculateApp.elements.outputDigitsPalette.style.backgroundImage = "url(icons/digits-12.svg)";
  224. break;
  225. case 12:
  226. CalculateApp.data.outputDigits = 15;
  227. CalculateApp.elements.outputDigitsPalette.style.backgroundImage = "url(icons/digits-15.svg)";
  228. break;
  229. case 15:
  230. CalculateApp.data.outputDigits = 6;
  231. CalculateApp.elements.outputDigitsPalette.style.backgroundImage = "url(icons/digits-6.svg)";
  232. break;
  233. }
  234. }
  235. /* Handling of the radian degree palette button click */
  236. function onradianDegreeClick(input) {
  237. if (CalculateApp.data.isRadian) {
  238. CalculateApp.data.isRadian = false;
  239. CalculateApp.elements.radianDegreePalette.style.backgroundImage = "url(icons/format-deg.svg)";
  240. } else {
  241. CalculateApp.data.isRadian = true;
  242. CalculateApp.elements.radianDegreePalette.style.backgroundImage = "url(icons/format-rad.svg)";
  243. }
  244. }
  245. /* Handling of a choosen trigo function inside trigo palette */
  246. function ontrigoClick(input) {
  247. if (CalculateApp.data.calculationDone && isNaN(input.detail.value)) {
  248. CalculateApp.elements.calcInput.value = "";
  249. }
  250. CalculateApp.data.calculationDone = false;
  251. addTextInsideInput(input.detail.value);
  252. }
  253. /* Handling of a choosen button inside algebra palette */
  254. function onalgebraClick(input) {
  255. if (CalculateApp.data.calculationDone && isNaN(input.detail.value)) {
  256. CalculateApp.elements.calcInput.value = "";
  257. }
  258. CalculateApp.data.calculationDone = false;
  259. addTextInsideInput(input.detail.value);
  260. }
  261. /* Gui init */
  262. function initGui() {
  263. CalculateApp.elements.modalDiv = document.getElementById("modal");
  264. CalculateApp.elements.resultsZoneDiv = document.getElementById("results-zone");
  265. CalculateApp.elements.calcInputDiv = document.getElementById("calc-input-div");
  266. CalculateApp.elements.calcInput = document.getElementById("calc-input");
  267. CalculateApp.elements.labelInput = document.getElementById("label-input");
  268. CalculateApp.elements.trigoPaletteDiv = document.getElementById("trigo-palette");
  269. CalculateApp.elements.algebraPaletteDiv = document.getElementById("algebra-palette");
  270. CalculateApp.elements.basePaletteDiv = document.getElementById("base-palette");
  271. CalculateApp.elements.radianDegreePalette = document.getElementById("radian-degree-palette");
  272. CalculateApp.elements.outputDigitsPalette = document.getElementById("output-digits-palette");
  273. CalculateApp.elements.calcInput.addEventListener("keyup", calcInputKeyUp, false);
  274. var buttonInputs = document.getElementsByClassName("calcbuttoninput");
  275. for (var i = 0; i < buttonInputs.length; i++) {
  276. buttonInputs[i].addEventListener("click", calcButtonClick, false);
  277. }
  278. var buttonClear = document.getElementsByClassName("calcbuttonclear");
  279. for (i = 0; i < buttonClear.length; i++) {
  280. CalculateApp.elements.calcButtonClear = buttonClear[i];
  281. buttonClear[i].addEventListener("click", calcClearClick, false);
  282. }
  283. var buttonDel = document.getElementsByClassName("calcbuttondel");
  284. for (i = 0; i < buttonDel.length; i++) {
  285. buttonDel[i].addEventListener("click", calcDelClick, false);
  286. }
  287. var buttonCalc = document.getElementsByClassName("calcbuttoncalc");
  288. for (i = 0; i < buttonCalc.length; i++) {
  289. buttonCalc[i].addEventListener("click", calcEqualClick, false);
  290. }
  291. window.onresize = CalculateApp.onResize;
  292. //Binding of the palettes
  293. CalculateApp.elements.trigoPalette = new CalculateApp.libs.trigopalette.trigoPalette(CalculateApp.elements.trigoPaletteDiv, undefined);
  294. CalculateApp.elements.trigoPalette.addEventListener('trigoClick', ontrigoClick);
  295. CalculateApp.elements.algebraPalette = new CalculateApp.libs.algebrapalette.algebraPalette(CalculateApp.elements.algebraPaletteDiv, undefined);
  296. CalculateApp.elements.algebraPalette.addEventListener('algebraClick', onalgebraClick);
  297. var isIos = (navigator.userAgent.match(/iPad|iPhone|iPod/g) ? true : false)
  298. if (isIos) {
  299. CalculateApp.elements.basePaletteDiv.addEventListener('touchstart', onbaseClick);
  300. CalculateApp.elements.radianDegreePalette.addEventListener('touchstart', onradianDegreeClick);
  301. CalculateApp.elements.outputDigitsPalette.addEventListener('touchstart', onOutputDigitsClick);
  302. } else {
  303. CalculateApp.elements.basePaletteDiv.addEventListener('click', onbaseClick);
  304. CalculateApp.elements.radianDegreePalette.addEventListener('click', onradianDegreeClick);
  305. CalculateApp.elements.outputDigitsPalette.addEventListener('click', onOutputDigitsClick);
  306. }
  307. }