* Favorites: Make favorites item sortable and store their orders as user preference

This commit is contained in:
Hadi Nategh 2014-04-23 11:42:48 +00:00
parent b61c69f3dc
commit 9805f019a1
2 changed files with 126 additions and 28 deletions

View File

@ -99,18 +99,20 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
var egw_fw = egw_getFramework();
this.sidebox_target = $j("#"+this.options.sidebox_target,egw_fw.sidemenuDiv);
}
// Store array of sorted items
this.favSortedList = [];
var apps = egw().user('apps');
this.is_admin = (typeof apps['admin'] != "undefined");
this.stored_filters = this.load_favorites(this.options.app);
this.preferred = egw.preference(this.options.default_pref,this.options.app);
if(!this.preferred || typeof this.stored_filters[this.preferred] == "undefined")
{
this.preferred = "blank";
}
// It helps to have the ID properly set before we get too far
this.set_id(this.id);
@ -149,7 +151,33 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
self.button.removeClass("ui-state-active",2000);
});
});
//Add Sortable handler to nm fav. menu
$j(this.menu).sortable({
items:'li:not([data-id^="add"])',
placeholder:'ui-fav-sortable-placeholder',
update: function (event, ui)
{
self.favSortedList = jQuery(this).sortable('toArray', {attribute:'data-id'});
self.egw().set_preference(self.options.app,'fav_sort_pref',self.favSortedList);
this.sidebox_target.children()
}
});
$j(this.sidebox_target.children()).sortable({
items:'li:not([data-id^="add"])',
placeholder:'ui-fav-sortable-placeholder',
update: function (event, ui)
{
self.favSortedList = jQuery(this).sortable('toArray', {attribute:'data-id'});
self.egw().set_preference(self.options.app,'fav_sort_pref',self.favSortedList);
self.init_filters(self,self.load_favorites(self.options.app));
}
});
// Add a listener on the delete to remove
this.menu.on("click","div.ui-icon-trash", app[self.options.app], function() {
// App instance might not be ready yet, so don't bind directly
@ -167,7 +195,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
this.init_filters(this);
}
},
/**
* Load favorites from preferences
*
@ -182,7 +210,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
state: {}
}
};
// Load saved favorites
var preferences = egw.preference("*",app);
for(var pref_name in preferences)
@ -197,12 +225,34 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
stored_filters[pref_name].state = preferences[pref_name].filters;
}
}
if (pref_name == 'fav_sort_pref')
{
this.favSortedList = preferences[pref_name];
}
}
if(typeof stored_filters == "undefined" || !stored_filters)
{
stored_filters = {};
}
else if (this.favSortedList.length > 0)
{
var sortedListObj = {};
for (var i=0;i < this.favSortedList.length;i++)
{
if (typeof stored_filters[this.favSortedList[i]] != 'undefined')
{
sortedListObj[this.favSortedList[i]] = stored_filters[this.favSortedList[i]];
}
else
{
this.favSortedList.splice(i,1);
this.egw().set_preference (this.options.app,'fav_sort_pref',this.favSortedList);
}
}
stored_filters = jQuery.extend(sortedListObj,stored_filters);
}
return stored_filters;
},

View File

@ -40,32 +40,36 @@ class egw_favorites
*/
public static function list_favorites($app, $default=null)
{
if(!$app) return '';
if (!$app)
{
return '';
}
if (!$default) $default = "nextmatch-$app.index.rows-favorite";
if (!$default)
{
$default = "nextmatch-$app.index.rows-favorite";
}
// This target is used client-side to find & enable adding new favorites
$target = 'favorite_sidebox_'.$app;
$pref_prefix = 'favorite_';
$filters = array(
'blank' => array(
'name' => lang('No filters'),
// Old
'filter' => array(),
// New
'state' => array(),
'group' => true
)
) + self::get_favorites($app);
/* @var $filters array an array of favorites*/
$filters = self::get_favorites($app);
$is_admin = $GLOBALS['egw_info']['user']['apps']['admin'];
$html = "<span id='$target' class='ui-helper-clearfix sidebox-favorites'><ul class='ui-menu ui-widget-content ui-corner-all favorites' role='listbox'>\n";
$default_filter = $GLOBALS['egw_info']['user']['preferences'][$app][$default];
if(!isset($default_filter) || !isset($filters[$default_filter])) $default_filter = "blank";
if (!isset($default_filter) || !isset($filters[$default_filter]))
{
$default_filter = "blank";
}
// Get link for if there is no nextmatch - this is the fallback
$registry = egw_link::get_registry($app,'list');
if(!$registry) $registry = egw_link::get_registry($app,'index');
if (!$registry)
{
$registry = egw_link::get_registry($app, 'index');
}
foreach($filters as $name => $filter)
{
$href = "javascript:app.$app.setState(" . json_encode($filter,JSON_FORCE_OBJECT) . ');';
@ -81,7 +85,7 @@ class egw_favorites
}
// If were're here, the app supports favorites, so add a 'Add' link too
$html .= "<li class='ui-menu-item' role='menuitem'><a href='javascript:app.$app.add_favorite()' class='ui-corner-all'>";
$html .= "<li data-id='add' class='ui-menu-item' role='menuitem'><a href='javascript:app.$app.add_favorite()' class='ui-corner-all'>";
$html .= html::image($app, 'new') . lang('Add current'). '</a></li>';
$html .= '</ul></span>';
@ -95,7 +99,29 @@ class egw_favorites
),
);
}
/**
* Get preferenced favorites sorted list
*
* @param string $app Application name as string
*
* @return (array|boolean) An array of sorted favorites or False if there's no preferenced sorted list
*
*/
public static function get_fav_sort_pref ($app)
{
$fav_sorted_list = array();
if (($fav_sorted_list = $GLOBALS['egw_info']['user']['preferences'][$app]['fav_sort_pref']))
{
return $fav_sorted_list;
}
else
{
return false;
}
}
/**
* Get a list of actual user favorites
* The default 'Blank' favorite is not included here
@ -106,9 +132,21 @@ class egw_favorites
*/
public static function get_favorites($app)
{
$favorites = array();
$favorites = array(
'blank' => array(
'name' => lang('No filters'),
// Old
'filter' => array(),
// New
'state' => array(),
'group' => true
)
);
$pref_prefix = 'favorite_';
$sorted_list = array();
$fav_sort_pref = self::get_fav_sort_pref($app);
// Look through all preferences & pull out favorites
foreach($GLOBALS['egw_info']['user']['preferences'][$app] as $pref_name => $pref)
{
@ -116,7 +154,10 @@ class egw_favorites
{
if(!is_array($pref)) // old favorite
{
if (!($pref = unserialize($pref))) continue;
if (!($pref = unserialize($pref)))
{
continue;
}
$pref = array(
'name' => substr($pref_name,strlen($pref_prefix)),
'group' => !isset($GLOBALS['egw']->preferences->user[$app][$pref_name]),
@ -128,7 +169,14 @@ class egw_favorites
$favorites[(string)substr($pref_name,strlen($pref_prefix))] = $pref;
}
}
if (is_array($sorted_list))
{
foreach ($fav_sort_pref as $key)
{
$sorted_list[$key] = $favorites[$key];
}
$favorites = $sorted_list;
}
return $favorites;
}