suggest¶
-
class
webix.ui.
suggest
(data)¶ Arguments: - data (object) – A configuration object
Suggest view.
References¶
- views
popup()
.- helpers
_event()
,bind()
,copy()
,delay()
,extend()
,isUndefined()
,protoUI()
,template()
,toFunctor()
,toNode()
.
External references¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | webix.protoUI({
name:"suggest",
defaults:{
autofocus:false,
type:"list",
keyPressTimeout:1,
body:{
yCount:10,
autoheight:true,
body:true,
select:true,
borderless:true,
navigation:true
},
filter:function(item,value){
if (item.value.toString().toLowerCase().indexOf(value.toLowerCase())===0) return true;
return false;
}
},
template_setter:webix.template,
filter_setter:function(value){
return webix.toFunctor(value, this.$scope);
},
$init:function(obj){
var temp = {};
webix.extend(temp, webix.copy(this.defaults.body));
temp.view = obj.type || this.defaults.type;
var etemp = this._get_extendable_cell(temp);
if (obj.body)
webix.extend(etemp, obj.body, true);
if (obj.data)
etemp.data = obj.data;
if (obj.url)
etemp.url = obj.url;
if (obj.datatype)
etemp.datatype = obj.datatype;
if (obj.id)
temp.id = temp.id || (obj.id+"_"+temp.view);
obj.body = temp;
this.$ready.push(this._set_on_popup_click);
this.attachEvent("onShow", function(){
if (this._settings.master){
var master = webix.$$(this._settings.master);
if(master){
var node = master._getInputDiv ? master._getInputDiv() : master.getInputNode();
node.setAttribute("aria-expanded", "true");
}
}
this._show_selection();
});
this.attachEvent("onHide", function(){
if (this._settings.master){
var master = webix.$$(this._settings.master);
if(master){
var node = master._getInputDiv ? master._getInputDiv() : master.getInputNode();
node.setAttribute("aria-expanded", "false");
}
}
});
this._old_text = {};
},
_get_extendable_cell:function(obj){
return obj;
},
_preselectMasterOption: function(data){
var master, node, text;
if (data){
if (this._settings.master){
master = webix.$$(this._settings.master);
node = master.getInputNode();
if(node && master.$setValueHere){
master.$setValueHere(data.value);
}
else if (node){
if(master.options_setter)
text = this.getItemText(data.id);
else if(data.value && master._get_visible_text)
text = master._get_visible_text(data.value);
if (webix.isUndefined(node.value))
node.innerHTML = text;
else
node.value = text.replace(/<[^>]*>/g,"");
}
}
}
node = node || this._last_input_target;
if(node)
node.focus();
},
setMasterValue:function(data, refresh){
var text = data.id ? this.getItemText(data.id) : (data.text||data.value);
if (this._settings.master){
var master = webix.$$(this._settings.master);
if (refresh && data.id)
master.refresh();
else if (master.options_setter)
master.setValue(data.$empty?"":data.id);
else if(master.setValueHere)
master.setValueHere(text);
else
master.setValue(text);
} else if (this._last_input_target){
this._last_input_target.value = text;
}
if (!refresh){
this.hide(true);
if (this._last_input_target)
this._last_input_target.focus();
}
this.callEvent("onValueSuggest", [data, text]);
webix.delay(function(){
webix.callEvent("onEditEnd",[]);
});
},
getMasterValue:function(){
if (this._settings.master)
return webix.$$(this._settings.master).getValue();
return null;
},
getItemText:function(id){
var item = this.getList().getItem(id);
if (!item)
return this._old_text[id] || "";
if (this._settings.template)
return this._settings.template.call(this, item, this.type);
if (this._settings.textValue)
return item[this._settings.textValue];
var type = this.getList().type;
var text = type.template.call(type, item, type);
return (this._old_text[id] = text);
},
getSuggestion:function(){
var id,
list = this.getList(),
order = list.data.order;
if (list.getSelectedId)
id = list.getSelectedId();
if (order.length && (!id || order.find(id) <0) )
id = order[0];
//complex id in datatable
if (id && typeof id == "object") id = id+"";
return id;
},
getList:function(){
return this._body_cell;
},
_set_on_popup_click:function(){
var list = this.getList();
var type = this._settings.type;
if (list.count){
list.attachEvent("onItemClick", webix.bind(function(item){
this.setMasterValue(list.getItem(item));
}, this));
list.data.attachEvent("onstoreupdated",webix.bind(function(id, obj, mode){
if (mode == "delete" && id == this.getMasterValue())
this.setMasterValue({ id:"", text:"" }, 1);
else if (mode == "update" && id == this.getMasterValue()){
this.setMasterValue(obj, 1);
}
}, this));
list.data.attachEvent("onAfterFilter", webix.bind(this._suggest_after_filter, this));
list.data.attachEvent("onStoreLoad", webix.bind(this._suggest_after_filter, this));
if (webix.isUndefined(this._settings.fitMaster))
this._settings.fitMaster = true;
} else if (type == "calendar"){
list.attachEvent("onDateSelect", function(date){
this.getParentView().setMasterValue({ value:date});
});
list.attachEvent("onTodaySet", function(date){
this.getParentView().setMasterValue({ value:date});
});
list.attachEvent("onDateClear", function(date){
this.getParentView().setMasterValue({ value:date});
});
} else if (type == "colorboard"){
list.attachEvent("onItemClick", function(value){
this.getParentView().setMasterValue({ value:value });
});
}
},
input_setter: function(value) {
this.linkInput(value);
return 0;
},
linkInput: function(input){
var node;
if (input.getInputNode){
node = input.getInputNode();
node.webix_master_id = input._settings.id;
} else
node = webix.toNode(input);
webix._event(node,"keydown",function(e){
if (node != document.body || this.isVisible())
this._suggestions(e);
},{bind:this});
if(input._getInputDiv)
node = input._getInputDiv();
node.setAttribute("aria-autocomplete", "list");
node.setAttribute("aria-expanded", "false");
if(node.tagName === "DIV"){
node.setAttribute("aria-live", "assertive");
node.setAttribute("aria-atomic", "true");
}
this._non_ui_mode = true;
},
_suggestions: function(e){
e = (e||event);
var list = this.getList();
var trg = e.target||e.srcElement;
this._last_input_target = trg;
this._settings.master = trg.webix_master_id;
window.clearTimeout(this._key_timer);
var code = e.keyCode;
//shift and ctrl
if (code == 16 || code == 17) return;
// tab - hide popup and do nothing
if (code == 9)
return this._tab_key(this,list);
// escape - hide popup
if (code == 27)
return this._escape_key(this,list);
// enter
if (code == 13)
return this.$enterKey(this,list);
// up/down/right/left are used for navigation
if (this._navigate(e)) {
webix.html.preventEvent(e);
return false;
}
if (webix.isUndefined(trg.value)) return;
clearTimeout(this._last_delay);
this._last_delay = webix.delay(function(){
//focus moved to the different control, suggest is not necessary
if (!this._non_ui_mode &&
webix.UIManager.getFocus() != webix.$$(this._settings.master)) return;
this._resolve_popup = true;
//for multicombo
var val = trg.value;
// used to prevent showing popup when it was initialized
if (list.config.dataFeed)
list.filter("value", val);
else if (list.filter){
list.filter(webix.bind(function(item){
return this._settings.filter.call(this,item,val);
}, this));
}
},this, [], this._settings.keyPressTimeout);
},
_suggest_after_filter: function() {
if (!this._resolve_popup) return true;
this._resolve_popup = false;
var list = this.getList();
// filtering is complete
// if there are as min 1 variant it must be shown, hidden otherwise
if (list.count() >0){
this.adjust();
if(!this.isVisible())
this._dont_unfilter = true;
this.show(this._last_input_target,null,true);
this._dont_unfilter = false;
} else {
this.hide(true);
this._last_input_target = null;
}
},
show:function(node){
if (!this.isVisible()){
var list = this.getList();
if (list.filter && !this._dont_unfilter){
list.filter("");
}
if(this.$customWidth){
this.$customWidth(node);
}
if (node.tagName && this._settings.fitMaster){
this._settings.width = node.offsetWidth -2 ; //2 - borders
}
if (list._zoom_level)
list.render();
this.adjust();
// needed to return focus
if(node.tagName == "INPUT")
this._last_input_target = node;
}
webix.ui.popup.prototype.show.apply(this, arguments);
},
_show_selection:function(list){
list = list||this.getList();
var value = this.getMasterValue();
if( list.select && list.showItem ){
if (value && list.exists && list.exists(value)){
list.select(value);
list.showItem(value);
}
else{
list.unselect();
list.showItem(list.getFirstId());
}
}
else if(list.setValue){
if (this._settings.master)
value = webix.$$(this._settings.master).$prepareValue(value);
list.setValue(value);
}
},
$enterKey: function(popup,list) {
var value;
if (popup.isVisible()) {
if (list.count && list.count()){
value = list.getSelectedId(false, true);
if(list.count()==1 && list.getFirstId()!=value)
value = list.getFirstId();
if(value)
value = list.getItem(value);
}
else if(list.getSelectedDate && list.getSelectedDate())
value = { value:list.getSelectedDate() };
else if(list.getValue && list.getValue())
value = {value: list.getValue() };
if (value)
this.setMasterValue(value);
popup.hide(true);
}
else
popup.show(this._last_input_target);
},
_escape_key: function(popup, list) {
return popup.hide(true);
},
_tab_key: function(popup, list) {
return popup.hide(true);
},
/*! suggestions navigation: up/down buttons move selection
* @param e
* event object
**/
_navigate: function(e) {
var list = this.getList();
var code = e.keyCode;
var data;
if( list.moveSelection && code < 41 && code > 32 && !e.ctrlKey && !e.metaKey && !e.shiftKey && !e.altKey) {
// down arrow
if (code === 40 ) {
var visible = this.isVisible();
if (!visible)
this.show(this._last_input_target);
list.moveSelection("down");
}// other arrows
else {
var master = this._settings.master;
if((list.count && code !==38) || (!list.count && !list.isVisible()))
return false;
var dir;
if(code == 33) dir = "pgup";
if(code == 34) dir = "pgdown";
if(code == 35) dir = "bottom";
if(code == 36) dir = "top";
if(code == 37) dir = "left";
if(code == 38) dir = "up";
if(code == 39) dir = "right";
list.moveSelection(dir);
}
if(list.count)
data = list.getSelectedItem();
else if(list.getSelectedDate)
data = { value:list.getVisibleDate()};
else if(list.getValue)
data = { value:list.getValue() };
this._preselectMasterOption(data);
return true;
}
return false;
},
getValue:function(){
var list = this.getList();
var value = (list.getValue ? list.getValue() : list.getSelectedId()) || "";
value = value.id || value;
// check empty
if(list.getItem){
var item = list.getItem(value);
if(item && item.$empty)
return "";
}
return value;
},
setValue:function(value){
var list = this.getList();
if(value){
if(list.exists(value)){
list.select(value);
list.showItem(value);
}
}else{
list.unselect();
list.showItem(list.getFirstId());
}
}
}, webix.ui.popup);
/*aria-style handling for options of multiple-value controls (radio, segmented, tabbar)*/
|