2012-03-27 12:18:42 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Nextmatch object
|
2012-03-27 12:18:42 +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
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @copyright Stylite 2012
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default action for nextmatch rows, runs action specified _action.data.nm_action: see nextmatch_widget::egw_actions()
|
|
|
|
*
|
|
|
|
* @param _action action object with attributes caption, id, nm_action, ...
|
|
|
|
* @param _senders array of rows selected
|
|
|
|
*/
|
2012-03-29 16:11:22 +02:00
|
|
|
function nm_action(_action, _senders, _target, _ids)
|
2012-03-27 12:18:42 +02:00
|
|
|
{
|
|
|
|
// ignore checkboxes, unless they have an explicit defined nm_action
|
|
|
|
if (_action.checkbox && (!_action.data || typeof _action.data.nm_action == 'undefined')) return;
|
|
|
|
|
|
|
|
if (typeof _action.data == 'undefined' || !_action.data) _action.data = {};
|
2013-04-01 20:19:59 +02:00
|
|
|
if (typeof _action.data.nm_action == 'undefined' && _action.type == 'popup') _action.data.nm_action = 'submit';
|
2012-03-29 16:11:22 +02:00
|
|
|
|
2013-03-20 23:20:05 +01:00
|
|
|
if(typeof _ids == 'undefined')
|
|
|
|
{
|
|
|
|
// Get IDs from nextmatch - nextmatch is in the top of the action tree
|
|
|
|
// @see et2_extension_nextmatch_controller._initActions()
|
|
|
|
var nm = null;
|
2013-04-13 21:00:13 +02:00
|
|
|
var action = _action;
|
2013-03-20 23:20:05 +01:00
|
|
|
while(nm == null && action != null)
|
|
|
|
{
|
|
|
|
if(action.data != null && action.data.nextmatch)
|
|
|
|
{
|
|
|
|
nm = action.data.nextmatch;
|
|
|
|
}
|
2013-04-13 21:00:13 +02:00
|
|
|
action = action.parent;
|
2013-03-20 23:20:05 +01:00
|
|
|
}
|
|
|
|
if(nm)
|
|
|
|
{
|
|
|
|
_ids = nm.getSelection();
|
|
|
|
_action.data.nextmatch = nm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:11:22 +02:00
|
|
|
// ----------------------
|
|
|
|
// TODO: Parse the _ids.inverted flag!
|
|
|
|
// ----------------------
|
2012-03-30 13:43:39 +02:00
|
|
|
|
|
|
|
// Translate the internal uids back to server uids
|
2012-03-29 16:11:22 +02:00
|
|
|
var idsArr = _ids.ids;
|
2012-03-30 13:43:39 +02:00
|
|
|
for (var i = 0; i < idsArr.length; i++)
|
|
|
|
{
|
|
|
|
idsArr[i] = idsArr[i].split("::").pop();
|
|
|
|
}
|
2012-03-29 16:11:22 +02:00
|
|
|
|
2012-03-30 13:43:39 +02:00
|
|
|
// Calculate the ids parameters
|
2012-03-27 12:18:42 +02:00
|
|
|
var ids = "";
|
2012-03-29 16:11:22 +02:00
|
|
|
for (var i = 0; i < idsArr.length; i++)
|
2012-03-27 12:18:42 +02:00
|
|
|
{
|
2012-03-30 13:43:39 +02:00
|
|
|
var id = idsArr[i];
|
2012-03-27 15:12:51 +02:00
|
|
|
ids += (id.indexOf(',') >= 0 ? '"'+id.replace(/"/g,'""')+'"' : id) +
|
2012-03-29 16:11:22 +02:00
|
|
|
((i < idsArr.length - 1) ? "," : "");
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|
|
|
|
//console.log(_action); console.log(_senders);
|
|
|
|
|
|
|
|
var mgr = _action.getManager();
|
|
|
|
|
|
|
|
var select_all = mgr.getActionById("select_all");
|
2012-03-29 16:11:22 +02:00
|
|
|
var confirm_msg = (idsArr.length > 1 || select_all && select_all.checked) &&
|
2012-03-27 12:18:42 +02:00
|
|
|
typeof _action.data.confirm_multiple != 'undefined' ?
|
|
|
|
_action.data.confirm_multiple : _action.data.confirm;
|
|
|
|
|
|
|
|
// let user confirm the action first (if not select_all set and nm_action == 'submit' --> confirmed later)
|
|
|
|
if (!(select_all && select_all.checked && _action.data.nm_action == 'submit') &&
|
|
|
|
typeof _action.data.confirm != 'undefined')
|
|
|
|
{
|
|
|
|
if (!confirm(confirm_msg)) return;
|
|
|
|
}
|
|
|
|
// in case we only need to confirm multiple selected (only _action.data.confirm_multiple)
|
2012-03-29 16:11:22 +02:00
|
|
|
else if (typeof _action.data.confirm_multiple != 'undefined' && (idsArr.length > 1 || select_all && select_all.checked))
|
2012-03-27 12:18:42 +02:00
|
|
|
{
|
|
|
|
if (!confirm(_action.data.confirm_multiple)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = '#';
|
|
|
|
if (typeof _action.data.url != 'undefined')
|
|
|
|
{
|
|
|
|
url = _action.data.url.replace(/(\$|%24)id/,encodeURIComponent(ids));
|
|
|
|
}
|
|
|
|
|
|
|
|
var target = null;
|
|
|
|
if (typeof _action.data.target != 'undefined')
|
|
|
|
{
|
|
|
|
target = _action.data.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(_action.data.nm_action)
|
|
|
|
{
|
|
|
|
case 'alert':
|
|
|
|
alert(_action.caption + " (\'" + _action.id + "\') executed on rows: " + ids);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'location':
|
|
|
|
if (typeof _action.data.targetapp != 'undefined')
|
|
|
|
{
|
|
|
|
top.egw_appWindowOpen(_action.data.targetapp, url);
|
|
|
|
}
|
|
|
|
else if(target)
|
|
|
|
{
|
|
|
|
window.open(url, target);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.location.href = url;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'popup':
|
|
|
|
egw_openWindowCentered2(url,target,_action.data.width,_action.data.height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'egw_open':
|
|
|
|
var params = _action.data.egw_open.split('-'); // type-appname-idNum (idNum is part of id split by :), eg. "edit-infolog"
|
|
|
|
console.log(params);
|
2013-02-05 10:14:14 +01:00
|
|
|
var egw_open_id = idsArr[0];
|
2012-03-27 12:18:42 +02:00
|
|
|
if (typeof params[2] != 'undefined') egw_open_id = egw_open_id.split(':')[params[2]];
|
2012-05-22 17:40:10 +02:00
|
|
|
egw(params[1],window).open(egw_open_id,params[1],params[0],params[3],target);
|
2012-03-27 12:18:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'open_popup':
|
|
|
|
// open div styled as popup contained in current form and named action.id+'_popup'
|
|
|
|
if (nm_popup_action == null)
|
|
|
|
{
|
2012-04-18 00:56:04 +02:00
|
|
|
nm_open_popup(_action, _ids.ids);
|
2012-03-27 12:18:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through, if popup is open --> submit form
|
|
|
|
case 'submit':
|
|
|
|
// let user confirm select-all
|
|
|
|
if (select_all && select_all.checked)
|
|
|
|
{
|
|
|
|
if (!confirm((confirm_msg ? confirm_msg : _action.caption.replace(/^( | | )+/,''))+"\n\n"+select_all.hint)) return;
|
|
|
|
}
|
|
|
|
var checkboxes = mgr.getActionsByAttr("checkbox", true);
|
|
|
|
var checkboxes_elem = document.getElementById(mgr.etemplate_var_prefix+'[nm][checkboxes]');
|
|
|
|
if (checkboxes && checkboxes_elem)
|
|
|
|
for (var i in checkboxes)
|
|
|
|
checkboxes_elem.value += checkboxes[i].id + ":" + (checkboxes[i].checked ? "1" : "0") + ";";
|
|
|
|
|
2012-04-10 22:27:37 +02:00
|
|
|
var nextmatch = _action.data.nextmatch;
|
|
|
|
if(!nextmatch && _senders.length)
|
2012-03-27 12:18:42 +02:00
|
|
|
{
|
2012-04-10 22:27:37 +02:00
|
|
|
// Pull it from deep within, where it was stuffed in et2_dataview_controller_selection._attachActionObject()
|
|
|
|
nextmatch = _senders[0]._context._widget;
|
|
|
|
}
|
|
|
|
if(nextmatch)
|
|
|
|
{
|
|
|
|
// Fake a getValue() function
|
2013-08-19 22:23:26 +02:00
|
|
|
var old_value = nextmatch.getValue;
|
|
|
|
var value = nextmatch.getValue();
|
|
|
|
jQuery.extend(value, this.activeFilters, {
|
|
|
|
"selected": idsArr,
|
|
|
|
"checkboxes": checkboxes_elem ? checkboxes_elem.value : null
|
|
|
|
});
|
|
|
|
value[nextmatch.options.settings.action_var]= _action.id;
|
|
|
|
|
2012-04-10 22:27:37 +02:00
|
|
|
nextmatch.getValue = function() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-04-23 22:25:21 +02:00
|
|
|
if(_action.data.nm_action == 'open_popup')
|
2012-04-18 00:56:04 +02:00
|
|
|
{
|
2012-06-06 23:56:18 +02:00
|
|
|
// Force nextmatch to re-load affected rows
|
2013-05-22 20:10:26 +02:00
|
|
|
nextmatch.refresh(idsArr);
|
2012-06-06 23:56:18 +02:00
|
|
|
|
2012-04-18 00:56:04 +02:00
|
|
|
nextmatch.getInstanceManager().submit();
|
|
|
|
|
2013-08-19 22:23:26 +02:00
|
|
|
// Reset action in case there's another one
|
|
|
|
nextmatch.getValue = old_value;
|
2012-04-18 00:56:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Full POST
|
|
|
|
nextmatch.getInstanceManager().postSubmit();
|
|
|
|
}
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-10 22:27:37 +02:00
|
|
|
egw().debug("error", "Missing nextmatch widget, could not submit", _action);
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to check if a certain field (_action.data.fieldId) is (not) equal to given value (_action.data.fieldValue)
|
|
|
|
*
|
|
|
|
* If field is not found, we return false too!
|
|
|
|
*
|
|
|
|
* @param _action egwAction object, we use _action.data.fieldId to check agains _action.data.fieldValue
|
|
|
|
* @param _senders array of egwActionObject objects
|
|
|
|
* @param _target egwActionObject object, get's called for every object in _senders
|
|
|
|
* @returns boolean true if field found and has specified value, false otherwise
|
|
|
|
*/
|
|
|
|
function nm_compare_field(_action, _senders, _target)
|
|
|
|
{
|
2013-06-10 21:26:04 +02:00
|
|
|
var value = false;
|
|
|
|
|
|
|
|
// This probably won't work...
|
2012-03-27 12:18:42 +02:00
|
|
|
var field = document.getElementById(_action.data.fieldId);
|
|
|
|
|
2013-06-10 21:26:04 +02:00
|
|
|
// Use widget
|
|
|
|
if (!field)
|
|
|
|
{
|
|
|
|
var nextmatch = _action.data.nextmatch;
|
|
|
|
if(!nextmatch && _senders.length)
|
|
|
|
{
|
|
|
|
// Pull it from deep within, where it was stuffed in et2_dataview_controller_selection._attachActionObject()
|
|
|
|
nextmatch = _senders[0]._context._widget;
|
|
|
|
}
|
|
|
|
if(!nextmatch) return false;
|
|
|
|
|
|
|
|
field = nextmatch.getWidgetById(_action.data.fieldId);
|
|
|
|
value = field.getValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = $j(field).val();
|
|
|
|
}
|
2012-03-27 12:18:42 +02:00
|
|
|
if (!field) return false;
|
|
|
|
|
|
|
|
|
|
|
|
if (_action.data.fieldValue.substr(0,1) == '!')
|
|
|
|
return value != _action.data.fieldValue.substr(1);
|
|
|
|
|
|
|
|
return value == _action.data.fieldValue;
|
|
|
|
}
|
|
|
|
|
2012-03-29 16:11:22 +02:00
|
|
|
// TODO: This code is rather suboptimal! No global variables as this code will
|
|
|
|
// run in a global context
|
|
|
|
var nm_popup_action, nm_popup_ids = null;
|
2012-03-27 12:18:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open popup for a certain action requiring further input
|
|
|
|
*
|
|
|
|
* Popup needs to have eTemplate name of action id plus "_popup"
|
|
|
|
*
|
|
|
|
* @param _action
|
2012-03-29 16:11:22 +02:00
|
|
|
* @param _ids
|
2012-03-27 12:18:42 +02:00
|
|
|
*/
|
2012-03-29 16:11:22 +02:00
|
|
|
function nm_open_popup(_action, _ids)
|
2012-03-27 12:18:42 +02:00
|
|
|
{
|
2013-08-21 00:23:18 +02:00
|
|
|
// Find the popup div
|
|
|
|
var popup = jQuery("#"+(_action.data.nextmatch.getInstanceManager().uniqueId||"") + "_"+_action.id+"_popup").first() || jQuery("[id*='" + _action.id + "_popup']").first();
|
2012-03-27 12:18:42 +02:00
|
|
|
if (popup) {
|
|
|
|
nm_popup_action = _action;
|
2012-04-24 21:47:23 +02:00
|
|
|
if(_ids.length && typeof _ids[0] == 'object')
|
|
|
|
{
|
2012-07-23 19:58:04 +02:00
|
|
|
egw().debug("warn", 'Not proper format for IDs, should be array',_ids);
|
2012-04-24 21:47:23 +02:00
|
|
|
_action.data.nextmatch = _ids[0]._context._widget;
|
|
|
|
nm_popup_ids = [];
|
|
|
|
for(var i = 0; i < _ids.length; i++)
|
|
|
|
{
|
|
|
|
nm_popup_ids.push(_ids[i].id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nm_popup_ids = _ids;
|
|
|
|
}
|
2012-05-03 00:33:46 +02:00
|
|
|
|
2012-04-18 00:56:04 +02:00
|
|
|
var dialog = jQuery('.action_popup-content',popup);
|
2012-05-30 00:23:02 +02:00
|
|
|
if(dialog.length == 0)
|
|
|
|
{
|
|
|
|
// Couldn't get the dialog, use the div less the first (header) & last (buttons) nodes
|
|
|
|
dialog = jQuery(document.createElement('div'))
|
|
|
|
.addClass('action_popup-content');
|
|
|
|
if(popup.children().length == 1)
|
|
|
|
{
|
|
|
|
dialog.append(popup.children().children().slice(1,popup.children().children().length-1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog.append(popup.children().slice(1,popup.children().length-1));
|
|
|
|
}
|
|
|
|
dialog.appendTo(popup);
|
|
|
|
}
|
|
|
|
if(dialog.length == 1)
|
2012-05-03 00:33:46 +02:00
|
|
|
{
|
2013-08-21 00:23:18 +02:00
|
|
|
// Set up the buttons
|
2012-05-03 00:33:46 +02:00
|
|
|
var dialog_parent = dialog.parent();
|
|
|
|
var d_buttons = [];
|
|
|
|
var action = _action;
|
|
|
|
jQuery('button',popup).each(function(index) {
|
|
|
|
var but = jQuery(this);
|
2013-08-21 00:23:18 +02:00
|
|
|
if(but.attr("id"))
|
|
|
|
{
|
|
|
|
// Find the associated widget
|
|
|
|
var widget_id = but.attr("id").replace(_action.data.nextmatch.getInstanceManager().uniqueId+'_', '');
|
|
|
|
var button = nm_popup_action.data.nextmatch.getRoot().getWidgetById(widget_id);
|
|
|
|
}
|
2012-05-03 00:33:46 +02:00
|
|
|
d_buttons.push({
|
|
|
|
text: but.text(),
|
2013-08-21 00:23:18 +02:00
|
|
|
click: button && button.onclick ? function(e) {
|
2012-05-03 00:33:46 +02:00
|
|
|
dialog.dialog("close");
|
|
|
|
nm_popup_action = action;
|
|
|
|
button.onclick.apply(button, e.currentTarget);
|
|
|
|
} : function(e) {
|
|
|
|
dialog.dialog("close");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
dialog.dialog({
|
|
|
|
title: jQuery('.promptheader',popup).text(),
|
|
|
|
modal: true,
|
|
|
|
buttons: d_buttons,
|
|
|
|
close: function(event, ui) {
|
2012-04-18 00:56:04 +02:00
|
|
|
// Need to destroy the dialog, etemplate widget needs divs back where they were
|
|
|
|
dialog.dialog("destroy");
|
2012-05-03 00:33:46 +02:00
|
|
|
|
|
|
|
// Put it back where it came from, or et2 will error when clear() is called
|
|
|
|
dialog.appendTo(dialog_parent);
|
|
|
|
}
|
2012-04-18 00:56:04 +02:00
|
|
|
});
|
2012-05-03 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset global variables
|
|
|
|
nm_popup_action = null;
|
|
|
|
nm_popup_senders = null;
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a popup action
|
|
|
|
*/
|
|
|
|
function nm_submit_popup(button)
|
|
|
|
{
|
2012-04-10 22:27:37 +02:00
|
|
|
if(button.form)
|
|
|
|
{
|
|
|
|
button.form.submit_button.value = button.name; // set name of button (sub-action)
|
|
|
|
}
|
2012-04-18 00:56:04 +02:00
|
|
|
else if (nm_popup_action.data.nextmatch)
|
|
|
|
{
|
2013-08-21 00:23:18 +02:00
|
|
|
// Find the associated widget
|
|
|
|
var widget_id = $j(button).attr("id").replace(nm_popup_action.data.nextmatch.getInstanceManager().uniqueId+'_', '');
|
|
|
|
nm_popup_action.data.nextmatch.getRoot().getWidgetById(widget_id).clicked = true;
|
2012-04-18 00:56:04 +02:00
|
|
|
}
|
2012-03-27 12:18:42 +02:00
|
|
|
|
2012-04-10 22:27:37 +02:00
|
|
|
// Mangle senders to get IDs where nm_action() wants them
|
|
|
|
// No idea why this is needed
|
|
|
|
var ids = {ids:[]};
|
|
|
|
for(var i in nm_popup_ids)
|
|
|
|
{
|
2012-04-18 00:56:04 +02:00
|
|
|
ids.ids.push(nm_popup_ids[i]);
|
2012-04-10 22:27:37 +02:00
|
|
|
}
|
2012-03-27 12:18:42 +02:00
|
|
|
// call regular nm_action to transmit action and senders correct
|
2012-04-18 00:56:04 +02:00
|
|
|
nm_action(nm_popup_action,nm_popup_ids, button, ids);
|
2012-04-24 21:47:23 +02:00
|
|
|
|
|
|
|
nm_hide_popup(button, null);
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide popup
|
|
|
|
*/
|
|
|
|
function nm_hide_popup(element, div_id)
|
|
|
|
{
|
|
|
|
var prefix = element.id.substring(0,element.id.indexOf('['));
|
2012-05-02 21:37:24 +02:00
|
|
|
var popup = div_id ? document.getElementById(div_id) : jQuery("#"+prefix+"_popup").get(0) || jQuery("[id*='" + prefix + "_popup']").get(0);
|
2012-03-27 12:18:42 +02:00
|
|
|
|
|
|
|
// Hide popup
|
|
|
|
if(popup) {
|
|
|
|
popup.style.display = 'none';
|
|
|
|
}
|
|
|
|
nm_popup_action = null;
|
|
|
|
nm_popup_senders = null;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate/click first link in row
|
|
|
|
*/
|
|
|
|
function nm_activate_link(_action, _senders)
|
|
|
|
{
|
2012-03-29 13:52:56 +02:00
|
|
|
$j(_senders[0].iface.getDOMNode()).find('.et2_clickable:first').trigger('click');
|
2012-03-27 12:18:42 +02:00
|
|
|
}
|