2009-11-24 22:41:31 +03:00
|
|
|
var ContextMenu = new Class({
|
|
|
|
|
2014-11-30 16:14:09 +03:00
|
|
|
//implements
|
|
|
|
Implements: [Options, Events],
|
|
|
|
|
|
|
|
//options
|
|
|
|
options: {
|
|
|
|
actions: {},
|
|
|
|
menu: 'contextmenu',
|
|
|
|
stopEvent: true,
|
|
|
|
targets: 'body',
|
|
|
|
trigger: 'contextmenu',
|
|
|
|
offsets: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
onShow: $empty,
|
|
|
|
onHide: $empty,
|
|
|
|
onClick: $empty,
|
|
|
|
fadeSpeed: 200
|
|
|
|
},
|
|
|
|
|
|
|
|
//initialization
|
|
|
|
initialize: function(options) {
|
|
|
|
//set options
|
|
|
|
this.setOptions(options)
|
|
|
|
|
|
|
|
//option diffs menu
|
|
|
|
this.menu = $(this.options.menu);
|
|
|
|
this.targets = $$(this.options.targets);
|
|
|
|
|
|
|
|
//fx
|
|
|
|
this.fx = new Fx.Tween(this.menu, {
|
|
|
|
property: 'opacity',
|
|
|
|
duration: this.options.fadeSpeed,
|
|
|
|
onComplete: function() {
|
|
|
|
if (this.getStyle('opacity')) {
|
|
|
|
this.setStyle('visibility', 'visible');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setStyle('visibility', 'hidden');
|
|
|
|
}
|
|
|
|
}.bind(this.menu)
|
|
|
|
});
|
|
|
|
|
|
|
|
//hide and begin the listener
|
|
|
|
this.hide().startListener();
|
|
|
|
|
|
|
|
//hide the menu
|
|
|
|
this.menu.setStyles({
|
|
|
|
'position': 'absolute',
|
|
|
|
'top': '-900000px',
|
|
|
|
'display': 'block'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addTarget: function(t) {
|
|
|
|
this.targets[this.targets.length] = t;
|
|
|
|
t.addEvent(this.options.trigger, function(e) {
|
|
|
|
//enabled?
|
|
|
|
if (!this.options.disabled) {
|
|
|
|
//prevent default, if told to
|
|
|
|
if (this.options.stopEvent) {
|
|
|
|
e.stop();
|
|
|
|
}
|
|
|
|
//record this as the trigger
|
|
|
|
this.options.element = $(t);
|
|
|
|
//position the menu
|
|
|
|
this.menu.setStyles({
|
|
|
|
top: (e.page.y + this.options.offsets.y),
|
|
|
|
left: (e.page.x + this.options.offsets.x),
|
|
|
|
position: 'absolute',
|
|
|
|
'z-index': '2000'
|
|
|
|
});
|
|
|
|
//show the menu
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
t.addEvent('click', function(e) {
|
|
|
|
this.hide();
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
//get things started
|
|
|
|
startListener: function() {
|
|
|
|
/* all elements */
|
|
|
|
this.targets.each(function(el) {
|
|
|
|
/* show the menu */
|
|
|
|
el.addEvent(this.options.trigger, function(e) {
|
|
|
|
//enabled?
|
|
|
|
if (!this.options.disabled) {
|
|
|
|
//prevent default, if told to
|
|
|
|
if (this.options.stopEvent) {
|
|
|
|
e.stop();
|
|
|
|
}
|
|
|
|
//record this as the trigger
|
|
|
|
this.options.element = $(el);
|
|
|
|
//position the menu
|
|
|
|
this.menu.setStyles({
|
|
|
|
top: (e.page.y + this.options.offsets.y),
|
|
|
|
left: (e.page.x + this.options.offsets.x),
|
|
|
|
position: 'absolute',
|
|
|
|
'z-index': '2000'
|
|
|
|
});
|
|
|
|
//show the menu
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
el.addEvent('click', function(e) {
|
|
|
|
this.hide();
|
|
|
|
}.bind(this));
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
/* menu items */
|
|
|
|
this.menu.getElements('a').each(function(item) {
|
|
|
|
item.addEvent('click', function(e) {
|
2014-12-14 12:00:00 +03:00
|
|
|
e.preventDefault();
|
2014-11-30 16:14:09 +03:00
|
|
|
if (!item.hasClass('disabled')) {
|
|
|
|
this.execute(item.get('href').split('#')[1], $(this.options.element));
|
|
|
|
this.fireEvent('click', [item, e]);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
//hide on body click
|
|
|
|
$(document.body).addEvent('click', function() {
|
|
|
|
this.hide();
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
updateMenuItems: function () {
|
|
|
|
all_are_seq_dl = true;
|
2014-12-09 21:03:14 +03:00
|
|
|
there_are_seq_dl = false;
|
2014-12-09 19:54:35 +03:00
|
|
|
all_are_f_l_piece_prio = true;
|
2014-12-09 21:03:14 +03:00
|
|
|
there_are_f_l_piece_prio = false;
|
2014-12-09 19:54:35 +03:00
|
|
|
all_are_downloaded = true;
|
2014-12-09 21:03:14 +03:00
|
|
|
all_are_paused = true;
|
|
|
|
there_are_paused = false;
|
2015-04-15 20:13:18 +03:00
|
|
|
all_are_force_start = true;
|
2015-07-14 04:06:34 +03:00
|
|
|
there_are_force_start = false;
|
|
|
|
all_are_super_seeding = true;
|
2014-12-09 19:54:35 +03:00
|
|
|
|
2015-11-11 22:58:30 +03:00
|
|
|
var h = torrentsTable.selectedRowsIds();
|
2014-12-09 19:54:35 +03:00
|
|
|
h.each(function(item, index){
|
2015-11-11 22:58:30 +03:00
|
|
|
var data = torrentsTable.rows.get(item).full_data;
|
2014-12-09 21:03:14 +03:00
|
|
|
|
2014-12-24 04:39:18 +03:00
|
|
|
if (data['seq_dl'] != true)
|
2014-12-09 19:54:35 +03:00
|
|
|
all_are_seq_dl = false;
|
2014-12-09 21:03:14 +03:00
|
|
|
else
|
|
|
|
there_are_seq_dl = true;
|
|
|
|
|
2014-12-24 04:39:18 +03:00
|
|
|
if (data['f_l_piece_prio'] != true)
|
2014-12-09 19:54:35 +03:00
|
|
|
all_are_f_l_piece_prio = false;
|
2014-12-09 21:03:14 +03:00
|
|
|
else
|
|
|
|
there_are_f_l_piece_prio = true;
|
|
|
|
|
2014-12-24 04:39:18 +03:00
|
|
|
if (data['progress'] != 1.0) // not downloaded
|
2014-12-09 19:54:35 +03:00
|
|
|
all_are_downloaded = false;
|
2015-01-30 23:58:27 +03:00
|
|
|
else if (data['super_seeding'] != true)
|
|
|
|
all_are_super_seeding = false;
|
2014-12-09 21:03:14 +03:00
|
|
|
|
2015-07-14 04:06:34 +03:00
|
|
|
if (data['state'] != 'pausedUP' && data['state'] != 'pausedDL')
|
2014-12-09 21:03:14 +03:00
|
|
|
all_are_paused = false;
|
|
|
|
else
|
|
|
|
there_are_paused = true;
|
2015-07-14 04:06:34 +03:00
|
|
|
|
|
|
|
if (data['force_start'] != true)
|
|
|
|
all_are_force_start = false;
|
|
|
|
else
|
|
|
|
there_are_force_start = true;
|
2014-12-09 19:54:35 +03:00
|
|
|
});
|
|
|
|
|
2014-12-09 21:03:14 +03:00
|
|
|
show_seq_dl = true;
|
|
|
|
|
|
|
|
if (!all_are_seq_dl && there_are_seq_dl)
|
|
|
|
show_seq_dl = false;
|
|
|
|
|
|
|
|
show_f_l_piece_prio = true;
|
|
|
|
|
|
|
|
if (!all_are_f_l_piece_prio && there_are_f_l_piece_prio)
|
|
|
|
show_f_l_piece_prio = false;
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
if (all_are_downloaded) {
|
|
|
|
this.hideItem('SequentialDownload');
|
|
|
|
this.hideItem('FirstLastPiecePrio');
|
2015-01-30 23:58:27 +03:00
|
|
|
this.showItem('SuperSeeding');
|
|
|
|
this.setItemChecked('SuperSeeding', all_are_super_seeding);
|
2014-12-09 19:54:35 +03:00
|
|
|
} else {
|
2014-12-09 21:03:14 +03:00
|
|
|
if (!show_seq_dl && show_f_l_piece_prio)
|
|
|
|
this.menu.getElement('a[href$=FirstLastPiecePrio]').parentNode.addClass('separator');
|
|
|
|
else
|
|
|
|
this.menu.getElement('a[href$=FirstLastPiecePrio]').parentNode.removeClass('separator');
|
|
|
|
|
|
|
|
if (show_seq_dl)
|
|
|
|
this.showItem('SequentialDownload');
|
|
|
|
else
|
|
|
|
this.hideItem('SequentialDownload');
|
|
|
|
|
|
|
|
if (show_f_l_piece_prio)
|
|
|
|
this.showItem('FirstLastPiecePrio');
|
|
|
|
else
|
|
|
|
this.hideItem('FirstLastPiecePrio');
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
this.setItemChecked('SequentialDownload', all_are_seq_dl);
|
|
|
|
this.setItemChecked('FirstLastPiecePrio', all_are_f_l_piece_prio);
|
2015-01-30 23:58:27 +03:00
|
|
|
|
|
|
|
this.hideItem('SuperSeeding');
|
2014-12-09 19:54:35 +03:00
|
|
|
}
|
2014-12-09 21:03:14 +03:00
|
|
|
|
2015-07-14 04:06:34 +03:00
|
|
|
this.showItem('Start');
|
|
|
|
this.showItem('Pause');
|
|
|
|
this.showItem('ForceStart');
|
|
|
|
if (all_are_paused)
|
2014-12-09 21:03:14 +03:00
|
|
|
this.hideItem('Pause');
|
2015-07-14 04:06:34 +03:00
|
|
|
else if (all_are_force_start)
|
|
|
|
this.hideItem('ForceStart');
|
|
|
|
else if (!there_are_paused && !there_are_force_start)
|
|
|
|
this.hideItem('Start');
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
},
|
|
|
|
|
2014-11-30 16:14:09 +03:00
|
|
|
//show menu
|
|
|
|
show: function(trigger) {
|
2014-12-09 19:54:35 +03:00
|
|
|
this.updateMenuItems();
|
2014-11-30 16:14:09 +03:00
|
|
|
this.fx.start(1);
|
|
|
|
this.fireEvent('show');
|
|
|
|
this.shown = true;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
//hide the menu
|
|
|
|
hide: function(trigger) {
|
|
|
|
if (this.shown) {
|
|
|
|
this.fx.start(0);
|
|
|
|
//this.menu.fade('out');
|
|
|
|
this.fireEvent('hide');
|
|
|
|
this.shown = false;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
setItemChecked: function(item, checked) {
|
|
|
|
this.menu.getElement('a[href$=' + item + ']').firstChild.style.opacity =
|
|
|
|
checked ? '1' : '0';
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2015-01-30 23:58:27 +03:00
|
|
|
getItemChecked: function(item) {
|
|
|
|
return '0' != this.menu.getElement('a[href$=' + item + ']').firstChild.style.opacity;
|
|
|
|
},
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
//hide an item
|
|
|
|
hideItem: function(item) {
|
|
|
|
this.menu.getElement('a[href$=' + item + ']').parentNode.addClass('invisible');
|
2014-11-30 16:14:09 +03:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2014-12-09 19:54:35 +03:00
|
|
|
//show an item
|
|
|
|
showItem: function(item) {
|
|
|
|
this.menu.getElement('a[href$=' + item + ']').parentNode.removeClass('invisible');
|
2014-11-30 16:14:09 +03:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2015-05-04 02:09:30 +03:00
|
|
|
//disable the entire menu
|
2014-11-30 16:14:09 +03:00
|
|
|
disable: function() {
|
|
|
|
this.options.disabled = true;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
//enable the entire menu
|
|
|
|
enable: function() {
|
|
|
|
this.options.disabled = false;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
//execute an action
|
|
|
|
execute: function(action, element) {
|
|
|
|
if (this.options.actions[action]) {
|
|
|
|
this.options.actions[action](element, this);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-07-14 04:06:34 +03:00
|
|
|
});
|