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.5 KiB

  1. define(["sugar-web/graphics/palette"], function (palette) {
  2. 'use strict';
  3. var filterpalette = {};
  4. filterpalette.FilterPalette = function (invoker, primaryText, menuData) {
  5. palette.Palette.call(this, invoker, primaryText);
  6. this.filterEvent = document.createEvent("CustomEvent");
  7. this.filterEvent.initCustomEvent('filter', 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 filter-item';
  20. newbutton.setAttribute('id', newcategories[i].id);
  21. newbutton.setAttribute('title', newcategories[i].title);
  22. newbutton.innerHTML = newcategories[i].title;
  23. var newid = newcategories[i].id;
  24. newbutton.onclick = function() {
  25. that.setFilter(this.id);
  26. }
  27. this.buttons.push(newbutton);
  28. div.appendChild(newbutton);
  29. }
  30. this.setContent([div]);
  31. }
  32. this.setFilter = function(newfilter) {
  33. var currentFilter = this.getFilter();
  34. var noFilter = (currentFilter.length != 0 && currentFilter == newfilter);
  35. var filterIndex = -1;
  36. for (var i = 0 ; i < this.categories.length ; i++) {
  37. if (this.categories[i].id == newfilter) {
  38. filterIndex = i;
  39. break;
  40. }
  41. }
  42. if (filterIndex == -1) {
  43. return;
  44. }
  45. for (var i = 0 ; i < this.buttons.length ; i++) {
  46. this.buttons[i].className = 'toolbutton palette-button palette-button-notselected filter-item';
  47. }
  48. if (noFilter) {
  49. this.getPalette().dispatchEvent(this.filterEvent);
  50. return;
  51. }
  52. this.buttons[filterIndex].className = 'toolbutton palette-button palette-button-selected filter-item';
  53. that.getPalette().dispatchEvent(that.filterEvent);
  54. }
  55. };
  56. var addEventListener = function (type, listener, useCapture) {
  57. return this.getPalette().addEventListener(type, listener, useCapture);
  58. };
  59. filterpalette.FilterPalette.prototype =
  60. Object.create(palette.Palette.prototype, {
  61. addEventListener: {
  62. value: addEventListener,
  63. enumerable: true,
  64. configurable: true,
  65. writable: true
  66. }
  67. });
  68. filterpalette.FilterPalette.prototype.setCategories = function(newcategories) {
  69. this.setCategories(newcategories);
  70. }
  71. filterpalette.FilterPalette.prototype.setFilter = function(newfilter) {
  72. this.setFilter(newfilter);
  73. }
  74. filterpalette.FilterPalette.prototype.getFilter = function() {
  75. for (var i = 0 ; i < this.buttons.length ; i++) {
  76. if (this.buttons[i].className == 'toolbutton palette-button palette-button-selected filter-item')
  77. return this.categories[i].id;
  78. }
  79. return "";
  80. }
  81. return filterpalette;
  82. });