Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor selection batching code a bit; underscore internal methods

  • Loading branch information...
commit cfef930e264db279a91b51c877eb97977f133dcc 1 parent b661201
SitePenKenFranqueiro SitePenKenFranqueiro authored

Showing 3 changed files with 43 additions and 39 deletions. Show diff stats Hide diff stats

  1. +1 1  CellSelection.js
  2. +27 26 Selection.js
  3. +15 12 Selector.js
2  CellSelection.js
@@ -62,7 +62,7 @@ return declare([Selection], {
62 62 }
63 63 }
64 64 if(value != previous && element){
65   - this.selectionEventQueue(value, "cells").push(cell);
  65 + this._selectionEventQueue(value, "cells").push(cell);
66 66 }
67 67 if(toCell){
68 68 // a range
53 Selection.js
@@ -179,29 +179,29 @@ return declare([List], {
179 179 return true;
180 180 },
181 181
182   - selectionEventQueue: function(value, type){
183   - var event = "dgrid-" + (value ? "select" : "deselect");
184   - // get the event queue
185   - var rows = this[event];
186   - if(!rows){
187   - var grid = this;
188   - // create a timeout to fire an event for the accumulated rows once everything is done
189   - setTimeout(this.fireSelectionEvent = function(){ // we setup a method here in case the event needs to fired immediately
190   - if(rows){
191   - var eventObject = {
192   - bubbles: true,
193   - grid: grid
194   - }
195   - eventObject[type] = rows;
196   - on.emit(grid.contentNode, event, eventObject);
197   - rows = null;
198   - // clear the queue, so we create a new one as needed
199   - delete grid[event];
200   - }
201   - });
202   - rows = this[event] = [];
203   - }
204   - return rows;
  182 + _selectionEventQueue: function(value, type){
  183 + var grid = this,
  184 + event = "dgrid-" + (value ? "select" : "deselect"),
  185 + rows = this[event]; // current event queue (actually cells for CellSelection)
  186 +
  187 + if(rows){ return rows; } // return existing queue, allowing to push more
  188 +
  189 + // Create a timeout to fire an event for the accumulated rows once everything is done.
  190 + // We expose the callback in case the event needs to be fired immediately.
  191 + setTimeout(this._fireSelectionEvent = function(){
  192 + if(!rows){ return; } // rows will be set only the first time this is called
  193 +
  194 + var eventObject = {
  195 + bubbles: true,
  196 + grid: grid
  197 + };
  198 + eventObject[type] = rows;
  199 + on.emit(grid.contentNode, event, eventObject);
  200 + rows = null;
  201 + // clear the queue, so we create a new one as needed
  202 + delete grid[event];
  203 + }, 0);
  204 + return (rows = this[event] = []);
205 205 },
206 206 select: function(row, toRow, value){
207 207 if(value === undefined){
@@ -234,7 +234,7 @@ return declare([List], {
234 234 }
235 235 if(value != previousValue && element){
236 236 // add to the queue of row events
237   - this.selectionEventQueue(value, "rows").push(row);
  237 + this._selectionEventQueue(value, "rows").push(row);
238 238 }
239 239
240 240 if(toRow){
@@ -286,8 +286,9 @@ return declare([List], {
286 286 refresh: function(){
287 287 if(this.deselectOnRefresh){
288 288 this.clearSelection();
289   - // need to fire the selection event now because after the refresh the nodes that we will fire for will be gone
290   - this.fireSelectionEvent && this.fireSelectionEvent();
  289 + // Need to fire the selection event now because after the refresh,
  290 + // the nodes that we will fire for will be gone.
  291 + this._fireSelectionEvent && this._fireSelectionEvent();
291 292 }
292 293 this._lastSelected = null;
293 294 this.inherited(arguments);
27 Selector.js
@@ -8,8 +8,11 @@ define(["dojo/on", "dojo/aspect", "dojo/_base/sniff", "put-selector/put"], funct
8 8 function changeInput(value){
9 9 // creates a function that modifies the input on an event
10 10 return function(event){
11   - var rows = event.rows;
12   - for(var i = 0; i < rows.length; i++){
  11 + var rows = event.rows,
  12 + len = rows.length,
  13 + selection, mixed, i;
  14 +
  15 + for(i = 0; i < len; i++){
13 16 var element = grid.cell(rows[i], column.id).element;
14 17 element = (element.contents || element).input;
15 18 if(!element.disabled){
@@ -17,17 +20,17 @@ define(["dojo/on", "dojo/aspect", "dojo/_base/sniff", "put-selector/put"], funct
17 20 element.checked = value;
18 21 }
19 22 }
20   - // see if the header checkbox needs to be indeterminate
21   - var mixed = false;
22   - var selection = grid.selection;
23   - for(var i in selection){
24   - // if there is anything in the selection, than it is indeterminate
25   - if(selection[i] != grid.allSelected){
26   - mixed = true;
27   - break;
28   - }
29   - }
30 23 if(headerCheckbox.type == "checkbox"){
  24 + selection = grid.selection;
  25 + mixed = false;
  26 + // see if the header checkbox needs to be indeterminate
  27 + for(i in selection){
  28 + // if there is anything in the selection, than it is indeterminate
  29 + if(selection[i] != grid.allSelected){
  30 + mixed = true;
  31 + break;
  32 + }
  33 + }
31 34 headerCheckbox.indeterminate = mixed;
32 35 headerCheckbox.checked = grid.allSelected;
33 36 }

0 comments on commit cfef930

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