Refactor duplicate code

This commit is contained in:
Thomas Piccirello 2019-07-16 01:37:35 -07:00
parent 7071c5bda0
commit f441d561a2

View file

@ -39,7 +39,6 @@ var ContextMenu = new Class({
menu: 'menu_id',
stopEvent: true,
targets: 'body',
trigger: 'contextmenu',
offsets: {
x: 0,
y: 0
@ -142,50 +141,41 @@ var ContextMenu = new Class({
}
},
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);
this.adjustMenuPosition(e);
//show the menu
this.show();
}
setupEventListeners: function(elem) {
elem.addEvent('contextmenu', function(e) {
this.triggerMenu(e, elem);
}.bind(this));
t.addEvent('click', function(e) {
elem.addEvent('click', function(e) {
this.hide();
}.bind(this));
},
addTarget: function(t) {
this.targets[this.targets.length] = t;
this.setupEventListeners(t);
},
triggerMenu: function(e, el) {
if (this.options.disabled)
return;
//prevent default, if told to
if (this.options.stopEvent) {
e.stop();
}
//record this as the trigger
this.options.element = $(el);
this.adjustMenuPosition(e);
//show the menu
this.show();
},
//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);
this.adjustMenuPosition(e);
//show the menu
this.show();
}
}.bind(this));
el.addEvent('click', function(e) {
this.hide();
}.bind(this));
}, this);
this.setupEventListeners(el);
}.bind(this), this);
/* menu items */
this.menu.getElements('a').each(function(item) {