Continue moving most favorite functionality from favorite widget into framework. Favorite widget now uses framework.

This commit is contained in:
Nathan Gray 2013-12-12 23:45:09 +00:00
parent 23fd7066bf
commit a79f7b87a6
3 changed files with 66 additions and 258 deletions

View File

@ -121,12 +121,6 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
// Set the default (button) value // Set the default (button) value
this.set_value(this.preferred,true); this.set_value(this.preferred,true);
// If a pre-selected filter was passed from server
if(typeof this.options.value != "undefined")
{
this.set_value(this.options.value);
}
var self = this; var self = this;
// Add a listener on the radio buttons to set default filter // Add a listener on the radio buttons to set default filter
@ -158,7 +152,10 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
}); });
// Add a listener on the delete to remove // Add a listener on the delete to remove
this.menu.on("click","div.ui-icon-trash", this, this.delete_favorite) 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[self.options.app].delete_favorite.apply(app[self.options.app],arguments);
})
// Wrap and unwrap because jQueryUI styles use a parent, and we don't want to change the state of the menu item // Wrap and unwrap because jQueryUI styles use a parent, and we don't want to change the state of the menu item
// Wrap in a span instead of a div because div gets a border // Wrap in a span instead of a div because div gets a border
.on("mouseenter","div.ui-icon-trash", function() {$j(this).wrap("<span class='ui-state-active'/>");}) .on("mouseenter","div.ui-icon-trash", function() {$j(this).wrap("<span class='ui-state-active'/>");})
@ -172,20 +169,6 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
} }
}, },
destroy: function() {
if(this.popup != null)
{
if(this.popup.group)
{
this.popup.group.free();
delete this.popup.group;
}
this.popup.dialog("destroy");
this.popup = null;
}
this._super.apply(this, arguments);
},
/** /**
* Load favorites from preferences * Load favorites from preferences
* *
@ -197,7 +180,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
var stored_filters = { var stored_filters = {
'blank': { 'blank': {
name: this.egw().lang("No filters"), name: this.egw().lang("No filters"),
filters: {}, state: {},
} }
}; };
@ -224,49 +207,6 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
return stored_filters; return stored_filters;
}, },
/**
* Delete a favorite from the list and update preferences
* Registered as a handler on the delete icons
*/
delete_favorite: function(event)
{
// Don't do the menu
event.stopImmediatePropagation();
var header = event.data;
var name = $j(this).parentsUntil("li").parent().attr("data-id");
var trash = this;
// Make sure first
var do_delete = function(button_id)
{
if(button_id != et2_dialog.YES_BUTTON) return;
// Hide the trash
$j(trash).hide();
// Delete preference server side
var request = egw.json(header.egw().getAppName() + ".etemplate_widget_nextmatch.ajax_set_favorite.template",
[header.app, name, "delete", header.stored_filters[name].group ? header.stored_filters[name].group : '', ''],
function(result) {
if(result)
{
// Remove line from list
this.slideUp("slow", function() { header.menu.hide();});
delete header.stored_filters[name];
header.init_filters(header);
}
},
$j(trash).parentsUntil("li").parent(),
true,
$j(trash).parentsUntil("li").parent()
);
request.sendRequest();
}
et2_dialog.show_dialog(do_delete, (header.egw().lang("Delete") + " " +header.stored_filters[name].name +"?"),
"Delete", et2_dialog.YES_NO, et2_dialog.QUESTION_MESSAGE);
},
// Create & set filter options for dropdown menu // Create & set filter options for dropdown menu
init_filters: function(widget, filters) init_filters: function(widget, filters)
{ {
@ -295,24 +235,6 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
// Set radio to current value // Set radio to current value
$j("input[value='"+ this.preferred +"']:radio", this.menu).attr("checked",true); $j("input[value='"+ this.preferred +"']:radio", this.menu).attr("checked",true);
// Clone for sidebox
/*
if(this.sidebox_target.length)
{
this.sidebox_target.empty();
var sidebox_clone = widget.menu.clone();
sidebox_clone
.appendTo(this.sidebox_target)
.removeAttr('style')
.menu()
.removeClass("ui-widget")
.show()
.find("input:checked").replaceWith("<div class='ui-icon ui-icon-heart'/>");
sidebox_clone
.find("input").replaceWith("<img class='sideboxstar'/>");
}
*/
}, },
set_nm_filters: function(filters) set_nm_filters: function(filters)
@ -340,213 +262,59 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
} }
}, },
// Apply the favorite when you pick from the list // Apply the favorite when you pick from the list
change: function(selected_node) { change: function(selected_node) {
this.value = $j(selected_node).attr("data-id"); this.value = $j(selected_node).attr("data-id");
if(this.value == "add" && this.nextmatch) if(this.value == "add" && this.nextmatch)
{ {
// Get current filters // Get current filters
this.popup.current_filters = $j.extend({},this.nextmatch.activeFilters); var current_filters = $j.extend({},this.nextmatch.activeFilters);
// Add in extras // Add in extras
for(var extra in this.options.filters) for(var extra in this.options.filters)
{ {
// Don't overwrite what nm has, chances are nm has more up-to-date value // Don't overwrite what nm has, chances are nm has more up-to-date value
if(typeof this.popup.current_filters == 'undefined') if(typeof current_filters == 'undefined')
{ {
this.popup.current_filters[extra] = this.nextmatch.options.settings[extra]; current_filters[extra] = this.nextmatch.options.settings[extra];
} }
} }
// Skip columns for now // Skip columns for now
delete this.popup.current_filters.selcolumns; delete current_filters.selcolumns;
// Add in application's settings // Add in application's settings
if(this.filters != true) if(this.filters != true)
{ {
for(var i = 0; i < this.filters.length; i++) 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]]; current_filters[this.options.filters[i]] = this.nextmatch.options.settings[this.options.filters[i]];
} }
} }
// Remove some internal values // Remove some internal values
delete this.popup.current_filters[this.id]; delete current_filters[this.id];
if(this.popup.group != undefined) if(this.popup.group != undefined)
{ {
delete this.popup.current_filters[this.popup.group.id]; delete current_filters[this.popup.group.id];
} }
// Make sure it's an object - deep copy to prevent references in sub-objects (col_filters) // Call framework
this.popup.current_filters = jQuery.extend(true,{},this.popup.current_filters); app[this.options.app].add_favorite(current_filters);
// 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>");
}
add_to_popup(this.popup.current_filters);
$j("#nm_favorites_popup_filters",this.popup)
.replaceWith(
$j(filter_list.join("")).attr("id","nm_favorites_popup_filters")
);
$j("#nm_favorites_popup_filters",this.popup)
.hide()
.siblings(".ui-icon-circle-plus")
.removeClass("ui-icon-circle-minus");
// Popup
this.popup.dialog("open");
// Reset value // Reset value
this.set_value(this.preferred,true); this.set_value(this.preferred,true);
} }
}, },
/**
* Create the "Add new" popup dialog
*/
create_popup: function()
{
var self = this;
// Clear old, if existing
if(this.popup && this.popup.group)
{
this.popup.group.free();
delete this.popup;
}
// Create popup
this.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.dom_id+'nm_favorites_popup_admin"/>\
<span>'+ this.egw().lang("Details") + '</span><span style="float:left;" class="ui-icon ui-icon-circle-plus" />\
<ul id="nm_favorites_popup_filters"/>\
</form>\
</div>'
).appendTo(this.div);
$j(".ui-icon-circle-plus",this.popup).prev().andSelf().click(function() {
var details = $j("#nm_favorites_popup_filters",this.popup)
.slideToggle()
.siblings(".ui-icon-circle-plus")
.toggleClass("ui-icon-circle-minus");
});
// Add some controls if user is an admin
if(this.is_admin)
{
this.popup.group = et2_createWidget("select-account",{
id: "favorite[group]",
account_type: "groups",
empty_label: "Groups",
no_lang: true,
parent_node: this.dom_id+'nm_favorites_popup_admin'
},this);
}
var buttons = {};
buttons[this.egw().lang("save")] = function() {
// Add a new favorite
var name = $j("#name",this);
if(name.val())
{
// Add to the list
name.val(name.val().replace(/(<([^>]+)>)/ig,""));
var safe_name = name.val().replace(/[^A-Za-z0-9-_]/g,"_");
self.stored_filters[safe_name] = {
name: name.val(),
group: (typeof self.popup.group != "undefined" &&
self.popup.group.get_value() ? self.popup.group.get_value() : false),
state: self.popup.current_filters
};
self.init_filters(self);
var favorite_pref = self.favorite_prefix+safe_name;
// Save to preferences
if(typeof self.popup.group != "undefined" && self.popup.group.getValue() != '')
{
// Admin stuff - save preference server side
var request = egw.json("home.egw_framework.ajax_set_favorite.template",
[
self.options.app,
name.val(),
"add",
self.popup.group.get_value(),
self.popup.current_filters
]
);
request.sendRequest();
self.popup.group.set_value('');
}
else
{
// Normal user - just save to preferences client side
self.egw().set_preference(self.options.app,favorite_pref,{
name: name.val(),
group: false,
state:self.popup.current_filters
});
}
delete self.popup.current_filters;
}
// Reset form
name.val("");
$j("#filters",self.popup).empty();
$j(this).dialog("close");
};
buttons[this.egw().lang("cancel")] = function() {
self.popup.group.set_value(null);
$j(this).dialog("close");
};
this.popup.dialog({
autoOpen: false,
modal: true,
buttons: buttons,
close: function() {
}
});
},
set_value: function(filter_name, parent) { set_value: function(filter_name, parent) {
if(parent) if(parent)
{ {
return this._super.apply(filter_name); return this._super.apply(filter_name);
} }
if(this.nextmatch)
{ app[this.options.app].setState(this.stored_filters[filter_name]);
if(this.stored_filters[filter_name]) return false;
{
// Apply selected filter - make sure it's an object, and not a reference
this.set_nm_filters(jQuery.extend(true, {},this.stored_filters[filter_name].filter));
}
}
else
{
// Too soon - nm doesn't exist yet
this.nm_filter = filter_name;
}
}, },
getValue: function() getValue: function()
@ -570,9 +338,6 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader],
// Re-generate filter list so we can add 'Add current' // Re-generate filter list so we can add 'Add current'
this.init_filters(this); this.init_filters(this);
// With no Add current, this is only needed when there's a nm
this.create_popup();
} }
}); });
et2_register_widget(et2_favorites, ["favorites"]); et2_register_widget(et2_favorites, ["favorites"]);

View File

@ -1789,7 +1789,10 @@ $LAB.setOptions({AlwaysPreserveOrder:true,BasePath:"'.$GLOBALS['egw_info']['serv
$filters = array( $filters = array(
'blank' => array( 'blank' => array(
'name' => lang('No filters'), 'name' => lang('No filters'),
// Old
'filter' => array(), 'filter' => array(),
// New
'state' => array(),
'group' => true 'group' => true
) )
); );

View File

@ -481,10 +481,30 @@ var AppJS = Class.extend(
name.val() /*+(filter['group'] != false ? " ♦" :"")*/; name.val() /*+(filter['group'] != false ? " ♦" :"")*/;
html += "<div class='ui-icon ui-icon-trash' title='" + egw.lang('Delete') + "'/>"; html += "<div class='ui-icon ui-icon-trash' title='" + egw.lang('Delete') + "'/>";
html += "</a></li>\n"; html += "</a></li>\n";
$j(html).on('click.favorites',jQuery.proxy(function() {app[self.appname].setState(this);},state)) $j(html).on('click.favorites',jQuery.proxy(function(event) {
// Don't apply on delete
if($j(event.target).hasClass('ui-icon-trash'))
{
return;
}
app[self.appname].setState(this);
},state))
.insertBefore($j('li',self.sidebox).last()); .insertBefore($j('li',self.sidebox).last());
self._init_sidebox(self.sidebox); self._init_sidebox(self.sidebox);
} }
// Try to update nextmatch favorites too
if(etemplate2 && etemplate2.getByApplication)
{
var et2 = etemplate2.getByApplication(self.appname);
for(var i = 0; i < et2.length; i++)
{
et2[i].widgetContainer.iterateOver(function(_widget) {
var faves = _widget.load_favorites(self.appname);
_widget.init_filters(_widget,faves);
}, self, et2_favorites);
}
}
} }
// Reset form // Reset form
delete self.favorite_popup.state; delete self.favorite_popup.state;
@ -505,6 +525,8 @@ var AppJS = Class.extend(
close: function() { close: function() {
} }
}); });
return false;
}, },
/** /**
@ -517,17 +539,18 @@ var AppJS = Class.extend(
event.stopImmediatePropagation(); event.stopImmediatePropagation();
var app = event.data; var app = event.data;
var line = $j(this).parentsUntil("li").parent(); var id = $j(this).parentsUntil('li').parent().attr("data-id");
var line = $j('li[data-id="'+id+'"]',app.sidebox);
var name = line.first().text(); var name = line.first().text();
var trash = this; var trash = this;
$j(line).addClass('loading'); line.addClass('loading');
// Make sure first // Make sure first
var do_delete = function(button_id) var do_delete = function(button_id)
{ {
if(button_id != et2_dialog.YES_BUTTON) if(button_id != et2_dialog.YES_BUTTON)
{ {
$j(line).removeClass('loading'); line.removeClass('loading');
return; return;
} }
@ -536,12 +559,27 @@ var AppJS = Class.extend(
// Delete preference server side // Delete preference server side
var request = egw.json(app.appname + ".egw_framework.ajax_set_favorite.template", var request = egw.json(app.appname + ".egw_framework.ajax_set_favorite.template",
[app.appname, name, "delete", '', ''], [app.appname, id, "delete", '', ''],
function(result) { function(result) {
if(result) if(result)
{ {
// Remove line from list // Remove line from list
line.slideUp("slow", function() { }); line.slideUp("slow", function() { });
// Try to update nextmatch favorites too
if(etemplate2 && etemplate2.getByApplication)
{
var safe_name = name.replace(/[^A-Za-z0-9-_]/g,"_");
var et2 = etemplate2.getByApplication(app.appname);
for(var i = 0; i < et2.length; i++)
{
et2[i].widgetContainer.iterateOver(function(_widget) {
var faves = _widget.load_favorites(app.appname);
delete faves[safe_name];
_widget.init_filters(_widget,faves);
}, app, et2_favorites);
}
}
} }
else else
{ {
@ -557,5 +595,7 @@ var AppJS = Class.extend(
} }
et2_dialog.show_dialog(do_delete, (egw.lang("Delete") + " " +name +"?"), et2_dialog.show_dialog(do_delete, (egw.lang("Delete") + " " +name +"?"),
"Delete", et2_dialog.YES_NO, et2_dialog.QUESTION_MESSAGE); "Delete", et2_dialog.YES_NO, et2_dialog.QUESTION_MESSAGE);
},
return false;
}
}); });