* 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(); var egw_fw = egw_getFramework();
this.sidebox_target = $j("#"+this.options.sidebox_target,egw_fw.sidemenuDiv); this.sidebox_target = $j("#"+this.options.sidebox_target,egw_fw.sidemenuDiv);
} }
// Store array of sorted items
this.favSortedList = [];
var apps = egw().user('apps'); var apps = egw().user('apps');
this.is_admin = (typeof apps['admin'] != "undefined"); this.is_admin = (typeof apps['admin'] != "undefined");
this.stored_filters = this.load_favorites(this.options.app); this.stored_filters = this.load_favorites(this.options.app);
this.preferred = egw.preference(this.options.default_pref,this.options.app); this.preferred = egw.preference(this.options.default_pref,this.options.app);
if(!this.preferred || typeof this.stored_filters[this.preferred] == "undefined") if(!this.preferred || typeof this.stored_filters[this.preferred] == "undefined")
{ {
this.preferred = "blank"; this.preferred = "blank";
} }
// It helps to have the ID properly set before we get too far // It helps to have the ID properly set before we get too far
this.set_id(this.id); 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); 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 // Add a listener on the delete to remove
this.menu.on("click","div.ui-icon-trash", app[self.options.app], function() { 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 // 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); this.init_filters(this);
} }
}, },
/** /**
* Load favorites from preferences * Load favorites from preferences
* *
@ -182,7 +210,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
state: {} state: {}
} }
}; };
// Load saved favorites // Load saved favorites
var preferences = egw.preference("*",app); var preferences = egw.preference("*",app);
for(var pref_name in preferences) 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; 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) if(typeof stored_filters == "undefined" || !stored_filters)
{ {
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; return stored_filters;
}, },

View File

@ -40,32 +40,36 @@ class egw_favorites
*/ */
public static function list_favorites($app, $default=null) 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 // This target is used client-side to find & enable adding new favorites
$target = 'favorite_sidebox_'.$app; $target = 'favorite_sidebox_'.$app;
$pref_prefix = 'favorite_';
$filters = array( /* @var $filters array an array of favorites*/
'blank' => array( $filters = self::get_favorites($app);
'name' => lang('No filters'),
// Old
'filter' => array(),
// New
'state' => array(),
'group' => true
)
) + self::get_favorites($app);
$is_admin = $GLOBALS['egw_info']['user']['apps']['admin']; $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"; $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]; $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 // Get link for if there is no nextmatch - this is the fallback
$registry = egw_link::get_registry($app,'list'); $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) foreach($filters as $name => $filter)
{ {
$href = "javascript:app.$app.setState(" . json_encode($filter,JSON_FORCE_OBJECT) . ');'; $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 // 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 .= html::image($app, 'new') . lang('Add current'). '</a></li>';
$html .= '</ul></span>'; $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 * Get a list of actual user favorites
* The default 'Blank' favorite is not included here * The default 'Blank' favorite is not included here
@ -106,9 +132,21 @@ class egw_favorites
*/ */
public static function get_favorites($app) 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_'; $pref_prefix = 'favorite_';
$sorted_list = array();
$fav_sort_pref = self::get_fav_sort_pref($app);
// Look through all preferences & pull out favorites // Look through all preferences & pull out favorites
foreach($GLOBALS['egw_info']['user']['preferences'][$app] as $pref_name => $pref) 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(!is_array($pref)) // old favorite
{ {
if (!($pref = unserialize($pref))) continue; if (!($pref = unserialize($pref)))
{
continue;
}
$pref = array( $pref = array(
'name' => substr($pref_name,strlen($pref_prefix)), 'name' => substr($pref_name,strlen($pref_prefix)),
'group' => !isset($GLOBALS['egw']->preferences->user[$app][$pref_name]), '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; $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; return $favorites;
} }