-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin-sort.js
147 lines (128 loc) · 5.36 KB
/
plugin-sort.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function SimpleSortPlugin () {
this.prefix = this.constructor.name.toLowerCase();
ActionButtonPlugin.call(this, this.prefix, 'Sort', 'fas fa-sort', '#app-content-plugins-filter');
}
// Inherit from ActionButtonPlugin
SimpleSortPlugin.prototype = Object.create(ActionButtonPlugin.prototype);
SimpleSortPlugin.prototype.constructor = SimpleSortPlugin;
SimpleSortPlugin.prototype.init = function () {
var plugin = this;
this.render();
// Button: order dropdown
var btn_order = $('<div id="' + this.prefix + '-btn-sortorder"></div>')
// Add classes
.addClass('btn-group btn-group-sm')
// Add dropdown button
.append('<button type="button" class="btn btn-outline-light dropdown-toggle text-left text-truncate" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\
<strong>Order</strong>: <em id="' + this.prefix + '-btn-sortorder-current"></em>\
</button>')
// Add dropdown menu
.append('<div class="dropdown-menu" id="' + this.prefix + '-btn-sortorder-options"></div>');
// Button: sort by name
var btn_by_name = $('<button class="btn btn-outline-light" id="' + this.prefix + '-btn-by-name" type="button">Name</button>')
.on('click', function(){
if (!$(this).hasClass('disabled')) {
// TODO: Indicate we are working, if sorting takes some time
plugin.toggleButtonActivity(this);
plugin.sort(config.v.titleElement);
plugin.toggleButtonActivity(this);
}
});
// Button: sort by id
var btn_by_id = $('<button class="btn btn-outline-light" id="' + this.prefix + '-btn-by-id" type="button">ID</button>')
.on('click', function(){
if (!$(this).hasClass('disabled')) {
// TODO: Indicate we are working, if sorting takes some time
plugin.toggleButtonActivity(this);
plugin.sort('id');
plugin.toggleButtonActivity(this);
}
});
// Button: sort by status
var btn_by_status = $('<button class="btn btn-outline-light" id="' + this.prefix + '-btn-by-status" type="button">Status</button>')
.on('click', function(){
if (!$(this).hasClass('disabled')) {
// TODO: Indicate we are working, if sorting takes some time
plugin.toggleButtonActivity(this);
plugin.sort(config.v.statusElement);
plugin.toggleButtonActivity(this);
}
});
this.registerButton(btn_order)
.registerButton(btn_by_name)
.registerButton(btn_by_id)
.registerButton(btn_by_status)
.buildOrderButtonDropDown();
// Dirty workaround to get the plugin in visible state. Usually this is done in
// plugin-actions.js, triggered by the event "datasetLoaded"
//this.plugin.removeClass('plugin-hidden');
this.log('Initialized.');
return this;
}
SimpleSortPlugin.prototype.buildOrderButtonDropDown = function (current) {
var plugin = this;
var options = ['ascending', 'descending'];
var current = current || 'ascending';
// display current sortorder
$('#' + this.prefix + '-btn-sortorder-current').html(current);
// Rebuild dropdown
$('#' + this.prefix + '-btn-sortorder-options').html('');
options.forEach(function(option){
var btn = $('<button class="dropdown-item" type="button"></button>');
if (current == option) {
btn.addClass('disabled');
}
btn.append(option)
.appendTo('#' + plugin.prefix + '-btn-sortorder-options')
.on('click', function() {
if (!$(this).hasClass('disabled')) {
// display current sortorder
$('#' + plugin.prefix + '-btn-sortorder-current').html(option);
// enable other disabled buttons
$(this).parent().find('.disabled').removeClass('disabled');
// disable current button
$(this).addClass('disabled');
// Hide plugins
$('#app-content-plugins-area').collapse('hide');
}
});
});
return this;
}
SimpleSortPlugin.prototype.toggleButtonActivity = function (btn) {
// TODO
return this;
}
SimpleSortPlugin.prototype.sort = function (attribute) {
this.log('Started sorting.');
var order = $('#' + this.prefix + '-btn-sortorder-current').html() // Get current order
var container = $('#result-container div.list-group');
var items = container.children('div.list-group-item');
items.sort(function (a, b) {
var obj1 = getLocalObjectById(idm.getObjectId(a.id));
var obj2 = getLocalObjectById(idm.getObjectId(b.id));
// Default is sort by name
// Be aware of config dependency here
var comp1 = obj1[attribute];
var comp2 = obj2[attribute];
// Define sort order by multiplying with 1 (asc) or -1 (desc)
var ord = 1;
if (order === 'descending') {
ord = -1;
}
if (comp1 < comp2) {
return -1 * ord;
}
if (comp1 > comp2) {
return 1 * ord;
}
// names must be equal
return 0;
})
items.detach().appendTo(container);
this.log('Finished sorting.');
return this;
}
// Instantiate and initialize plugin
var ssp = new SimpleSortPlugin();
ssp.init();