Skip to content

Commit

Permalink
[cmd:sort] fix #2472 keep stay the context menu when choosing the sor…
Browse files Browse the repository at this point in the history
…t menu
  • Loading branch information
nao-pon committed Apr 11, 2018
1 parent 201a730 commit 402ef45
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
72 changes: 52 additions & 20 deletions js/commands/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,52 @@ elFinder.prototype.commands.sort = function() {
setVar = function() {
self.variants = [];
$.each(fm.sortRules, function(name, value) {
var sort = {
type : name,
order : name == fm.sortType ? fm.sortOrder == 'asc' ? 'desc' : 'asc' : fm.sortOrder
};
if ($.inArray(name, fm.sorters) !== -1) {
var arr = name == fm.sortType ? (sort.order == 'asc'? 's' : 'n') : '';
self.variants.push([sort, (arr? '<span class="ui-icon ui-icon-arrowthick-1-'+arr+'"></span>' : '') + '&nbsp;' + fm.i18n('sort'+name)]);
var arr = (name === fm.sortType)? (fm.sortOrder === 'asc'? 'n' : 's') : '';
self.variants.push([name, (arr? '<span class="ui-icon ui-icon-arrowthick-1-'+arr+'"></span>' : '') + '&nbsp;' + fm.i18n('sort'+name)]);
}
});
self.variants.push('|');
self.variants.push([
{
type : fm.sortType,
order : fm.sortOrder,
stick : !fm.sortStickFolders,
tree : fm.sortAlsoTreeview
},
'stick',
(fm.sortStickFolders? '<span class="ui-icon ui-icon-check"/>' : '') + '&nbsp;' + fm.i18n('sortFoldersFirst')
]);
if (fm.ui.tree) {
self.variants.push('|');
self.variants.push([
{
type : fm.sortType,
order : fm.sortOrder,
stick : fm.sortStickFolders,
tree : !fm.sortAlsoTreeview
},
'tree',
(fm.sortAlsoTreeview? '<span class="ui-icon ui-icon-check"/>' : '') + '&nbsp;' + fm.i18n('sortAlsoTreeview')
]);
}
};
updateContextmenu();
},
updateContextmenu = function() {
var cm = fm.getUI('contextmenu'),
icon, sub;
if (cm.is(':visible')) {
icon = cm.find('span.elfinder-button-icon-sort');
sub = icon.siblings('div.elfinder-contextmenu-sub');
sub.find('span.ui-icon').remove();
sub.children('div.elfinder-contextsubmenu-item').each(function() {
var tgt = $(this).children('span'),
name = tgt.text().trim(),
arr;
if (name === (i18Name.stick || (i18Name.stick = fm.i18n('sortFoldersFirst')))) {
if (fm.sortStickFolders) {
tgt.prepend('<span class="ui-icon ui-icon-check"/>');
}
} else if (name === (i18Name.tree || (i18Name.tree = fm.i18n('sortAlsoTreeview')))) {
if (fm.sortAlsoTreeview) {
tgt.prepend('<span class="ui-icon ui-icon-check"/>');
}
} else if (name === (i18Name[fm.sortType] || (i18Name[fm.sortType] = fm.i18n('sort' + fm.sortType)))) {
arr = fm.sortOrder === 'asc'? 'n' : 's';
tgt.prepend('<span class="ui-icon ui-icon-arrowthick-1-'+arr+'"></span>');
}
});
}
},
i18Name = {};

/**
* Command options
Expand All @@ -51,6 +65,8 @@ elFinder.prototype.commands.sort = function() {
*/
this.options = {ui : 'sortbutton'};

this.keepContextmenu = true;

fm.bind('open sortchange', setVar)
.bind('open', function() {
fm.unbind('add', setVar).one('add', setVar);
Expand Down Expand Up @@ -97,8 +113,24 @@ elFinder.prototype.commands.sort = function() {
return 0;
};

this.exec = function(hashes, sortopt) {
this.exec = function(hashes, cOpt) {
var fm = this.fm,
sortopt = $.isPlainObject(cOpt)? cOpt : (function() {
cOpt += '';
var sOpts = {};
if (cOpt === 'stick') {
sOpts.stick = !fm.sortStickFolders;
} else if (cOpt === 'tree') {
sOpts.tree = !fm.sortAlsoTreeview;
} else if ($.inArray(cOpt, fm.sorters) !== -1) {
if (fm.sortType === cOpt) {
sOpts.order = fm.sortOrder === 'asc'? 'desc' : 'asc';
} else {
sOpts.type = cOpt;
}
}
return sOpts;
})(),
sort = Object.assign({
type : fm.sortType,
order : fm.sortOrder,
Expand Down
22 changes: 22 additions & 0 deletions js/elFinder.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,33 @@ elFinder.prototype.command = function(fm) {
*/
this._disabled = false;

/**
* If true, this command is disabled on serach results
*
* @type Boolean
*/
this.disableOnSearch = false;

/**
* Call update() when event select fired
*
* @type Boolean
*/
this.updateOnSelect = true;

/**
* Sync toolbar button title on change
*
* @type Boolean
*/
this.syncTitleOnChange = false;

/**
* Keep display of the context menu when command execution
*
* @type Boolean
*/
this.keepContextmenu = false;

/**
* elFinder events defaults handlers.
Expand Down
6 changes: 6 additions & 0 deletions js/elFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9328,6 +9328,12 @@ if (!String.prototype.repeat) {
return rpt;
};
}
// String.trim
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
// Array.apply
(function () {
try {
Expand Down
34 changes: 20 additions & 14 deletions js/ui/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ $.fn.elfindercontextmenu = function(fm) {
cmItem = 'elfinder-contextmenu-item',
smItem = 'elfinder-contextsubmenu-item',
exIcon = 'elfinder-contextmenu-extra-icon',
cHover = fm.res('class', 'hover'),
dragOpt = {
distance: 8,
start: function() {
menu.data('drag', true).data('touching') && menu.find('.ui-state-hover').removeClass('ui-state-hover');
menu.data('drag', true).data('touching') && menu.find('.'+cHover).removeClass(cHover);
},
stop: function() {
menu.data('draged', true).removeData('drag');
Expand All @@ -22,19 +23,19 @@ $.fn.elfindercontextmenu = function(fm) {
menu = $(this).addClass('touch-punch ui-helper-reset ui-front ui-widget ui-state-default ui-corner-all elfinder-contextmenu elfinder-contextmenu-'+fm.direction)
.hide()
.on('touchstart', function(e) {
menu.data('touching', true).children().removeClass('ui-state-hover');
menu.data('touching', true).children().removeClass(cHover);
})
.on('touchend', function(e) {
menu.removeData('touching');
})
.on('mouseenter mouseleave', '.'+cmItem, function(e) {
$(this).toggleClass('ui-state-hover', (e.type === 'mouseenter' || (! menu.data('draged') && menu.data('submenuKeep'))? true : false));
$(this).toggleClass(cHover, (e.type === 'mouseenter' || (! menu.data('draged') && menu.data('submenuKeep'))? true : false));
if (menu.data('draged') && menu.data('submenuKeep')) {
menu.find('.elfinder-contextmenu-sub:visible').parent().addClass('ui-state-hover');
menu.find('.elfinder-contextmenu-sub:visible').parent().addClass(cHover);
}
})
.on('mouseenter mouseleave', '.'+exIcon, function(e) {
$(this).parent().toggleClass('ui-state-hover', e.type === 'mouseleave');
$(this).parent().toggleClass(cHover, e.type === 'mouseleave');
})
.on('mouseenter mouseleave', '.'+cmItem+',.'+smItem, function(e) {
var setIndex = function(target, sub) {
Expand All @@ -54,15 +55,15 @@ $.fn.elfindercontextmenu = function(fm) {
var target = $(this),
unHover = function() {
if (selected && !selected.children('div.elfinder-contextmenu-sub:visible').length) {
selected.removeClass('ui-state-hover');
selected.removeClass(cHover);
}
};
if (e.type === 'mouseenter') {
// mouseenter
if (target.hasClass(smItem)) {
// submenu
if (subselected) {
subselected.removeClass('ui-state-hover');
subselected.removeClass(cHover);
}
if (selected) {
subnodes = selected.find('div.'+smItem);
Expand Down Expand Up @@ -197,7 +198,7 @@ $.fn.elfindercontextmenu = function(fm) {
if (code == ESC || code === sublev) {
if (selected && subnodes && subselected) {
subselected.trigger('mouseleave');
selected.addClass('ui-state-hover');
selected.addClass(cHover);
subnodes = null;
subselected = null;
} else {
Expand All @@ -224,7 +225,7 @@ $.fn.elfindercontextmenu = function(fm) {
} else if (code == UP && (! selected || --nodes._cur < 0)) {
nodes._cur = nodes.length - 1;
}
selected = nodes.eq(nodes._cur).addClass('ui-state-hover');
selected = nodes.eq(nodes._cur).addClass(cHover);
}
} else if (selected && (code == ENT || code === subent)) {
if (selected.hasClass('elfinder-contextmenu-group')) {
Expand All @@ -234,7 +235,7 @@ $.fn.elfindercontextmenu = function(fm) {
selected.trigger('mouseenter');
subnodes = selected.find('div.'+smItem);
subnodes._cur = 0;
subselected = subnodes.first().addClass('ui-state-hover');
subselected = subnodes.first().addClass(cHover);
}
} else {
code == ENT && selected.click();
Expand Down Expand Up @@ -433,8 +434,13 @@ $.fn.elfindercontextmenu = function(fm) {
var opts, $this;
e.stopPropagation();
if (! menu.data('draged')) {
menu.hide();
$this = $(this);
if (!cmd.keepContextmenu) {
menu.hide();
} else {
$this.removeClass(cHover);
node.addClass(cHover);
}
opts = $this.data('exec');
if (typeof opts === 'undefined') {
opts = {};
Expand All @@ -444,7 +450,7 @@ $.fn.elfindercontextmenu = function(fm) {
opts._currentType = type;
opts._currentNode = $this;
}
close();
!cmd.keepContextmenu && close();
fm.exec(cmd.name, targets, opts);
}
})
Expand All @@ -455,7 +461,7 @@ $.fn.elfindercontextmenu = function(fm) {
}
})
.on('mouseenter mouseleave', function(e){
if (! menu.data('touching')) {
if (! menu.data('touching') && !$(e.target).closest('.elfinder-contextmenu-sub', menu).length) {
if (node.data('timer')) {
clearTimeout(node.data('timer'));
node.removeData('timer');
Expand Down Expand Up @@ -495,7 +501,7 @@ $.fn.elfindercontextmenu = function(fm) {
} else {
node = item(cmd.title, cmd.className? cmd.className : cmd.name, function() {
if (! menu.data('draged')) {
close();
!cmd.keepContextmenu && close();
fm.exec(cmd.name, targets, {_userAction: true, _currentType: type, _currentNode: node});
}
});
Expand Down
27 changes: 12 additions & 15 deletions js/ui/sortbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $.fn.elfindersortbutton = function(cmd) {
button = $(this).addClass('ui-state-default elfinder-button elfinder-menubutton elfiner-button-'+name)
.attr('title', cmd.title)
.append('<span class="elfinder-button-icon elfinder-button-icon-'+name+'"/>', text)
.on('mouseenter mouseleave', function(e) { !button.hasClass(disabled) && button.toggleClass(hover); })
.on('mouseenter mouseleave', function(e) { !button.hasClass(disabled) && button.toggleClass(hover, e.type === 'mouseenter'); })
.on('click', function(e) {
if (!button.hasClass(disabled)) {
e.stopPropagation();
Expand All @@ -30,11 +30,10 @@ $.fn.elfindersortbutton = function(cmd) {
menu = $('<div class="ui-front ui-widget ui-widget-content elfinder-button-menu ui-corner-all"/>')
.hide()
.appendTo(fm.getUI())
.on('mouseenter mouseleave', '.'+item, function() { $(this).toggleClass(hover); })
.on('click', '.'+item, function(e) {
.on('mouseenter mouseleave', '.'+item, function(e) { $(this).toggleClass(hover, e.type === 'mouseenter'); })
.on('click', function(e) {
e.preventDefault();
e.stopPropagation();
hide();
}),
update = function() {
menu.children('[rel]').removeClass(selected+' '+asc+' '+desc)
Expand All @@ -61,33 +60,31 @@ $.fn.elfindersortbutton = function(cmd) {
});

menu.children().on('click', function(e) {
var type = $(this).attr('rel');

cmd.exec([], {
type : type,
order : type == fm.sortType ? fm.sortOrder == 'asc' ? 'desc' : 'asc' : fm.sortOrder,
stick : fm.sortStickFolders,
tree : fm.sortAlsoTreeview
});
cmd.exec([], $(this).removeClass(hover).attr('rel'));
});

$('<div class="'+item+' '+item+'-separated elfinder-sort-ext elfinder-sort-stick"><span class="ui-icon ui-icon-check"/>'+fm.i18n('sortFoldersFirst')+'</div>')
.appendTo(menu)
.on('click', function() {
cmd.exec([], {type : fm.sortType, order : fm.sortOrder, stick : !fm.sortStickFolders, tree : fm.sortAlsoTreeview});
cmd.exec([], 'stick');
});

if ($.fn.elfindertree && $.inArray('tree', fm.options.ui) !== -1) {
$('<div class="'+item+' '+item+'-separated elfinder-sort-ext elfinder-sort-tree"><span class="ui-icon ui-icon-check"/>'+fm.i18n('sortAlsoTreeview')+'</div>')
.appendTo(menu)
.on('click', function() {
cmd.exec([], {type : fm.sortType, order : fm.sortOrder, stick : fm.sortStickFolders, tree : !fm.sortAlsoTreeview});
cmd.exec([], 'tree');
});
}

fm.bind('disable select', hide).getUI().on('click', hide);

fm.bind('sortchange', update);
fm.bind('open', function() {
menu.children('[rel]').each(function() {
var $this = $(this);
$this.toggle($.inArray($this.attr('rel'), fm.sorters) !== -1);
});
}).bind('sortchange', update);

if (menu.children().length > 1) {
cmd.change(function() {
Expand Down

0 comments on commit 402ef45

Please sign in to comment.