forked from OlegKi/jqGrid-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjQuery.jqGrid.columnChooser.js
205 lines (190 loc) · 7.23 KB
/
jQuery.jqGrid.columnChooser.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*global jQuery */
/*jslint browser: true, devel: true, eqeq: true, nomen: true, white: true */
(function ($) {
"use strict";
$.jgrid.extend({
columnChooser : function(opts) {
var self = this, selector, select, colMap = {}, fixedCols = [], dopts, mopts, $dialogContent, multiselectData, listHeight,
colModel = self.jqGrid("getGridParam", "colModel"),
colNames = self.jqGrid("getGridParam", "colNames"),
getMultiselectWidgetData = function ($elem) {
return ($.ui.multiselect.prototype && $elem.data($.ui.multiselect.prototype.widgetFullName || $.ui.multiselect.prototype.widgetName)) ||
$elem.data("ui-multiselect") || $elem.data("multiselect");
};
if ($("#colchooser_" + $.jgrid.jqID(self[0].p.id)).length) { return; }
selector = $('<div id="colchooser_'+self[0].p.id+'" style="position:relative;overflow:hidden"><div><select multiple="multiple"></select></div></div>');
select = $('select', selector);
function insert(perm,i,v) {
var a, b;
if(i>=0){
a = perm.slice();
b = a.splice(i,Math.max(perm.length-i,i));
if(i>perm.length) { i = perm.length; }
a[i] = v;
return a.concat(b);
}
return perm;
}
function call(fn, obj) {
if (!fn) { return; }
if (typeof fn === 'string') {
if ($.fn[fn]) {
$.fn[fn].apply(obj, $.makeArray(arguments).slice(2));
}
} else if ($.isFunction(fn)) {
fn.apply(obj, $.makeArray(arguments).slice(2));
}
}
opts = $.extend({
width : 400,
height : 240,
classname : null,
done : function(perm) { if (perm) { self.jqGrid("remapColumns", perm, true); } },
/* msel is either the name of a ui widget class that
extends a multiselect, or a function that supports
creating a multiselect object (with no argument,
or when passed an object), and destroying it (when
passed the string "destroy"). */
msel : "multiselect",
/* "msel_opts" : {}, */
/* dlog is either the name of a ui widget class that
behaves in a dialog-like way, or a function, that
supports creating a dialog (when passed dlog_opts)
or destroying a dialog (when passed the string
"destroy")
*/
dlog : "dialog",
dialog_opts : {
minWidth: 470,
dialogClass: "ui-jqdialog"
},
/* dlog_opts is either an option object to be passed
to "dlog", or (more likely) a function that creates
the options object.
The default produces a suitable options object for
ui.dialog */
dlog_opts : function(options) {
var buttons = {};
buttons[options.bSubmit] = function() {
options.apply_perm();
options.cleanup(false);
};
buttons[options.bCancel] = function() {
options.cleanup(true);
};
return $.extend(true, {
buttons: buttons,
close: function() {
options.cleanup(true);
},
modal: options.modal || false,
resizable: options.resizable || true,
width: options.width + 70,
resize: function () {
var widgetData = getMultiselectWidgetData(select),
$thisDialogContent = widgetData.container.closest(".ui-dialog-content");
if ($thisDialogContent.length > 0 && typeof $thisDialogContent[0].style === "object") {
$thisDialogContent[0].style.width = "";
} else {
$thisDialogContent.css("width", ""); // or just remove width style
}
widgetData.selectedList.height(Math.max(widgetData.selectedContainer.height() - widgetData.selectedActions.outerHeight() - 1, 1));
widgetData.availableList.height(Math.max(widgetData.availableContainer.height() - widgetData.availableActions.outerHeight() - 1, 1));
}
}, options.dialog_opts || {});
},
/* Function to get the permutation array, and pass it to the
"done" function */
apply_perm : function() {
var perm = [];
$('option',select).each(function() {
if ($(this).is("[selected]")) {
self.jqGrid("showCol", colModel[this.value].name);
} else {
self.jqGrid("hideCol", colModel[this.value].name);
}
});
//fixedCols.slice(0);
$('option[selected]',select).each(function() { perm.push(parseInt(this.value,10)); });
$.each(perm, function() { delete colMap[colModel[parseInt(this,10)].name]; });
$.each(colMap, function() {
var ti = parseInt(this,10);
perm = insert(perm,ti,ti);
});
if (opts.done) {
opts.done.call(self, perm);
}
self.jqGrid("setGridWidth", self[0].p.tblwidth, self[0].p.shrinkToFit);
},
/* Function to cleanup the dialog, and select. Also calls the
done function with no permutation (to indicate that the
columnChooser was aborted */
cleanup : function(calldone) {
call(opts.dlog, selector, 'destroy');
call(opts.msel, select, 'destroy');
selector.remove();
if (calldone && opts.done) {
opts.done.call(self);
}
},
msel_opts : {}
}, $.jgrid.col, opts || {});
if($.ui) {
if ($.ui.multiselect && $.ui.multiselect.defaults) {
if (!$.jgrid._multiselect) {
// should be in language file
alert("Multiselect plugin loaded after jqGrid. Please load the plugin before the jqGrid!");
return;
}
opts.msel_opts = $.extend($.ui.multiselect.defaults, opts.msel_opts);
}
}
if (opts.caption) {
selector.attr("title", opts.caption);
}
if (opts.classname) {
selector.addClass(opts.classname);
select.addClass(opts.classname);
}
if (opts.width) {
$(">div",selector).css({width: opts.width,margin:"0 auto"});
select.css("width", opts.width);
}
if (opts.height) {
$(">div",selector).css("height", opts.height);
select.css("height", opts.height - 10);
}
select.empty();
$.each(colModel, function(i) {
colMap[this.name] = i;
if (this.hidedlg) {
if (!this.hidden) {
fixedCols.push(i);
}
return;
}
select.append("<option value='"+i+"' "+
(this.hidden?"":"selected='selected'")+">"+$.jgrid.stripHtml(colNames[i])+"</option>");
});
dopts = $.isFunction(opts.dlog_opts) ? opts.dlog_opts.call(self, opts) : opts.dlog_opts;
call(opts.dlog, selector, dopts);
mopts = $.isFunction(opts.msel_opts) ? opts.msel_opts.call(self, opts) : opts.msel_opts;
call(opts.msel, select, mopts);
// fix height of elements of the multiselect widget
$dialogContent = $("#colchooser_" + $.jgrid.jqID(self[0].p.id));
$dialogContent.css({ margin: "auto" });
$dialogContent.find(">div").css({ width: "100%", height: "100%", margin: "auto" });
multiselectData = getMultiselectWidgetData(select);
multiselectData.container.css({ width: "100%", height: "100%", margin: "auto" });
multiselectData.selectedContainer.css({ width: multiselectData.options.dividerLocation * 100 + "%", height: "100%", margin: "auto", boxSizing: "border-box" });
multiselectData.availableContainer.css({ width: (100 - multiselectData.options.dividerLocation * 100) + "%", height: "100%", margin: "auto", boxSizing: "border-box" });
// set height for both selectedList and availableList
multiselectData.selectedList.css("height", "auto");
multiselectData.availableList.css("height", "auto");
listHeight = Math.max(multiselectData.selectedList.height(), multiselectData.availableList.height());
listHeight = Math.min(listHeight, $(window).height());
multiselectData.selectedList.css("height", listHeight);
multiselectData.availableList.css("height", listHeight);
}
});
}(jQuery));