Add the ability to track named popups opened using egw.open() or egw.open_link(). Unnamed popups or those opened in other ways are not tracked. You can get the list using egw.getOpenWindows(app, regex_filter), which returns a list of names.

Also, a wrapper around session storage.
This commit is contained in:
Nathan Gray 2013-10-05 09:28:12 +00:00
parent 7bf3db1a6f
commit 893648e189
3 changed files with 161 additions and 1 deletions

View File

@ -25,6 +25,7 @@
egw_jsonq;
egw_files;
egw_json;
egw_store;
egw_tooltip;
egw_css;
egw_calendar;

View File

@ -21,6 +21,46 @@
* @augments Class
*/
egw.extend('open', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
function _storeWindow(appname, popup)
{
// Don't store if it has no name
if(!popup.name || ['_blank'].indexOf(popup.name) >= 0)
{
return;
}
var _target_app = appname || this.appName || egw_appName || 'common';
var open_windows = JSON.parse(this.getSessionItem(_target_app, 'windows')) || [];
if(open_windows.indexOf(popup.name) >= 0)
{
// Already in there - don't add it again
return;
}
open_windows.push(popup.name);
this.setSessionItem(_target_app, 'windows', JSON.stringify(open_windows));
// Forget window when it closes
var _egw = this;
(function() {
var app = _target_app;
var window_name = popup.name;
var poll_timer = _wnd.setInterval(function() {
if(popup.closed !== false) {
this.clearInterval(poll_timer);
var open_windows = JSON.parse(_egw.getSessionItem(app, 'windows')) || [];
var index = open_windows.indexOf(window_name);
if(index >= 0)
{
open_windows.splice(index,1);
}
_egw.setSessionItem(app, 'windows', JSON.stringify(open_windows));
}
},1000);
})();
}
return {
/**
* View an EGroupware entry: opens a popup of correct size or redirects window.location to requested url
@ -161,7 +201,10 @@ egw.extend('open', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
{
var w_h = _popup.split('x');
if (w_h[1] == 'egw_getWindowOuterHeight()') w_h[1] = egw_getWindowOuterHeight();
return _wnd.egw_openWindowCentered2(url, _target, w_h[0], w_h[1], false, _target_app, true);
var popup_window = _wnd.egw_openWindowCentered2(url, _target, w_h[0], w_h[1], false, _target_app, true);
// Remember which windows are open
_storeWindow.call(this, _target_app, popup_window);
}
else if (typeof _wnd.egw_link_handler == 'function' && (typeof _target == 'undefined' || _target =='_self' || typeof this.link_app_list()[_target] != "undefined"))
{
@ -181,6 +224,35 @@ egw.extend('open', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
{
return _wnd.open(url, _target);
}
},
/**
* Get a list of the names of open popups
*
* Using the name, you can get a reference to the popup using:
* window.open('', name);
* Popups that were not given a name when they were opened are not tracked.
*
* @param {string} appname Application that owns/opened the popup
* @param {string} regex Optionally filter names by the given regular expression
*
* @returns {string[]} List of window names
*/
getOpenWindows: function(appname, regex) {
var open_windows = JSON.parse(this.getSessionItem(appname, 'windows')) || [];
if(typeof regex == 'undefined')
{
return open_windows;
}
var list = []
for(var i = 0; i < open_windows.length; i++)
{
if(open_windows[i].match(regex))
{
list.push(open_windows[i]);
}
}
return list;
}
};
});

View File

@ -0,0 +1,87 @@
/**
* EGroupware clientside API for persistant storage
*
* @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
egw_core;
egw_ready;
egw_debug;
*/
/**
* Store is a wrapper around browser based, persistant storage.
*
*
* @see http://www.w3.org/TR/webstorage/#storage
*
* @param {type} param1
* @param {type} param2
* @param {type} param3
*/
egw.extend('store', egw.MODULE_WND_LOCAL, function(_app, _wnd) {
var egw = this;
/**
* Since the storage is shared across at least all applications, make
* the key include some extra info.
*
* @param {string} key
* @returns {undefined}
*/
function mapKey(application, key)
{
return egw.config('phpgwapi', 'instance_id') + '-' + application + '-' + key;
}
return {
/**
* Retrieve a value from session storage
*
* @param {string} application Name of application, or common
* @param {string} key
* @returns {string}
*/
getSessionItem: function(application, key) {
key = mapKey(application, key);
return window.sessionStorage.getItem(key);
},
/**
* Set a value in session storage
*
* @param {string} application Name of application, or common
* @param {string} key
* @param {string} value
* @returns {@exp;window@pro;sessionStorage@call;setItem}
*/
setSessionItem: function(application, key, value) {
key = mapKey(application, key);
return window.sessionStorage.setItem(key, value);
},
/**
* Remove a value from session storage
* @param {string} application
* @param {string} key
* @returns {@exp;window@pro;sessionStorage@call;removeItem}
*/
removeSessionItem: function(application, key) {
key = uniqueKey(application, key);
return window.sessionStorage.removeItem(key);
},
}
});