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.

118 lines
3.4 KiB

  1. define(["sugar-web/graphics/palette", "mustache"],
  2. function(palette, mustache) {
  3. var algebrapalette = {};
  4. var isIos = (navigator.userAgent.match(/iPad|iPhone|iPod/g) ? true : false)
  5. algebrapalette.algebraPalette = function(invoker, primaryText) {
  6. palette.Palette.call(this, invoker, primaryText);
  7. this.algebraChangeEvent = document.createEvent("CustomEvent");
  8. this.algebraChangeEvent.initCustomEvent('algebraClick', true, true, {});
  9. this.template =
  10. '<tbody>' +
  11. '{{#rows}}' +
  12. '<tr>' +
  13. '{{#.}}' +
  14. '<td>' +
  15. '<button style="background:none; border-radius:0px; border:0px; margin:3px; width:55px; height:55px; background-image: url({{backgroundSvg}})" value="{{value}}"></button>' +
  16. '</td>' +
  17. '{{/.}}' +
  18. '</tr>' +
  19. '{{/rows}}' +
  20. '</tbody>';
  21. var algebraElem = document.createElement('table');
  22. algebraElem.className = "algebras";
  23. var algebraData = {
  24. rows: [
  25. [{
  26. title: "EXP",
  27. value: "exp(",
  28. backgroundSvg: "icons/algebra-exp.svg"
  29. }, {
  30. title: "LN",
  31. value: "log(",
  32. backgroundSvg: "icons/algebra-ln.svg"
  33. }, {
  34. title: "SQRT",
  35. value: "sqrt(",
  36. backgroundSvg: "icons/algebra-sqrt.svg"
  37. }],
  38. [{
  39. title: "X",
  40. value: "x",
  41. backgroundSvg: "icons/algebra-x.svg"
  42. }, {
  43. title: "POW",
  44. value: "^",
  45. backgroundSvg: "icons/algebra-xpowy.svg"
  46. }, {
  47. title: "PI",
  48. value: "3.14159",
  49. backgroundSvg: "icons/constants-pi.svg"
  50. }],
  51. [{
  52. title: "F(X)",
  53. value: "f(x)=",
  54. backgroundSvg: "icons/plot.svg"
  55. }]
  56. ]
  57. };
  58. algebraElem.innerHTML = mustache.render(this.template, algebraData);
  59. this.setContent([algebraElem]);
  60. // Pop-down the palette when a item in the menu is clicked.
  61. this.buttons = algebraElem.querySelectorAll('button');
  62. var that = this;
  63. function popDownOnButtonClick(event) {
  64. that.algebraChangeEvent.detail.value = event.target.value;
  65. that.getPalette().dispatchEvent(that.algebraChangeEvent);
  66. that.popDown();
  67. }
  68. for (var i = 0; i < this.buttons.length; i++) {
  69. if (isIos) {
  70. this.buttons[i].addEventListener('touchstart', popDownOnButtonClick);
  71. } else {
  72. this.buttons[i].addEventListener('click', popDownOnButtonClick);
  73. }
  74. }
  75. };
  76. var setColor = function(index) {
  77. // Click the nth button
  78. var event = document.createEvent("MouseEvents");
  79. event.initEvent("click", true, true);
  80. this.buttons[index].dispatchEvent(event);
  81. };
  82. var addEventListener = function(type, listener, useCapture) {
  83. return this.getPalette().addEventListener(type, listener, useCapture);
  84. };
  85. algebrapalette.algebraPalette.prototype =
  86. Object.create(palette.Palette.prototype, {
  87. setColor: {
  88. value: setColor,
  89. enumerable: true,
  90. configurable: true,
  91. writable: true
  92. },
  93. addEventListener: {
  94. value: addEventListener,
  95. enumerable: true,
  96. configurable: true,
  97. writable: true
  98. }
  99. });
  100. return algebrapalette;
  101. });