_Events.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.grid._Events"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid._Events"] = true;
  8. dojo.provide("dojox.grid._Events");
  9. dojo.declare("dojox.grid._Events", null, {
  10. // summary:
  11. // _Grid mixin that provides default implementations for grid events.
  12. // description:
  13. // Default synthetic events dispatched for _Grid. dojo.connect to events to
  14. // retain default implementation or override them for custom handling.
  15. // cellOverClass: String
  16. // css class to apply to grid cells over which the cursor is placed.
  17. cellOverClass: "dojoxGridCellOver",
  18. onKeyEvent: function(e){
  19. // summary: top level handler for Key Events
  20. this.dispatchKeyEvent(e);
  21. },
  22. onContentEvent: function(e){
  23. // summary: Top level handler for Content events
  24. this.dispatchContentEvent(e);
  25. },
  26. onHeaderEvent: function(e){
  27. // summary: Top level handler for header events
  28. this.dispatchHeaderEvent(e);
  29. },
  30. onStyleRow: function(inRow){
  31. // summary:
  32. // Perform row styling on a given row. Called whenever row styling is updated.
  33. //
  34. // inRow: Object
  35. // Object containing row state information: selected, true if the row is selcted; over:
  36. // true of the mouse is over the row; odd: true if the row is odd. Use customClasses and
  37. // customStyles to control row css classes and styles; both properties are strings.
  38. //
  39. // example: onStyleRow({ selected: true, over:true, odd:false })
  40. var i = inRow;
  41. i.customClasses += (i.odd?" dojoxGridRowOdd":"") + (i.selected?" dojoxGridRowSelected":"") + (i.over?" dojoxGridRowOver":"");
  42. this.focus.styleRow(inRow);
  43. this.edit.styleRow(inRow);
  44. },
  45. onKeyDown: function(e){
  46. // summary:
  47. // Grid key event handler. By default enter begins editing and applies edits, escape cancels an edit,
  48. // tab, shift-tab, and arrow keys move grid cell focus.
  49. if(e.altKey || e.metaKey){
  50. return;
  51. }
  52. var dk = dojo.keys;
  53. var colIdx;
  54. switch(e.keyCode){
  55. case dk.ESCAPE:
  56. this.edit.cancel();
  57. break;
  58. case dk.ENTER:
  59. if(!this.edit.isEditing()){
  60. colIdx = this.focus.getHeaderIndex();
  61. if(colIdx >= 0) {
  62. this.setSortIndex(colIdx);
  63. break;
  64. }else {
  65. this.selection.clickSelect(this.focus.rowIndex, dojo.isCopyKey(e), e.shiftKey);
  66. }
  67. dojo.stopEvent(e);
  68. }
  69. if(!e.shiftKey){
  70. var isEditing = this.edit.isEditing();
  71. this.edit.apply();
  72. if(!isEditing){
  73. this.edit.setEditCell(this.focus.cell, this.focus.rowIndex);
  74. }
  75. }
  76. if (!this.edit.isEditing()){
  77. var curView = this.focus.focusView || this.views.views[0]; //if no focusView than only one view
  78. curView.content.decorateEvent(e);
  79. this.onRowClick(e);
  80. dojo.stopEvent(e);
  81. }
  82. break;
  83. case dk.SPACE:
  84. if(!this.edit.isEditing()){
  85. colIdx = this.focus.getHeaderIndex();
  86. if(colIdx >= 0) {
  87. this.setSortIndex(colIdx);
  88. break;
  89. }else {
  90. this.selection.clickSelect(this.focus.rowIndex, dojo.isCopyKey(e), e.shiftKey);
  91. }
  92. dojo.stopEvent(e);
  93. }
  94. break;
  95. case dk.TAB:
  96. this.focus[e.shiftKey ? 'previousKey' : 'nextKey'](e);
  97. break;
  98. case dk.LEFT_ARROW:
  99. case dk.RIGHT_ARROW:
  100. if(!this.edit.isEditing()){
  101. var keyCode = e.keyCode; // IE seems to lose after stopEvent when modifier keys
  102. dojo.stopEvent(e);
  103. colIdx = this.focus.getHeaderIndex();
  104. if (colIdx >= 0 && (e.shiftKey && e.ctrlKey)){
  105. this.focus.colSizeAdjust(e, colIdx, (keyCode == dk.LEFT_ARROW ? -1 : 1)*5);
  106. }
  107. else{
  108. var offset = (keyCode == dk.LEFT_ARROW) ? 1 : -1;
  109. if(dojo._isBodyLtr()){ offset *= -1; }
  110. this.focus.move(0, offset);
  111. }
  112. }
  113. break;
  114. case dk.UP_ARROW:
  115. if(!this.edit.isEditing() && this.focus.rowIndex !== 0){
  116. dojo.stopEvent(e);
  117. this.focus.move(-1, 0);
  118. }
  119. break;
  120. case dk.DOWN_ARROW:
  121. if(!this.edit.isEditing() && this.focus.rowIndex+1 != this.rowCount){
  122. dojo.stopEvent(e);
  123. this.focus.move(1, 0);
  124. }
  125. break;
  126. case dk.PAGE_UP:
  127. if(!this.edit.isEditing() && this.focus.rowIndex !== 0){
  128. dojo.stopEvent(e);
  129. if(this.focus.rowIndex != this.scroller.firstVisibleRow+1){
  130. this.focus.move(this.scroller.firstVisibleRow-this.focus.rowIndex, 0);
  131. }else{
  132. this.setScrollTop(this.scroller.findScrollTop(this.focus.rowIndex-1));
  133. this.focus.move(this.scroller.firstVisibleRow-this.scroller.lastVisibleRow+1, 0);
  134. }
  135. }
  136. break;
  137. case dk.PAGE_DOWN:
  138. if(!this.edit.isEditing() && this.focus.rowIndex+1 != this.rowCount){
  139. dojo.stopEvent(e);
  140. if(this.focus.rowIndex != this.scroller.lastVisibleRow-1){
  141. this.focus.move(this.scroller.lastVisibleRow-this.focus.rowIndex-1, 0);
  142. }else{
  143. this.setScrollTop(this.scroller.findScrollTop(this.focus.rowIndex+1));
  144. this.focus.move(this.scroller.lastVisibleRow-this.scroller.firstVisibleRow-1, 0);
  145. }
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. },
  152. onMouseOver: function(e){
  153. // summary:
  154. // Event fired when mouse is over the grid.
  155. // e: Event
  156. // Decorated event object contains reference to grid, cell, and rowIndex
  157. e.rowIndex == -1 ? this.onHeaderCellMouseOver(e) : this.onCellMouseOver(e);
  158. },
  159. onMouseOut: function(e){
  160. // summary:
  161. // Event fired when mouse moves out of the grid.
  162. // e: Event
  163. // Decorated event object that contains reference to grid, cell, and rowIndex
  164. e.rowIndex == -1 ? this.onHeaderCellMouseOut(e) : this.onCellMouseOut(e);
  165. },
  166. onMouseDown: function(e){
  167. // summary:
  168. // Event fired when mouse is down inside grid.
  169. // e: Event
  170. // Decorated event object that contains reference to grid, cell, and rowIndex
  171. e.rowIndex == -1 ? this.onHeaderCellMouseDown(e) : this.onCellMouseDown(e);
  172. },
  173. onMouseOverRow: function(e){
  174. // summary:
  175. // Event fired when mouse is over any row (data or header).
  176. // e: Event
  177. // Decorated event object contains reference to grid, cell, and rowIndex
  178. if(!this.rows.isOver(e.rowIndex)){
  179. this.rows.setOverRow(e.rowIndex);
  180. e.rowIndex == -1 ? this.onHeaderMouseOver(e) : this.onRowMouseOver(e);
  181. }
  182. },
  183. onMouseOutRow: function(e){
  184. // summary:
  185. // Event fired when mouse moves out of any row (data or header).
  186. // e: Event
  187. // Decorated event object contains reference to grid, cell, and rowIndex
  188. if(this.rows.isOver(-1)){
  189. this.onHeaderMouseOut(e);
  190. }else if(!this.rows.isOver(-2)){
  191. this.rows.setOverRow(-2);
  192. this.onRowMouseOut(e);
  193. }
  194. },
  195. onMouseDownRow: function(e){
  196. // summary:
  197. // Event fired when mouse is down inside grid row
  198. // e: Event
  199. // Decorated event object that contains reference to grid, cell, and rowIndex
  200. if(e.rowIndex != -1)
  201. this.onRowMouseDown(e);
  202. },
  203. // cell events
  204. onCellMouseOver: function(e){
  205. // summary:
  206. // Event fired when mouse is over a cell.
  207. // e: Event
  208. // Decorated event object contains reference to grid, cell, and rowIndex
  209. if(e.cellNode){
  210. dojo.addClass(e.cellNode, this.cellOverClass);
  211. }
  212. },
  213. onCellMouseOut: function(e){
  214. // summary:
  215. // Event fired when mouse moves out of a cell.
  216. // e: Event
  217. // Decorated event object which contains reference to grid, cell, and rowIndex
  218. if(e.cellNode){
  219. dojo.removeClass(e.cellNode, this.cellOverClass);
  220. }
  221. },
  222. onCellMouseDown: function(e){
  223. // summary:
  224. // Event fired when mouse is down in a header cell.
  225. // e: Event
  226. // Decorated event object which contains reference to grid, cell, and rowIndex
  227. },
  228. onCellClick: function(e){
  229. // summary:
  230. // Event fired when a cell is clicked.
  231. // e: Event
  232. // Decorated event object which contains reference to grid, cell, and rowIndex
  233. this._click[0] = this._click[1];
  234. this._click[1] = e;
  235. if(!this.edit.isEditCell(e.rowIndex, e.cellIndex)){
  236. this.focus.setFocusCell(e.cell, e.rowIndex);
  237. }
  238. // in some cases click[0] is null which causes false doubeClicks. Fixes #100703
  239. if(this._click.length > 1 && this._click[0] == null){
  240. this._click.shift();
  241. }
  242. this.onRowClick(e);
  243. },
  244. onCellDblClick: function(e){
  245. // summary:
  246. // Event fired when a cell is double-clicked.
  247. // e: Event
  248. // Decorated event object contains reference to grid, cell, and rowIndex
  249. var event;
  250. if(this._click.length > 1 && dojo.isIE){
  251. event = this._click[1];
  252. }else if(this._click.length > 1 && this._click[0].rowIndex != this._click[1].rowIndex){
  253. event = this._click[0];
  254. }else{
  255. event = e;
  256. }
  257. this.focus.setFocusCell(event.cell, event.rowIndex);
  258. this.edit.setEditCell(event.cell, event.rowIndex);
  259. this.onRowDblClick(e);
  260. },
  261. onCellContextMenu: function(e){
  262. // summary:
  263. // Event fired when a cell context menu is accessed via mouse right click.
  264. // e: Event
  265. // Decorated event object which contains reference to grid, cell, and rowIndex
  266. this.onRowContextMenu(e);
  267. },
  268. onCellFocus: function(inCell, inRowIndex){
  269. // summary:
  270. // Event fired when a cell receives focus.
  271. // inCell: Object
  272. // Cell object containing properties of the grid column.
  273. // inRowIndex: Integer
  274. // Index of the grid row
  275. this.edit.cellFocus(inCell, inRowIndex);
  276. },
  277. // row events
  278. onRowClick: function(e){
  279. // summary:
  280. // Event fired when a row is clicked.
  281. // e: Event
  282. // Decorated event object which contains reference to grid, cell, and rowIndex
  283. this.edit.rowClick(e);
  284. this.selection.clickSelectEvent(e);
  285. },
  286. onRowDblClick: function(e){
  287. // summary:
  288. // Event fired when a row is double clicked.
  289. // e: Event
  290. // decorated event object which contains reference to grid, cell, and rowIndex
  291. },
  292. onRowMouseOver: function(e){
  293. // summary:
  294. // Event fired when mouse moves over a data row.
  295. // e: Event
  296. // Decorated event object which contains reference to grid, cell, and rowIndex
  297. },
  298. onRowMouseOut: function(e){
  299. // summary:
  300. // Event fired when mouse moves out of a data row.
  301. // e: Event
  302. // Decorated event object contains reference to grid, cell, and rowIndex
  303. },
  304. onRowMouseDown: function(e){
  305. // summary:
  306. // Event fired when mouse is down in a row.
  307. // e: Event
  308. // Decorated event object which contains reference to grid, cell, and rowIndex
  309. },
  310. onRowContextMenu: function(e){
  311. // summary:
  312. // Event fired when a row context menu is accessed via mouse right click.
  313. // e: Event
  314. // Decorated event object which contains reference to grid, cell, and rowIndex
  315. dojo.stopEvent(e);
  316. },
  317. // header events
  318. onHeaderMouseOver: function(e){
  319. // summary:
  320. // Event fired when mouse moves over the grid header.
  321. // e: Event
  322. // Decorated event object contains reference to grid, cell, and rowIndex
  323. },
  324. onHeaderMouseOut: function(e){
  325. // summary:
  326. // Event fired when mouse moves out of the grid header.
  327. // e: Event
  328. // Decorated event object which contains reference to grid, cell, and rowIndex
  329. },
  330. onHeaderCellMouseOver: function(e){
  331. // summary:
  332. // Event fired when mouse moves over a header cell.
  333. // e: Event
  334. // Decorated event object which contains reference to grid, cell, and rowIndex
  335. if(e.cellNode){
  336. dojo.addClass(e.cellNode, this.cellOverClass);
  337. }
  338. },
  339. onHeaderCellMouseOut: function(e){
  340. // summary:
  341. // Event fired when mouse moves out of a header cell.
  342. // e: Event
  343. // Decorated event object which contains reference to grid, cell, and rowIndex
  344. if(e.cellNode){
  345. dojo.removeClass(e.cellNode, this.cellOverClass);
  346. }
  347. },
  348. onHeaderCellMouseDown: function(e) {
  349. // summary:
  350. // Event fired when mouse is down in a header cell.
  351. // e: Event
  352. // Decorated event object which contains reference to grid, cell, and rowIndex
  353. },
  354. onHeaderClick: function(e){
  355. // summary:
  356. // Event fired when the grid header is clicked.
  357. // e: Event
  358. // Decorated event object which contains reference to grid, cell, and rowIndex
  359. },
  360. onHeaderCellClick: function(e){
  361. // summary:
  362. // Event fired when a header cell is clicked.
  363. // e: Event
  364. // Decorated event object which contains reference to grid, cell, and rowIndex
  365. this.setSortIndex(e.cell.index);
  366. this.onHeaderClick(e);
  367. },
  368. onHeaderDblClick: function(e){
  369. // summary:
  370. // Event fired when the grid header is double clicked.
  371. // e: Event
  372. // Decorated event object which contains reference to grid, cell, and rowIndex
  373. },
  374. onHeaderCellDblClick: function(e){
  375. // summary:
  376. // Event fired when a header cell is double clicked.
  377. // e: Event
  378. // Decorated event object which contains reference to grid, cell, and rowIndex
  379. this.onHeaderDblClick(e);
  380. },
  381. onHeaderCellContextMenu: function(e){
  382. // summary:
  383. // Event fired when a header cell context menu is accessed via mouse right click.
  384. // e: Event
  385. // Decorated event object which contains reference to grid, cell, and rowIndex
  386. this.onHeaderContextMenu(e);
  387. },
  388. onHeaderContextMenu: function(e){
  389. // summary:
  390. // Event fired when the grid header context menu is accessed via mouse right click.
  391. // e: Event
  392. // Decorated event object which contains reference to grid, cell, and rowIndex
  393. if(!this.headerMenu){
  394. dojo.stopEvent(e);
  395. }
  396. },
  397. // editing
  398. onStartEdit: function(inCell, inRowIndex){
  399. // summary:
  400. // Event fired when editing is started for a given grid cell
  401. // inCell: Object
  402. // Cell object containing properties of the grid column.
  403. // inRowIndex: Integer
  404. // Index of the grid row
  405. },
  406. onApplyCellEdit: function(inValue, inRowIndex, inFieldIndex){
  407. // summary:
  408. // Event fired when editing is applied for a given grid cell
  409. // inValue: String
  410. // Value from cell editor
  411. // inRowIndex: Integer
  412. // Index of the grid row
  413. // inFieldIndex: Integer
  414. // Index in the grid's data store
  415. },
  416. onCancelEdit: function(inRowIndex){
  417. // summary:
  418. // Event fired when editing is cancelled for a given grid cell
  419. // inRowIndex: Integer
  420. // Index of the grid row
  421. },
  422. onApplyEdit: function(inRowIndex){
  423. // summary:
  424. // Event fired when editing is applied for a given grid row
  425. // inRowIndex: Integer
  426. // Index of the grid row
  427. },
  428. onCanSelect: function(inRowIndex){
  429. // summary:
  430. // Event to determine if a grid row may be selected
  431. // inRowIndex: Integer
  432. // Index of the grid row
  433. // returns: Boolean
  434. // true if the row can be selected
  435. return true;
  436. },
  437. onCanDeselect: function(inRowIndex){
  438. // summary:
  439. // Event to determine if a grid row may be deselected
  440. // inRowIndex: Integer
  441. // Index of the grid row
  442. // returns: Boolean
  443. // true if the row can be deselected
  444. return true;
  445. },
  446. onSelected: function(inRowIndex){
  447. // summary:
  448. // Event fired when a grid row is selected
  449. // inRowIndex: Integer
  450. // Index of the grid row
  451. this.updateRowStyles(inRowIndex);
  452. },
  453. onDeselected: function(inRowIndex){
  454. // summary:
  455. // Event fired when a grid row is deselected
  456. // inRowIndex: Integer
  457. // Index of the grid row
  458. this.updateRowStyles(inRowIndex);
  459. },
  460. onSelectionChanged: function(){
  461. }
  462. });
  463. }