2013-10-04 09:29:38 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS toolbar object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2013
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
jquery.jquery;
|
|
|
|
jquery.jquery-ui;
|
|
|
|
et2_DOMWidget;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This toolbar gets its contents from its actions
|
2014-01-06 17:45:54 +01:00
|
|
|
*
|
2013-10-04 09:29:38 +02:00
|
|
|
* @augments et2_valueWidget
|
2014-01-06 17:45:54 +01:00
|
|
|
*/
|
2013-10-04 09:29:38 +02:00
|
|
|
var et2_toolbar = et2_DOMWidget.extend(
|
|
|
|
{
|
|
|
|
attributes: {
|
2014-02-05 17:04:58 +01:00
|
|
|
"view_range": {
|
|
|
|
"name": "View range",
|
|
|
|
"type": "string",
|
2014-02-10 15:10:17 +01:00
|
|
|
"default": "5",
|
2014-02-05 17:04:58 +01:00
|
|
|
"description": "Define minimum action view range to show actions by both icons and caption"
|
2014-02-07 11:33:05 +01:00
|
|
|
}
|
2013-10-04 09:29:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default buttons, so there is something for the widget browser / editor to show
|
|
|
|
*/
|
|
|
|
default_toolbar: {
|
2014-02-10 16:43:51 +01:00
|
|
|
view: {caption:'View', icons: {primary: 'ui-icon-check'}, group:1, toolbarDefault:true},
|
|
|
|
edit: {caption:'Edit', group:1, toolbarDefault:true},
|
|
|
|
save: {caption:'Save', group:2, toolbarDefault:true}
|
2013-10-04 09:29:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2014-01-06 17:45:54 +01:00
|
|
|
*
|
2013-10-04 09:29:38 +02:00
|
|
|
* @memberOf et2_dropdown_button
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
this.div = $j(document.createElement('div'))
|
|
|
|
.addClass('et2_toolbar ui-widget-header ui-corner-all');
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2014-01-13 11:26:20 +01:00
|
|
|
//actionbox is the div for stored actions
|
|
|
|
this.actionbox = $j(document.createElement('div'))
|
|
|
|
.addClass("et2_toolbar_activeList")
|
|
|
|
.attr('id',this.id +'-'+ 'actionbox');
|
|
|
|
//actionlist is div for active actions
|
|
|
|
this.actionlist = $j(document.createElement('div'))
|
2014-02-06 12:25:56 +01:00
|
|
|
.addClass("et2_toolbar_actionlist")
|
|
|
|
.attr('id',this.id +'-'+ 'actionlist');
|
2014-01-13 11:26:20 +01:00
|
|
|
|
2014-02-05 17:04:58 +01:00
|
|
|
this.countActions = 0;
|
2013-10-04 09:29:38 +02:00
|
|
|
this.dropdowns = {};
|
2014-01-13 11:26:20 +01:00
|
|
|
this.preference = {};
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
this._build_menu(this.default_toolbar);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
// Destroy widget
|
|
|
|
if(this.div && this.div.data('ui-menu')) this.menu.menu("destroy");
|
|
|
|
|
|
|
|
// Null children
|
|
|
|
|
|
|
|
// Remove
|
|
|
|
this.div.empty().remove();
|
2014-01-13 11:26:20 +01:00
|
|
|
this.actionbox.empty().remove();
|
|
|
|
this.actionlist.empty().remove();
|
2013-10-04 09:29:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go through actions and build buttons for the toolbar
|
2014-01-20 19:19:04 +01:00
|
|
|
*
|
|
|
|
* @param {Object} actions egw-actions to build menu from
|
2013-10-04 09:29:38 +02:00
|
|
|
*/
|
|
|
|
_build_menu: function(actions)
|
|
|
|
{
|
|
|
|
// Clear existing
|
|
|
|
this.div.empty();
|
2014-01-13 11:26:20 +01:00
|
|
|
this.actionbox.empty();
|
|
|
|
this.actionlist.empty();
|
2014-02-06 14:43:11 +01:00
|
|
|
this.actionbox.append('<h class="ui-toolbar-menulistHeader">'+egw.lang('more')+' ...'+'</h>');
|
2014-01-13 11:26:20 +01:00
|
|
|
this.actionbox.append('<div id="' + this.id + '-menulist' +'" class="ui-toolbar-menulist" ></div>');
|
|
|
|
|
2014-02-07 11:33:05 +01:00
|
|
|
var pref = egw.preference(this.id,this.egw().getAppName());
|
|
|
|
if (pref && !jQuery.isArray(pref)) this.preference = pref;
|
2014-02-10 16:43:51 +01:00
|
|
|
|
|
|
|
//Set the default actions for the first time
|
|
|
|
if (typeof pref === 'undefined')
|
|
|
|
{
|
|
|
|
for (var name in actions)
|
|
|
|
{
|
|
|
|
if (!actions[name].toolbarDefault)
|
|
|
|
this.set_prefered(actions[name].id,'add');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:04:58 +01:00
|
|
|
this.countActions = Object.keys(actions).length - Object.keys(this.preference).length;
|
2013-10-04 09:29:38 +02:00
|
|
|
var last_group = false;
|
2014-01-17 14:24:18 +01:00
|
|
|
var last_group_id = false;
|
2013-10-04 09:29:38 +02:00
|
|
|
for(var name in actions)
|
|
|
|
{
|
|
|
|
var action = actions[name];
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
// Add in divider
|
2014-01-17 14:24:18 +01:00
|
|
|
if(last_group_id != action.group)
|
2013-10-04 09:29:38 +02:00
|
|
|
{
|
2014-01-17 14:24:18 +01:00
|
|
|
last_group = $j('[data-group="' + action.group + '"]',this.actionlist);
|
|
|
|
if(last_group.length == 0)
|
|
|
|
{
|
2014-02-10 14:03:17 +01:00
|
|
|
$j('<span data-group="'+action.group+'">').appendTo(this.actionlist);
|
|
|
|
}
|
2014-01-17 14:24:18 +01:00
|
|
|
last_group_id = action.group;
|
2013-10-04 09:29:38 +02:00
|
|
|
}
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
// Make sure there's something to display
|
|
|
|
if(!action.caption && !action.icon && !action.iconUrl) continue;
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
if(action.children)
|
|
|
|
{
|
|
|
|
this.dropdowns[action.id] = $j(document.createElement('span'))
|
|
|
|
.addClass("ui-state-default")
|
2014-01-17 14:24:18 +01:00
|
|
|
.appendTo(last_group);
|
2013-10-04 09:29:38 +02:00
|
|
|
var children = {};
|
|
|
|
var add_children = function(root, children) {
|
|
|
|
for(var id in root.children)
|
|
|
|
{
|
|
|
|
var info = {
|
|
|
|
id: id || root.children[id].id,
|
|
|
|
label: root.children[id].caption
|
|
|
|
};
|
|
|
|
if(root.children[id].iconUrl)
|
|
|
|
{
|
|
|
|
info.icon = root.children[id].iconUrl;
|
|
|
|
}
|
|
|
|
if(root.children[id].children)
|
|
|
|
{
|
|
|
|
add_children(root.children[id], info);
|
|
|
|
}
|
|
|
|
children[id] = info;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
add_children(action, children);
|
|
|
|
var dropdown = et2_createWidget("dropdown_button", {
|
|
|
|
id: action.id,
|
|
|
|
label: action.caption
|
|
|
|
},this);
|
|
|
|
dropdown.set_select_options(children);
|
|
|
|
var toolbar = this;
|
|
|
|
dropdown.onchange = jQuery.proxy(function(selected, dropdown)
|
|
|
|
{
|
|
|
|
var action = toolbar._actionManager.getActionById(selected.attr('data-id'));
|
|
|
|
if(action)
|
|
|
|
{
|
|
|
|
if(action)
|
|
|
|
{
|
|
|
|
action.onExecute.exec(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.debug(selected, this, action);
|
|
|
|
},action);
|
|
|
|
dropdown.onclick = jQuery.proxy(function(selected, dropdown)
|
|
|
|
{
|
|
|
|
var action = toolbar._actionManager.getActionById(this.getValue());
|
|
|
|
if(action)
|
|
|
|
{
|
|
|
|
if(action)
|
|
|
|
{
|
|
|
|
action.onExecute.exec(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.debug(selected, this, action);
|
|
|
|
},dropdown);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._make_button(action);
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 11:26:20 +01:00
|
|
|
|
|
|
|
// ************** Drag and Drop feature for toolbar *****
|
2014-02-10 14:03:17 +01:00
|
|
|
this.actionlist.find('span').sort( function (lg,g){
|
|
|
|
return +lg.dataset.group - +g.dataset.group;
|
|
|
|
}).appendTo(this.actionlist);
|
|
|
|
|
2014-01-13 11:26:20 +01:00
|
|
|
this.actionlist.appendTo(this.div);
|
|
|
|
this.actionbox.appendTo(this.div);
|
|
|
|
|
2014-01-17 14:24:18 +01:00
|
|
|
var toolbar =jQuery('#'+this.id+'-'+'actionlist').find('button'),
|
2014-01-13 11:26:20 +01:00
|
|
|
toolbox = jQuery('#'+this.id+'-'+'actionbox'),
|
|
|
|
menulist = jQuery('#'+this.id+'-'+'menulist');
|
2014-02-06 12:25:56 +01:00
|
|
|
var that = this;
|
2014-01-13 11:26:20 +01:00
|
|
|
toolbar.draggable({
|
|
|
|
cancel:true,
|
2014-02-11 15:18:30 +01:00
|
|
|
zIndex: 1000,
|
2014-01-13 11:26:20 +01:00
|
|
|
//revert:"invalid",
|
|
|
|
containment: "document",
|
|
|
|
cursor: "move",
|
2014-02-05 17:04:58 +01:00
|
|
|
helper: "clone",
|
2014-02-06 12:25:56 +01:00
|
|
|
appendTo:'body',
|
2014-02-05 17:04:58 +01:00
|
|
|
stop: function(event, ui){
|
|
|
|
that._build_menu(actions);
|
|
|
|
}
|
2014-01-13 11:26:20 +01:00
|
|
|
});
|
|
|
|
menulist.children().draggable({
|
|
|
|
cancel:true,
|
|
|
|
containment:"document",
|
|
|
|
helper:"clone",
|
2014-02-06 12:25:56 +01:00
|
|
|
appendTo:'body',
|
2014-02-11 15:18:30 +01:00
|
|
|
zIndex: 1000,
|
2014-01-20 19:19:04 +01:00
|
|
|
cursor:"move"
|
2014-01-13 11:26:20 +01:00
|
|
|
});
|
2014-02-06 12:25:56 +01:00
|
|
|
toolbox.children().droppable({
|
2014-01-13 11:26:20 +01:00
|
|
|
accept:toolbar,
|
|
|
|
drop:function (event, ui) {
|
|
|
|
that.set_prefered(ui.draggable.attr('id').replace(that.id+'-',''),"add");
|
|
|
|
ui.draggable.appendTo(menulist);
|
2014-02-11 17:06:02 +01:00
|
|
|
if (that.actionlist.find(".ui-draggable").length == 0)
|
2014-01-21 14:52:32 +01:00
|
|
|
{
|
2014-02-05 17:04:58 +01:00
|
|
|
that.preference = {};
|
|
|
|
egw.set_preference(that.egw().getAppName(),that.id,that.preference);
|
2014-01-21 14:52:32 +01:00
|
|
|
}
|
2014-01-13 11:26:20 +01:00
|
|
|
},
|
2014-02-06 12:25:56 +01:00
|
|
|
tolerance:"touch"
|
2014-01-20 19:19:04 +01:00
|
|
|
});
|
2014-01-13 11:26:20 +01:00
|
|
|
|
|
|
|
jQuery('#'+this.id+'-'+'actionlist').droppable({
|
|
|
|
tolerance:"pointer",
|
|
|
|
drop:function (event,ui) {
|
|
|
|
that.set_prefered(ui.draggable.attr('id').replace(that.id+'-',''),"remove");
|
|
|
|
ui.draggable.appendTo(jQuery('#'+that.id+'-'+'actionlist'));
|
2014-02-05 17:04:58 +01:00
|
|
|
that._build_menu(actions);
|
2014-01-13 11:26:20 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
toolbox.accordion({
|
2014-01-17 14:24:18 +01:00
|
|
|
heightStyle:"fill",
|
|
|
|
collapsible: true,
|
2014-02-07 11:33:05 +01:00
|
|
|
active:'none'
|
2014-01-17 14:24:18 +01:00
|
|
|
});
|
2014-01-13 11:26:20 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add/Or remove an action from prefence
|
|
|
|
*
|
|
|
|
* @param {string} _action name of the action which needs to be stored in pereference
|
|
|
|
* @param {string} _do if set to "add" add the action to preference and "remove" remove one from preference
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
set_prefered: function(_action,_do)
|
|
|
|
{
|
|
|
|
switch(_do)
|
|
|
|
{
|
|
|
|
case "add":
|
2014-02-07 11:33:05 +01:00
|
|
|
this.preference[_action] = true;
|
2014-01-13 11:26:20 +01:00
|
|
|
egw.set_preference(this.egw().getAppName(),this.id,this.preference);
|
|
|
|
break;
|
|
|
|
case "remove":
|
|
|
|
delete this.preference[_action];
|
|
|
|
egw.set_preference(this.egw().getAppName(),this.id,this.preference);
|
|
|
|
}
|
2013-10-04 09:29:38 +02:00
|
|
|
},
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
/**
|
|
|
|
* Make a button based on the given action
|
2014-01-20 19:19:04 +01:00
|
|
|
*
|
|
|
|
* @param {Object} action action object with attributes icon, caption, ...
|
2013-10-04 09:29:38 +02:00
|
|
|
*/
|
|
|
|
_make_button: function(action)
|
|
|
|
{
|
|
|
|
var button_options = {
|
|
|
|
};
|
|
|
|
var button = $j(document.createElement('button'))
|
2014-02-05 17:04:58 +01:00
|
|
|
.addClass("et2_button et2_button_text et2_button_with_image")
|
2014-01-06 17:45:54 +01:00
|
|
|
.attr('id', this.id+'-'+action.id)
|
2014-02-07 12:02:35 +01:00
|
|
|
.attr('title', (action.hint ? action.hint : action.caption))
|
2014-01-17 14:24:18 +01:00
|
|
|
.appendTo(this.preference[action.id]?this.actionbox.children()[1]:$j('[data-group='+action.group+']',this.actionlist));
|
2014-02-06 12:25:56 +01:00
|
|
|
|
2014-02-06 15:01:37 +01:00
|
|
|
if ( action.iconUrl)
|
2014-02-05 17:04:58 +01:00
|
|
|
{
|
|
|
|
button.attr('style','background-image:url(' + action.iconUrl + ')');
|
|
|
|
}
|
2014-02-06 15:01:37 +01:00
|
|
|
if (action.caption)
|
2013-10-04 09:29:38 +02:00
|
|
|
{
|
2014-02-11 17:06:02 +01:00
|
|
|
if (this.countActions <= parseInt(this.view_range) || this.preference[action.id] )
|
2014-02-05 17:04:58 +01:00
|
|
|
{
|
2014-02-11 09:50:19 +01:00
|
|
|
button[0].textContent = action.caption;
|
2014-02-05 17:04:58 +01:00
|
|
|
}
|
2013-10-04 09:29:38 +02:00
|
|
|
}
|
|
|
|
if(action.icon)
|
|
|
|
{
|
2014-01-20 19:19:04 +01:00
|
|
|
button_options.icon = action.icon;
|
2013-10-04 09:29:38 +02:00
|
|
|
}
|
2014-02-05 17:04:58 +01:00
|
|
|
if (!jQuery.isEmptyObject(button_options))
|
|
|
|
{
|
|
|
|
button.button(button_options);
|
|
|
|
}
|
2013-10-04 09:29:38 +02:00
|
|
|
// Set up the click action
|
|
|
|
var click = function(e) {
|
|
|
|
var action = this._actionManager.getActionById(e.data);
|
|
|
|
if(action)
|
|
|
|
{
|
|
|
|
action.data.event = e;
|
|
|
|
action.onExecute.exec(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
button.click(action.id, jQuery.proxy(click, this));
|
|
|
|
},
|
2013-10-11 15:34:17 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-04 09:29:38 +02:00
|
|
|
* Link the actions to the DOM nodes / widget bits.
|
|
|
|
*
|
2014-01-20 19:19:04 +01:00
|
|
|
* @param {Object} actions egw-actions to build menu from
|
2013-10-04 09:29:38 +02:00
|
|
|
*/
|
|
|
|
_link_actions: function(actions)
|
|
|
|
{
|
|
|
|
this._build_menu(actions);
|
|
|
|
},
|
2014-01-06 17:45:54 +01:00
|
|
|
|
2013-10-04 09:29:38 +02:00
|
|
|
getDOMNode: function(asker)
|
|
|
|
{
|
|
|
|
if(asker != this && asker.id && this.dropdowns[asker.id])
|
|
|
|
{
|
|
|
|
return this.dropdowns[asker.id][0];
|
|
|
|
}
|
|
|
|
return this.div[0];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
et2_register_widget(et2_toolbar, ["toolbar"]);
|