2011-08-07 15:43:46 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Button object
|
2011-08-07 15:43:46 +02:00
|
|
|
*
|
|
|
|
* @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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
jquery.jquery;
|
2011-08-25 15:35:53 +02:00
|
|
|
et2_core_interfaces;
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_baseWidget;
|
2011-08-07 15:43:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which implements the "button" XET-Tag
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_baseWidget
|
2011-08-07 15:43:46 +02:00
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_button = et2_baseWidget.extend([et2_IInput, et2_IDetachedDOM],
|
|
|
|
{
|
2011-08-10 16:36:31 +02:00
|
|
|
attributes: {
|
|
|
|
"label": {
|
|
|
|
"name": "caption",
|
|
|
|
"type": "string",
|
2011-08-23 19:05:05 +02:00
|
|
|
"description": "Label of the button",
|
|
|
|
"translate": true
|
2011-08-15 11:24:32 +02:00
|
|
|
},
|
2011-09-14 20:37:38 +02:00
|
|
|
"image": {
|
|
|
|
"name": "Icon",
|
|
|
|
"type": "string",
|
|
|
|
"description": "Use an icon instead of label (when available)"
|
|
|
|
},
|
2012-03-23 20:25:50 +01:00
|
|
|
"ro_image": {
|
|
|
|
"name": "Read-only Icon",
|
|
|
|
"type": "string",
|
|
|
|
"description": "Use this icon instead of hiding for read-only"
|
|
|
|
},
|
2011-08-15 11:24:32 +02:00
|
|
|
"onclick": {
|
|
|
|
"name": "onclick",
|
2013-10-09 16:35:03 +02:00
|
|
|
"type": "js",
|
2011-08-15 11:24:32 +02:00
|
|
|
"description": "JS code which gets executed when the button is clicked"
|
2012-03-23 20:25:50 +01:00
|
|
|
},
|
2012-07-24 01:54:16 +02:00
|
|
|
"accesskey": {
|
|
|
|
"name": "Access Key",
|
|
|
|
"type": "string",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Alt + <key> activates widget"
|
|
|
|
},
|
|
|
|
"tabindex": {
|
|
|
|
"name": "Tab index",
|
|
|
|
"type": "integer",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Specifies the tab order of a widget when the 'tab' button is used for navigating."
|
|
|
|
},
|
2012-03-23 20:25:50 +01:00
|
|
|
// No such thing as a required button
|
|
|
|
"needed": {
|
|
|
|
"ignore": true,
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
},
|
2011-08-07 15:43:46 +02:00
|
|
|
|
2012-03-27 17:05:30 +02:00
|
|
|
legacyOptions: ["image", "ro_image"],
|
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_button
|
|
|
|
*/
|
2011-08-23 17:05:13 +02:00
|
|
|
init: function() {
|
2011-08-10 16:36:31 +02:00
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this.label = "";
|
2011-08-15 16:29:58 +02:00
|
|
|
this.clicked = false;
|
2011-08-16 14:31:18 +02:00
|
|
|
this.btn = null;
|
2012-03-05 22:40:44 +01:00
|
|
|
this.image = null;
|
2011-08-10 16:36:31 +02:00
|
|
|
|
2012-03-23 20:25:50 +01:00
|
|
|
if (this.options.image || this.options.ro_image)
|
2012-03-08 18:10:26 +01:00
|
|
|
{
|
|
|
|
this.image = jQuery(document.createElement("img"))
|
|
|
|
.addClass("et2_button et2_button_icon");
|
|
|
|
this.setDOMNode(this.image[0]);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-23 17:05:13 +02:00
|
|
|
if (!this.options.readonly)
|
2011-08-16 14:31:18 +02:00
|
|
|
{
|
|
|
|
this.btn = $j(document.createElement("button"))
|
2011-09-22 21:10:21 +02:00
|
|
|
.addClass("et2_button et2_button_text");
|
2011-08-16 14:31:18 +02:00
|
|
|
this.setDOMNode(this.btn[0]);
|
|
|
|
}
|
2011-08-07 15:43:46 +02:00
|
|
|
},
|
|
|
|
|
2012-07-24 01:54:16 +02:00
|
|
|
set_accesskey: function(key) {
|
|
|
|
jQuery(this.node).attr("accesskey", key);
|
|
|
|
},
|
2013-04-12 19:19:18 +02:00
|
|
|
/**
|
|
|
|
* Set image and update current image
|
|
|
|
*
|
|
|
|
* @param _image
|
|
|
|
*/
|
|
|
|
set_image: function(_image) {
|
|
|
|
this.options.image = _image;
|
|
|
|
this.update_image();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set readonly image and update current image
|
|
|
|
*
|
|
|
|
* @param _image
|
|
|
|
*/
|
2012-03-23 20:25:50 +01:00
|
|
|
set_ro_image: function(_image) {
|
2013-04-12 19:19:18 +02:00
|
|
|
this.options.ro_image = _image;
|
|
|
|
this.update_image();
|
2012-03-23 20:25:50 +01:00
|
|
|
},
|
2013-04-12 19:19:18 +02:00
|
|
|
/**
|
|
|
|
* Set current image (dont update options.image)
|
|
|
|
*
|
|
|
|
* @param _image
|
|
|
|
*/
|
|
|
|
update_image: function(_image) {
|
2012-03-08 18:10:26 +01:00
|
|
|
if(!this.isInTree() || this.image == null) return;
|
2013-04-12 19:19:18 +02:00
|
|
|
|
|
|
|
if (typeof _image == 'undefined')
|
|
|
|
_image = this.options.readonly ? this.options.ro_image : this.options.image;
|
2011-09-22 23:23:30 +02:00
|
|
|
|
2012-03-23 20:25:50 +01:00
|
|
|
// Silently blank for percentages instead of warning about missing image - use a progress widget
|
2013-07-08 21:40:17 +02:00
|
|
|
if(_image.match(/^[0-9]+\%$/))
|
2012-03-23 20:25:50 +01:00
|
|
|
{
|
|
|
|
_image = "";
|
|
|
|
//this.egw().debug("warn", "Use a progress widget instead of percentage images", this);
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:37:38 +02:00
|
|
|
var found_image = false;
|
2013-04-12 19:19:18 +02:00
|
|
|
if(_image != "")
|
2011-09-14 20:37:38 +02:00
|
|
|
{
|
2012-03-08 18:10:26 +01:00
|
|
|
var src = this.egw().image(_image);
|
|
|
|
if(src)
|
2011-09-14 20:37:38 +02:00
|
|
|
{
|
2012-03-08 18:10:26 +01:00
|
|
|
this.image.attr("src", src);
|
|
|
|
found_image = true;
|
2011-09-14 20:37:38 +02:00
|
|
|
}
|
2012-03-08 18:10:26 +01:00
|
|
|
// allow url's too
|
|
|
|
else if (_image[0] == '/' || _image.substr(0,4) == 'http')
|
|
|
|
{
|
|
|
|
this.image.attr('src', _image);
|
|
|
|
found_image = true;
|
2011-09-22 23:23:30 +02:00
|
|
|
}
|
2011-09-14 20:37:38 +02:00
|
|
|
}
|
2011-09-22 23:23:30 +02:00
|
|
|
if(!found_image)
|
2011-09-14 20:37:38 +02:00
|
|
|
{
|
2012-03-05 22:40:44 +01:00
|
|
|
this.set_label(this.label);
|
2011-09-14 20:37:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-12 19:19:18 +02:00
|
|
|
/**
|
|
|
|
* Set options.readonly and update image
|
|
|
|
*
|
|
|
|
* @param boolean _ro
|
|
|
|
*/
|
|
|
|
set_readonly: function(_ro)
|
|
|
|
{
|
|
|
|
if (_ro != this.options.readonly)
|
|
|
|
{
|
|
|
|
this.options.readonly = _ro;
|
|
|
|
|
|
|
|
if (this.image)
|
|
|
|
{
|
|
|
|
this.update_image();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-09-14 20:37:38 +02:00
|
|
|
getDOMNode: function() {
|
2012-03-08 18:10:26 +01:00
|
|
|
return this.btn ? this.btn[0] : (this.image ? this.image[0] : null);
|
2011-09-14 20:37:38 +02:00
|
|
|
},
|
|
|
|
|
2013-10-09 16:35:03 +02:00
|
|
|
/**
|
|
|
|
* Overwritten to maintain an internal clicked attribute
|
|
|
|
*
|
|
|
|
* @param _ev
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
click: function(_ev) {
|
2012-04-18 00:56:04 +02:00
|
|
|
this.clicked = true;
|
|
|
|
|
2013-10-09 16:35:03 +02:00
|
|
|
if (!this._super.apply(this, arguments))
|
2011-08-15 11:24:32 +02:00
|
|
|
{
|
2013-10-09 16:35:03 +02:00
|
|
|
this.clicked = false;
|
|
|
|
return false;
|
2011-08-15 11:24:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-15 16:29:58 +02:00
|
|
|
// Submit the form
|
2011-08-21 15:24:20 +02:00
|
|
|
if (this._type != "buttononly")
|
|
|
|
{
|
2011-11-09 22:34:02 +01:00
|
|
|
this.getInstanceManager().submit(this); //TODO: this only needs to be passed if it's in a datagrid
|
2011-08-21 15:24:20 +02:00
|
|
|
}
|
2012-04-18 00:56:04 +02:00
|
|
|
this.clicked = false;
|
2013-10-09 16:35:03 +02:00
|
|
|
return true;
|
2011-08-15 11:24:32 +02:00
|
|
|
},
|
|
|
|
|
2011-08-07 15:43:46 +02:00
|
|
|
set_label: function(_value) {
|
2011-08-16 14:31:18 +02:00
|
|
|
if (this.btn)
|
2011-08-07 15:43:46 +02:00
|
|
|
{
|
|
|
|
this.label = _value;
|
|
|
|
|
|
|
|
this.btn.text(_value);
|
|
|
|
}
|
2012-03-08 18:10:26 +01:00
|
|
|
if(this.image)
|
|
|
|
{
|
2012-04-04 22:25:20 +02:00
|
|
|
this.image.attr("alt", _value);
|
|
|
|
// Don't set title if there's a tooltip, browser may show both
|
|
|
|
if(!this.options.statustext)
|
|
|
|
{
|
|
|
|
this.image.attr("title",_value);
|
|
|
|
}
|
2012-03-08 18:10:26 +01:00
|
|
|
}
|
2011-08-15 16:29:58 +02:00
|
|
|
},
|
|
|
|
|
2012-07-24 01:54:16 +02:00
|
|
|
/**
|
|
|
|
* Set tab index
|
|
|
|
*/
|
|
|
|
set_tabindex: function(index) {
|
|
|
|
jQuery(this.btn).attr("tabindex", index);
|
|
|
|
},
|
2011-08-15 18:03:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the et2_IInput interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always return false as a button is never dirty
|
|
|
|
*/
|
2011-08-15 16:29:58 +02:00
|
|
|
isDirty: function() {
|
2011-08-15 18:03:53 +02:00
|
|
|
return false;
|
2011-08-15 16:29:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
resetDirty: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
getValue: function() {
|
|
|
|
if (this.clicked)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-15 18:03:53 +02:00
|
|
|
|
|
|
|
// If "null" is returned, the result is not added to the submitted
|
|
|
|
// array.
|
2011-08-15 16:29:58 +02:00
|
|
|
return null;
|
2011-09-14 20:37:38 +02:00
|
|
|
},
|
2013-06-26 22:50:10 +02:00
|
|
|
isValid: function() {
|
|
|
|
return true;
|
|
|
|
},
|
2011-08-07 15:43:46 +02:00
|
|
|
|
2011-09-14 20:37:38 +02:00
|
|
|
/**
|
|
|
|
* et2_IDetachedDOM
|
|
|
|
*/
|
|
|
|
getDetachedAttributes: function(_attrs)
|
|
|
|
{
|
2012-03-23 20:25:50 +01:00
|
|
|
_attrs.push("label", "value", "class", "image", "ro_image","onclick" );
|
2011-09-14 20:37:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getDetachedNodes: function()
|
|
|
|
{
|
2012-03-08 18:10:26 +01:00
|
|
|
return [
|
|
|
|
this.btn != null ? this.btn[0] : null,
|
|
|
|
this.image != null ? this.image[0] : null
|
|
|
|
];
|
2011-09-14 20:37:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setDetachedAttributes: function(_nodes, _values)
|
|
|
|
{
|
2012-03-08 18:10:26 +01:00
|
|
|
// Datagrid puts in the row for null
|
2012-03-27 17:05:30 +02:00
|
|
|
this.btn = _nodes[0].nodeName[0] != '#' ? jQuery(_nodes[0]) : null;
|
2012-03-08 18:10:26 +01:00
|
|
|
this.image = jQuery(_nodes[1]);
|
2011-09-21 23:07:21 +02:00
|
|
|
|
2011-09-14 20:37:38 +02:00
|
|
|
if (typeof _values["id"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_id(_values["id"]);
|
|
|
|
}
|
2011-09-28 01:44:07 +02:00
|
|
|
if (typeof _values["label"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_label(_values["label"]);
|
|
|
|
}
|
2011-09-14 20:37:38 +02:00
|
|
|
if (typeof _values["value"] != "undefined")
|
|
|
|
{
|
|
|
|
}
|
2012-03-05 22:40:44 +01:00
|
|
|
if (typeof _values["image"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_image(_values["image"]);
|
|
|
|
}
|
2012-03-23 20:25:50 +01:00
|
|
|
if (typeof _values["ro_image"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_ro_image(_values["ro_image"]);
|
|
|
|
}
|
2011-09-14 20:37:38 +02:00
|
|
|
if (typeof _values["class"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_class(_values["class"]);
|
|
|
|
}
|
2011-09-21 23:07:21 +02:00
|
|
|
|
2011-09-28 01:44:07 +02:00
|
|
|
if (typeof _values["onclick"] != "undefined")
|
2011-09-21 23:07:21 +02:00
|
|
|
{
|
|
|
|
this.options.onclick = _values["onclick"];
|
|
|
|
}
|
2013-04-13 21:00:13 +02:00
|
|
|
var type = this._type;
|
2012-03-12 19:25:36 +01:00
|
|
|
var attrs = jQuery.extend(_values, this.options);
|
|
|
|
var parent = this._parent;
|
2012-03-08 18:10:26 +01:00
|
|
|
jQuery(this.getDOMNode()).bind("click.et2_baseWidget", this, function(e) {
|
2012-03-12 19:25:36 +01:00
|
|
|
var widget = et2_createWidget(type,attrs,parent);
|
|
|
|
e.data = widget;
|
2011-11-14 23:57:06 +01:00
|
|
|
e.data.set_id(_values["id"]);
|
2011-09-22 18:25:41 +02:00
|
|
|
return e.data.click.call(e.data,e);
|
|
|
|
});
|
2011-09-14 20:37:38 +02:00
|
|
|
}
|
2011-08-07 15:43:46 +02:00
|
|
|
});
|
2011-08-21 15:24:20 +02:00
|
|
|
et2_register_widget(et2_button, ["button", "buttononly"]);
|
2011-08-07 15:43:46 +02:00
|
|
|
|