Skip to content

Commit b6852e4

Browse files
committed
-Context menu can now be built dynamically with a function callback.
1 parent a4ca2ea commit b6852e4

File tree

3 files changed

+147
-69
lines changed

3 files changed

+147
-69
lines changed

Build/wcDocker.js

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -544,43 +544,55 @@ wcDocker.prototype = {
544544
// http://medialize.github.io/jQuery-contextMenu/docs.html
545545
// for more information.
546546
// Params:
547-
// selector A JQuery selector string that designates the
548-
// elements who use this menu.
549-
// itemList An array with each context menu item in it, each item
550-
// is an object {name:string, callback:function(key, opts, panel)}.
551-
// includeDefault If true, all default panel menu options will also be shown.
552-
basicMenu: function(selector, itemList, includeDefault) {
547+
// selector A JQuery selector string that designates the
548+
// elements who use this menu.
549+
// itemListOrBuildFunc An array with each context menu item in it, each item
550+
// is an object {name:string, callback:function(key, opts, panel)}.
551+
// This can also be a function that dynamically builds and
552+
// returns the item list, parameters given are the $trigger object
553+
// of the menu and the menu event object.
554+
// includeDefault If true, all default panel menu options will also be shown.
555+
basicMenu: function(selector, itemListOrBuildFunc, includeDefault) {
553556
var self = this;
554-
var finalItems = {};
555-
for (var i = 0; i < itemList.length; ++i) {
556-
var callback = itemList[i].callback;
557-
558-
(function(listItem, callback) {
559-
listItem.callback = function(key, opts) {
560-
var panel = null;
561-
var $frame = opts.$trigger.parents('.wcFrame').first();
562-
if ($frame.length) {
563-
for (var a = 0; a < self._frameList.length; ++a) {
564-
if ($frame[0] === self._frameList[a].$frame[0]) {
565-
panel = self._frameList[a].panel();
566-
}
567-
}
568-
}
569-
570-
callback(key, opts, panel);
571-
};
572-
})(itemList[i], callback);
573-
finalItems[itemList[i].name] = itemList[i];
574-
}
575-
576557
if (!includeDefault) {
577558
$.contextMenu({
578559
selector: selector,
579-
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
580-
reposition: false,
581-
autoHide: true,
582-
zIndex: 200,
583-
items: finalItems,
560+
build: function($trigger, event) {
561+
var finalItems = {};
562+
var itemList = itemListOrBuildFunc;
563+
if (typeof itemListOrBuildFunc === 'function') {
564+
itemList = itemListOrBuildFunc($trigger, event);
565+
}
566+
567+
for (var i = 0; i < itemList.length; ++i) {
568+
var callback = itemList[i].callback;
569+
570+
(function(listItem, callback) {
571+
listItem.callback = function(key, opts) {
572+
var panel = null;
573+
var $frame = opts.$trigger.parents('.wcFrame').first();
574+
if ($frame.length) {
575+
for (var a = 0; a < self._frameList.length; ++a) {
576+
if ($frame[0] === self._frameList[a].$frame[0]) {
577+
panel = self._frameList[a].panel();
578+
}
579+
}
580+
}
581+
582+
callback(key, opts, panel);
583+
};
584+
})(itemList[i], callback);
585+
finalItems[itemList[i].name] = itemList[i];
586+
}
587+
588+
return {
589+
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
590+
reposition: false,
591+
autoHide: true,
592+
zIndex: 200,
593+
items: finalItems,
594+
};
595+
}
584596
});
585597
} else {
586598
$.contextMenu({
@@ -627,6 +639,33 @@ wcDocker.prototype = {
627639
}
628640
}
629641

642+
var finalItems = {};
643+
var itemList = itemListOrBuildFunc;
644+
if (typeof itemListOrBuildFunc === 'function') {
645+
itemList = itemListOrBuildFunc($trigger, event);
646+
}
647+
648+
for (var i = 0; i < itemList.length; ++i) {
649+
var callback = itemList[i].callback;
650+
651+
(function(listItem, callback) {
652+
listItem.callback = function(key, opts) {
653+
var panel = null;
654+
var $frame = opts.$trigger.parents('.wcFrame').first();
655+
if ($frame.length) {
656+
for (var a = 0; a < self._frameList.length; ++a) {
657+
if ($frame[0] === self._frameList[a].$frame[0]) {
658+
panel = self._frameList[a].panel();
659+
}
660+
}
661+
}
662+
663+
callback(key, opts, panel);
664+
};
665+
})(itemList[i], callback);
666+
finalItems[itemList[i].name] = itemList[i];
667+
}
668+
630669
var items = finalItems;
631670
if (!$.isEmptyObject(finalItems)) {
632671
items['sep0'] = "---------";
@@ -728,7 +767,7 @@ wcDocker.prototype = {
728767
},
729768
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
730769
reposition: false,
731-
// autoHide: true,
770+
autoHide: true,
732771
zIndex: 200,
733772
items: items,
734773
};

Build/wcDocker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Code/docker.js

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -544,43 +544,55 @@ wcDocker.prototype = {
544544
// http://medialize.github.io/jQuery-contextMenu/docs.html
545545
// for more information.
546546
// Params:
547-
// selector A JQuery selector string that designates the
548-
// elements who use this menu.
549-
// itemList An array with each context menu item in it, each item
550-
// is an object {name:string, callback:function(key, opts, panel)}.
551-
// includeDefault If true, all default panel menu options will also be shown.
552-
basicMenu: function(selector, itemList, includeDefault) {
547+
// selector A JQuery selector string that designates the
548+
// elements who use this menu.
549+
// itemListOrBuildFunc An array with each context menu item in it, each item
550+
// is an object {name:string, callback:function(key, opts, panel)}.
551+
// This can also be a function that dynamically builds and
552+
// returns the item list, parameters given are the $trigger object
553+
// of the menu and the menu event object.
554+
// includeDefault If true, all default panel menu options will also be shown.
555+
basicMenu: function(selector, itemListOrBuildFunc, includeDefault) {
553556
var self = this;
554-
var finalItems = {};
555-
for (var i = 0; i < itemList.length; ++i) {
556-
var callback = itemList[i].callback;
557-
558-
(function(listItem, callback) {
559-
listItem.callback = function(key, opts) {
560-
var panel = null;
561-
var $frame = opts.$trigger.parents('.wcFrame').first();
562-
if ($frame.length) {
563-
for (var a = 0; a < self._frameList.length; ++a) {
564-
if ($frame[0] === self._frameList[a].$frame[0]) {
565-
panel = self._frameList[a].panel();
566-
}
567-
}
568-
}
569-
570-
callback(key, opts, panel);
571-
};
572-
})(itemList[i], callback);
573-
finalItems[itemList[i].name] = itemList[i];
574-
}
575-
576557
if (!includeDefault) {
577558
$.contextMenu({
578559
selector: selector,
579-
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
580-
reposition: false,
581-
autoHide: true,
582-
zIndex: 200,
583-
items: finalItems,
560+
build: function($trigger, event) {
561+
var finalItems = {};
562+
var itemList = itemListOrBuildFunc;
563+
if (typeof itemListOrBuildFunc === 'function') {
564+
itemList = itemListOrBuildFunc($trigger, event);
565+
}
566+
567+
for (var i = 0; i < itemList.length; ++i) {
568+
var callback = itemList[i].callback;
569+
570+
(function(listItem, callback) {
571+
listItem.callback = function(key, opts) {
572+
var panel = null;
573+
var $frame = opts.$trigger.parents('.wcFrame').first();
574+
if ($frame.length) {
575+
for (var a = 0; a < self._frameList.length; ++a) {
576+
if ($frame[0] === self._frameList[a].$frame[0]) {
577+
panel = self._frameList[a].panel();
578+
}
579+
}
580+
}
581+
582+
callback(key, opts, panel);
583+
};
584+
})(itemList[i], callback);
585+
finalItems[itemList[i].name] = itemList[i];
586+
}
587+
588+
return {
589+
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
590+
reposition: false,
591+
autoHide: true,
592+
zIndex: 200,
593+
items: finalItems,
594+
};
595+
}
584596
});
585597
} else {
586598
$.contextMenu({
@@ -627,6 +639,33 @@ wcDocker.prototype = {
627639
}
628640
}
629641

642+
var finalItems = {};
643+
var itemList = itemListOrBuildFunc;
644+
if (typeof itemListOrBuildFunc === 'function') {
645+
itemList = itemListOrBuildFunc($trigger, event);
646+
}
647+
648+
for (var i = 0; i < itemList.length; ++i) {
649+
var callback = itemList[i].callback;
650+
651+
(function(listItem, callback) {
652+
listItem.callback = function(key, opts) {
653+
var panel = null;
654+
var $frame = opts.$trigger.parents('.wcFrame').first();
655+
if ($frame.length) {
656+
for (var a = 0; a < self._frameList.length; ++a) {
657+
if ($frame[0] === self._frameList[a].$frame[0]) {
658+
panel = self._frameList[a].panel();
659+
}
660+
}
661+
}
662+
663+
callback(key, opts, panel);
664+
};
665+
})(itemList[i], callback);
666+
finalItems[itemList[i].name] = itemList[i];
667+
}
668+
630669
var items = finalItems;
631670
if (!$.isEmptyObject(finalItems)) {
632671
items['sep0'] = "---------";
@@ -728,7 +767,7 @@ wcDocker.prototype = {
728767
},
729768
animation: {duration: 250, show: 'fadeIn', hide: 'fadeOut'},
730769
reposition: false,
731-
// autoHide: true,
770+
autoHide: true,
732771
zIndex: 200,
733772
items: items,
734773
};

0 commit comments

Comments
 (0)