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.

93 lines
3.6 KiB

  1. define(["sugar-web/graphics/palette"], function (palette) {
  2. 'use strict';
  3. var formatpalette = {};
  4. formatpalette.Formatpalette = function (invoker, primaryText, menuData) {
  5. palette.Palette.call(this, invoker, primaryText);
  6. this.formatEvent = document.createEvent("CustomEvent");
  7. this.formatEvent.initCustomEvent('format', true, true, {});
  8. this.remoteEvent = document.createEvent("CustomEvent");
  9. this.remoteEvent.initCustomEvent('remote', true, true, {});
  10. var that = this;
  11. that.categories = [];
  12. that.buttons = [];
  13. this.setCategories = function( newcategories) {
  14. this.categories = newcategories;
  15. this.buttons = [];
  16. var div = document.createElement('div');
  17. for (var i = 0 ; i < newcategories.length ; i++) {
  18. var newbutton = document.createElement('button');
  19. newbutton.className = 'toolbutton palette-button palette-button-notselected';
  20. newbutton.setAttribute('id', newcategories[i].id);
  21. newbutton.setAttribute('title', newcategories[i].title);
  22. var url = "url(icons/"+newcategories[i].cmd+".svg)";
  23. newbutton.style.backgroundImage = url;
  24. var newid = newcategories[i].id;
  25. newbutton.onclick = function() {
  26. that.setFormat(this.id);
  27. }
  28. this.buttons.push(newbutton);
  29. div.appendChild(newbutton);
  30. }
  31. this.setContent([div]);
  32. }
  33. this.setFormat = function(newformat) {
  34. var currentFormat = this.getFormat();
  35. var formatIndex = -1;
  36. for (var i = 0 ; i < this.categories.length ; i++) {
  37. if (this.categories[i].id == newformat) {
  38. formatIndex = i;
  39. break;
  40. }
  41. }
  42. if (formatIndex == -1) {
  43. return;
  44. }
  45. // Removed highlighting of format buttons to avoid confusion for user
  46. // console.log(this.buttons[formatIndex].className);
  47. // if(this.buttons[formatIndex].className == 'toolbutton palette-button palette-button-notselected'){
  48. // this.buttons[formatIndex].className = 'toolbutton palette-button palette-button-selected';
  49. // } else {
  50. // this.buttons[formatIndex].className = 'toolbutton palette-button palette-button-notselected';
  51. // }
  52. that.getPalette().dispatchEvent(that.formatEvent);
  53. }
  54. };
  55. var addEventListener = function (type, listener, useCapture) {
  56. return this.getPalette().addEventListener(type, listener, useCapture);
  57. };
  58. formatpalette.Formatpalette.prototype =
  59. Object.create(palette.Palette.prototype, {
  60. addEventListener: {
  61. value: addEventListener,
  62. enumerable: true,
  63. configurable: true,
  64. writable: true
  65. }
  66. });
  67. formatpalette.Formatpalette.prototype.setCategories = function(newcategories) {
  68. this.setCategories(newcategories);
  69. }
  70. formatpalette.Formatpalette.prototype.setFormat = function(newformat) {
  71. this.setFormat(newformat);
  72. }
  73. formatpalette.Formatpalette.prototype.getFormat = function() {
  74. for (var i = 0 ; i < this.buttons.length ; i++) {
  75. if (this.buttons[i].className == 'toolbutton palette-button palette-button-selected')
  76. return this.categories[i].id;
  77. }
  78. return "";
  79. }
  80. return formatpalette;
  81. });