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.

81 lines
2.7 KiB

  1. define(["sugar-web/graphics/palette"], function (palette) {
  2. 'use strict';
  3. var templatepalette = {};
  4. templatepalette.TemplatePalette = function (invoker, primaryText, templates) {
  5. palette.Palette.call(this, invoker, primaryText);
  6. this.sharedEvent = document.createEvent("CustomEvent");
  7. this.sharedEvent.initCustomEvent('template', true, true, {});
  8. this.buttons = [];
  9. var div = document.createElement('div');
  10. for (var i = 0; i < templates.length; i++) {
  11. var template = templates[i];
  12. var button = document.createElement("div");
  13. button.value = template;
  14. button.onmouseover = function() {
  15. this.style.background = "#ccc";
  16. };
  17. button.onmouseout = function() {
  18. this.style.background = "#000";
  19. };
  20. button.style.borderRadius = "0";
  21. button.style.width = "100%";
  22. if (i != 0) {
  23. button.style.marginTop = "3px";
  24. }
  25. button.innerHTML = "<img style='vertical-align: middle; margin-right:3px;' src='icons/" + template.icon + "'>" + template.name;
  26. div.appendChild(button);
  27. this.buttons.push(button);
  28. }
  29. this.setContent([div]);
  30. // Pop-down the palette when a item in the menu is clicked.
  31. var that = this;
  32. that.getPalette().firstChild.style.backgroundColor = "transparent";
  33. that.getPalette().firstChild.style.backgroundImage = "";
  34. function popDownOnButtonClick(event) {
  35. console.log(event);
  36. that.popDown();
  37. }
  38. for (var i = 0; i < this.buttons.length; i++) {
  39. var t = this;
  40. var button = t.buttons[i];
  41. var template = templates[i];
  42. (function (button, template) {
  43. button.addEventListener("click", function () {
  44. that.sharedEvent.detail.value = template;
  45. that.getPalette().dispatchEvent(that.sharedEvent);
  46. that.popDown();
  47. });
  48. })(button, template);
  49. button.addEventListener('template', popDownOnButtonClick);
  50. }
  51. };
  52. var addEventListener = function (type, listener, useCapture) {
  53. return this.getPalette().addEventListener(type, listener, useCapture);
  54. };
  55. templatepalette.TemplatePalette.prototype =
  56. Object.create(palette.Palette.prototype, {
  57. addEventListener: {
  58. value: addEventListener,
  59. enumerable: true,
  60. configurable: true,
  61. writable: true
  62. }
  63. });
  64. return templatepalette;
  65. });