Portlet.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // wrapped by build app
  2. define("dojox/widget/Portlet", ["dijit","dojo","dojox","dojo/require!dijit/TitlePane,dojo/fx"], function(dijit,dojo,dojox){
  3. dojo.experimental("dojox.widget.Portlet");
  4. dojo.provide("dojox.widget.Portlet");
  5. dojo.require("dijit.TitlePane");
  6. dojo.require("dojo.fx");
  7. dojo.declare("dojox.widget.Portlet", [dijit.TitlePane, dijit._Container],{
  8. // summary: A container widget that is designed to be contained
  9. // in a dojox.layout.GridContainer. Child widgets can insert
  10. // an icon into the title bar of the Portlet, which when
  11. // clicked, executes the "toggle" method of the child widget.
  12. // A child widget must specify the attribute
  13. // "portletIconClass", and the optional class
  14. // "portletIconHoverClass", as well as the
  15. // "toggle" function.
  16. // resizeChildren: Boolean
  17. // If true, when the Portlet is resized, any child widgets
  18. // with a 'resize' method have that method called.
  19. resizeChildren: true,
  20. // closable: Boolean
  21. // If true, a close button is placed in the title bar,
  22. // and the Portlet can be hidden. If false, the Portlet
  23. // cannot be closed.
  24. closable: true,
  25. // _parents: Array
  26. // An array of all the StackContainer widgets that this Portlet
  27. // is contained in. These are used to determine if the portlet
  28. // is visible or not.
  29. _parents: null,
  30. // _size: Object
  31. // Cache of the previous size of the portlet, used to determine
  32. // if the size has changed and if the child widgets should be
  33. // resized.
  34. _size: null,
  35. // dragRestriction: Boolean
  36. // To remove the drag capability.
  37. dragRestriction : false,
  38. buildRendering: function(){
  39. this.inherited(arguments);
  40. // Hide the portlet until it is fully constructed.
  41. dojo.style(this.domNode, "visibility", "hidden");
  42. },
  43. postCreate: function(){
  44. this.inherited(arguments);
  45. // Add the portlet classes
  46. dojo.addClass(this.domNode, "dojoxPortlet");
  47. dojo.removeClass(this.arrowNode, "dijitArrowNode");
  48. dojo.addClass(this.arrowNode, "dojoxPortletIcon dojoxArrowDown");
  49. dojo.addClass(this.titleBarNode, "dojoxPortletTitle");
  50. dojo.addClass(this.hideNode, "dojoxPortletContentOuter");
  51. // Choose the class to add depending on if the portlet is draggable or not.
  52. dojo.addClass(this.domNode, "dojoxPortlet-" + (!this.dragRestriction ? "movable" : "nonmovable"));
  53. var _this = this;
  54. if(this.resizeChildren){
  55. // If children should be resized when the portlet size changes,
  56. // listen for items being dropped, when the window is resized,
  57. // or when another portlet's size changes.
  58. this.subscribe("/dnd/drop", function(){_this._updateSize();});
  59. this.subscribe("/Portlet/sizechange", function(widget){_this.onSizeChange(widget);});
  60. this.connect(window, "onresize", function(){_this._updateSize();});
  61. // Subscribe to all possible child-selection events that could affect this
  62. // portlet
  63. var doSelectSubscribe = dojo.hitch(this, function(id, lastId){
  64. var widget = dijit.byId(id);
  65. if(widget.selectChild){
  66. var s = this.subscribe(id + "-selectChild", function(child){
  67. var n = _this.domNode.parentNode;
  68. while(n){
  69. if(n == child.domNode){
  70. // Only fire this once, as the widget is now visible
  71. // at least once, so child measurements should be accurate.
  72. _this.unsubscribe(s);
  73. _this._updateSize();
  74. break;
  75. }
  76. n = n.parentNode;
  77. }
  78. });
  79. // Record the StackContainer and child widget that this portlet
  80. // is in, so it can figure out whether or not it is visible.
  81. // If it is not visible, it will not update it's size dynamically.
  82. var child = dijit.byId(lastId);
  83. if(widget && child){
  84. _this._parents.push({parent: widget, child: child});
  85. }
  86. }
  87. });
  88. var lastId;
  89. this._parents = [];
  90. // Find all parent widgets, and if they are StackContainers,
  91. // subscribe to their selectChild method calls.
  92. for(var p = this.domNode.parentNode; p != null; p = p.parentNode){
  93. var id = p.getAttribute ? p.getAttribute("widgetId") : null;
  94. if(id){
  95. doSelectSubscribe(id, lastId);
  96. lastId = id;
  97. }
  98. }
  99. }
  100. // Prevent clicks on icons from causing a drag to start.
  101. this.connect(this.titleBarNode, "onmousedown", function(evt){
  102. if (dojo.hasClass(evt.target, "dojoxPortletIcon")) {
  103. dojo.stopEvent(evt);
  104. return false;
  105. }
  106. return true;
  107. });
  108. // Inform all portlets that the size of this one has changed,
  109. // and therefore perhaps they have too
  110. this.connect(this._wipeOut, "onEnd", function(){_this._publish();});
  111. this.connect(this._wipeIn, "onEnd", function(){_this._publish();});
  112. if(this.closable){
  113. this.closeIcon = this._createIcon("dojoxCloseNode", "dojoxCloseNodeHover", dojo.hitch(this, "onClose"));
  114. dojo.style(this.closeIcon, "display", "");
  115. }
  116. },
  117. startup: function(){
  118. if(this._started){return;}
  119. var children = this.getChildren();
  120. this._placeSettingsWidgets();
  121. // Start up the children
  122. dojo.forEach(children, function(child){
  123. try{
  124. if(!child.started && !child._started){
  125. child.startup()
  126. }
  127. }
  128. catch(e){
  129. console.log(this.id + ":" + this.declaredClass, e);
  130. }
  131. });
  132. this.inherited(arguments);
  133. //this._updateSize();
  134. dojo.style(this.domNode, "visibility", "visible");
  135. },
  136. _placeSettingsWidgets: function(){
  137. // summary: Checks all the children to see if they are instances
  138. // of dojox.widget.PortletSettings. If they are,
  139. // create an icon for them in the title bar which when clicked,
  140. // calls their toggle() method.
  141. dojo.forEach(this.getChildren(), dojo.hitch(this, function(child){
  142. if(child.portletIconClass && child.toggle && !child.attr("portlet")){
  143. this._createIcon(child.portletIconClass, child.portletIconHoverClass, dojo.hitch(child, "toggle"));
  144. dojo.place(child.domNode, this.containerNode, "before");
  145. child.attr("portlet", this);
  146. this._settingsWidget = child;
  147. }
  148. }));
  149. },
  150. _createIcon: function(clazz, hoverClazz, fn){
  151. // summary:
  152. // creates an icon in the title bar.
  153. var icon = dojo.create("div",{
  154. "class": "dojoxPortletIcon " + clazz,
  155. "waiRole": "presentation"
  156. });
  157. dojo.place(icon, this.arrowNode, "before");
  158. this.connect(icon, "onclick", fn);
  159. if(hoverClazz){
  160. this.connect(icon, "onmouseover", function(){
  161. dojo.addClass(icon, hoverClazz);
  162. });
  163. this.connect(icon, "onmouseout", function(){
  164. dojo.removeClass(icon, hoverClazz);
  165. });
  166. }
  167. return icon;
  168. },
  169. onClose: function(evt){
  170. // summary:
  171. // Hides the portlet. Note that it does not
  172. // persist this, so it is up to the client to
  173. // listen to this method and persist the closed state
  174. // in their own way.
  175. dojo.style(this.domNode, "display", "none");
  176. },
  177. onSizeChange: function(widget){
  178. // summary:
  179. // Updates the Portlet size if any other Portlet
  180. // changes its size.
  181. if(widget == this){
  182. return;
  183. }
  184. this._updateSize();
  185. },
  186. _updateSize: function(){
  187. // summary:
  188. // Updates the size of all child widgets.
  189. if(!this.open || !this._started || !this.resizeChildren){
  190. return;
  191. }
  192. if(this._timer){
  193. clearTimeout(this._timer);
  194. }
  195. // Delay applying the size change in case the size
  196. // changes very frequently, for performance reasons.
  197. this._timer = setTimeout(dojo.hitch(this, function(){
  198. var size ={
  199. w: dojo.style(this.domNode, "width"),
  200. h: dojo.style(this.domNode, "height")
  201. };
  202. // If the Portlet is in a StackWidget, and it is not
  203. // visible, do not update the size, as it could
  204. // make child widgets miscalculate.
  205. for(var i = 0; i < this._parents.length; i++){
  206. var p = this._parents[i];
  207. var sel = p.parent.selectedChildWidget
  208. if(sel && sel != p.child){
  209. return;
  210. }
  211. }
  212. if(this._size){
  213. // If the size of the portlet hasn't changed, don't
  214. // resize the children, as this can be expensive
  215. if(this._size.w == size.w && this._size.h == size.h){
  216. return;
  217. }
  218. }
  219. this._size = size;
  220. var fns = ["resize", "layout"];
  221. this._timer = null;
  222. var kids = this.getChildren();
  223. dojo.forEach(kids, function(child){
  224. for(var i = 0; i < fns.length; i++){
  225. if(dojo.isFunction(child[fns[i]])){
  226. try{
  227. child[fns[i]]();
  228. } catch(e){
  229. console.log(e);
  230. }
  231. break;
  232. }
  233. }
  234. });
  235. this.onUpdateSize();
  236. }), 100);
  237. },
  238. onUpdateSize: function(){
  239. // summary:
  240. // Stub function called when the size is changed.
  241. },
  242. _publish: function(){
  243. // summary: Publishes an event that all other portlets listen to.
  244. // This causes them to update their child widgets if their
  245. // size has changed.
  246. dojo.publish("/Portlet/sizechange",[this]);
  247. },
  248. _onTitleClick: function(evt){
  249. if(evt.target == this.arrowNode){
  250. this.inherited(arguments);
  251. }
  252. },
  253. addChild: function(child){
  254. // summary:
  255. // Adds a child widget to the portlet.
  256. this._size = null;
  257. this.inherited(arguments);
  258. if(this._started){
  259. this._placeSettingsWidgets();
  260. this._updateSize();
  261. }
  262. if(this._started && !child.started && !child._started){
  263. child.startup();
  264. }
  265. },
  266. destroyDescendants: function(/*Boolean*/ preserveDom){
  267. this.inherited(arguments);
  268. if(this._settingsWidget){
  269. this._settingsWidget.destroyRecursive(preserveDom);
  270. }
  271. },
  272. destroy: function(){
  273. if(this._timer){
  274. clearTimeout(this._timer);
  275. }
  276. this.inherited(arguments);
  277. },
  278. _setCss: function(){
  279. this.inherited(arguments);
  280. dojo.style(this.arrowNode, "display", this.toggleable ? "":"none");
  281. }
  282. });
  283. dojo.declare("dojox.widget.PortletSettings", [dijit._Container, dijit.layout.ContentPane],{
  284. // summary:
  285. // A settings widget to be used with a dojox.widget.Portlet.
  286. // description:
  287. // This widget should be placed inside a dojox.widget.Portlet widget.
  288. // It is used to set some preferences for that Portlet. It is essentially
  289. // a ContentPane, and should contain other widgets and DOM nodes that
  290. // do the real work of setting preferences for the portlet.
  291. // portletIconClass: String
  292. // The CSS class to apply to the icon in the Portlet title bar that is used
  293. // to toggle the visibility of this widget.
  294. portletIconClass: "dojoxPortletSettingsIcon",
  295. // portletIconHoverClass: String
  296. // The CSS class to apply to the icon in the Portlet title bar that is used
  297. // to toggle the visibility of this widget when the mouse hovers over it.
  298. portletIconHoverClass: "dojoxPortletSettingsIconHover",
  299. postCreate: function(){
  300. // summary:
  301. // Sets the require CSS classes on the widget.
  302. // Start the PortletSettings widget hidden, always.
  303. dojo.style(this.domNode, "display", "none");
  304. dojo.addClass(this.domNode, "dojoxPortletSettingsContainer");
  305. // Remove the unwanted content pane class.
  306. dojo.removeClass(this.domNode, "dijitContentPane");
  307. },
  308. _setPortletAttr: function(portlet){
  309. // summary:
  310. // Sets the portlet that encloses this widget.
  311. this.portlet = portlet;
  312. },
  313. toggle: function(){
  314. // summary:
  315. // Toggles the visibility of this widget.
  316. var n = this.domNode;
  317. if(dojo.style(n, "display") == "none"){
  318. dojo.style(n,{
  319. "display": "block",
  320. "height": "1px",
  321. "width": "auto"
  322. });
  323. dojo.fx.wipeIn({
  324. node: n
  325. }).play();
  326. }else{
  327. dojo.fx.wipeOut({
  328. node: n,
  329. onEnd: dojo.hitch(this, function(){
  330. dojo.style(n,{"display": "none", "height": "", "width":""});
  331. }
  332. )}).play();
  333. }
  334. }
  335. });
  336. dojo.declare("dojox.widget.PortletDialogSettings",
  337. dojox.widget.PortletSettings,{
  338. // summary:
  339. // A settings widget to be used with a dojox.widget.Portlet, which displays
  340. // the contents of this widget in a dijit.Dialog box.
  341. // dimensions: Array
  342. // The size of the dialog to display. This defaults to [300, 300]
  343. dimensions: null,
  344. constructor: function(props, node){
  345. this.dimensions = props.dimensions || [300, 100];
  346. },
  347. toggle: function(){
  348. // summary:
  349. // Shows and hides the Dialog box.
  350. if(!this.dialog){
  351. dojo["require"]("dijit.Dialog");
  352. this.dialog = new dijit.Dialog({title: this.title});
  353. dojo.body().appendChild(this.dialog.domNode);
  354. // Move this widget inside the dialog
  355. this.dialog.containerNode.appendChild(this.domNode);
  356. dojo.style(this.dialog.domNode,{
  357. "width" : this.dimensions[0] + "px",
  358. "height" : this.dimensions[1] + "px"
  359. });
  360. dojo.style(this.domNode, "display", "");
  361. }
  362. if(this.dialog.open){
  363. this.dialog.hide();
  364. }else{
  365. this.dialog.show(this.domNode);
  366. }
  367. }
  368. });
  369. });