Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make event handling more consistent, with uniform access to "grid", "…

…cell" or "row" as a Cell or Row object, and "parentType" as an indicator of the event trigger type, equal to null or undefined if programmatically triggered.
  • Loading branch information...
commit a10ac2887c36c9cd0a2237e3d0133cbbf81606b1 1 parent bb68bc9
Kris Zyp authored

Showing 4 changed files with 92 additions and 75 deletions. Show diff stats Hide diff stats

  1. +11 5 Keyboard.js
  2. +50 44 Selection.js
  3. +7 6 editor.js
  4. +24 20 selector.js
16 Keyboard.js
@@ -55,11 +55,15 @@ return declare([List], {
55 55 next;
56 56
57 57 function focusOnCell(element, event, dontFocus){
58   - var cell = grid[grid.cellNavigation ? "cell" : "row"](element);
  58 + var cellOrRowType = grid.cellNavigation ? "cell" : "row";
  59 + var cell = grid[cellOrRowType](element);
59 60
60 61 element = cell && cell.element;
61 62 if(!element){ return; }
62   -
  63 + event = lang.mixin({
  64 + parentType: event.type,
  65 + grid: grid
  66 + }, event);
63 67 if(!event.bubbles){
64 68 // IE doesn't always have a bubbles property already true, Opera will throw an error if you try to set it to true if it is already true
65 69 event.bubbles = true;
@@ -72,11 +76,13 @@ return declare([List], {
72 76 // clean up after workaround below (for non-input cases)
73 77 cellFocusedElement.style.position = "";
74 78 }
75   - event.cell = cellFocusedElement;
  79 + event.cell = cellFocusedElement; // keeping this for some level of back-compat for when we passed in an element
  80 + event[cellOrRowType] = grid[cellOrRowType](cellFocusedElement); // set the cell or row
76 81 on.emit(element, "dgrid-cellfocusout", event);
77 82 }
78 83 cellFocusedElement = element;
79   - event.cell = element;
  84 + event.cell = element; // keeping this for some level of back-compat for when we passed in an element
  85 + event[cellOrRowType] = cell;
80 86 if(!dontFocus){
81 87 if(has("ie") < 8){
82 88 // setting the position to relative magically makes the outline
@@ -88,7 +94,7 @@ return declare([List], {
88 94 element.focus();
89 95 }
90 96 put(element, ".dgrid-focus");
91   - on.emit(cellFocusedElement, "dgrid-cellfocusin", lang.mixin({ parentType: event.type }, event));
  97 + on.emit(cellFocusedElement, "dgrid-cellfocusin", event);
92 98 }
93 99
94 100 while((next = cellFocusedElement.firstChild) && next.tagName){
94 Selection.js
@@ -67,53 +67,57 @@ return declare([List], {
67 67 return;
68 68 }
69 69 this._waitForMouseUp = null;
70   -
71   - var ctrlKey = !event.keyCode ? event[ctrlEquiv] : event.ctrlKey;
72   - if(!event.keyCode || !event.ctrlKey || event.keyCode == 32){
73   - var mode = this.selectionMode,
74   - row = currentTarget,
75   - rowObj = this.row(row),
76   - lastRow = this._lastSelected;
77   -
78   - if(mode == "single"){
79   - if(lastRow == row){
80   - if(ctrlKey){
81   - // allow deselection even within single select mode
82   - this.select(row, null, null);
  70 + this._triggerEvent = event;
  71 + try{
  72 + var ctrlKey = !event.keyCode ? event[ctrlEquiv] : event.ctrlKey;
  73 + if(!event.keyCode || !event.ctrlKey || event.keyCode == 32){
  74 + var mode = this.selectionMode,
  75 + row = currentTarget,
  76 + rowObj = this.row(row),
  77 + lastRow = this._lastSelected;
  78 +
  79 + if(mode == "single"){
  80 + if(lastRow == row){
  81 + if(ctrlKey){
  82 + // allow deselection even within single select mode
  83 + this.select(row, null, null);
  84 + }
  85 + }else{
  86 + this.clearSelection();
  87 + this.select(row);
83 88 }
  89 + this._lastSelected = row;
  90 + }else if(this.selection[rowObj.id] && !event.shiftKey && event.type == "mousedown"){
  91 + // we wait for the mouse up if we are clicking a selected item so that drag n' drop
  92 + // is possible without losing our selection
  93 + this._waitForMouseUp = row;
84 94 }else{
85   - this.clearSelection();
86   - this.select(row);
87   - }
88   - this._lastSelected = row;
89   - }else if(this.selection[rowObj.id] && !event.shiftKey && event.type == "mousedown"){
90   - // we wait for the mouse up if we are clicking a selected item so that drag n' drop
91   - // is possible without losing our selection
92   - this._waitForMouseUp = row;
93   - }else{
94   - var value;
95   - // clear selection first for non-ctrl-clicks in extended mode,
96   - // as well as for right-clicks on unselected targets
97   - if((event.button != 2 && mode == "extended" && !ctrlKey) ||
98   - (event.button == 2 && !(this.selection[rowObj.id]))){
99   - this.clearSelection(rowObj.id, true);
100   - }
101   - if(!event.shiftKey){
102   - // null == toggle; undefined == true;
103   - lastRow = value = ctrlKey ? null : undefined;
  95 + var value;
  96 + // clear selection first for non-ctrl-clicks in extended mode,
  97 + // as well as for right-clicks on unselected targets
  98 + if((event.button != 2 && mode == "extended" && !ctrlKey) ||
  99 + (event.button == 2 && !(this.selection[rowObj.id]))){
  100 + this.clearSelection(rowObj.id, true);
  101 + }
  102 + if(!event.shiftKey){
  103 + // null == toggle; undefined == true;
  104 + lastRow = value = ctrlKey ? null : undefined;
  105 + }
  106 + this.select(row, lastRow, value);
  107 +
  108 + if(!lastRow){
  109 + // update lastRow reference for potential subsequent shift+select
  110 + // (current row was already selected by earlier logic)
  111 + this._lastSelected = row;
  112 + }
104 113 }
105   - this.select(row, lastRow, value);
106   -
107   - if(!lastRow){
108   - // update lastRow reference for potential subsequent shift+select
109   - // (current row was already selected by earlier logic)
110   - this._lastSelected = row;
  114 + if(!event.keyCode && (event.shiftKey || ctrlKey)){
  115 + // prevent selection in firefox
  116 + event.preventDefault();
111 117 }
112 118 }
113   - if(!event.keyCode && (event.shiftKey || ctrlKey)){
114   - // prevent selection in firefox
115   - event.preventDefault();
116   - }
  119 + }finally{
  120 + this._triggerEvent = null;
117 121 }
118 122 },
119 123
@@ -182,7 +186,8 @@ return declare([List], {
182 186 _selectionEventQueue: function(value, type){
183 187 var grid = this,
184 188 event = "dgrid-" + (value ? "select" : "deselect"),
185   - rows = this[event]; // current event queue (actually cells for CellSelection)
  189 + rows = this[event], // current event queue (actually cells for CellSelection)
  190 + triggerEvent = grid._triggerEvent;
186 191
187 192 if(rows){ return rows; } // return existing queue, allowing to push more
188 193
@@ -193,7 +198,8 @@ return declare([List], {
193 198
194 199 var eventObject = {
195 200 bubbles: true,
196   - grid: grid
  201 + grid: grid,
  202 + parentType: triggerEvent && triggerEvent.type
197 203 };
198 204 eventObject[type] = rows;
199 205 on.emit(grid.contentNode, event, eventObject);
13 editor.js
@@ -46,7 +46,7 @@ function dataFromEditor(column, cmp){
46 46 }
47 47 }
48 48
49   -function setProperty(grid, cellElement, oldValue, value){
  49 +function setProperty(grid, cellElement, oldValue, value, triggerEvent){
50 50 // Updates dirty hash and fires dgrid-datachange event for a changed value.
51 51 var cell, row, column;
52 52 // test whether old and new values are inequal, with coercion (e.g. for Dates)
@@ -64,7 +64,8 @@ function setProperty(grid, cellElement, oldValue, value){
64 64 oldValue: oldValue,
65 65 value: value,
66 66 bubbles: true,
67   - cancelable: true
  67 + cancelable: true,
  68 + parentType: triggerEvent && triggerEvent.type
68 69 })){
69 70 if(grid.updateDirty){
70 71 // for OnDemandGrid: update dirty data, and save if autoSave is true
@@ -83,12 +84,12 @@ function setProperty(grid, cellElement, oldValue, value){
83 84 }
84 85
85 86 // intermediary frontend to setProperty for HTML and widget editors
86   -function setPropertyFromEditor(grid, column, cmp) {
  87 +function setPropertyFromEditor(grid, column, cmp, triggerEvent) {
87 88 var value;
88 89 if(!cmp.isValid || cmp.isValid()){
89 90 value = setProperty(grid, (cmp.domNode || cmp).parentNode,
90 91 activeCell ? activeValue : cmp._dgridLastValue,
91   - dataFromEditor(column, cmp));
  92 + dataFromEditor(column, cmp), triggerEvent);
92 93
93 94 if(activeCell){ // for editors with editOn defined
94 95 activeValue = value;
@@ -123,14 +124,14 @@ function createEditor(column){
123 124 // the latter is delayed by setTimeouts in Dijit and will fire too late.
124 125 cmp.connect(cmp, editOn ? "onBlur" : "onChange", function(){
125 126 if(!cmp._dgridIgnoreChange){
126   - setPropertyFromEditor(grid, column, this);
  127 + setPropertyFromEditor(grid, column, this, {type: "widget"});
127 128 }
128 129 });
129 130 }else{
130 131 handleChange = function(evt){
131 132 var target = evt.target;
132 133 if("_dgridLastValue" in target && target.className.indexOf("dgrid-input") > -1){
133   - setPropertyFromEditor(grid, column, target);
  134 + setPropertyFromEditor(grid, column, target, evt);
134 135 }
135 136 };
136 137
44 selector.js
@@ -53,29 +53,33 @@ function(kernel, arrayUtil, on, aspect, has, put){
53 53 // the shiftKey property from
54 54 if(event.type == "click" || event.keyCode == 32 || event.keyCode == 0){
55 55 var row = grid.row(event), lastRow = grid._lastSelected && grid.row(grid._lastSelected);
56   -
57   - if(type == "radio"){
58   - if(!lastRow || lastRow.id != row.id){
59   - grid.clearSelection();
60   - grid.select(row, null, true);
61   - grid._lastSelected = row.element;
62   - }
63   - }else{
64   - if(row){
65   - if(event.shiftKey){
66   - // make sure the last input always ends up checked for shift key
67   - changeInput(true)({rows: [row]});
68   - }else{
69   - // no shift key, so no range selection
70   - lastRow = null;
  56 + grid._triggerEvent = event;
  57 + try{
  58 + if(type == "radio"){
  59 + if(!lastRow || lastRow.id != row.id){
  60 + grid.clearSelection();
  61 + grid.select(row, null, true);
  62 + grid._lastSelected = row.element;
71 63 }
72   - lastRow = event.shiftKey ? lastRow : null;
73   - grid.select(lastRow|| row, row, lastRow ? undefined : null);
74   - grid._lastSelected = row.element;
75 64 }else{
76   - put(this, (grid.allSelected ? "!" : ".") + "dgrid-select-all");
77   - grid[grid.allSelected ? "clearSelection" : "selectAll"]();
  65 + if(row){
  66 + if(event.shiftKey){
  67 + // make sure the last input always ends up checked for shift key
  68 + changeInput(true)({rows: [row]});
  69 + }else{
  70 + // no shift key, so no range selection
  71 + lastRow = null;
  72 + }
  73 + lastRow = event.shiftKey ? lastRow : null;
  74 + grid.select(lastRow|| row, row, lastRow ? undefined : null);
  75 + grid._lastSelected = row.element;
  76 + }else{
  77 + put(this, (grid.allSelected ? "!" : ".") + "dgrid-select-all");
  78 + grid[grid.allSelected ? "clearSelection" : "selectAll"]();
  79 + }
78 80 }
  81 + }finally{
  82 + grid._triggerEvent = null;
79 83 }
80 84 }
81 85 }

0 comments on commit a10ac28

Please sign in to comment.
Something went wrong with that request. Please try again.