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.

95 lines
3.0 KiB

  1. /* Text palette to enter text and to choose font */
  2. define([
  3. 'sugar-web/graphics/palette',
  4. 'mustache'
  5. ], function(palette, mustache) {
  6. var textpalette = {};
  7. /* We setup the palette with fonts */
  8. textpalette.TextPalette = function(invoker, primaryText) {
  9. palette.Palette.call(this, invoker, primaryText);
  10. this.getPalette().id = "fontpalette";
  11. this.fontChangeEvent = document.createEvent('CustomEvent');
  12. this.fontChangeEvent.initCustomEvent('fontChange', true, true, {
  13. 'family': 'Arial'
  14. });
  15. this.template = '<center><table><tbody>' + '{{#rows}}' + '<tr>' + '{{#.}}' + '<td>' + '<button lineHeight="{{lineHeight}}" title="{{fontFamily}}" style="height:55px; width:55px; background-size:40px !important; background: #fff url({{ icon }}) no-repeat center; "></button>' + '</td>' + '{{/.}}' + '</tr>' + '{{/rows}}' + '</tbody></table></center>' + '<br/>';
  16. var rows = [
  17. [{
  18. icon: 'icons/font-arial.svg',
  19. lineHeight: '0.7',
  20. fontFamily: 'Arial'
  21. }, {
  22. icon: 'icons/font-comic-sans.svg',
  23. lineHeight: '0.8',
  24. fontFamily: 'Comic Sans MS'
  25. }, {
  26. icon: 'icons/font-verdana.svg',
  27. lineHeight: '0.8',
  28. fontFamily: 'Verdana'
  29. }]
  30. ];
  31. var textsElem = document.createElement('div');
  32. textsElem.innerHTML = mustache.render(this.template, {
  33. rows: rows
  34. });
  35. this.setContent([textsElem]);
  36. var buttons = textsElem.querySelectorAll('button');
  37. var that = this;
  38. function popDownOnButtonClick(event) {
  39. for (var i = 0; i < buttons.length; i++) {
  40. buttons[i].style.border = '0px solid #000';
  41. }
  42. event.target.style.border = '1px solid #f00';
  43. that.popDown();
  44. }
  45. for (var i = 0; i < buttons.length; i++) {
  46. buttons[i].addEventListener('click', function(e) {
  47. for (var i = 0; i < buttons.length; i++) {
  48. buttons[i].style.border = '0px solid #000';
  49. }
  50. e.target.style.border = '1px solid #f00';
  51. that.fontChangeEvent.detail.family = e.target.title;
  52. that.getPalette().dispatchEvent(that.fontChangeEvent);
  53. that.popDown();
  54. });
  55. }
  56. popDownOnButtonClick({
  57. target: buttons[0]
  58. });
  59. };
  60. var setFont = function (font) {
  61. var buttons = this.getPalette().querySelectorAll('button');
  62. for (var i = 0; i < buttons.length; i++) {
  63. if (font == buttons[i].title)
  64. buttons[i].style.border = '1px solid #f00';
  65. else
  66. buttons[i].style.border = '0px solid #000';
  67. }
  68. };
  69. var addEventListener = function(type, listener, useCapture) {
  70. return this.getPalette().addEventListener(type, listener, useCapture);
  71. };
  72. textpalette.TextPalette.prototype = Object.create(palette.Palette.prototype, {
  73. setFont: {
  74. value: setFont,
  75. enumerable: true,
  76. configurable: true,
  77. writable: true
  78. },
  79. addEventListener: {
  80. value: addEventListener,
  81. enumerable: true,
  82. configurable: true,
  83. writable: true
  84. }
  85. });
  86. return textpalette;
  87. });