2013-04-09 10:45:59 +02:00
|
|
|
/**
|
|
|
|
* EGroupware clientside Application javascript base 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
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
2013-07-19 17:22:54 +02:00
|
|
|
egw_inheritance;
|
2013-04-09 10:45:59 +02:00
|
|
|
*/
|
|
|
|
|
2013-11-04 21:54:23 +01:00
|
|
|
/**
|
|
|
|
* Object to collect instanciated appliction objects
|
|
|
|
*
|
|
|
|
* Attributes classes collects loaded application classes,
|
|
|
|
* which can get instanciated:
|
|
|
|
*
|
|
|
|
* app[appname] = new app.classes[appname]();
|
|
|
|
*
|
|
|
|
* On destruction only app[appname] gets deleted, app.classes[appname] need to be used again!
|
|
|
|
*
|
|
|
|
* @type object
|
|
|
|
*/
|
|
|
|
window.app = {classes: {}};
|
2013-04-09 10:45:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Common base class for application javascript
|
|
|
|
* Each app should extend as needed.
|
|
|
|
*
|
|
|
|
* All application javascript should be inside. Intitialization goes in init(),
|
2013-11-04 21:54:23 +01:00
|
|
|
* clean-up code goes in destroy(). Initialization is done once all js is loaded.
|
2013-04-09 10:45:59 +02:00
|
|
|
*
|
|
|
|
* var app.appname = AppJS.extend({
|
|
|
|
* // Actually set this one, the rest is example
|
|
|
|
* appname: appname,
|
|
|
|
*
|
|
|
|
* internal_var: 1000,
|
|
|
|
*
|
|
|
|
* init: function()
|
|
|
|
* {
|
|
|
|
* // Call the super
|
|
|
|
* this._super.apply(this, arguments);
|
2013-11-04 21:54:23 +01:00
|
|
|
*
|
2013-04-09 10:45:59 +02:00
|
|
|
* // Init the stuff
|
2013-11-04 21:54:23 +01:00
|
|
|
* if ( egw.preference('dateformat', 'common') )
|
2013-04-09 10:45:59 +02:00
|
|
|
* {
|
|
|
|
* // etc
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* _private: function()
|
|
|
|
* {
|
|
|
|
* // Underscore private by convention
|
|
|
|
* }
|
|
|
|
* });
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @class AppJS
|
|
|
|
* @augments Class
|
2013-04-09 10:45:59 +02:00
|
|
|
*/
|
2013-11-04 21:54:23 +01:00
|
|
|
var AppJS = Class.extend(
|
|
|
|
{
|
2013-04-09 10:45:59 +02:00
|
|
|
/**
|
|
|
|
* Internal application name - override this
|
|
|
|
*/
|
|
|
|
appname: '',
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-08-20 21:20:10 +02:00
|
|
|
/**
|
|
|
|
* Internal reference to etemplate2 widget tree
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @var {et2_container}
|
2013-08-20 21:20:10 +02:00
|
|
|
*/
|
|
|
|
et2: null,
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-10-07 18:53:13 +02:00
|
|
|
/**
|
|
|
|
* Internal reference to egw client-side api object for current app and window
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @var {egw}
|
2013-10-07 18:53:13 +02:00
|
|
|
*/
|
|
|
|
egw: null,
|
2013-04-09 10:45:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization and setup goes here, but the etemplate2 object
|
|
|
|
* is not yet ready.
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
window.app[this.appname] = this;
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-10-07 18:53:13 +02:00
|
|
|
this.egw = egw(this.appname, window);
|
2013-12-06 22:26:55 +01:00
|
|
|
|
2014-05-13 08:44:50 +02:00
|
|
|
// Initialize sidebox for non-popups.
|
2014-05-12 22:36:18 +02:00
|
|
|
// ID set server side
|
2014-05-13 08:44:50 +02:00
|
|
|
if(!this.egw.is_popup())
|
2013-12-07 00:12:05 +01:00
|
|
|
{
|
2014-05-12 22:36:18 +02:00
|
|
|
var sidebox = jQuery('#favorite_sidebox_'+this.appname);
|
|
|
|
if(sidebox.length == 0 && egw_getFramework() != null)
|
|
|
|
{
|
|
|
|
var egw_fw = egw_getFramework();
|
|
|
|
sidebox= $j('#favorite_sidebox_'+this.appname,egw_fw.sidemenuDiv);
|
|
|
|
}
|
|
|
|
// Make sure we're running in the top window when we init sidebox
|
|
|
|
if(window.top.app[this.appname] !== this && window.top.app[this.appname])
|
|
|
|
{
|
|
|
|
window.top.app[this.appname]._init_sidebox(sidebox);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._init_sidebox(sidebox);
|
|
|
|
}
|
2014-04-29 18:51:57 +02:00
|
|
|
}
|
2013-04-09 10:45:59 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up any created objects & references
|
|
|
|
*/
|
|
|
|
destroy: function() {
|
2013-08-20 21:20:10 +02:00
|
|
|
delete this.et2;
|
2014-02-19 16:10:39 +01:00
|
|
|
if (this.sidebox)
|
|
|
|
this.sidebox.off();
|
2013-12-12 00:54:42 +01:00
|
|
|
delete this.sidebox;
|
2013-04-09 10:45:59 +02:00
|
|
|
delete window.app[this.appname];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when the etemplate2 object is loaded
|
|
|
|
* and ready. If you must store a reference to the et2 object,
|
2013-08-21 01:20:56 +02:00
|
|
|
* make sure to clean it up in destroy(). Note that this can be called
|
|
|
|
* several times, with different et2 objects, as templates are loaded.
|
2013-04-09 10:45:59 +02:00
|
|
|
*
|
2014-02-17 15:58:20 +01:00
|
|
|
* @param {etemplate2} et2
|
|
|
|
* @param {string} name template name
|
2013-04-09 10:45:59 +02:00
|
|
|
*/
|
2014-02-17 15:58:20 +01:00
|
|
|
et2_ready: function(et2,name) {
|
2013-08-21 01:20:56 +02:00
|
|
|
if(this.et2 !== null)
|
2013-08-20 21:20:10 +02:00
|
|
|
{
|
2013-08-21 01:20:56 +02:00
|
|
|
egw.debug('log', "Changed et2 object");
|
2013-08-20 21:20:10 +02:00
|
|
|
}
|
2013-08-21 01:20:56 +02:00
|
|
|
this.et2 = et2.widgetContainer;
|
2013-08-20 21:20:10 +02:00
|
|
|
},
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-08-20 21:20:10 +02:00
|
|
|
/**
|
2013-11-04 21:54:23 +01:00
|
|
|
* Open an entry.
|
|
|
|
*
|
2013-08-20 21:20:10 +02:00
|
|
|
* Designed to be used with the action system as a callback
|
|
|
|
* eg: onExecute => app.<appname>.open
|
2013-11-04 21:54:23 +01:00
|
|
|
*
|
2013-08-20 21:20:10 +02:00
|
|
|
* @param _action
|
|
|
|
* @param _senders
|
|
|
|
*/
|
|
|
|
open: function(_action, _senders) {
|
2014-02-12 12:23:04 +01:00
|
|
|
var id_app = _senders[0].id.split('::');
|
2013-08-20 21:20:10 +02:00
|
|
|
egw.open(id_app[1], this.appname);
|
|
|
|
},
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-08-20 21:20:10 +02:00
|
|
|
/**
|
|
|
|
* A generic method to action to server asynchronously
|
2013-11-04 21:54:23 +01:00
|
|
|
*
|
2013-08-20 21:20:10 +02:00
|
|
|
* Designed to be used with the action system as a callback.
|
|
|
|
* In the PHP side, set the action
|
2013-11-04 21:54:23 +01:00
|
|
|
* 'onExecute' => 'javaScript:app.<appname>.action', and
|
2013-08-20 21:20:10 +02:00
|
|
|
* implement _do_action(action_id, selected)
|
2013-11-04 21:54:23 +01:00
|
|
|
*
|
|
|
|
* @param {egwAction} _action
|
2013-08-20 21:20:10 +02:00
|
|
|
* @param {egwActionObject[]} _elems
|
|
|
|
*/
|
|
|
|
action: function(_action, _elems)
|
|
|
|
{
|
|
|
|
// let user confirm select-all
|
|
|
|
var select_all = _action.getManager().getActionById("select_all");
|
2013-11-04 21:54:23 +01:00
|
|
|
var confirm_msg = (_elems.length > 1 || select_all && select_all.checked) &&
|
2013-08-20 21:20:10 +02:00
|
|
|
typeof _action.data.confirm_multiple != 'undefined' ?
|
|
|
|
_action.data.confirm_multiple : _action.data.confirm;
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-08-20 21:20:10 +02:00
|
|
|
if (typeof confirm_msg != 'undefined')
|
|
|
|
{
|
|
|
|
var that = this;
|
|
|
|
var action_id = _action.id;
|
|
|
|
et2_dialog.show_dialog(function(button_id,value)
|
|
|
|
{
|
|
|
|
if (button_id != et2_dialog.NO_BUTTON)
|
|
|
|
{
|
2013-11-04 21:54:23 +01:00
|
|
|
that._do_action(action_id, _elems);
|
2013-08-20 21:20:10 +02:00
|
|
|
}
|
|
|
|
}, confirm_msg, egw.lang('Confirmation required'), et2_dialog.BUTTONS_YES_NO, et2_dialog.QUESTION_MESSAGE);
|
|
|
|
}
|
|
|
|
else if (typeof this._do_action == 'function')
|
|
|
|
{
|
2013-11-04 21:54:23 +01:00
|
|
|
this._do_action(_action.id, _elems);
|
2013-08-20 21:20:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If this is a nextmatch action, do an ajax submit setting the action
|
|
|
|
var nm = null;
|
|
|
|
var action = _action;
|
|
|
|
while(nm == null && action.parent != null)
|
|
|
|
{
|
|
|
|
if(action.data.nextmatch) nm = action.data.nextmatch;
|
|
|
|
action = action.parent;
|
|
|
|
}
|
|
|
|
if(nm != null)
|
|
|
|
{
|
|
|
|
var value = {};
|
|
|
|
value[nm.options.settings.action_var] = _action.id;
|
|
|
|
nm.set_value(value);
|
|
|
|
nm.getInstanceManager().submit();
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 01:00:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the application's state to the given state.
|
|
|
|
*
|
|
|
|
* While not pretending to implement the history API, it is patterned similarly
|
|
|
|
* @link http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html
|
|
|
|
*
|
|
|
|
* The default implementation works with the favorites to apply filters to a nextmatch.
|
|
|
|
*
|
|
|
|
*
|
2013-12-06 19:24:29 +01:00
|
|
|
* @param {{name: string, state: object}|string} state Object (or JSON string) for a state.
|
2013-12-07 00:12:05 +01:00
|
|
|
* Only state is required, and its contents are application specific.
|
|
|
|
*
|
|
|
|
* @return {boolean} false - Returns false to stop event propagation
|
2013-12-05 01:00:43 +01:00
|
|
|
*/
|
|
|
|
setState: function(state)
|
|
|
|
{
|
2013-12-06 19:24:29 +01:00
|
|
|
// State should be an object, not a string, but we'll parse
|
2013-12-05 01:00:43 +01:00
|
|
|
if(typeof state == "string")
|
|
|
|
{
|
|
|
|
if(state.indexOf('{') != -1 || state =='null')
|
|
|
|
{
|
|
|
|
state = JSON.parse(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(typeof state != "object")
|
|
|
|
{
|
|
|
|
egw.debug('error', 'Unable to set state to %o, needs to be an object',state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(state == null)
|
|
|
|
{
|
|
|
|
state = {};
|
|
|
|
}
|
2013-11-04 21:54:23 +01:00
|
|
|
|
2013-12-07 00:12:05 +01:00
|
|
|
// Check for egw.open() parameters
|
|
|
|
if(state.state && state.state.id && state.state.app)
|
|
|
|
{
|
|
|
|
return egw.open(state.state,undefined,undefined,{},'_self');
|
|
|
|
}
|
|
|
|
|
2013-12-05 01:00:43 +01:00
|
|
|
// Try and find a nextmatch widget, and set its filters
|
|
|
|
var nextmatched = false;
|
|
|
|
var et2 = etemplate2.getByApplication(this.appname);
|
|
|
|
for(var i = 0; i < et2.length; i++)
|
|
|
|
{
|
|
|
|
et2[i].widgetContainer.iterateOver(function(_widget) {
|
2014-02-06 18:53:29 +01:00
|
|
|
// Firefox has trouble with spaces in search
|
2014-02-12 12:23:04 +01:00
|
|
|
if(state.state && state.state.search) state.state.search = unescape(state.state.search);
|
|
|
|
|
2013-12-05 01:00:43 +01:00
|
|
|
// Apply
|
2013-12-06 22:26:55 +01:00
|
|
|
_widget.activeFilters = state.state || state.filter || {};
|
2013-12-05 01:00:43 +01:00
|
|
|
_widget.applyFilters();
|
|
|
|
nextmatched = true;
|
|
|
|
}, this, et2_nextmatch);
|
2013-12-07 00:12:05 +01:00
|
|
|
if(nextmatched) return false;
|
2013-12-05 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
2013-12-07 00:12:05 +01:00
|
|
|
// Try a redirect to list
|
|
|
|
// 'blank' is the special name for no filters, send that instead of the nice translated name
|
|
|
|
var safe_name = jQuery.isEmptyObject(state) || jQuery.isEmptyObject(state.state||state.filter) ? 'blank' : state.name.replace(/[^A-Za-z0-9-_]/g, '_');
|
|
|
|
egw.open('',this.appname,'list',{'favorite': safe_name},this.appname);
|
2013-12-12 03:42:08 +01:00
|
|
|
|
2014-02-12 12:23:04 +01:00
|
|
|
return false;
|
2013-12-05 01:00:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current state of the application for future restoration
|
|
|
|
*
|
|
|
|
* The state can be anything, as long as it's an object. The contents are
|
|
|
|
* application specific. The default implementation finds a nextmatch and
|
|
|
|
* returns its value.
|
2013-12-06 22:26:55 +01:00
|
|
|
* The return value of this function cannot be passed directly to setState(),
|
|
|
|
* since setState is expecting an additional wrapper, eg:
|
|
|
|
* {name: 'something', state: getState()}
|
2013-12-05 01:00:43 +01:00
|
|
|
*
|
2013-12-06 22:26:55 +01:00
|
|
|
* @return {object} Application specific map representing the current state
|
2013-12-05 01:00:43 +01:00
|
|
|
*/
|
|
|
|
getState: function()
|
|
|
|
{
|
|
|
|
var state = {};
|
|
|
|
|
|
|
|
// Try and find a nextmatch widget, and set its filters
|
|
|
|
var et2 = etemplate2.getByApplication(this.appname);
|
|
|
|
for(var i = 0; i < et2.length; i++)
|
|
|
|
{
|
2013-12-06 00:22:54 +01:00
|
|
|
et2[i].widgetContainer.iterateOver(function(_widget) {
|
2013-12-05 01:00:43 +01:00
|
|
|
state = _widget.getValue();
|
|
|
|
}, this, et2_nextmatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2013-12-06 00:22:54 +01:00
|
|
|
},
|
|
|
|
|
2013-12-06 22:26:55 +01:00
|
|
|
/**
|
|
|
|
* Initializes actions and handlers on sidebox (delete)
|
2014-02-12 12:23:04 +01:00
|
|
|
*
|
|
|
|
* @param {jQuery} sidebox jQuery of DOM node
|
2013-12-06 22:26:55 +01:00
|
|
|
*/
|
|
|
|
_init_sidebox: function(sidebox)
|
|
|
|
{
|
|
|
|
if(sidebox.length)
|
|
|
|
{
|
|
|
|
var self = this;
|
2014-04-29 18:51:57 +02:00
|
|
|
if(this.sidebox) this.sidebox.off();
|
2013-12-06 22:26:55 +01:00
|
|
|
this.sidebox = sidebox;
|
|
|
|
sidebox
|
|
|
|
.off()
|
2014-02-26 16:44:00 +01:00
|
|
|
// removed .on("mouse(enter|leave)" (wrapping trash icon), as it stalls delete in IE11
|
2013-12-06 22:26:55 +01:00
|
|
|
.on("click","div.ui-icon-trash", this, this.delete_favorite)
|
2014-02-13 16:37:49 +01:00
|
|
|
// need to install a favorite handler, as we switch original one off with .off()
|
2014-03-11 09:16:52 +01:00
|
|
|
.on('click','li[data-id]', this, function(event) {
|
2014-02-13 16:37:49 +01:00
|
|
|
var href = jQuery('a[href^="javascript:"]', this).prop('href');
|
2014-02-13 17:50:49 +01:00
|
|
|
var matches = href ? href.match(/^javascript:([^\(]+)\((.*)?\);?$/) : null;
|
|
|
|
if (matches && matches.length > 1 && matches[2] !== undefined)
|
2014-02-13 16:37:49 +01:00
|
|
|
{
|
2014-03-11 09:16:52 +01:00
|
|
|
event.stopImmediatePropagation();
|
|
|
|
self.setState.call(self, JSON.parse(matches[2]));
|
|
|
|
return false;
|
2014-02-13 16:37:49 +01:00
|
|
|
}
|
|
|
|
})
|
2013-12-06 22:26:55 +01:00
|
|
|
.addClass("ui-helper-clearfix");
|
2014-04-29 18:51:57 +02:00
|
|
|
|
2014-04-25 09:37:11 +02:00
|
|
|
//Add Sortable handler to sideBox fav. menu
|
|
|
|
jQuery('ul','#favorite_sidebox_'+this.appname).sortable({
|
|
|
|
|
|
|
|
items:'li:not([data-id$="add"])',
|
|
|
|
placeholder:'ui-fav-sortable-placeholder',
|
|
|
|
update: function (event, ui)
|
|
|
|
{
|
|
|
|
var favSortedList = jQuery(this).sortable('toArray', {attribute:'data-id'});
|
|
|
|
|
|
|
|
self.egw.set_preference(self.appname,'fav_sort_pref',favSortedList);
|
2014-04-29 18:51:57 +02:00
|
|
|
|
2014-04-25 09:37:11 +02:00
|
|
|
self._refresh_fav_nm();
|
|
|
|
}
|
2014-04-29 18:51:57 +02:00
|
|
|
});
|
2013-12-06 22:26:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2013-12-12 01:38:06 +01:00
|
|
|
/**
|
|
|
|
* Add a new favorite
|
|
|
|
*
|
|
|
|
* Fetches the current state from the application, then opens a dialog to get the
|
|
|
|
* name and other settings. If user proceeds, the favorite is saved, and if possible
|
|
|
|
* the sidebox is directly updated to include the new favorite
|
|
|
|
*
|
|
|
|
* @param {object} [state] State settings to be merged into the application state
|
|
|
|
*/
|
|
|
|
add_favorite: function(state)
|
2013-12-06 00:22:54 +01:00
|
|
|
{
|
|
|
|
if(typeof this.favorite_popup == "undefined")
|
|
|
|
{
|
|
|
|
this._create_favorite_popup();
|
|
|
|
}
|
|
|
|
// Get current state
|
2013-12-12 01:38:06 +01:00
|
|
|
this.favorite_popup.state = jQuery.extend({}, this.getState(), state||{});
|
2013-12-06 00:22:54 +01:00
|
|
|
/*
|
|
|
|
// Add in extras
|
|
|
|
for(var extra in this.options.filters)
|
|
|
|
{
|
|
|
|
// Don't overwrite what nm has, chances are nm has more up-to-date value
|
|
|
|
if(typeof this.popup.current_filters == 'undefined')
|
|
|
|
{
|
|
|
|
this.popup.current_filters[extra] = this.nextmatch.options.settings[extra];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in application's settings
|
|
|
|
if(this.filters != true)
|
|
|
|
{
|
|
|
|
for(var i = 0; i < this.filters.length; i++)
|
|
|
|
{
|
|
|
|
this.popup.current_filters[this.options.filters[i]] = this.nextmatch.options.settings[this.options.filters[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// Make sure it's an object - deep copy to prevent references in sub-objects (col_filters)
|
|
|
|
this.favorite_popup.state = jQuery.extend(true,{},this.favorite_popup.state);
|
|
|
|
|
|
|
|
// Update popup with current set filters (more for debug than user)
|
|
|
|
var filter_list = [];
|
|
|
|
var add_to_popup = function(arr) {
|
|
|
|
filter_list.push("<ul>");
|
|
|
|
jQuery.each(arr, function(index, filter) {
|
|
|
|
filter_list.push("<li id='index'><span class='filter_id'>"+index+"</span>" +
|
|
|
|
(typeof filter != "object" ? "<span class='filter_value'>"+filter+"</span>": "")
|
|
|
|
);
|
|
|
|
if(typeof filter == "object" && filter != null) add_to_popup(filter);
|
|
|
|
filter_list.push("</li>");
|
|
|
|
});
|
|
|
|
filter_list.push("</ul>");
|
2014-02-12 12:23:04 +01:00
|
|
|
};
|
2013-12-06 00:22:54 +01:00
|
|
|
add_to_popup(this.favorite_popup.state);
|
|
|
|
$j("#"+this.appname+"_favorites_popup_state",this.favorite_popup)
|
|
|
|
.replaceWith(
|
|
|
|
$j(filter_list.join("")).attr("id",this.appname+"_favorites_popup_state")
|
|
|
|
);
|
|
|
|
$j("#"+this.appname+"_favorites_popup_state",this.favorite_popup)
|
|
|
|
.hide()
|
|
|
|
.siblings(".ui-icon-circle-plus")
|
|
|
|
.removeClass("ui-icon-circle-minus");
|
|
|
|
|
|
|
|
// Popup
|
|
|
|
this.favorite_popup.dialog("open");
|
|
|
|
console.log(this);
|
2013-12-12 03:42:08 +01:00
|
|
|
|
|
|
|
// Stop the normal bubbling if this is called on click
|
|
|
|
return false;
|
2013-12-06 00:22:54 +01:00
|
|
|
},
|
2014-05-13 08:44:50 +02:00
|
|
|
|
2014-04-25 09:37:11 +02:00
|
|
|
/**
|
2014-05-13 08:44:50 +02:00
|
|
|
* Update favorite items in nm fav. menu
|
|
|
|
*
|
2014-04-25 09:37:11 +02:00
|
|
|
*/
|
|
|
|
_refresh_fav_nm: function ()
|
|
|
|
{
|
|
|
|
var self = this;
|
2014-05-13 08:44:50 +02:00
|
|
|
|
2014-04-25 09:37:11 +02:00
|
|
|
if(etemplate2 && etemplate2.getByApplication)
|
|
|
|
{
|
|
|
|
var et2 = etemplate2.getByApplication(self.appname);
|
|
|
|
for(var i = 0; i < et2.length; i++)
|
|
|
|
{
|
|
|
|
et2[i].widgetContainer.iterateOver(function(_widget) {
|
|
|
|
_widget.stored_filters = _widget.load_favorites(self.appname);
|
|
|
|
_widget.init_filters(_widget);
|
|
|
|
}, self, et2_favorites);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Error ("_refresh_fav_nm():Either et2 is not ready/ not there yet. Make sure that etemplate2 is ready before call this method.");
|
|
|
|
}
|
|
|
|
},
|
2014-05-13 08:44:50 +02:00
|
|
|
|
2013-12-06 00:22:54 +01:00
|
|
|
/**
|
|
|
|
* Create the "Add new" popup dialog
|
|
|
|
*/
|
|
|
|
_create_favorite_popup: function()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
var favorite_prefix = 'favorite_';
|
|
|
|
|
|
|
|
// Clear old, if existing
|
|
|
|
if(this.favorite_popup && this.favorite_popup.group)
|
|
|
|
{
|
|
|
|
this.favorite_popup.group.free();
|
|
|
|
delete this.favorite_popup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create popup
|
|
|
|
this.favorite_popup = $j('<div id="'+this.dom_id + '_nm_favorites_popup" title="' + egw().lang("New favorite") + '">\
|
|
|
|
<form>\
|
|
|
|
<label for="name">'+
|
|
|
|
this.egw.lang("name") +
|
|
|
|
'</label>' +
|
|
|
|
|
|
|
|
'<input type="text" name="name" id="name"/>\
|
|
|
|
<div id="'+this.appname+'_favorites_popup_admin"/>\
|
|
|
|
<span>'+ this.egw.lang("Details") + '</span><span style="float:left;" class="ui-icon ui-icon-circle-plus ui-button" />\
|
|
|
|
<ul id="'+this.appname+'_favorites_popup_state"/>\
|
|
|
|
</form>\
|
|
|
|
</div>'
|
|
|
|
).appendTo(this.et2 ? this.et2.getDOMNode() : $j('body'));
|
|
|
|
|
|
|
|
$j(".ui-icon-circle-plus",this.favorite_popup).prev().andSelf().click(function() {
|
|
|
|
var details = $j("#"+self.appname+"_favorites_popup_state",self.favorite_popup)
|
|
|
|
.slideToggle()
|
|
|
|
.siblings(".ui-icon-circle-plus")
|
|
|
|
.toggleClass("ui-icon-circle-minus");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add some controls if user is an admin
|
|
|
|
var apps = egw().user('apps');
|
|
|
|
var is_admin = (typeof apps['admin'] != "undefined");
|
|
|
|
if(is_admin)
|
|
|
|
{
|
|
|
|
this.favorite_popup.group = et2_createWidget("select-account",{
|
|
|
|
id: "favorite[group]",
|
|
|
|
account_type: "groups",
|
|
|
|
empty_label: "Groups",
|
|
|
|
no_lang: true,
|
|
|
|
parent_node: this.appname+'_favorites_popup_admin'
|
|
|
|
},this.et2 || null);
|
2013-12-06 22:26:55 +01:00
|
|
|
this.favorite_popup.group.loadingFinished();
|
2013-12-06 00:22:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var buttons = {};
|
2014-03-03 19:51:58 +01:00
|
|
|
buttons['save'] = {
|
|
|
|
text: this.egw.lang('save'),
|
|
|
|
default: true,
|
|
|
|
click: function() {
|
|
|
|
// Add a new favorite
|
|
|
|
var name = $j("#name",this);
|
|
|
|
|
|
|
|
if(name.val())
|
2013-12-06 00:22:54 +01:00
|
|
|
{
|
2014-03-03 19:51:58 +01:00
|
|
|
// Add to the list
|
|
|
|
name.val(name.val().replace(/(<([^>]+)>)/ig,""));
|
|
|
|
var safe_name = name.val().replace(/[^A-Za-z0-9-_]/g,"_");
|
|
|
|
var favorite = {
|
|
|
|
name: name.val(),
|
|
|
|
group: (typeof self.favorite_popup.group != "undefined" &&
|
|
|
|
self.favorite_popup.group.get_value() ? self.favorite_popup.group.get_value() : false),
|
|
|
|
state: self.favorite_popup.state
|
|
|
|
};
|
|
|
|
|
|
|
|
var favorite_pref = favorite_prefix+safe_name;
|
|
|
|
|
|
|
|
// Save to preferences
|
|
|
|
if(typeof self.favorite_popup.group != "undefined" && self.favorite_popup.group.getValue() != '')
|
|
|
|
{
|
|
|
|
// Admin stuff - save preference server side
|
|
|
|
self.egw.jsonq(self.appname+'.egw_framework.ajax_set_favorite.template',
|
|
|
|
[
|
|
|
|
self.appname,
|
|
|
|
name.val(),
|
|
|
|
"add",
|
|
|
|
self.favorite_popup.group.get_value(),
|
|
|
|
self.favorite_popup.state
|
|
|
|
]
|
|
|
|
);
|
|
|
|
self.favorite_popup.group.set_value('');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Normal user - just save to preferences client side
|
|
|
|
self.egw.set_preference(self.appname,favorite_pref,favorite);
|
|
|
|
}
|
2013-12-06 22:26:55 +01:00
|
|
|
|
2014-03-03 19:51:58 +01:00
|
|
|
// Add to list immediately
|
|
|
|
if(self.sidebox)
|
|
|
|
{
|
|
|
|
// Remove any existing with that name
|
|
|
|
$j('[data-id="'+safe_name+'"]',self.sidebox).remove();
|
|
|
|
|
|
|
|
// Create new item
|
|
|
|
var html = "<li data-id='"+safe_name+"' data-group='" + favorite.group + "' class='ui-menu-item' role='menuitem'>\n";
|
|
|
|
var href = 'javascript:app.'+self.appname+'.setState('+JSON.stringify(favorite)+');';
|
|
|
|
html += "<a href='"+href+"' class='ui-corner-all' tabindex='-1'>";
|
|
|
|
html += "<div class='" + 'sideboxstar' + "'></div>"+
|
|
|
|
favorite.name;
|
|
|
|
html += "<div class='ui-icon ui-icon-trash' title='" + egw.lang('Delete') + "'/>";
|
|
|
|
html += "</a></li>\n";
|
|
|
|
$j(html).insertBefore($j('li',self.sidebox).last());
|
|
|
|
self._init_sidebox(self.sidebox);
|
|
|
|
}
|
2013-12-13 00:45:09 +01:00
|
|
|
|
2014-03-03 19:51:58 +01:00
|
|
|
// Try to update nextmatch favorites too
|
2014-04-25 09:37:11 +02:00
|
|
|
self._refresh_fav_nm();
|
2013-12-13 00:45:09 +01:00
|
|
|
}
|
2014-03-03 19:51:58 +01:00
|
|
|
// Reset form
|
|
|
|
delete self.favorite_popup.state;
|
|
|
|
name.val("");
|
|
|
|
$j("#filters",self.favorite_popup).empty();
|
2013-12-06 00:22:54 +01:00
|
|
|
|
2014-03-03 19:51:58 +01:00
|
|
|
$j(this).dialog("close");
|
2014-03-11 09:16:52 +01:00
|
|
|
}
|
2013-12-06 00:22:54 +01:00
|
|
|
};
|
|
|
|
buttons[this.egw.lang("cancel")] = function() {
|
2014-02-05 21:53:08 +01:00
|
|
|
if(typeof self.favorite_popup.group !== 'undefined' && self.favorite_popup.group.set_value)
|
|
|
|
{
|
|
|
|
self.favorite_popup.group.set_value(null);
|
|
|
|
}
|
2013-12-06 00:22:54 +01:00
|
|
|
$j(this).dialog("close");
|
|
|
|
};
|
|
|
|
|
|
|
|
this.favorite_popup.dialog({
|
|
|
|
autoOpen: false,
|
|
|
|
modal: true,
|
|
|
|
buttons: buttons,
|
|
|
|
close: function() {
|
|
|
|
}
|
|
|
|
});
|
2013-12-13 00:45:09 +01:00
|
|
|
|
2014-03-03 19:51:58 +01:00
|
|
|
// Bind handler for enter keypress
|
|
|
|
this.favorite_popup.off('keydown').on('keydown', jQuery.proxy(function(e) {
|
|
|
|
var tagName = e.target.tagName.toLowerCase();
|
|
|
|
tagName = (tagName === 'input' && e.target.type === 'button') ? 'button' : tagName;
|
|
|
|
|
|
|
|
if(e.keyCode == jQuery.ui.keyCode.ENTER && tagName !== 'textarea' && tagName !== 'select' && tagName !=='button')
|
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
$j('button[default]',this.favorite_popup.parent()).trigger('click');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},this));
|
|
|
|
|
2013-12-13 00:45:09 +01:00
|
|
|
return false;
|
2013-12-06 00:22:54 +01:00
|
|
|
},
|
2013-12-06 22:26:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a favorite from the list and update preferences
|
|
|
|
* Registered as a handler on the delete icons
|
2014-02-12 12:23:04 +01:00
|
|
|
*
|
|
|
|
* @param {jQuery.event} event event object
|
2013-12-06 22:26:55 +01:00
|
|
|
*/
|
|
|
|
delete_favorite: function(event)
|
|
|
|
{
|
|
|
|
// Don't do the menu
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
var app = event.data;
|
2013-12-13 00:45:09 +01:00
|
|
|
var id = $j(this).parentsUntil('li').parent().attr("data-id");
|
2014-02-12 18:33:22 +01:00
|
|
|
var group = $j(this).parentsUntil('li').parent().attr("data-group") || '';
|
2013-12-13 00:45:09 +01:00
|
|
|
var line = $j('li[data-id="'+id+'"]',app.sidebox);
|
2013-12-12 01:38:06 +01:00
|
|
|
var name = line.first().text();
|
2013-12-06 22:26:55 +01:00
|
|
|
var trash = this;
|
2013-12-13 00:45:09 +01:00
|
|
|
line.addClass('loading');
|
2013-12-06 22:26:55 +01:00
|
|
|
|
|
|
|
// Make sure first
|
|
|
|
var do_delete = function(button_id)
|
|
|
|
{
|
2013-12-12 01:00:54 +01:00
|
|
|
if(button_id != et2_dialog.YES_BUTTON)
|
|
|
|
{
|
2013-12-13 00:45:09 +01:00
|
|
|
line.removeClass('loading');
|
2013-12-12 01:00:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-06 22:26:55 +01:00
|
|
|
|
|
|
|
// Hide the trash
|
|
|
|
$j(trash).hide();
|
|
|
|
|
|
|
|
// Delete preference server side
|
|
|
|
var request = egw.json(app.appname + ".egw_framework.ajax_set_favorite.template",
|
2014-02-12 18:33:22 +01:00
|
|
|
[app.appname, id, "delete", group, ''],
|
2013-12-06 22:26:55 +01:00
|
|
|
function(result) {
|
2014-02-12 18:33:22 +01:00
|
|
|
// Got the full response from callback, which we don't want
|
|
|
|
if(result.type) return;
|
2014-02-12 22:51:25 +01:00
|
|
|
|
2014-02-12 18:33:22 +01:00
|
|
|
if(result && typeof result == 'boolean')
|
2013-12-06 22:26:55 +01:00
|
|
|
{
|
|
|
|
// Remove line from list
|
|
|
|
line.slideUp("slow", function() { });
|
2014-02-12 12:23:04 +01:00
|
|
|
|
2014-05-13 23:39:23 +02:00
|
|
|
app._refresh_fav_nm();
|
2013-12-06 22:26:55 +01:00
|
|
|
}
|
2013-12-07 00:26:48 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Something went wrong server side
|
|
|
|
line.removeClass('loading').addClass('ui-state-error');
|
|
|
|
}
|
2013-12-06 22:26:55 +01:00
|
|
|
},
|
|
|
|
$j(trash).parentsUntil("li").parent(),
|
|
|
|
true,
|
|
|
|
$j(trash).parentsUntil("li").parent()
|
|
|
|
);
|
|
|
|
request.sendRequest(true);
|
2014-02-12 12:23:04 +01:00
|
|
|
};
|
2013-12-06 22:26:55 +01:00
|
|
|
et2_dialog.show_dialog(do_delete, (egw.lang("Delete") + " " +name +"?"),
|
|
|
|
"Delete", et2_dialog.YES_NO, et2_dialog.QUESTION_MESSAGE);
|
2013-12-13 00:45:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-09 10:45:59 +02:00
|
|
|
});
|