MenuItemSources.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 EditorUI = require("ui/EditorUI");
  24. var UIMenuItemSource = Atomic.UIMenuItemSource;
  25. var UIMenuItem = Atomic.UIMenuItem;
  26. var StringID = strings.StringID;
  27. var srcLookup = {};
  28. export function getMenuItemSource(id: string): Atomic.UIMenuItemSource {
  29. return srcLookup[id];
  30. }
  31. function createMenuItemSourceRecursive(items: any): Atomic.UIMenuItemSource {
  32. var src = new UIMenuItemSource();
  33. for (var key in items) {
  34. if (items.hasOwnProperty(key)) {
  35. var value = items[key];
  36. if (typeof value === "string") {
  37. src.addItem(new UIMenuItem(key, value));
  38. } else if (value == null) {
  39. // add a separator
  40. src.addItem(new UIMenuItem(key));
  41. } else if (Object.prototype.toString.call(value) === "[object Array]") {
  42. if (value.length == 1)
  43. src.addItem(new UIMenuItem(key, value[0]));
  44. else if (value.length == 2) {
  45. var prospectShortcut = value[1]; // raw shortcut or editor shortcut
  46. if ( strings.EditorString.GetString(value[1]) != undefined ) // editor defined shortcut
  47. prospectShortcut = strings.EditorString.GetString(value[1]);
  48. src.addItem(new UIMenuItem(key, value[0], prospectShortcut));
  49. }
  50. else if (value.length == 3) {
  51. var str = "";
  52. if (value[1]) {
  53. str = value[1];
  54. if ( strings.EditorString.GetString(value[1]) != undefined )
  55. str = strings.EditorString.GetString(value[1]);
  56. }
  57. var skinBg = "";
  58. if (value[2])
  59. skinBg = value[2];
  60. src.addItem(new UIMenuItem(key, value[0], str, skinBg));
  61. }
  62. }
  63. else if (typeof value === "object") {
  64. createSubMenuItemSource(src, key, value);
  65. }
  66. }
  67. }
  68. return src;
  69. }
  70. export function createSubMenuItemSource(src: Atomic.UIMenuItemSource, id: string, items: any): Atomic.UIMenuItemSource {
  71. var subsrc = createMenuItemSourceRecursive(items);
  72. var item = new Atomic.UIMenuItem(id);
  73. item.subSource = subsrc;
  74. src.addItem(item);
  75. return subsrc;
  76. }
  77. export function createMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  78. srcLookup[id] = createMenuItemSourceRecursive(items);
  79. return srcLookup[id];
  80. }
  81. export function deleteMenuItemSource(id: string) {
  82. if (srcLookup[id]) {
  83. delete srcLookup[id];
  84. }
  85. }