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.

110 lines
3.6 KiB

  1. /* Stamp palette to pick a stamp */
  2. define([
  3. 'sugar-web/graphics/palette',
  4. 'mustache'
  5. ], function (palette, mustache) {
  6. var stamppalette = {};
  7. /* We setup the palette with stamps */
  8. function getRows() {
  9. var platform = 'webkit';
  10. var isFirefox = typeof InstallTrigger !== 'undefined';
  11. if (isFirefox) {
  12. platform = 'gecko';
  13. }
  14. var rows = [
  15. [
  16. {
  17. stampBase: 'stamps/heart-{platform}.svg',
  18. proportionnal: true
  19. },
  20. {
  21. stampBase: 'stamps/star-{platform}.svg',
  22. proportionnal: true
  23. },
  24. {
  25. stampBase: 'stamps/square-{platform}.svg',
  26. proportionnal: false
  27. }
  28. ],
  29. [
  30. {
  31. stampBase: 'stamps/circle-{platform}.svg',
  32. proportionnal: true
  33. },
  34. {
  35. stampBase: 'stamps/triangle-{platform}.svg',
  36. proportionnal: true
  37. },
  38. {
  39. stampBase: 'stamps/flower-{platform}.svg',
  40. proportionnal: true
  41. }
  42. ]
  43. ];
  44. for (var i = 0; i < rows.length; i++) {
  45. for (var j = 0; j < rows[i].length; j++) {
  46. rows[i][j].stamp = rows[i][j].stampBase.replace('{platform}', platform);
  47. }
  48. }
  49. return rows;
  50. }
  51. stamppalette.StampPalette = function (invoker, primaryText) {
  52. palette.Palette.call(this, invoker, primaryText);
  53. this.stampChangeEvent = document.createEvent('CustomEvent');
  54. this.stampChangeEvent.initCustomEvent('stampChange', true, true, {});
  55. this.template = '<tbody>' + '{{#rows}}' + '<tr>' + '{{#.}}' + '<td>' + '<button base="{{stampBase}}" proportionnal="{{proportionnal}}" value="{{stamp}}" style="height:55px; width:55px; background-size:40px; background-image: url({{ stamp }}); background-repeat: no-repeat; background-position: center; "></button>' + '</td>' + '{{/.}}' + '</tr>' + '{{/rows}}' + '</tbody>';
  56. var stampsElem = document.createElement('table');
  57. stampsElem.className = 'stamps';
  58. var stampsData = { rows: getRows() };
  59. stampsElem.innerHTML = mustache.render(this.template, stampsData);
  60. this.setContent([stampsElem]);
  61. // Pop-down the palette when a item in the menu is clicked.
  62. var buttons = stampsElem.querySelectorAll('button');
  63. this.buttons = buttons;
  64. var that = this;
  65. function popDownOnButtonClick(event) {
  66. for (var i = 0; i < buttons.length; i++) {
  67. buttons[i].style.border = '0px solid #000';
  68. }
  69. event.target.style.border = '1px solid #f00';
  70. that.stampChangeEvent.detail.proportionnal = event.target.getAttribute('proportionnal');
  71. that.stampChangeEvent.detail.stampBase = event.target.getAttribute('base');
  72. that.stampChangeEvent.detail.stamp = event.target.value;
  73. that.getPalette().dispatchEvent(that.stampChangeEvent);
  74. that.popDown();
  75. }
  76. for (var i = 0; i < buttons.length; i++) {
  77. buttons[i].addEventListener('click', popDownOnButtonClick);
  78. }
  79. };
  80. var setStamp = function (index) {
  81. // Click the nth button
  82. var event = document.createEvent('MouseEvents');
  83. event.initEvent('click', true, true);
  84. this.buttons[index].dispatchEvent(event);
  85. };
  86. var addEventListener = function (type, listener, useCapture) {
  87. return this.getPalette().addEventListener(type, listener, useCapture);
  88. };
  89. stamppalette.StampPalette.prototype = Object.create(palette.Palette.prototype, {
  90. setStamp: {
  91. value: setStamp,
  92. enumerable: true,
  93. configurable: true,
  94. writable: true
  95. },
  96. addEventListener: {
  97. value: addEventListener,
  98. enumerable: true,
  99. configurable: true,
  100. writable: true
  101. }
  102. });
  103. return stamppalette;
  104. });