MenuItemSources.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import strings = require("ui/EditorStrings");
  23. import EditorEvents = require("editor/EditorEvents");
  24. import EditorUI = require("ui/EditorUI");
  25. var UIMenuItemSource = Atomic.UIMenuItemSource;
  26. var UIMenuItem = Atomic.UIMenuItem;
  27. var StringID = strings.StringID;
  28. var srcLookup = {};
  29. export function getMenuItemSource(id: string): Atomic.UIMenuItemSource {
  30. return srcLookup[id];
  31. }
  32. function createMenuItemSourceRecursive(items: any): Atomic.UIMenuItemSource {
  33. var src = new UIMenuItemSource();
  34. for (var key in items) {
  35. if (items.hasOwnProperty(key)) {
  36. var value = items[key];
  37. if (typeof value === "string") {
  38. src.addItem(new UIMenuItem(key, value));
  39. } else if (value == null) {
  40. // add a separator
  41. src.addItem(new UIMenuItem(key));
  42. } else if (Object.prototype.toString.call(value) === "[object Array]") {
  43. if (value.length == 1)
  44. src.addItem(new UIMenuItem(key, value[0]));
  45. else if (value.length == 2)
  46. src.addItem(new UIMenuItem(key, value[0], strings.EditorString.GetString(value[1])));
  47. else if (value.length == 3) {
  48. var str = "";
  49. if (value[1])
  50. str = strings.EditorString.GetString(value[1]);
  51. var skinBg = "";
  52. if (value[2])
  53. skinBg = value[2];
  54. src.addItem(new UIMenuItem(key, value[0], str, skinBg));
  55. }
  56. }
  57. else if (typeof value === "object") {
  58. createSubMenuItemSource(src, key, value);
  59. }
  60. }
  61. }
  62. return src;
  63. }
  64. export function createSubMenuItemSource(src: Atomic.UIMenuItemSource, id: string, items: any): Atomic.UIMenuItemSource {
  65. var subsrc = createMenuItemSourceRecursive(items);
  66. var item = new Atomic.UIMenuItem(id);
  67. item.subSource = subsrc;
  68. src.addItem(item);
  69. return subsrc;
  70. }
  71. export function createMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  72. srcLookup[id] = createMenuItemSourceRecursive(items);
  73. return srcLookup[id];
  74. }
  75. export function deleteMenuItemSource(id: string) {
  76. if (srcLookup[id]) {
  77. delete srcLookup[id];
  78. }
  79. }