-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin-actions.js
107 lines (88 loc) · 3.6 KB
/
plugin-actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function BasicAppPlugin (id, title, icon_classes, container_selector, dataset_dependency) {
this.id = id;
this.title = title;
this.icon_classes = icon_classes;
this.DOMContainer = $(container_selector);
this.dataset_dependency = dataset_dependency || 'YES';
}
BasicAppPlugin.prototype.log = function (msg, type, data) {
var type_str = '';
if (type != undefined) {
type_str = ' (' + type + ')';
}
var entry = this.constructor.name + type_str + ': ' + msg;
if (data != undefined) {
console.log(entry, data);
} else {
console.log(entry);
}
return this;
};
BasicAppPlugin.prototype.renderPluginBase = function () {
this.plugin = $('<div id="' + this.id + '"></div>');
this.plugin
.addClass('app-plugin')
// add heading
.append('<h6 class="text-light app-plugin-title"><i class="' + this.icon_classes + '"></i> ' + this.title + '</h6>')
.append('<div class="app-plugin-content"></div>')
.appendTo(this.DOMContainer);
// add class to hide an action section
if (this.dataset_dependency == 'YES') {
this.plugin.addClass('plugin-hidden');
}
return this;
}
BasicAppPlugin.prototype.renderPluginContent = function (content) {
if (Array.isArray(content)) {
this.plugin.find('.app-plugin-content').html('');
for (i in content) {
this.plugin.find('.app-plugin-content').append(content[i]);
}
} else {
this.plugin.find('.app-plugin-content').html($(content));
}
return this;
}
BasicAppPlugin.prototype.render = function (content) {
var content = content || null;
this.renderPluginBase().renderPluginContent(content);
return this;
}
// if dataset is loaded remove class to show the sections
$('body').on('datasetLoaded', function (e) {
$('.plugin-hidden').removeClass('plugin-hidden').show();
})
function ActionButtonPlugin (id, title, icon_classes, container_selector) {
BasicAppPlugin.call(this, id, title, icon_classes, container_selector);
this.buttons = [];
}
ActionButtonPlugin.prototype = Object.create(BasicAppPlugin.prototype);
ActionButtonPlugin.prototype.constructor = ActionButtonPlugin;
ActionButtonPlugin.prototype.render = function () {
var buttongroup = $('<div class="btn-group btn-group-sm d-block d-sm-inline-flex d-xl-block" role="group" aria-label="action-buttons"></div>');
this.renderPluginBase().renderPluginContent(buttongroup);
return this;
}
ActionButtonPlugin.prototype.registerButton = function (btn) {
// btn should be one JQuery element which is either <a> or <button> with class "btn"
// or a <div> with class "btn-group".
if (btn.length == 1) {
var n = btn[0].nodeName.toLowerCase();
var has_btn_class = btn.hasClass('btn');
var has_btngroup_class = btn.hasClass('btn-group');
if (((n == 'a' || n == 'button') && has_btn_class) || (n == 'div' && has_btngroup_class)) {
this.buttons.push(btn);
// Add to first button group, which is the plugin button group.
// Other button groups could exist inside, e.g. as dropdowns.
this.plugin.find('.btn-group').first().append(btn);
} else {
this.log('Wrong parameter for registerButton()', 'ERROR');
}
} else {
this.log('Too many objects for registerButton() given. Expected 1, got ' + btn.length + '.', 'ERROR');
}
return this;
}
// Initialize actions plugin
var basicPluginActions = new ActionButtonPlugin('app-plugin-actions', 'Actions', 'fas fa-diagnoses', '#app-content-plugins-actions');
basicPluginActions.render();