mirror of
https://github.com/Karaka-Management/jsOMS.git
synced 2026-01-11 09:58:39 +00:00
152 lines
4.7 KiB
JavaScript
152 lines
4.7 KiB
JavaScript
/**
|
|
* Form manager class.
|
|
*
|
|
* @author OMS Development Team <dev@oms.com>
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
* @copyright 2013 Dennis Eichhorn
|
|
* @license OMS License 1.0
|
|
* @version 1.0.0 * @since 1.0.0
|
|
*/
|
|
(function (jsOMS)
|
|
{
|
|
"use strict";
|
|
|
|
/** @namespace jsOMS.UI.Input*/
|
|
jsOMS.Autoloader.defineNamespace('jsOMS.UI.Input');
|
|
|
|
/**
|
|
* Unbind input element
|
|
*
|
|
* @param {Object} input Input element
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
jsOMS.UI.Input.unbind = function (input)
|
|
{
|
|
this.app.inputManager.getKeyboardManager().unbind(input);
|
|
input.removeEventListener('change', changeBind, false);
|
|
};
|
|
|
|
/**
|
|
* Bind input elment
|
|
*
|
|
* @param {Object} input Input elment
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
jsOMS.UI.Input.bind = function (input)
|
|
{
|
|
let self = this;
|
|
|
|
input.addEventListener('change', function changeBind(event)
|
|
{
|
|
/* Handle remote datalist/autocomplete input element */
|
|
let listId, list;
|
|
if (typeof (listId = this.getAttribute('list')) !== 'undefined' && (list = document.getElementById(listId)).getAttribute('data-list-src') !== 'undefined') {
|
|
self.addRemoteDatalistOptions(this, list);
|
|
}
|
|
|
|
/* Handle html defined functions */
|
|
let change;
|
|
if (typeof (change = this.getAttribute('data-change-func')) !== 'undefined') {
|
|
change(this);
|
|
}
|
|
|
|
/* Handle pre-defined dynamic change events */
|
|
let ref;
|
|
if (typeof (ref = this.getAttribute('data-ref')) !== 'undefined') {
|
|
let e = document.getElementById(ref);
|
|
|
|
switch (e.tagName) {
|
|
case 'ul':
|
|
break;
|
|
case 'table':
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
let dataButton;
|
|
if (typeof (dataButton = input.getAttribute('data-button')) !== 'undefined') {
|
|
this.app.inputManager.getKeyboardManager().bind(input, 13, function ()
|
|
{
|
|
document.getElementById(dataButton).click();
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Add remote datalist options
|
|
*
|
|
* This only applies for datalists that have remote options
|
|
*
|
|
* @param {Object} input Input elment
|
|
* @param {Object} datalist Datalist elment
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
jsOMS.UI.Input.addRemoteDatalistOptions = function (input, datalist)
|
|
{
|
|
this.clearDatalistOptions(datalist);
|
|
|
|
let request = new jsOMS.Message.Request();
|
|
request.setData(input.value);
|
|
request.setType(jsOMS.Message.Response.ResponseType.JSON);
|
|
request.setUri(datalist.getAttribute('data-list-src'));
|
|
request.setMethod(jsOMS.Message.Request.RequestMethod.POST);
|
|
request.setRequestHeader('Content-Type', 'application/json');
|
|
request.setSuccess(function (xhr)
|
|
{
|
|
try {
|
|
let o = JSON.parse(xhr.response),
|
|
response = new jsOMS.Message.Response(o),
|
|
responseLength = response.count(),
|
|
tempResponse = null,
|
|
success = null;
|
|
|
|
for (let k = 0; k < responseLength; k++) {
|
|
tempResponse = response.getByIndex(k);
|
|
console.log(tempResponse);
|
|
|
|
let option = null,
|
|
data = tempResponse.getData(),
|
|
length = data.length;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
option = document.createElement('option');
|
|
option.value = tempResponse.value;
|
|
option.text = tempResponse.text;
|
|
|
|
datalist.appendChild(option);
|
|
}
|
|
}
|
|
} catch (exception) {
|
|
self.app.logger.error('Invalid JSON object: ' + xhr, 'FormManager');
|
|
return false;
|
|
}
|
|
});
|
|
|
|
request.send();
|
|
};
|
|
|
|
/**
|
|
* Remove all datalist options from datalist
|
|
*
|
|
* @param {Object} datalist Datalist elment
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
jsOMS.UI.Input.clearDatalistOptions = function (datalist)
|
|
{
|
|
let length = datalist.options.length;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
datalist.remove(0);
|
|
}
|
|
};
|
|
}(window.jsOMS = window.jsOMS || {}));
|