RollingList.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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.RollingList"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.widget.RollingList"] = true;
  8. dojo.provide("dojox.widget.RollingList");
  9. dojo.experimental("dojox.widget.RollingList");
  10. dojo.require("dojo.window");
  11. dojo.require("dijit.layout.ContentPane");
  12. dojo.require("dijit._Templated");
  13. dojo.require("dijit._Contained");
  14. dojo.require("dijit.layout._LayoutWidget");
  15. dojo.require("dijit.Menu");
  16. dojo.require("dijit.form.Button");
  17. dojo.require("dojox.html.metrics");
  18. dojo.require("dojo.i18n");
  19. dojo.requireLocalization("dijit", "common", null, "ROOT,ar,az,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  20. dojo.declare("dojox.widget._RollingListPane",
  21. [dijit.layout.ContentPane, dijit._Templated, dijit._Contained], {
  22. // summary: a core pane that can be attached to a RollingList. All panes
  23. // should extend this one
  24. // templateString: string
  25. // our template
  26. templateString: '<div class="dojoxRollingListPane"><table><tbody><tr><td dojoAttachPoint="containerNode"></td></tr></tbody></div>',
  27. // parentWidget: dojox.widget.RollingList
  28. // Our rolling list widget
  29. parentWidget: null,
  30. // parentPane: dojox.widget._RollingListPane
  31. // The pane that immediately precedes ours
  32. parentPane: null,
  33. // store: store
  34. // the store we must use
  35. store: null,
  36. // items: item[]
  37. // an array of (possibly not-yet-loaded) items to display in this.
  38. // If this array is null, then the query and query options are used to
  39. // get the top-level items to use. This array is also used to watch and
  40. // see if the pane needs to be reloaded (store notifications are handled)
  41. // by the pane
  42. items: null,
  43. // query: object
  44. // a query to pass to the datastore. This is only used if items are null
  45. query: null,
  46. // queryOptions: object
  47. // query options to be passed to the datastore
  48. queryOptions: null,
  49. // focusByNode: boolean
  50. // set to false if the subclass will handle its own node focusing
  51. _focusByNode: true,
  52. // minWidth: integer
  53. // the width (in px) for this pane
  54. minWidth: 0,
  55. _setContentAndScroll: function(/*String|DomNode|Nodelist*/cont, /*Boolean?*/isFakeContent){
  56. // summary: sets the value of the content and scrolls it into view
  57. this._setContent(cont, isFakeContent);
  58. this.parentWidget.scrollIntoView(this);
  59. },
  60. _updateNodeWidth: function(n, min){
  61. // summary: updates the min width of the pane to be minPaneWidth
  62. n.style.width = "";
  63. var nWidth = dojo.marginBox(n).w;
  64. if(nWidth < min){
  65. dojo.marginBox(n, {w: min});
  66. }
  67. },
  68. _onMinWidthChange: function(v){
  69. // Called when the min width of a pane has changed
  70. this._updateNodeWidth(this.domNode, v);
  71. },
  72. _setMinWidthAttr: function(v){
  73. if(v !== this.minWidth){
  74. this.minWidth = v;
  75. this._onMinWidthChange(v);
  76. }
  77. },
  78. startup: function(){
  79. if(this._started){ return; }
  80. if(this.store && this.store.getFeatures()["dojo.data.api.Notification"]){
  81. window.setTimeout(dojo.hitch(this, function(){
  82. // Set connections after a slight timeout to avoid getting in the
  83. // condition where we are setting them while events are still
  84. // being fired
  85. this.connect(this.store, "onSet", "_onSetItem");
  86. this.connect(this.store, "onNew", "_onNewItem");
  87. this.connect(this.store, "onDelete", "_onDeleteItem");
  88. }), 1);
  89. }
  90. this.connect(this.focusNode||this.domNode, "onkeypress", "_focusKey");
  91. this.parentWidget._updateClass(this.domNode, "Pane");
  92. this.inherited(arguments);
  93. this._onMinWidthChange(this.minWidth);
  94. },
  95. _focusKey: function(/*Event*/e){
  96. // summary: called when a keypress happens on the widget
  97. if(e.charOrCode == dojo.keys.BACKSPACE){
  98. dojo.stopEvent(e);
  99. return;
  100. }else if(e.charOrCode == dojo.keys.LEFT_ARROW && this.parentPane){
  101. this.parentPane.focus();
  102. this.parentWidget.scrollIntoView(this.parentPane);
  103. }else if(e.charOrCode == dojo.keys.ENTER){
  104. this.parentWidget._onExecute();
  105. }
  106. },
  107. focus: function(/*boolean*/force){
  108. // summary: sets the focus to this current widget
  109. if(this.parentWidget._focusedPane != this){
  110. this.parentWidget._focusedPane = this;
  111. this.parentWidget.scrollIntoView(this);
  112. if(this._focusByNode && (!this.parentWidget._savedFocus || force)){
  113. try{(this.focusNode||this.domNode).focus();}catch(e){}
  114. }
  115. }
  116. },
  117. _onShow: function(){
  118. // summary: checks that the store is loaded
  119. if((this.store || this.items) && ((this.refreshOnShow && this.domNode) || (!this.isLoaded && this.domNode))){
  120. this.refresh();
  121. }
  122. },
  123. _load: function(){
  124. // summary: sets the "loading" message and then kicks off a query asyncronously
  125. this.isLoaded = false;
  126. if(this.items){
  127. this._setContentAndScroll(this.onLoadStart(), true);
  128. window.setTimeout(dojo.hitch(this, "_doQuery"), 1);
  129. }else{
  130. this._doQuery();
  131. }
  132. },
  133. _doLoadItems: function(/*item[]*/items, /*function*/callback){
  134. // summary: loads the given items, and then calls the callback when they
  135. // are finished.
  136. var _waitCount = 0, store = this.store;
  137. dojo.forEach(items, function(item){
  138. if(!store.isItemLoaded(item)){ _waitCount++; }
  139. });
  140. if(_waitCount === 0){
  141. callback();
  142. }else{
  143. var onItem = function(item){
  144. _waitCount--;
  145. if((_waitCount) === 0){
  146. callback();
  147. }
  148. };
  149. dojo.forEach(items, function(item){
  150. if(!store.isItemLoaded(item)){
  151. store.loadItem({item: item, onItem: onItem});
  152. }
  153. });
  154. }
  155. },
  156. _doQuery: function(){
  157. // summary: either runs the query or loads potentially not-yet-loaded items.
  158. if(!this.domNode){return;}
  159. var preload = this.parentWidget.preloadItems;
  160. preload = (preload === true || (this.items && this.items.length <= Number(preload)));
  161. if(this.items && preload){
  162. this._doLoadItems(this.items, dojo.hitch(this, "onItems"));
  163. }else if(this.items){
  164. this.onItems();
  165. }else{
  166. this._setContentAndScroll(this.onFetchStart(), true);
  167. this.store.fetch({query: this.query,
  168. onComplete: function(items){
  169. this.items = items;
  170. this.onItems();
  171. },
  172. onError: function(e){
  173. this._onError("Fetch", e);
  174. },
  175. scope: this});
  176. }
  177. },
  178. _hasItem: function(/* item */ item){
  179. // summary: returns whether or not the given item is handled by this
  180. // pane
  181. var items = this.items || [];
  182. for(var i = 0, myItem; (myItem = items[i]); i++){
  183. if(this.parentWidget._itemsMatch(myItem, item)){
  184. return true;
  185. }
  186. }
  187. return false;
  188. },
  189. _onSetItem: function(/* item */ item,
  190. /* attribute-name-string */ attribute,
  191. /* object | array */ oldValue,
  192. /* object | array */ newValue){
  193. // Summary: called when an item in the store has changed
  194. if(this._hasItem(item)){
  195. this.refresh();
  196. }
  197. },
  198. _onNewItem: function(/* item */ newItem, /*object?*/ parentInfo){
  199. // Summary: called when an item is added to the store
  200. var sel;
  201. if((!parentInfo && !this.parentPane) ||
  202. (parentInfo && this.parentPane && this.parentPane._hasItem(parentInfo.item) &&
  203. (sel = this.parentPane._getSelected()) && this.parentWidget._itemsMatch(sel.item, parentInfo.item))){
  204. this.items.push(newItem);
  205. this.refresh();
  206. }else if(parentInfo && this.parentPane && this._hasItem(parentInfo.item)){
  207. this.refresh();
  208. }
  209. },
  210. _onDeleteItem: function(/* item */ deletedItem){
  211. // Summary: called when an item is removed from the store
  212. if(this._hasItem(deletedItem)){
  213. this.items = dojo.filter(this.items, function(i){
  214. return (i != deletedItem);
  215. });
  216. this.refresh();
  217. }
  218. },
  219. onFetchStart: function(){
  220. // summary:
  221. // called before a fetch starts
  222. return this.loadingMessage;
  223. },
  224. onFetchError: function(/*Error*/ error){
  225. // summary:
  226. // called when a fetch error occurs.
  227. return this.errorMessage;
  228. },
  229. onLoadStart: function(){
  230. // summary:
  231. // called before a load starts
  232. return this.loadingMessage;
  233. },
  234. onLoadError: function(/*Error*/ error){
  235. // summary:
  236. // called when a load error occurs.
  237. return this.errorMessage;
  238. },
  239. onItems: function(){
  240. // summary:
  241. // called after a fetch or load - at this point, this.items should be
  242. // set and loaded. Override this function to "do your stuff"
  243. if(!this.onLoadDeferred){
  244. this.cancel();
  245. this.onLoadDeferred = new dojo.Deferred(dojo.hitch(this, "cancel"));
  246. }
  247. this._onLoadHandler();
  248. }
  249. });
  250. dojo.declare("dojox.widget._RollingListGroupPane",
  251. [dojox.widget._RollingListPane], {
  252. // summary: a pane that will handle groups (treats them as menu items)
  253. // templateString: string
  254. // our template
  255. templateString: '<div><div dojoAttachPoint="containerNode"></div>' +
  256. '<div dojoAttachPoint="menuContainer">' +
  257. '<div dojoAttachPoint="menuNode"></div>' +
  258. '</div></div>',
  259. // _menu: dijit.Menu
  260. // The menu that we will call addChild() on for adding items
  261. _menu: null,
  262. _setContent: function(/*String|DomNode|Nodelist*/cont){
  263. if(!this._menu){
  264. // Only set the content if we don't already have a menu
  265. this.inherited(arguments);
  266. }
  267. },
  268. _onMinWidthChange: function(v){
  269. // override and resize the menu instead
  270. if(!this._menu){ return; }
  271. var dWidth = dojo.marginBox(this.domNode).w;
  272. var mWidth = dojo.marginBox(this._menu.domNode).w;
  273. this._updateNodeWidth(this._menu.domNode, v - (dWidth - mWidth));
  274. },
  275. onItems: function(){
  276. // summary:
  277. // called after a fetch or load
  278. var selectItem, hadChildren = false;
  279. if(this._menu){
  280. selectItem = this._getSelected();
  281. this._menu.destroyRecursive();
  282. }
  283. this._menu = this._getMenu();
  284. var child, selectMenuItem;
  285. if(this.items.length){
  286. dojo.forEach(this.items, function(item){
  287. child = this.parentWidget._getMenuItemForItem(item, this);
  288. if(child){
  289. if(selectItem && this.parentWidget._itemsMatch(child.item, selectItem.item)){
  290. selectMenuItem = child;
  291. }
  292. this._menu.addChild(child);
  293. }
  294. }, this);
  295. }else{
  296. child = this.parentWidget._getMenuItemForItem(null, this);
  297. if(child){
  298. this._menu.addChild(child);
  299. }
  300. }
  301. if(selectMenuItem){
  302. this._setSelected(selectMenuItem);
  303. if((selectItem && !selectItem.children && selectMenuItem.children) ||
  304. (selectItem && selectItem.children && !selectMenuItem.children)){
  305. var itemPane = this.parentWidget._getPaneForItem(selectMenuItem.item, this, selectMenuItem.children);
  306. if(itemPane){
  307. this.parentWidget.addChild(itemPane, this.getIndexInParent() + 1);
  308. }else{
  309. this.parentWidget._removeAfter(this);
  310. this.parentWidget._onItemClick(null, this, selectMenuItem.item, selectMenuItem.children);
  311. }
  312. }
  313. }else if(selectItem){
  314. this.parentWidget._removeAfter(this);
  315. }
  316. this.containerNode.innerHTML = "";
  317. this.containerNode.appendChild(this._menu.domNode);
  318. this.parentWidget.scrollIntoView(this);
  319. this._checkScrollConnection(true);
  320. this.inherited(arguments);
  321. this._onMinWidthChange(this.minWidth);
  322. },
  323. _checkScrollConnection: function(doLoad){
  324. // summary: checks whether or not we need to connect to our onscroll
  325. // function
  326. var store = this.store
  327. if(this._scrollConn){
  328. this.disconnect(this._scrollConn);
  329. }
  330. delete this._scrollConn;
  331. if(!dojo.every(this.items, function(i){return store.isItemLoaded(i);})){
  332. if(doLoad){
  333. this._loadVisibleItems();
  334. }
  335. this._scrollConn = this.connect(this.domNode, "onscroll", "_onScrollPane");
  336. }
  337. },
  338. startup: function(){
  339. this.inherited(arguments);
  340. this.parentWidget._updateClass(this.domNode, "GroupPane");
  341. },
  342. focus: function(/*boolean*/force){
  343. // summary: sets the focus to this current widget
  344. if(this._menu){
  345. if(this._pendingFocus){
  346. this.disconnect(this._pendingFocus);
  347. }
  348. delete this._pendingFocus;
  349. // We focus the right widget - either the focusedChild, the
  350. // selected node, the first menu item, or the menu itself
  351. var focusWidget = this._menu.focusedChild;
  352. if(!focusWidget){
  353. var focusNode = dojo.query(".dojoxRollingListItemSelected", this.domNode)[0];
  354. if(focusNode){
  355. focusWidget = dijit.byNode(focusNode);
  356. }
  357. }
  358. if(!focusWidget){
  359. focusWidget = this._menu.getChildren()[0] || this._menu;
  360. }
  361. this._focusByNode = false;
  362. if(focusWidget.focusNode){
  363. if(!this.parentWidget._savedFocus || force){
  364. try{focusWidget.focusNode.focus();}catch(e){}
  365. }
  366. window.setTimeout(function(){
  367. try{
  368. dojo.window.scrollIntoView(focusWidget.focusNode);
  369. }catch(e){}
  370. }, 1);
  371. }else if(focusWidget.focus){
  372. if(!this.parentWidget._savedFocus || force){
  373. focusWidget.focus();
  374. }
  375. }else{
  376. this._focusByNode = true;
  377. }
  378. this.inherited(arguments);
  379. }else if(!this._pendingFocus){
  380. this._pendingFocus = this.connect(this, "onItems", "focus");
  381. }
  382. },
  383. _getMenu: function(){
  384. // summary: returns a widget to be used for the container widget.
  385. var self = this;
  386. var menu = new dijit.Menu({
  387. parentMenu: this.parentPane ? this.parentPane._menu : null,
  388. onCancel: function(/*Boolean*/ closeAll){
  389. if(self.parentPane){
  390. self.parentPane.focus(true);
  391. }
  392. },
  393. _moveToPopup: function(/*Event*/ evt){
  394. if(this.focusedChild && !this.focusedChild.disabled){
  395. this.focusedChild._onClick(evt);
  396. }
  397. }
  398. }, this.menuNode);
  399. this.connect(menu, "onItemClick", function(/*dijit.MenuItem*/ item, /*Event*/ evt){
  400. if(item.disabled){ return; }
  401. evt.alreadySelected = dojo.hasClass(item.domNode, "dojoxRollingListItemSelected");
  402. if(evt.alreadySelected &&
  403. ((evt.type == "keypress" && evt.charOrCode != dojo.keys.ENTER) ||
  404. (evt.type == "internal"))){
  405. var p = this.parentWidget.getChildren()[this.getIndexInParent() + 1];
  406. if(p){
  407. p.focus(true);
  408. this.parentWidget.scrollIntoView(p);
  409. }
  410. }else{
  411. this._setSelected(item, menu);
  412. this.parentWidget._onItemClick(evt, this, item.item, item.children);
  413. if(evt.type == "keypress" && evt.charOrCode == dojo.keys.ENTER){
  414. this.parentWidget._onExecute();
  415. }
  416. }
  417. });
  418. if(!menu._started){
  419. menu.startup();
  420. }
  421. return menu;
  422. },
  423. _onScrollPane: function(){
  424. // summary: called when the pane has been scrolled - it sets a timeout
  425. // so that we don't try and load our visible items too often during
  426. // a scroll
  427. if(this._visibleLoadPending){
  428. window.clearTimeout(this._visibleLoadPending);
  429. }
  430. this._visibleLoadPending = window.setTimeout(dojo.hitch(this, "_loadVisibleItems"), 500);
  431. },
  432. _loadVisibleItems: function(){
  433. // summary: loads the items that are currently visible in the pane
  434. delete this._visibleLoadPending
  435. var menu = this._menu;
  436. if(!menu){ return; }
  437. var children = menu.getChildren();
  438. if(!children || !children.length){ return; }
  439. var gpbme = function(n, m, pb){
  440. var s = dojo.getComputedStyle(n);
  441. var r = 0;
  442. if(m){ r += dojo._getMarginExtents(n, s).t; }
  443. if(pb){ r += dojo._getPadBorderExtents(n, s).t; }
  444. return r;
  445. };
  446. var topOffset = gpbme(this.domNode, false, true) +
  447. gpbme(this.containerNode, true, true) +
  448. gpbme(menu.domNode, true, true) +
  449. gpbme(children[0].domNode, true, false);
  450. var h = dojo.contentBox(this.domNode).h;
  451. var minOffset = this.domNode.scrollTop - topOffset - (h/2);
  452. var maxOffset = minOffset + (3*h/2);
  453. var menuItemsToLoad = dojo.filter(children, function(c){
  454. var cnt = c.domNode.offsetTop;
  455. var s = c.store;
  456. var i = c.item;
  457. return (cnt >= minOffset && cnt <= maxOffset && !s.isItemLoaded(i));
  458. })
  459. var itemsToLoad = dojo.map(menuItemsToLoad, function(c){
  460. return c.item;
  461. });
  462. var onItems = dojo.hitch(this, function(){
  463. var selectItem = this._getSelected();
  464. var selectMenuItem;
  465. dojo.forEach(itemsToLoad, function(item, idx){
  466. var newItem = this.parentWidget._getMenuItemForItem(item, this);
  467. var oItem = menuItemsToLoad[idx];
  468. var oIdx = oItem.getIndexInParent();
  469. menu.removeChild(oItem);
  470. if(newItem){
  471. if(selectItem && this.parentWidget._itemsMatch(newItem.item, selectItem.item)){
  472. selectMenuItem = newItem;
  473. }
  474. menu.addChild(newItem, oIdx);
  475. if(menu.focusedChild == oItem){
  476. menu.focusChild(newItem);
  477. }
  478. }
  479. oItem.destroy();
  480. }, this);
  481. this._checkScrollConnection(false);
  482. });
  483. this._doLoadItems(itemsToLoad, onItems);
  484. },
  485. _getSelected: function(/*dijit.Menu?*/ menu){
  486. // summary:
  487. // returns the selected menu item - or null if none are selected
  488. if(!menu){ menu = this._menu; }
  489. if(menu){
  490. var children = this._menu.getChildren();
  491. for(var i = 0, item; (item = children[i]); i++){
  492. if(dojo.hasClass(item.domNode, "dojoxRollingListItemSelected")){
  493. return item;
  494. }
  495. }
  496. }
  497. return null;
  498. },
  499. _setSelected: function(/*dijit.MenuItem?*/ item, /*dijit.Menu?*/ menu){
  500. // summary:
  501. // selectes the given item in the given menu (defaults to pane's menu)
  502. if(!menu){ menu = this._menu;}
  503. if(menu){
  504. dojo.forEach(menu.getChildren(), function(i){
  505. this.parentWidget._updateClass(i.domNode, "Item", {"Selected": (item && (i == item && !i.disabled))});
  506. }, this);
  507. }
  508. }
  509. });
  510. dojo.declare("dojox.widget.RollingList",
  511. [dijit._Widget, dijit._Templated, dijit._Container], {
  512. // summary: a rolling list that can be tied to a data store with children
  513. // templateString: String
  514. // The template to be used to construct the widget.
  515. templateString: dojo.cache("dojox.widget", "RollingList/RollingList.html", "<div class=\"dojoxRollingList ${className}\"\n\t><div class=\"dojoxRollingListContainer\" dojoAttachPoint=\"containerNode\" dojoAttachEvent=\"onkeypress:_onKey\"\n\t></div\n\t><div class=\"dojoxRollingListButtons\" dojoAttachPoint=\"buttonsNode\"\n ><button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"okButton\"\n\t\t\t\tdojoAttachEvent=\"onClick:_onExecute\">${okButtonLabel}</button\n ><button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"cancelButton\"\n\t\t\t\tdojoAttachEvent=\"onClick:_onCancel\">${cancelButtonLabel}</button\n\t></div\n></div>\n"),
  516. widgetsInTemplate: true,
  517. // className: string
  518. // an additional class (or space-separated classes) to add for our widget
  519. className: "",
  520. // store: store
  521. // the store we must use
  522. store: null,
  523. // query: object
  524. // a query to pass to the datastore. This is only used if items are null
  525. query: null,
  526. // queryOptions: object
  527. // query options to be passed to the datastore
  528. queryOptions: null,
  529. // childrenAttrs: String[]
  530. // one ore more attributes that holds children of a node
  531. childrenAttrs: ["children"],
  532. // parentAttr: string
  533. // the attribute to read for finding our parent item (if any)
  534. parentAttr: "",
  535. // value: item
  536. // The value that has been selected
  537. value: null,
  538. // executeOnDblClick: boolean
  539. // Set to true if you want to call onExecute when an item is
  540. // double-clicked, false if you want to call onExecute yourself. (mainly
  541. // used for popups to control how they want to be handled)
  542. executeOnDblClick: true,
  543. // preloadItems: boolean or int
  544. // if set to true, then onItems will be called only *after* all items have
  545. // been loaded (ie store.isLoaded will return true for all of them). If
  546. // false, then no preloading will occur. If set to an integer, preloading
  547. // will occur if the number of items is less than or equal to the value
  548. // of the integer. The onItems function will need to be aware of handling
  549. // items that may not be loaded
  550. preloadItems: false,
  551. // showButtons: boolean
  552. // if set to true, then buttons for "OK" and "Cancel" will be provided
  553. showButtons: false,
  554. // okButtonLabel: string
  555. // The string to use for the OK button - will use dijit's common "OK" string
  556. // if not set
  557. okButtonLabel: "",
  558. // cancelButtonLabel: string
  559. // The string to use for the Cancel button - will use dijit's common
  560. // "Cancel" string if not set
  561. cancelButtonLabel: "",
  562. // minPaneWidth: integer
  563. // the minimum pane width (in px) for all child panes. If they are narrower,
  564. // the width will be increased to this value.
  565. minPaneWidth: 0,
  566. postMixInProperties: function(){
  567. // summary: Mix in our labels, if they are not set
  568. this.inherited(arguments);
  569. var loc = dojo.i18n.getLocalization("dijit", "common");
  570. this.okButtonLabel = this.okButtonLabel || loc.buttonOk;
  571. this.cancelButtonLabel = this.cancelButtonLabel || loc.buttonCancel;
  572. },
  573. _setShowButtonsAttr: function(doShow){
  574. // summary: Sets the visibility of the buttons for the widget
  575. var needsLayout = false;
  576. if((this.showButtons != doShow && this._started) ||
  577. (this.showButtons == doShow && !this.started)){
  578. needsLayout = true;
  579. }
  580. dojo.toggleClass(this.domNode, "dojoxRollingListButtonsHidden", !doShow);
  581. this.showButtons = doShow;
  582. if(needsLayout){
  583. if(this._started){
  584. this.layout();
  585. }else{
  586. window.setTimeout(dojo.hitch(this, "layout"), 0);
  587. }
  588. }
  589. },
  590. _itemsMatch: function(/*item*/ item1, /*item*/ item2){
  591. // Summary: returns whether or not the two items match - checks ID if
  592. // they aren't the exact same object
  593. if(!item1 && !item2){
  594. return true;
  595. }else if(!item1 || !item2){
  596. return false;
  597. }
  598. return (item1 == item2 ||
  599. (this._isIdentity && this.store.getIdentity(item1) == this.store.getIdentity(item2)));
  600. },
  601. _removeAfter: function(/*Widget or int*/ idx){
  602. // summary: removes all widgets after the given widget (or index)
  603. if(typeof idx != "number"){
  604. idx = this.getIndexOfChild(idx);
  605. }
  606. if(idx >= 0){
  607. dojo.forEach(this.getChildren(), function(c, i){
  608. if(i > idx){
  609. this.removeChild(c);
  610. c.destroyRecursive();
  611. }
  612. }, this);
  613. }
  614. var children = this.getChildren(), child = children[children.length - 1];
  615. var selItem = null;
  616. while(child && !selItem){
  617. var val = child._getSelected ? child._getSelected() : null;
  618. if(val){
  619. selItem = val.item;
  620. }
  621. child = child.parentPane;
  622. }
  623. if(!this._setInProgress){
  624. this._setValue(selItem);
  625. }
  626. },
  627. addChild: function(/*dijit._Widget*/ widget, /*int?*/ insertIndex){
  628. // summary: adds a child to this rolling list - if passed an insertIndex,
  629. // then all children from that index on will be removed and destroyed
  630. // before adding the child.
  631. if(insertIndex > 0){
  632. this._removeAfter(insertIndex - 1);
  633. }
  634. this.inherited(arguments);
  635. if(!widget._started){
  636. widget.startup();
  637. }
  638. widget.attr("minWidth", this.minPaneWidth);
  639. this.layout();
  640. if(!this._savedFocus){
  641. widget.focus();
  642. }
  643. },
  644. _setMinPaneWidthAttr: function(value){
  645. // summary:
  646. // Sets the min pane width of all children
  647. if(value !== this.minPaneWidth){
  648. this.minPaneWidth = value;
  649. dojo.forEach(this.getChildren(), function(c){
  650. c.attr("minWidth", value);
  651. });
  652. }
  653. },
  654. _updateClass: function(/* Node */ node, /* String */ type, /* Object? */ options){
  655. // summary:
  656. // sets the state of the given node with the given type and options
  657. // options:
  658. // an object with key-value-pairs. The values are boolean, if true,
  659. // the key is added as a class, if false, it is removed.
  660. if(!this._declaredClasses){
  661. this._declaredClasses = ("dojoxRollingList " + this.className).split(" ");
  662. }
  663. dojo.forEach(this._declaredClasses, function(c){
  664. if(c){
  665. dojo.addClass(node, c + type);
  666. for(var k in options||{}){
  667. dojo.toggleClass(node, c + type + k, options[k]);
  668. }
  669. dojo.toggleClass(node, c + type + "FocusSelected",
  670. (dojo.hasClass(node, c + type + "Focus") && dojo.hasClass(node, c + type + "Selected")));
  671. dojo.toggleClass(node, c + type + "HoverSelected",
  672. (dojo.hasClass(node, c + type + "Hover") && dojo.hasClass(node, c + type + "Selected")));
  673. }
  674. });
  675. },
  676. scrollIntoView: function(/*dijit._Widget*/ childWidget){
  677. // summary: scrolls the given widget into view
  678. if(this._scrollingTimeout){
  679. window.clearTimeout(this._scrollingTimeout);
  680. }
  681. delete this._scrollingTimeout;
  682. this._scrollingTimeout = window.setTimeout(dojo.hitch(this, function(){
  683. if(childWidget.domNode){
  684. dojo.window.scrollIntoView(childWidget.domNode);
  685. }
  686. delete this._scrollingTimeout;
  687. return;
  688. }), 1);
  689. },
  690. resize: function(args){
  691. dijit.layout._LayoutWidget.prototype.resize.call(this, args);
  692. },
  693. layout: function(){
  694. var children = this.getChildren();
  695. if(this._contentBox){
  696. var bn = this.buttonsNode;
  697. var height = this._contentBox.h - dojo.marginBox(bn).h -
  698. dojox.html.metrics.getScrollbar().h;
  699. dojo.forEach(children, function(c){
  700. dojo.marginBox(c.domNode, {h: height});
  701. });
  702. }
  703. if(this._focusedPane){
  704. var foc = this._focusedPane;
  705. delete this._focusedPane;
  706. if(!this._savedFocus){
  707. foc.focus();
  708. }
  709. }else if(children && children.length){
  710. if(!this._savedFocus){
  711. children[0].focus();
  712. }
  713. }
  714. },
  715. _onChange: function(/*item*/ value){
  716. this.onChange(value);
  717. },
  718. _setValue: function(/* item */ value){
  719. // summary: internally sets the value and fires onchange
  720. delete this._setInProgress;
  721. if(!this._itemsMatch(this.value, value)){
  722. this.value = value;
  723. this._onChange(value);
  724. }
  725. },
  726. _setValueAttr: function(/* item */ value){
  727. // summary: sets the value of this widget to the given store item
  728. if(this._itemsMatch(this.value, value) && !value){ return; }
  729. if(this._setInProgress && this._setInProgress === value){ return; }
  730. this._setInProgress = value;
  731. if(!value || !this.store.isItem(value)){
  732. var pane = this.getChildren()[0];
  733. pane._setSelected(null);
  734. this._onItemClick(null, pane, null, null);
  735. return;
  736. }
  737. var fetchParentItems = dojo.hitch(this, function(/*item*/ item, /*function*/callback){
  738. // Summary: Fetchs the parent items for the given item
  739. var store = this.store, id;
  740. if(this.parentAttr && store.getFeatures()["dojo.data.api.Identity"] &&
  741. ((id = this.store.getValue(item, this.parentAttr)) || id === "")){
  742. // Fetch by parent attribute
  743. var cb = function(i){
  744. if(store.getIdentity(i) == store.getIdentity(item)){
  745. callback(null);
  746. }else{
  747. callback([i]);
  748. }
  749. };
  750. if(id === ""){
  751. callback(null);
  752. }else if(typeof id == "string"){
  753. store.fetchItemByIdentity({identity: id, onItem: cb});
  754. }else if(store.isItem(id)){
  755. cb(id);
  756. }
  757. }else{
  758. // Fetch by finding children
  759. var numCheck = this.childrenAttrs.length;
  760. var parents = [];
  761. dojo.forEach(this.childrenAttrs, function(attr){
  762. var q = {};
  763. q[attr] = item;
  764. store.fetch({query: q, scope: this,
  765. onComplete: function(items){
  766. if(this._setInProgress !== value){
  767. return;
  768. }
  769. parents = parents.concat(items);
  770. numCheck--;
  771. if(numCheck === 0){
  772. callback(parents);
  773. }
  774. }
  775. });
  776. }, this);
  777. }
  778. });
  779. var setFromChain = dojo.hitch(this, function(/*item[]*/itemChain, /*integer*/idx){
  780. // Summary: Sets the value of the widget at the given index in the chain - onchanges are not
  781. // fired here
  782. var set = itemChain[idx];
  783. var child = this.getChildren()[idx];
  784. var conn;
  785. if(set && child){
  786. var fx = dojo.hitch(this, function(){
  787. if(conn){
  788. this.disconnect(conn);
  789. }
  790. delete conn;
  791. if(this._setInProgress !== value){
  792. return;
  793. }
  794. var selOpt = dojo.filter(child._menu.getChildren(), function(i){
  795. return this._itemsMatch(i.item, set);
  796. }, this)[0];
  797. if(selOpt){
  798. idx++;
  799. child._menu.onItemClick(selOpt, {type: "internal",
  800. stopPropagation: function(){},
  801. preventDefault: function(){}});
  802. if(itemChain[idx]){
  803. setFromChain(itemChain, idx);
  804. }else{
  805. this._setValue(set);
  806. this.onItemClick(set, child, this.getChildItems(set));
  807. }
  808. }
  809. });
  810. if(!child.isLoaded){
  811. conn = this.connect(child, "onLoad", fx);
  812. }else{
  813. fx();
  814. }
  815. }else if(idx === 0){
  816. this.set("value", null);
  817. }
  818. });
  819. var parentChain = [];
  820. var onParents = dojo.hitch(this, function(/*item[]*/ parents){
  821. // Summary: recursively grabs the parents - only the first one is followed
  822. if(parents && parents.length){
  823. parentChain.push(parents[0]);
  824. fetchParentItems(parents[0], onParents);
  825. }else{
  826. if(!parents){
  827. parentChain.pop();
  828. }
  829. parentChain.reverse();
  830. setFromChain(parentChain, 0);
  831. }
  832. });
  833. // Only set the value in display if we are shown - if we are in a dropdown,
  834. // and are hidden, don't actually do the scrolling in the display (it can
  835. // mess up layouts)
  836. var ns = this.domNode.style;
  837. if(ns.display == "none" || ns.visibility == "hidden"){
  838. this._setValue(value);
  839. }else if(!this._itemsMatch(value, this._visibleItem)){
  840. onParents([value]);
  841. }
  842. },
  843. _onItemClick: function(/* Event */ evt, /* dijit._Contained */ pane, /* item */ item, /* item[]? */ children){
  844. // summary: internally called when a widget should pop up its child
  845. if(evt){
  846. var itemPane = this._getPaneForItem(item, pane, children);
  847. var alreadySelected = (evt.type == "click" && evt.alreadySelected);
  848. if(alreadySelected && itemPane){
  849. this._removeAfter(pane.getIndexInParent() + 1);
  850. var next = pane.getNextSibling();
  851. if(next && next._setSelected){
  852. next._setSelected(null);
  853. }
  854. this.scrollIntoView(next);
  855. }else if(itemPane){
  856. this.addChild(itemPane, pane.getIndexInParent() + 1);
  857. if(this._savedFocus){
  858. itemPane.focus(true);
  859. }
  860. }else{
  861. this._removeAfter(pane);
  862. this.scrollIntoView(pane);
  863. }
  864. }else if(pane){
  865. this._removeAfter(pane);
  866. this.scrollIntoView(pane);
  867. }
  868. if(!evt || evt.type != "internal"){
  869. this._setValue(item);
  870. this.onItemClick(item, pane, children);
  871. }
  872. this._visibleItem = item;
  873. },
  874. _getPaneForItem: function(/* item? */ item, /* dijit._Contained? */ parentPane, /* item[]? */ children){ // summary: gets the pane for the given item, and mixes in our needed parts
  875. // Returns the pane for the given item (null if the root pane) - after mixing in
  876. // its stuff.
  877. var ret = this.getPaneForItem(item, parentPane, children);
  878. ret.store = this.store;
  879. ret.parentWidget = this;
  880. ret.parentPane = parentPane||null;
  881. if(!item){
  882. ret.query = this.query;
  883. ret.queryOptions = this.queryOptions;
  884. }else if(children){
  885. ret.items = children;
  886. }else{
  887. ret.items = [item];
  888. }
  889. return ret;
  890. },
  891. _getMenuItemForItem: function(/*item*/ item, /* dijit._Contained */ parentPane){
  892. // summary: returns a widget for the given store item. The returned
  893. // item will be added to this widget's container widget. null will
  894. // be passed in for an "empty" item.
  895. var store = this.store;
  896. if(!item || !store || !store.isItem(item)){
  897. var i = new dijit.MenuItem({
  898. label: "---",
  899. disabled: true,
  900. iconClass: "dojoxEmpty",
  901. focus: function(){
  902. // Do nothing on focus of this guy...
  903. }
  904. });
  905. this._updateClass(i.domNode, "Item");
  906. return i;
  907. }else{
  908. var itemLoaded = store.isItemLoaded(item);
  909. var childItems = itemLoaded ? this.getChildItems(item) : undefined;
  910. var widgetItem;
  911. if(childItems){
  912. widgetItem = this.getMenuItemForItem(item, parentPane, childItems);
  913. widgetItem.children = childItems;
  914. this._updateClass(widgetItem.domNode, "Item", {"Expanding": true});
  915. if(!widgetItem._started){
  916. var c = widgetItem.connect(widgetItem, "startup", function(){
  917. this.disconnect(c);
  918. dojo.style(this.arrowWrapper, "display", "");
  919. });
  920. }else{
  921. dojo.style(widgetItem.arrowWrapper, "display", "");
  922. }
  923. }else{
  924. widgetItem = this.getMenuItemForItem(item, parentPane, null);
  925. if(itemLoaded){
  926. this._updateClass(widgetItem.domNode, "Item", {"Single": true});
  927. }else{
  928. this._updateClass(widgetItem.domNode, "Item", {"Unloaded": true});
  929. widgetItem.attr("disabled", true);
  930. }
  931. }
  932. widgetItem.store = this.store;
  933. widgetItem.item = item;
  934. if(!widgetItem.label){
  935. widgetItem.attr("label", this.store.getLabel(item).replace(/</,"&lt;"));
  936. }
  937. if(widgetItem.focusNode){
  938. var self = this;
  939. widgetItem.focus = function(){
  940. // Don't set our class
  941. if(!this.disabled){try{this.focusNode.focus();}catch(e){}}
  942. };
  943. widgetItem.connect(widgetItem.focusNode, "onmouseenter", function(){
  944. if(!this.disabled){
  945. self._updateClass(this.domNode, "Item", {"Hover": true});
  946. }
  947. });
  948. widgetItem.connect(widgetItem.focusNode, "onmouseleave", function(){
  949. if(!this.disabled){
  950. self._updateClass(this.domNode, "Item", {"Hover": false});
  951. }
  952. });
  953. widgetItem.connect(widgetItem.focusNode, "blur", function(){
  954. self._updateClass(this.domNode, "Item", {"Focus": false, "Hover": false});
  955. });
  956. widgetItem.connect(widgetItem.focusNode, "focus", function(){
  957. self._updateClass(this.domNode, "Item", {"Focus": true});
  958. self._focusedPane = parentPane;
  959. });
  960. if(this.executeOnDblClick){
  961. widgetItem.connect(widgetItem.focusNode, "ondblclick", function(){
  962. self._onExecute();
  963. });
  964. }
  965. }
  966. return widgetItem;
  967. }
  968. },
  969. _setStore: function(/* dojo.data.api.Read */ store){
  970. // summary: sets the store for this widget */
  971. if(store === this.store && this._started){ return; }
  972. this.store = store;
  973. this._isIdentity = store.getFeatures()["dojo.data.api.Identity"];
  974. var rootPane = this._getPaneForItem();
  975. this.addChild(rootPane, 0);
  976. },
  977. _onKey: function(/*Event*/ e){
  978. // summary: called when a keypress event happens on this widget
  979. if(e.charOrCode == dojo.keys.BACKSPACE){
  980. dojo.stopEvent(e);
  981. return;
  982. }else if(e.charOrCode == dojo.keys.ESCAPE && this._savedFocus){
  983. try{dijit.focus(this._savedFocus);}catch(e){}
  984. dojo.stopEvent(e);
  985. return;
  986. }else if(e.charOrCode == dojo.keys.LEFT_ARROW ||
  987. e.charOrCode == dojo.keys.RIGHT_ARROW){
  988. dojo.stopEvent(e);
  989. return;
  990. }
  991. },
  992. _resetValue: function(){
  993. // Summary: function called when the value is reset.
  994. this.set("value", this._lastExecutedValue);
  995. },
  996. _onCancel: function(){
  997. // Summary: function called when the cancel button is clicked. It
  998. // resets its value to whatever was last executed and then cancels
  999. this._resetValue();
  1000. this.onCancel();
  1001. },
  1002. _onExecute: function(){
  1003. // Summary: function called when the OK button is clicked or when an
  1004. // item is selected (double-clicked or "enter" pressed on it)
  1005. this._lastExecutedValue = this.get("value");
  1006. this.onExecute();
  1007. },
  1008. focus: function(){
  1009. // summary: sets the focus state of this widget
  1010. var wasSaved = this._savedFocus;
  1011. this._savedFocus = dijit.getFocus(this);
  1012. if(!this._savedFocus.node){
  1013. delete this._savedFocus;
  1014. }
  1015. if(!this._focusedPane){
  1016. var child = this.getChildren()[0];
  1017. if(child && !wasSaved){
  1018. child.focus(true);
  1019. }
  1020. }else{
  1021. this._savedFocus = dijit.getFocus(this);
  1022. var foc = this._focusedPane;
  1023. delete this._focusedPane;
  1024. if(!wasSaved){
  1025. foc.focus(true);
  1026. }
  1027. }
  1028. },
  1029. handleKey:function(/*Event*/e){
  1030. // summary: handle the key for the given event - called by dropdown
  1031. // widgets
  1032. if(e.charOrCode == dojo.keys.DOWN_ARROW){
  1033. delete this._savedFocus;
  1034. this.focus();
  1035. return false;
  1036. }else if(e.charOrCode == dojo.keys.ESCAPE){
  1037. this._onCancel();
  1038. return false;
  1039. }
  1040. return true;
  1041. },
  1042. _updateChildClasses: function(){
  1043. // summary: Called when a child is added or removed - so that we can
  1044. // update the classes for styling the "current" one differently than
  1045. // the others
  1046. var children = this.getChildren();
  1047. var length = children.length;
  1048. dojo.forEach(children, function(c, idx){
  1049. dojo.toggleClass(c.domNode, "dojoxRollingListPaneCurrentChild", (idx == (length - 1)));
  1050. dojo.toggleClass(c.domNode, "dojoxRollingListPaneCurrentSelected", (idx == (length - 2)));
  1051. });
  1052. },
  1053. startup: function(){
  1054. if(this._started){ return; }
  1055. if(!this.getParent || !this.getParent()){
  1056. this.resize();
  1057. this.connect(dojo.global, "onresize", "resize");
  1058. }
  1059. this.connect(this, "addChild", "_updateChildClasses");
  1060. this.connect(this, "removeChild", "_updateChildClasses");
  1061. this._setStore(this.store);
  1062. this.set("showButtons", this.showButtons);
  1063. this.inherited(arguments);
  1064. this._lastExecutedValue = this.get("value");
  1065. },
  1066. getChildItems: function(/*item*/ item){
  1067. // summary: Returns the child items for the given store item
  1068. var childItems, store = this.store;
  1069. dojo.forEach(this.childrenAttrs, function(attr){
  1070. var vals = store.getValues(item, attr);
  1071. if(vals && vals.length){
  1072. childItems = (childItems||[]).concat(vals);
  1073. }
  1074. });
  1075. return childItems;
  1076. },
  1077. getMenuItemForItem: function(/*item*/ item, /* dijit._Contained */ parentPane, /* item[]? */ children){
  1078. // summary: user overridable function to return a widget for the given item
  1079. // and its children.
  1080. return new dijit.MenuItem({});
  1081. },
  1082. getPaneForItem: function(/* item? */ item, /* dijit._Contained? */ parentPane, /* item[]? */ children){
  1083. // summary: user-overridable function to return a pane that corresponds
  1084. // to the given item in the store. It can return null to not add a new pane
  1085. // (ie, you are planning on doing something else with it in onItemClick)
  1086. //
  1087. // Item is undefined for the root pane, children is undefined for non-group panes
  1088. if(!item || children){
  1089. return new dojox.widget._RollingListGroupPane({});
  1090. }else{
  1091. return null;
  1092. }
  1093. },
  1094. onItemClick: function(/* item */ item, /* dijit._Contained */ pane, /* item[]? */ children){
  1095. // summary: called when an item is clicked - it receives the store item
  1096. },
  1097. onExecute: function(){
  1098. // summary: exists so that popups don't disappear too soon
  1099. },
  1100. onCancel: function(){
  1101. // summary: exists so that we can close ourselves if we wish
  1102. },
  1103. onChange: function(/* item */ value){
  1104. // summary: called when the value of this widget has changed
  1105. }
  1106. });
  1107. }