| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/connect", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/_base/sniff", |
| 7 |
"dojo/_base/event", |
| 8 |
"dojo/_base/html", |
| 9 |
"dojo/_base/window", |
| 10 |
"dojo/query", |
| 11 |
"dijit/_Widget", |
| 12 |
"dijit/_TemplatedMixin", |
| 13 |
"dijit/_WidgetsInTemplateMixin", |
| 14 |
"dojo/fx", |
| 15 |
"dojo/_base/fx", |
| 16 |
"dojo/string", |
| 17 |
"dijit/focus", |
| 18 |
"dojo/text!../../templates/FilterBar.html" |
| 19 |
], function(declare, array, connect, lang, has, event, html, win, query, _Widget, |
| 20 |
_TemplatedMixin, _WidgetsInTemplateMixin, fx, baseFx, string, dijitFocus, template){ |
| 21 |
|
| 22 |
var _focusClass = "dojoxGridFBarHover", |
| 23 |
_filteredClass = "dojoxGridFBarFiltered", |
| 24 |
_stopEvent = function(evt){ |
| 25 |
try{ |
| 26 |
if(evt && evt.preventDefault){ |
| 27 |
event.stop(evt); |
| 28 |
} |
| 29 |
}catch(e){} |
| 30 |
}; |
| 31 |
|
| 32 |
return declare("dojox.grid.enhanced.plugins.filter.FilterBar", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin],{ |
| 33 |
|
| 34 |
|
| 35 |
templateString: template, |
| 36 |
|
| 37 |
widgetsInTemplate: true, |
| 38 |
|
| 39 |
_timeout_statusTooltip: 300, |
| 40 |
|
| 41 |
_handle_statusTooltip: null, |
| 42 |
|
| 43 |
_curColIdx: -1, |
| 44 |
|
| 45 |
plugin: null, |
| 46 |
|
| 47 |
postMixInProperties: function(){ |
| 48 |
var plugin = this.plugin; |
| 49 |
var nls = plugin.nls; |
| 50 |
this._filterBarDefBtnLabel = nls["filterBarDefButton"]; |
| 51 |
this._filterBarClearBtnLabel = nls["filterBarClearButton"]; |
| 52 |
this._closeFilterBarBtnLabel = nls["closeFilterBarBtn"]; |
| 53 |
var itemsName = plugin.args.itemsName || nls["defaultItemsName"]; |
| 54 |
this._noFilterMsg = string.substitute(nls["filterBarMsgNoFilterTemplate"], ["", itemsName]); |
| 55 |
|
| 56 |
var t = this.plugin.args.statusTipTimeout; |
| 57 |
if(typeof t == 'number'){ |
| 58 |
this._timeout_statusTooltip = t; |
| 59 |
} |
| 60 |
|
| 61 |
var g = plugin.grid; |
| 62 |
g.showFilterBar = lang.hitch(this, "showFilterBar"); |
| 63 |
g.toggleFilterBar = lang.hitch(this, "toggleFilterBar"); |
| 64 |
g.isFilterBarShown = lang.hitch(this, "isFilterBarShown"); |
| 65 |
}, |
| 66 |
postCreate: function(){ |
| 67 |
this.inherited(arguments); |
| 68 |
if(!this.plugin.args.closeFilterbarButton){ |
| 69 |
html.style(this.closeFilterBarButton.domNode, "display", "none"); |
| 70 |
} |
| 71 |
var _this = this, |
| 72 |
g = this.plugin.grid, |
| 73 |
old_func = this.oldGetHeaderHeight = lang.hitch(g,g._getHeaderHeight); |
| 74 |
|
| 75 |
this.placeAt(g.viewsHeaderNode, "after"); |
| 76 |
this.connect(this.plugin.filterDefDialog, "showDialog", "_onShowFilterDefDialog"); |
| 77 |
this.connect(this.plugin.filterDefDialog, "closeDialog", "_onCloseFilterDefDialog"); |
| 78 |
this.connect(g.layer("filter"), "onFiltered", this._onFiltered); |
| 79 |
|
| 80 |
this.defineFilterButton.domNode.title = this.plugin.nls["filterBarDefButton"]; |
| 81 |
if(html.hasClass(win.body(), "dijit_a11y")){ |
| 82 |
this.defineFilterButton.set("label", this.plugin.nls["a11yFilterBarDefButton"]); |
| 83 |
} |
| 84 |
this.connect(this.defineFilterButton.domNode, "click", _stopEvent); |
| 85 |
this.connect(this.clearFilterButton.domNode, "click", _stopEvent); |
| 86 |
this.connect(this.closeFilterBarButton.domNode, "click", _stopEvent); |
| 87 |
|
| 88 |
this.toggleClearFilterBtn(true); |
| 89 |
this._initAriaInfo(); |
| 90 |
|
| 91 |
|
| 92 |
g._getHeaderHeight = function(){ |
| 93 |
return old_func() + html.marginBox(_this.domNode).h; |
| 94 |
}; |
| 95 |
|
| 96 |
g.focus.addArea({ |
| 97 |
name: "filterbar", |
| 98 |
onFocus: lang.hitch(this, this._onFocusFilterBar, false), |
| 99 |
onBlur: lang.hitch(this, this._onBlurFilterBar) |
| 100 |
}); |
| 101 |
g.focus.placeArea("filterbar","after","header"); |
| 102 |
}, |
| 103 |
uninitialize: function(){ |
| 104 |
var g = this.plugin.grid; |
| 105 |
g._getHeaderHeight = this.oldGetHeaderHeight; |
| 106 |
g.focus.removeArea("filterbar"); |
| 107 |
this.plugin = null; |
| 108 |
}, |
| 109 |
isFilterBarShown: function(){ |
| 110 |
return html.style(this.domNode, "display") != "none"; |
| 111 |
}, |
| 112 |
showFilterBar: function(toShow, useAnim, animArgs){ |
| 113 |
var g = this.plugin.grid; |
| 114 |
if(useAnim){ |
| 115 |
if(Boolean(toShow) == this.isFilterBarShown()){ return; } |
| 116 |
animArgs = animArgs || {}; |
| 117 |
var anims = [], defaultDuration = 500; |
| 118 |
anims.push(fx[toShow ? "wipeIn" : "wipeOut"](lang.mixin({ |
| 119 |
"node": this.domNode, |
| 120 |
"duration": defaultDuration |
| 121 |
}, animArgs))); |
| 122 |
var curHeight = g.views.views[0].domNode.offsetHeight; |
| 123 |
var prop = { |
| 124 |
"duration": defaultDuration, |
| 125 |
"properties": { |
| 126 |
"height": { |
| 127 |
"end": lang.hitch(this, function(){ |
| 128 |
var barHeight = this.domNode.scrollHeight; |
| 129 |
if(has('ff')){ |
| 130 |
barHeight -= 2; |
| 131 |
} |
| 132 |
return toShow ? (curHeight - barHeight) : (curHeight + barHeight); |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
}; |
| 137 |
array.forEach(g.views.views, function(view){ |
| 138 |
anims.push(baseFx.animateProperty(lang.mixin({ |
| 139 |
"node": view.domNode |
| 140 |
}, prop, animArgs)), baseFx.animateProperty(lang.mixin({ |
| 141 |
"node": view.scrollboxNode |
| 142 |
}, prop, animArgs))); |
| 143 |
}); |
| 144 |
anims.push(baseFx.animateProperty(lang.mixin({ |
| 145 |
"node": g.viewsNode |
| 146 |
}, prop, animArgs))); |
| 147 |
fx.combine(anims).play(); |
| 148 |
}else{ |
| 149 |
html.style(this.domNode, "display", toShow ? "" : "none"); |
| 150 |
g.update(); |
| 151 |
} |
| 152 |
}, |
| 153 |
toggleFilterBar: function(useAnim, animArgs){ |
| 154 |
this.showFilterBar(!this.isFilterBarShown(), useAnim, animArgs); |
| 155 |
}, |
| 156 |
getColumnIdx: function(coordX){ |
| 157 |
var headers = query("[role='columnheader']", this.plugin.grid.viewsHeaderNode); |
| 158 |
var idx = -1; |
| 159 |
for(var i = headers.length - 1; i >= 0; --i){ |
| 160 |
var coord = html.position(headers[i]); |
| 161 |
if(coordX >= coord.x && coordX < coord.x + coord.w){ |
| 162 |
idx = i; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
if(idx >= 0 && this.plugin.grid.layout.cells[idx].filterable !== false){ |
| 167 |
return idx; |
| 168 |
}else{ |
| 169 |
return -1; |
| 170 |
} |
| 171 |
}, |
| 172 |
toggleClearFilterBtn: function(toHide){ |
| 173 |
html.style(this.clearFilterButton.domNode, "display", toHide ? "none" : ""); |
| 174 |
}, |
| 175 |
_closeFilterBar: function(e){ |
| 176 |
_stopEvent(e); |
| 177 |
var rulesCnt = this.plugin.filterDefDialog.getCriteria(); |
| 178 |
if(rulesCnt){ |
| 179 |
var handle = connect.connect(this.plugin.filterDefDialog, "clearFilter", this, function(){ |
| 180 |
this.showFilterBar(false, true); |
| 181 |
connect.disconnect(handle); |
| 182 |
}); |
| 183 |
this._clearFilterDefDialog(e); |
| 184 |
}else{ |
| 185 |
this.showFilterBar(false, true); |
| 186 |
} |
| 187 |
}, |
| 188 |
|
| 189 |
_showFilterDefDialog: function(e){ |
| 190 |
_stopEvent(e); |
| 191 |
this.plugin.filterDefDialog.showDialog(this._curColIdx); |
| 192 |
this.plugin.grid.focus.focusArea("filterbar"); |
| 193 |
}, |
| 194 |
_clearFilterDefDialog: function(e){ |
| 195 |
_stopEvent(e); |
| 196 |
this.plugin.filterDefDialog.onClearFilter(); |
| 197 |
this.plugin.grid.focus.focusArea("filterbar"); |
| 198 |
}, |
| 199 |
_onEnterButton: function(e){ |
| 200 |
|
| 201 |
|
| 202 |
this._onBlurFilterBar(); |
| 203 |
_stopEvent(e); |
| 204 |
}, |
| 205 |
_onMoveButton: function(e){ |
| 206 |
this._onBlurFilterBar(); |
| 207 |
}, |
| 208 |
_onLeaveButton: function(e){ |
| 209 |
this._leavingBtn = true; |
| 210 |
}, |
| 211 |
_onShowFilterDefDialog: function(colIdx){ |
| 212 |
if(typeof colIdx == "number"){ |
| 213 |
this._curColIdx = colIdx; |
| 214 |
} |
| 215 |
this._defPaneIsShown = true; |
| 216 |
}, |
| 217 |
_onCloseFilterDefDialog: function(){ |
| 218 |
this._defPaneIsShown = false; |
| 219 |
|
| 220 |
this._curColIdx = -1; |
| 221 |
dijitFocus.focus(this.defineFilterButton.domNode); |
| 222 |
}, |
| 223 |
_onClickFilterBar: function(e){ |
| 224 |
_stopEvent(e); |
| 225 |
this._clearStatusTipTimeout(); |
| 226 |
this.plugin.grid.focus.focusArea("filterbar"); |
| 227 |
this.plugin.filterDefDialog.showDialog(this.getColumnIdx(e.clientX)); |
| 228 |
}, |
| 229 |
_onMouseEnter: function(e){ |
| 230 |
this._onFocusFilterBar(true, null); |
| 231 |
this._updateTipPosition(e); |
| 232 |
this._setStatusTipTimeout(); |
| 233 |
}, |
| 234 |
_onMouseMove: function(e){ |
| 235 |
if(this._leavingBtn){ |
| 236 |
this._onFocusFilterBar(true, null); |
| 237 |
this._leavingBtn = false; |
| 238 |
} |
| 239 |
if(this._isFocused){ |
| 240 |
this._setStatusTipTimeout(); |
| 241 |
this._highlightHeader(this.getColumnIdx(e.clientX)); |
| 242 |
if(this._handle_statusTooltip){ |
| 243 |
this._updateTipPosition(e); |
| 244 |
} |
| 245 |
} |
| 246 |
}, |
| 247 |
_onMouseLeave: function(e){ |
| 248 |
this._onBlurFilterBar(); |
| 249 |
}, |
| 250 |
_updateTipPosition: function(evt){ |
| 251 |
this._tippos = { |
| 252 |
x: evt.pageX, |
| 253 |
y: evt.pageY |
| 254 |
}; |
| 255 |
}, |
| 256 |
_onFocusFilterBar: function(highlightOnly, evt, step){ |
| 257 |
if(!this.isFilterBarShown()){ |
| 258 |
return false; |
| 259 |
} |
| 260 |
this._isFocused = true; |
| 261 |
html.addClass(this.domNode,_focusClass); |
| 262 |
if(!highlightOnly){ |
| 263 |
var hasFilter = html.style(this.clearFilterButton.domNode, "display") !== "none"; |
| 264 |
var hasCloseButton = html.style(this.closeFilterBarButton.domNode, "display") !== "none"; |
| 265 |
if(typeof this._focusPos == "undefined"){ |
| 266 |
if(step > 0){ |
| 267 |
this._focusPos = 0; |
| 268 |
}else{ |
| 269 |
if(hasCloseButton){ |
| 270 |
this._focusPos = 1; |
| 271 |
}else{ |
| 272 |
this._focusPos = 0; |
| 273 |
} |
| 274 |
if(hasFilter){ |
| 275 |
++this._focusPos; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
if(this._focusPos === 0){ |
| 280 |
dijitFocus.focus(this.defineFilterButton.focusNode); |
| 281 |
}else if(this._focusPos === 1 && hasFilter){ |
| 282 |
dijitFocus.focus(this.clearFilterButton.focusNode); |
| 283 |
}else{ |
| 284 |
dijitFocus.focus(this.closeFilterBarButton.focusNode); |
| 285 |
} |
| 286 |
} |
| 287 |
_stopEvent(evt); |
| 288 |
return true; |
| 289 |
}, |
| 290 |
_onBlurFilterBar: function(evt, step){ |
| 291 |
if(this._isFocused){ |
| 292 |
this._isFocused = false; |
| 293 |
html.removeClass(this.domNode,_focusClass); |
| 294 |
this._clearStatusTipTimeout(); |
| 295 |
this._clearHeaderHighlight(); |
| 296 |
} |
| 297 |
var toBlur = true; |
| 298 |
if(step){ |
| 299 |
var buttonCount = 3; |
| 300 |
if(html.style(this.closeFilterBarButton.domNode, "display") === "none"){ |
| 301 |
--buttonCount; |
| 302 |
} |
| 303 |
if(html.style(this.clearFilterButton.domNode, "display") === "none"){ |
| 304 |
--buttonCount; |
| 305 |
} |
| 306 |
if(buttonCount == 1){ |
| 307 |
delete this._focusPos; |
| 308 |
}else{ |
| 309 |
var current = this._focusPos; |
| 310 |
for(var next = current + step; next < 0; next += buttonCount){} |
| 311 |
next %= buttonCount; |
| 312 |
if((step > 0 && next < current) || (step < 0 && next > current)){ |
| 313 |
delete this._focusPos; |
| 314 |
}else{ |
| 315 |
this._focusPos = next; |
| 316 |
toBlur = false; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
return toBlur; |
| 321 |
}, |
| 322 |
_onFiltered: function(filteredSize,originSize){ |
| 323 |
var p = this.plugin, |
| 324 |
itemsName = p.args.itemsName || p.nls["defaultItemsName"], |
| 325 |
msg = "", g = p.grid, |
| 326 |
filterLayer = g.layer("filter"); |
| 327 |
if(filterLayer.filterDef()){ |
| 328 |
msg = string.substitute(p.nls["filterBarMsgHasFilterTemplate"], [filteredSize, originSize, itemsName]); |
| 329 |
html.addClass(this.domNode, _filteredClass); |
| 330 |
}else{ |
| 331 |
msg = string.substitute(p.nls["filterBarMsgNoFilterTemplate"], [originSize, itemsName]); |
| 332 |
html.removeClass(this.domNode, _filteredClass); |
| 333 |
} |
| 334 |
this.statusBarNode.innerHTML = msg; |
| 335 |
this._focusPos = 0; |
| 336 |
}, |
| 337 |
_initAriaInfo: function(){ |
| 338 |
this.defineFilterButton.domNode.setAttribute("aria-label", this.plugin.nls["waiFilterBarDefButton"]); |
| 339 |
this.clearFilterButton.domNode.setAttribute("aria-label", this.plugin.nls["waiFilterBarClearButton"]); |
| 340 |
}, |
| 341 |
_isInColumn: function(mousePos_x, headerNode, colIndex){ |
| 342 |
var coord = html.position(headerNode); |
| 343 |
return mousePos_x >= coord.x && mousePos_x < coord.x + coord.w; |
| 344 |
}, |
| 345 |
_setStatusTipTimeout: function(){ |
| 346 |
this._clearStatusTipTimeout(); |
| 347 |
if(!this._defPaneIsShown){ |
| 348 |
this._handle_statusTooltip = setTimeout(lang.hitch(this,this._showStatusTooltip),this._timeout_statusTooltip); |
| 349 |
} |
| 350 |
}, |
| 351 |
_clearStatusTipTimeout: function(){ |
| 352 |
clearTimeout(this._handle_statusTooltip); |
| 353 |
this._handle_statusTooltip = null; |
| 354 |
}, |
| 355 |
_showStatusTooltip: function(){ |
| 356 |
this._handle_statusTooltip = null; |
| 357 |
if(this.plugin){ |
| 358 |
this.plugin.filterStatusTip.showDialog(this._tippos.x, this._tippos.y, this.getColumnIdx(this._tippos.x)); |
| 359 |
} |
| 360 |
}, |
| 361 |
_highlightHeader: function(colIdx){ |
| 362 |
if(colIdx != this._previousHeaderIdx){ |
| 363 |
var g = this.plugin.grid, |
| 364 |
cell = g.getCell(this._previousHeaderIdx); |
| 365 |
if(cell){ |
| 366 |
html.removeClass(cell.getHeaderNode(), "dojoxGridCellOver"); |
| 367 |
} |
| 368 |
cell = g.getCell(colIdx); |
| 369 |
if(cell){ |
| 370 |
html.addClass(cell.getHeaderNode(), "dojoxGridCellOver"); |
| 371 |
} |
| 372 |
this._previousHeaderIdx = colIdx; |
| 373 |
} |
| 374 |
}, |
| 375 |
_clearHeaderHighlight: function(){ |
| 376 |
if(typeof this._previousHeaderIdx != "undefined"){ |
| 377 |
var g = this.plugin.grid, |
| 378 |
cell = g.getCell(this._previousHeaderIdx); |
| 379 |
if(cell){ |
| 380 |
g.onHeaderCellMouseOut({ |
| 381 |
cellNode: cell.getHeaderNode() |
| 382 |
}); |
| 383 |
} |
| 384 |
delete this._previousHeaderIdx; |
| 385 |
} |
| 386 |
} |
| 387 |
}); |
| 388 |
}); |