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.

162 lines
5.5 KiB

  1. /* Color palette to choose note color */
  2. define([
  3. 'sugar-web/graphics/palette',
  4. 'mustache'
  5. ], function (palette, mustache) {
  6. var notepalette = {};
  7. function parseColor(input) {
  8. var colors = input.split("(")[1].split(")")[0].split(",");
  9. for (var i = 0; i < colors.length; i++) {
  10. colors[i] = parseInt(colors[i]);
  11. }
  12. return colors;
  13. }
  14. function initGui() {
  15. function onColorChangeFill(event) {
  16. PaintApp.data.color.fill = event.detail.color;
  17. }
  18. var colorsButtonFill = document.getElementById('colors-button-fill');
  19. var colorPaletteFill = new PaintApp.palettes.notePalette.NotePalette(colorsButtonFill, undefined);
  20. colorPaletteFill.addEventListener('colorChange', onColorChangeFill);
  21. var colorInvokerFill = colorPaletteFill.getPalette().querySelector('.palette-invoker');
  22. colorPaletteFill.setColor(0);
  23. }
  24. notepalette.initGui = initGui;
  25. /* We setup the palette with Sugar colors */
  26. notepalette.NotePalette = function (invoker, primaryText) {
  27. this.invoker = invoker;
  28. palette.Palette.call(this, invoker, primaryText);
  29. this.getPalette().className += " colorpalette";
  30. this.colorChangeEvent = document.createEvent('CustomEvent');
  31. this.colorChangeEvent.initCustomEvent('colorChange', true, true, {
  32. 'color': '#ed2529'
  33. });
  34. this.template = '\
  35. <style>\
  36. @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {\
  37. input[type=range] {\
  38. width:100px;\
  39. background-color: transparent !important; /* Hides the slider so custom styles can be added */\
  40. }\
  41. }\
  42. </style>\
  43. <div style="width:140px;">\
  44. <div style="float:left; width: 100px;">\
  45. <table>\
  46. <tbody>\
  47. {{#rows}}\
  48. <tr>\
  49. {{#.}}\
  50. <td>\
  51. <button style="background-color: {{ color }}"></button>\
  52. </td>\
  53. {{/.}}\
  54. </tr>\
  55. {{/rows}}\
  56. </tbody>\
  57. </table>\
  58. </div>\
  59. \
  60. </div>'
  61. // this.template = '<tbody>' + '{{#rows}}' + '<tr>' + '{{#.}}' + '<td>' + '<button style="background-color: {{ color }}"></button>' + '</td>' + '{{/.}}' + '</tr>' + '{{/rows}}' + '</tbody>';
  62. var colorsElem = document.createElement('div');
  63. colorsElem.className = 'colors';
  64. var colorsData = {
  65. rows: [
  66. [{
  67. color: '#FFF29F'
  68. }, {
  69. color: '#FFCBA6'
  70. }],
  71. [{
  72. color: '#81FFC4'
  73. }, {
  74. color: '#FFB7F9'
  75. }],
  76. [{
  77. color: '#C39AFF'
  78. }, {
  79. color: '#9AFAFF'
  80. }]
  81. ]
  82. };
  83. colorsElem.innerHTML = mustache.render(this.template, colorsData);
  84. this.setContent([colorsElem]);
  85. colorsElem.parentNode.style.backgroundColor = 'black';
  86. colorsElem.parentNode.parentNode.style.minWidth = '120px';
  87. colorsElem.parentNode.parentNode.style.maxWidth = '120px';
  88. colorsElem.parentNode.style.width = '120px';
  89. colorsElem.parentNode.style.height = '170px';
  90. colorsElem.parentNode.style.overflowY = 'auto';
  91. colorsElem.parentNode.style.overflowX = 'hidden';
  92. function popDownOnButtonClick(event, close, shouldUpdateSliders) {
  93. invoker.style.backgroundColor = event.target.style.backgroundColor;
  94. that.colorChangeEvent.detail.color = event.target.style.backgroundColor;
  95. that.getPalette().dispatchEvent(that.colorChangeEvent);
  96. that.getPalette().querySelector('.palette-invoker').style.backgroundColor = event.target.style.backgroundColor;
  97. if (close === null || close === undefined || close === true) {
  98. that.popDown();
  99. }
  100. }
  101. // Pop-down the palette when a item in the menu is clicked.
  102. this.buttons = colorsElem.querySelectorAll('button');
  103. var that = this;
  104. that.colorsElem = colorsElem;
  105. for (var i = 0; i < this.buttons.length; i++) {
  106. this.buttons[i].addEventListener('click', popDownOnButtonClick);
  107. }
  108. };
  109. var setColor = function (color) {
  110. // Look for matching button
  111. for (var i = 0 ; i < this.buttons.length ; i++) {
  112. if (this.buttons[i].style.backgroundColor == color) {
  113. var event = document.createEvent('MouseEvents');
  114. event.initEvent('click', true, true);
  115. this.buttons[i].dispatchEvent(event);
  116. break;
  117. }
  118. }
  119. // Set sliders values
  120. var colors = parseColor(color);
  121. this.getPalette().querySelector('.palette-invoker').style.backgroundColor = color;
  122. this.invoker.style.backgroundColor = color;
  123. };
  124. var addEventListener = function (type, listener, useCapture) {
  125. return this.getPalette().addEventListener(type, listener, useCapture);
  126. };
  127. notepalette.NotePalette.prototype = Object.create(palette.Palette.prototype, {
  128. setColor: {
  129. value: setColor,
  130. enumerable: true,
  131. configurable: true,
  132. writable: true
  133. },
  134. addEventListener: {
  135. value: addEventListener,
  136. enumerable: true,
  137. configurable: true,
  138. writable: true
  139. }
  140. });
  141. return notepalette;
  142. })
  143. ;