PlaceholderMenuItem.js 3.4 KB

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