_Events.js 14 KB

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