Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/context.js
2941 views
1
/*
2
* Context.js
3
* Copyright Jacob Kelley
4
* MIT License
5
*/
6
7
var context = context || (function () {
8
9
var options = {
10
fadeSpeed: 100,
11
filter: function ($obj) {
12
// Modify $obj, Do not return
13
},
14
above: 'auto',
15
preventDoubleContext: true,
16
compress: false
17
};
18
19
function initialize(opts) {
20
21
options = $.extend({}, options, opts);
22
23
$(document).on('click', 'html', function () {
24
$('.dropdown-context').fadeOut(options.fadeSpeed, function(){
25
$('.dropdown-context').css({display:''}).find('.drop-left').removeClass('drop-left');
26
});
27
});
28
if(options.preventDoubleContext){
29
$(document).on('contextmenu', '.dropdown-context', function (e) {
30
e.preventDefault();
31
});
32
}
33
$(document).on('mouseenter', '.dropdown-submenu', function(){
34
var $sub = $(this).find('.dropdown-context-sub:first'),
35
subWidth = $sub.width(),
36
subLeft = $sub.offset().left,
37
collision = (subWidth+subLeft) > window.innerWidth;
38
if(collision){
39
$sub.addClass('drop-left');
40
}
41
});
42
43
}
44
45
function updateOptions(opts){
46
options = $.extend({}, options, opts);
47
}
48
49
function buildMenu(data, id, subMenu) {
50
var subClass = (subMenu) ? ' dropdown-context-sub' : '',
51
compressed = options.compress ? ' compressed-context' : '',
52
$menu = $('<ul class="dropdown-menu dropdown-context' + subClass + compressed+'" id="dropdown-' + id + '"></ul>');
53
var i = 0, linkTarget = '';
54
for(i; i<data.length; i++) {
55
if (typeof data[i].divider !== 'undefined') {
56
$menu.append('<li class="divider"></li>');
57
} else if (typeof data[i].header !== 'undefined') {
58
$menu.append('<li class="nav-header">' + data[i].header + '</li>');
59
} else {
60
if (typeof data[i].href == 'undefined') {
61
data[i].href = '#';
62
}
63
if (typeof data[i].target !== 'undefined') {
64
linkTarget = ' target="'+data[i].target+'"';
65
}
66
if (typeof data[i].subMenu !== 'undefined') {
67
$sub = ('<li class="dropdown-submenu"><a tabindex="-1" href="' + data[i].href + '">' + data[i].text + '</a></li>');
68
} else {
69
$sub = $('<li><a tabindex="-1" href="' + data[i].href + '"'+linkTarget+'>' + data[i].text + '</a></li>');
70
}
71
if (typeof data[i].action !== 'undefined') {
72
var actiond = new Date(),
73
actionID = 'event-' + actiond.getTime() * Math.floor(Math.random()*100000),
74
eventAction = data[i].action;
75
$sub.find('a').attr('id', actionID);
76
$('#' + actionID).addClass('context-event');
77
$(document).on('click', '#' + actionID, eventAction);
78
}
79
$menu.append($sub);
80
if (typeof data[i].subMenu != 'undefined') {
81
var subMenuData = buildMenu(data[i].subMenu, id, true);
82
$menu.find('li:last').append(subMenuData);
83
}
84
}
85
if (typeof options.filter == 'function') {
86
options.filter($menu.find('li:last'));
87
}
88
}
89
return $menu;
90
}
91
92
function addContext(selector, data) {
93
94
var d = new Date(),
95
id = d.getTime(),
96
$menu = buildMenu(data, id);
97
98
$('body').append($menu);
99
100
101
$(document).on('contextmenu', selector, function (e) {
102
e.preventDefault();
103
e.stopPropagation();
104
105
$('.dropdown-context:not(.dropdown-context-sub)').hide();
106
107
$dd = $('#dropdown-' + id);
108
if (typeof options.above == 'boolean' && options.above) {
109
$dd.addClass('dropdown-context-up').css({
110
top: e.pageY - 20 - $('#dropdown-' + id).height(),
111
left: e.pageX - 13
112
}).fadeIn(options.fadeSpeed);
113
} else if (typeof options.above == 'string' && options.above == 'auto') {
114
$dd.removeClass('dropdown-context-up');
115
var autoH = $dd.height() + 12;
116
if ((e.pageY + autoH) > $('html').height()) {
117
$dd.addClass('dropdown-context-up').css({
118
top: e.pageY - 20 - autoH,
119
left: e.pageX - 13
120
}).fadeIn(options.fadeSpeed);
121
} else {
122
$dd.css({
123
top: e.pageY + 10,
124
left: e.pageX - 13
125
}).fadeIn(options.fadeSpeed);
126
}
127
}
128
});
129
}
130
131
function destroyContext(selector) {
132
$(document).off('contextmenu', selector).off('click', '.context-event');
133
}
134
135
return {
136
init: initialize,
137
settings: updateOptions,
138
attach: addContext,
139
destroy: destroyContext
140
};
141
})();
142