2018-12-12 23:23:13 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS Widget base class
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
et2_core_inputWidget;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* et2_editableWidget derives from et2_inputWidget and adds the ability to start
|
|
|
|
* readonly, then turn editable on double-click. If we decide to do this with
|
|
|
|
* more widgets, it should just be merged with et2_inputWidget.
|
|
|
|
*
|
|
|
|
* @augments et2_inputWidget
|
|
|
|
*/
|
2019-08-22 22:20:55 +02:00
|
|
|
var et2_editableWidget = (function(){ "use strict"; return et2_inputWidget.extend(et2_ISubmitListener,
|
2018-12-12 23:23:13 +01:00
|
|
|
{
|
|
|
|
attributes: {
|
|
|
|
readonly: {
|
|
|
|
name: "readonly",
|
|
|
|
type: "string", // | boolean
|
|
|
|
default: false,
|
|
|
|
description: "If set to 'editable' will start readonly, double clicking will make it editable and clicking out will save"
|
|
|
|
},
|
2019-10-17 18:31:57 +02:00
|
|
|
toggle_readonly: {
|
|
|
|
name: "toggle_readonly",
|
|
|
|
type: "boolean",
|
|
|
|
default: true,
|
|
|
|
description: "Double clicking makes widget editable. If off, must be made editable in some other way."
|
|
|
|
},
|
2018-12-12 23:23:13 +01:00
|
|
|
save_callback: {
|
|
|
|
name: "save_callback",
|
|
|
|
type: "string",
|
|
|
|
default: et2_no_init,
|
|
|
|
description: "Ajax callback to save changed value when readonly is 'editable'. If not provided, a regular submit is done."
|
|
|
|
},
|
|
|
|
save_callback_params: {
|
|
|
|
name: "readonly",
|
|
|
|
type: "string",
|
|
|
|
default: et2_no_init,
|
|
|
|
description: "Additional parameters passed to save_callback"
|
2019-02-27 11:00:53 +01:00
|
|
|
},
|
|
|
|
editable_height: {
|
|
|
|
name: "Editable height",
|
|
|
|
description: "Set height for widget while in edit mode",
|
|
|
|
type: "string"
|
2018-12-12 23:23:13 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_inputWidget
|
|
|
|
*/
|
|
|
|
init: function(_parent, _attrs) {
|
|
|
|
// 'Editable' really should be boolean for everything else to work
|
|
|
|
if(_attrs.readonly && typeof _attrs.readonly === 'string')
|
|
|
|
{
|
|
|
|
_attrs.readonly = true;
|
2019-10-17 18:31:57 +02:00
|
|
|
this._toggle_readonly = _attrs.toggle_readonly;
|
2018-12-12 23:23:13 +01:00
|
|
|
}
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
jQuery(node).off('.et2_editableWidget');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the validation errors from the server
|
|
|
|
*
|
|
|
|
* @param {object} _attrs
|
|
|
|
*/
|
|
|
|
transformAttributes: function(_attrs) {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
attachToDOM: function() {
|
|
|
|
this._super.apply(this,arguments);
|
|
|
|
var node = this.getDOMNode();
|
|
|
|
if (node && this._toggle_readonly)
|
|
|
|
{
|
|
|
|
jQuery(node)
|
|
|
|
.off('.et2_editableWidget')
|
|
|
|
.on("dblclick.et2_editableWidget", this, function(e) {
|
|
|
|
e.data.dblclick.call(e.data, this);
|
|
|
|
})
|
|
|
|
.addClass('et2_clickable et2_editable');
|
|
|
|
}
|
2019-11-11 14:09:35 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
jQuery(node).addClass('et2_editable_readonly');
|
|
|
|
}
|
2018-12-12 23:23:13 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
detatchFromDOM: function() {
|
|
|
|
this._super.apply(this,arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle double click
|
|
|
|
*
|
|
|
|
* Turn widget editable
|
|
|
|
*
|
|
|
|
* @param {DOMNode} _node
|
|
|
|
*/
|
|
|
|
dblclick: function (_node) {
|
|
|
|
// Turn off readonly
|
|
|
|
this.set_readonly(false);
|
|
|
|
|
|
|
|
jQuery('body').on("click.et2_editableWidget", this, function(e) {
|
|
|
|
// Make sure click comes from body, not a popup
|
2019-04-15 12:35:34 +02:00
|
|
|
if(jQuery.contains(this, e.target) && e.target.type != 'textarea')
|
2018-12-12 23:23:13 +01:00
|
|
|
{
|
|
|
|
jQuery(this).off("click.et2_editableWidget");
|
|
|
|
e.data.focusout.call(e.data, this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User clicked somewhere else, save and turn back to readonly
|
|
|
|
*
|
|
|
|
* @param {DOMNode} _node Body node
|
|
|
|
* @returns {et2_core_editableWidgetet2_editableWidget.et2_core_editableWidgetAnonym$0@call;getInstanceManager@call;submit}
|
|
|
|
*/
|
|
|
|
focusout: function (_node)
|
|
|
|
{
|
|
|
|
var value = this.get_value();
|
|
|
|
var oldValue = this._oldValue;
|
|
|
|
|
|
|
|
// Change back to readonly
|
|
|
|
this.set_readonly(true);
|
|
|
|
|
|
|
|
// No change, do nothing
|
|
|
|
if(value == oldValue) return;
|
|
|
|
|
2019-08-22 22:20:55 +02:00
|
|
|
|
2018-12-12 23:23:13 +01:00
|
|
|
// Submit
|
|
|
|
if(this.options.save_callback)
|
|
|
|
{
|
|
|
|
var params = [value];
|
|
|
|
if(this.options.save_callback_params)
|
|
|
|
{
|
|
|
|
params = params.concat(this.options.save_callback_params.split(','));
|
|
|
|
}
|
|
|
|
|
|
|
|
egw.json(this.options.save_callback, params, function() {
|
|
|
|
}, this, true, this).sendRequest();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-22 22:20:55 +02:00
|
|
|
this.set_value(value);
|
2018-12-12 23:23:13 +01:00
|
|
|
return this.getInstanceManager().submit();
|
|
|
|
}
|
2019-08-22 22:20:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever the template gets submitted.
|
|
|
|
* If we have a save_callback, we call that before the submit (no check on
|
|
|
|
* the result)
|
|
|
|
*
|
|
|
|
* @param _values contains the values which will be sent to the server.
|
|
|
|
* Listeners may change these values before they get submitted.
|
|
|
|
*/
|
|
|
|
submit: function(_values) {
|
|
|
|
if(this.options.readonly)
|
|
|
|
{
|
|
|
|
// Not currently editing, just continue on
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Change back to readonly
|
|
|
|
this.set_readonly(true);
|
2018-12-12 23:23:13 +01:00
|
|
|
|
2019-08-22 22:20:55 +02:00
|
|
|
var params = [this.get_value()];
|
|
|
|
if(this.options.save_callback_params)
|
|
|
|
{
|
|
|
|
params = params.concat(this.options.save_callback_params.split(','));
|
|
|
|
}
|
|
|
|
if(this.options.save_callback)
|
|
|
|
{
|
|
|
|
egw.json(this.options.save_callback, params, function() {
|
|
|
|
}, this, true, this).sendRequest();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-12 23:23:13 +01:00
|
|
|
});}).call(this);
|
|
|
|
|