2012-03-23 00:31:31 +01:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
|
|
|
|
* EGroupware eTemplate2 - JS Ajax select / auto complete object
|
2012-03-23 00:31:31 +01:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
2021-06-07 17:33:53 +02:00
|
|
|
* @link https://www.egroupware.org
|
2012-03-23 00:31:31 +01:00
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2012
|
|
|
|
*/
|
|
|
|
/*egw:uses
|
2020-02-13 15:39:10 +01:00
|
|
|
/vendor/bower-asset/jquery/dist/jquery.js;
|
|
|
|
/vendor/bower-asset/jquery-ui/jquery-ui.js;
|
|
|
|
et2_core_inputWidget;
|
|
|
|
et2_core_valueWidget;
|
2012-03-23 00:31:31 +01:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
import { et2_register_widget } from "./et2_core_widget";
|
|
|
|
import { et2_inputWidget } from "./et2_core_inputWidget";
|
|
|
|
import { ClassWithAttributes } from "./et2_core_inheritance";
|
|
|
|
import { et2_valueWidget } from "./et2_core_valueWidget";
|
|
|
|
import { et2_selectbox } from "./et2_widget_selectbox";
|
2012-03-23 00:31:31 +01:00
|
|
|
/**
|
|
|
|
* Using AJAX, this widget allows a type-ahead find similar to a ComboBox, where as the user enters information,
|
|
|
|
* a drop-down box is populated with the n closest matches. If the user clicks on an item in the drop-down, that
|
|
|
|
* value is selected.
|
|
|
|
* n is the maximum number of results set in the user's preferences.
|
|
|
|
* The user is restricted to selecting values in the list.
|
|
|
|
* This widget can get data from any function that can provide data to a nextmatch widget.
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_inputWidget
|
2016-02-29 21:40:43 +01:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_ajaxSelect extends et2_inputWidget {
|
2020-02-13 15:39:10 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_ajaxSelect
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
constructor(_parent, _attrs, _child) {
|
2020-02-13 15:39:10 +01:00
|
|
|
// Call the inherited constructor
|
2021-06-07 17:33:53 +02:00
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_ajaxSelect._attributes, _child || {}));
|
|
|
|
this.input = null;
|
2020-02-13 15:39:10 +01:00
|
|
|
if (typeof _attrs.get_rows == 'string') {
|
2021-06-07 17:33:53 +02:00
|
|
|
_attrs.get_rows = this.egw().link('/index.php', {
|
|
|
|
menuaction: this.options.get_rows
|
2020-02-13 15:39:10 +01:00
|
|
|
});
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
this.createInputWidget();
|
|
|
|
this.input = null;
|
|
|
|
this.createInputWidget();
|
2020-02-13 15:39:10 +01:00
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
createInputWidget() {
|
2020-02-13 15:39:10 +01:00
|
|
|
this.input = jQuery(document.createElement("input"));
|
|
|
|
this.input.addClass("et2_textbox");
|
|
|
|
this.setDOMNode(this.input[0]);
|
2021-06-07 17:33:53 +02:00
|
|
|
let widget = this;
|
2020-02-13 15:39:10 +01:00
|
|
|
this.input.autocomplete({
|
|
|
|
delay: 100,
|
|
|
|
source: this.options.get_rows ?
|
|
|
|
this.options.get_rows :
|
2021-06-07 17:33:53 +02:00
|
|
|
et2_selectbox.find_select_options(this, this.options.values),
|
2020-02-13 15:39:10 +01:00
|
|
|
select: function (event, ui) {
|
|
|
|
widget.value = ui.item[widget.options.id_field];
|
|
|
|
if (widget.options.get_title) {
|
|
|
|
if (typeof widget.options.get_title == 'function') {
|
|
|
|
widget.input.val(widget.options.get_title.call(widget.value));
|
|
|
|
}
|
|
|
|
else if (typeof widget.options.get_title == 'string') {
|
|
|
|
// TODO: Server side callback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget.input.val(ui.item.label);
|
|
|
|
}
|
|
|
|
// Prevent default action of setting field to the value
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
getValue() {
|
2020-02-13 15:39:10 +01:00
|
|
|
if (this.options.blur && this.input.val() == this.options.blur)
|
|
|
|
return "";
|
|
|
|
return this.value;
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
set_value(_value) {
|
2020-02-13 15:39:10 +01:00
|
|
|
this.value = _value;
|
|
|
|
if (this.input.autocomplete('instance')) {
|
2021-06-07 17:33:53 +02:00
|
|
|
let source = this.input.autocomplete('option', 'source');
|
2020-02-13 15:39:10 +01:00
|
|
|
if (typeof source == 'object') {
|
2021-06-07 17:33:53 +02:00
|
|
|
for (let i in source) {
|
2020-02-13 15:39:10 +01:00
|
|
|
if (typeof source[i].value != 'undefined' && typeof source[i].label != 'undefined' && source[i].value === _value) {
|
|
|
|
this.input.val(source[i].label);
|
|
|
|
}
|
|
|
|
else if (typeof source[i] == 'string') {
|
|
|
|
this.input.val(source[_value]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeof source == 'function') {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
set_blur(_value) {
|
2020-02-13 15:39:10 +01:00
|
|
|
if (_value) {
|
|
|
|
this.input.attr("placeholder", _value + ""); // HTML5
|
|
|
|
if (!this.input[0]["placeholder"]) {
|
|
|
|
// Not HTML5
|
|
|
|
if (this.input.val() == "")
|
|
|
|
this.input.val(this.options.blur);
|
|
|
|
this.input.focus(this, function (e) {
|
|
|
|
if (e.data.input.val() == e.data.options.blur)
|
|
|
|
e.data.input.val("");
|
|
|
|
}).blur(this, function (e) {
|
|
|
|
if (e.data.input.val() == "")
|
|
|
|
e.data.input.val(e.data.options.blur);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.input.removeAttr("placeholder");
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
et2_ajaxSelect._attributes = {
|
|
|
|
'get_rows': {
|
|
|
|
"name": "Data source",
|
|
|
|
"type": "any",
|
|
|
|
"default": "",
|
|
|
|
"description": "Function to get search results, either a javascript function or server-side."
|
|
|
|
},
|
|
|
|
'get_title': {
|
|
|
|
"name": "Title function",
|
|
|
|
"type": "any",
|
|
|
|
"default": "",
|
|
|
|
"description": "Function to get title for selected entry. Used when closed, and if no template is given."
|
|
|
|
},
|
|
|
|
'id_field': {
|
|
|
|
"name": "Result ID field",
|
|
|
|
"type": "string",
|
|
|
|
"default": "value",
|
|
|
|
"description": "Which key in result sub-array to look for row ID. If omitted, the key for the row will be used."
|
|
|
|
},
|
|
|
|
'template': {
|
|
|
|
"name": "Row template",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "ID of the template to use to display rows. If omitted, title will be shown for each result."
|
|
|
|
},
|
|
|
|
'filter': {
|
|
|
|
"name": "Filter",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "Apply filter to search results. Same as nextmatch."
|
|
|
|
},
|
|
|
|
'filter2': {
|
|
|
|
"name": "Filter 2",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "Apply filter to search results. Same as nextmatch."
|
|
|
|
},
|
|
|
|
'link': {
|
|
|
|
"name": "Read only link",
|
|
|
|
"type": "boolean",
|
|
|
|
"default": "true",
|
|
|
|
"description": "If readonly, widget will be text. If link is set, widget will be a link."
|
|
|
|
},
|
|
|
|
// Pass by code only
|
|
|
|
'values': {
|
|
|
|
"name": "Values",
|
|
|
|
"type": "any",
|
|
|
|
"default": {},
|
|
|
|
"description": "Specify the available options. Use this, or Data source."
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_ajaxSelect, ["ajax_select"]);
|
2012-03-23 00:31:31 +01:00
|
|
|
/**
|
2020-02-13 15:39:10 +01:00
|
|
|
* et2_textbox_ro is the dummy readonly implementation of the textbox.
|
|
|
|
* @augments et2_valueWidget
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_ajaxSelect_ro extends et2_valueWidget {
|
2020-02-13 15:39:10 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_ajaxSelect_ro
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
constructor(_parent, _attrs, _child) {
|
2020-02-13 15:39:10 +01:00
|
|
|
// Call the inherited constructor
|
2021-06-07 17:33:53 +02:00
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_ajaxSelect_ro._attributes, _child || {}));
|
|
|
|
this.value = "";
|
|
|
|
this.span = jQuery(document.createElement("span"));
|
|
|
|
this.setDOMNode(this.span[0]);
|
2020-02-13 15:39:10 +01:00
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
set_value(_value) {
|
2020-02-13 15:39:10 +01:00
|
|
|
this.value = _value;
|
|
|
|
if (!_value)
|
|
|
|
_value = "";
|
|
|
|
this.span.text(_value);
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
2020-02-13 15:39:10 +01:00
|
|
|
/**
|
|
|
|
* Code for implementing et2_IDetachedDOM
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
getDetachedAttributes(_attrs) {
|
2020-02-13 15:39:10 +01:00
|
|
|
_attrs.push("value");
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
getDetachedNodes() {
|
2020-02-13 15:39:10 +01:00
|
|
|
return [this.span[0]];
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
setDetachedAttributes(_nodes, _values) {
|
2020-02-13 15:39:10 +01:00
|
|
|
this.span = jQuery(_nodes[0]);
|
|
|
|
if (typeof _values["value"] != 'undefined') {
|
|
|
|
this.set_value(_values["value"]);
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Ignore all more advanced attributes.
|
|
|
|
*/
|
|
|
|
et2_ajaxSelect_ro._attributes = {
|
|
|
|
"multiline": {
|
|
|
|
"ignore": true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_ajaxSelect_ro, ["ajax_select_ro"]);
|
2020-02-13 15:39:10 +01:00
|
|
|
//# sourceMappingURL=et2_widget_ajaxSelect.js.map
|