PlaceholderMenuItem.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. define("dojox/widget/PlaceholderMenuItem", ["dojo", "dijit", "dojox", "dijit/Menu","dijit/MenuItem"], function(dojo, dijit, dojox){
  2. dojo.experimental("dojox.widget.PlaceholderMenuItem");
  3. dojo.declare("dojox.widget.PlaceholderMenuItem", dijit.MenuItem, {
  4. // summary:
  5. // A menu item that can be used as a placeholder. Set the label
  6. // of this item to a unique key and you can then use it to add new
  7. // items at that location. This item is not displayed.
  8. _replaced: false,
  9. _replacedWith: null,
  10. _isPlaceholder: true,
  11. postCreate: function(){
  12. this.domNode.style.display = "none";
  13. this._replacedWith = [];
  14. if(!this.label){
  15. this.label = this.containerNode.innerHTML;
  16. }
  17. this.inherited(arguments);
  18. },
  19. replace: function(/*dijit.MenuItem[]*/ menuItems){
  20. // summary:
  21. // replaces this menu item with the given menuItems. The original
  22. // menu item is not actually removed from the menu - so if you want
  23. // it removed, you must do that explicitly.
  24. // returns:
  25. // true if the replace happened, false if not
  26. if(this._replaced){ return false; }
  27. var index = this.getIndexInParent();
  28. if(index < 0){ return false; }
  29. var p = this.getParent();
  30. dojo.forEach(menuItems, function(item){
  31. p.addChild(item, index++);
  32. });
  33. this._replacedWith = menuItems;
  34. this._replaced = true;
  35. return true;
  36. },
  37. unReplace: function(/*Boolean?*/ destroy){
  38. // summary:
  39. // Removes menu items added by calling replace(). It returns the
  40. // array of items that were actually removed (in case you want to
  41. // clean them up later)
  42. // destroy:
  43. // Also call destroy on any removed items.
  44. // returns:
  45. // The array of items that were actually removed
  46. if(!this._replaced){ return []; }
  47. var p = this.getParent();
  48. if(!p){ return []; }
  49. var r = this._replacedWith;
  50. dojo.forEach(this._replacedWith, function(item){
  51. p.removeChild(item);
  52. if(destroy){
  53. item.destroyRecursive();
  54. }
  55. });
  56. this._replacedWith = [];
  57. this._replaced = false;
  58. return r; // dijit.MenuItem[]
  59. }
  60. });
  61. // Se need to extend dijit.Menu so that we have a getPlaceholders function.
  62. dojo.extend(dijit.Menu, {
  63. getPlaceholders: function(/*String?*/ label){
  64. // summary:
  65. // Returns an array of placeholders with the given label. There
  66. // can be multiples.
  67. // label:
  68. // Label to search for - if not specified, then all placeholders
  69. // are returned
  70. // returns:
  71. // An array of placeholders that match the given label
  72. var r = [];
  73. var children = this.getChildren();
  74. dojo.forEach(children, function(child){
  75. if(child._isPlaceholder && (!label || child.label == label)){
  76. r.push(child);
  77. }else if(child._started && child.popup && child.popup.getPlaceholders){
  78. r = r.concat(child.popup.getPlaceholders(label));
  79. }else if(!child._started && child.dropDownContainer){
  80. var node = dojo.query("[widgetId]", child.dropDownContainer)[0];
  81. var menu = dijit.byNode(node);
  82. if(menu.getPlaceholders){
  83. r = r.concat(menu.getPlaceholders(label));
  84. }
  85. }
  86. }, this);
  87. return r; // dojox.widget.PlaceholderMenuItem[]
  88. }
  89. });
  90. return dojox.widget.PlaceholderMenuItem;
  91. });