/
grid.jqueryui.js
510 lines (499 loc) · 17.5 KB
/
grid.jqueryui.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
;(function($){
/*
**
* jqGrid addons using jQuery UI
* Author: Mark Williams
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
* depends on jQuery UI
**/
if ($.browser.msie && $.browser.version==8) {
$.expr[":"].hidden = function(elem) {
return elem.offsetWidth === 0 || elem.offsetHeight === 0 ||
elem.style.display == "none";
};
}
// requiere load multiselect before grid
$.jgrid._multiselect = false;
if($.ui) {
if ($.ui.multiselect ) {
if($.ui.multiselect.prototype._setSelected) {
var setSelected = $.ui.multiselect.prototype._setSelected;
$.ui.multiselect.prototype._setSelected = function(item,selected) {
var ret = setSelected.call(this,item,selected);
if (selected && this.selectedList) {
var elt = this.element;
this.selectedList.find('li').each(function() {
if ($(this).data('optionLink')) {
$(this).data('optionLink').remove().appendTo(elt);
}
});
}
return ret;
};
}
if($.ui.multiselect.prototype.destroy) {
$.ui.multiselect.prototype.destroy = function() {
this.element.show();
this.container.remove();
if ($.Widget === undefined) {
$.widget.prototype.destroy.apply(this, arguments);
} else {
$.Widget.prototype.destroy.apply(this, arguments);
}
};
}
$.jgrid._multiselect = true;
}
}
$.jgrid.extend({
sortableColumns : function (tblrow)
{
return this.each(function (){
var ts = this, tid= $.jgrid.jqID( ts.p.id );
function start() {ts.p.disableClick = true;}
var sortable_opts = {
"tolerance" : "pointer",
"axis" : "x",
"scrollSensitivity": "1",
"items": '>th:not(:has(#jqgh_'+tid+'_cb'+',#jqgh_'+tid+'_rn'+',#jqgh_'+tid+'_subgrid),:hidden)',
"placeholder": {
element: function(item) {
var el = $(document.createElement(item[0].nodeName))
.addClass(item[0].className+" ui-sortable-placeholder ui-state-highlight")
.removeClass("ui-sortable-helper")[0];
return el;
},
update: function(self, p) {
p.height(self.currentItem.innerHeight() - parseInt(self.currentItem.css('paddingTop')||0, 10) - parseInt(self.currentItem.css('paddingBottom')||0, 10));
p.width(self.currentItem.innerWidth() - parseInt(self.currentItem.css('paddingLeft')||0, 10) - parseInt(self.currentItem.css('paddingRight')||0, 10));
}
},
"update": function(event, ui) {
var p = $(ui.item).parent(),
th = $(">th", p),
colModel = ts.p.colModel,
cmMap = {}, tid= ts.p.id+"_";
$.each(colModel, function(i) { cmMap[this.name]=i; });
var permutation = [];
th.each(function() {
var id = $(">div", this).get(0).id.replace(/^jqgh_/, "").replace(tid,"");
if (id in cmMap) {
permutation.push(cmMap[id]);
}
});
$(ts).jqGrid("remapColumns",permutation, true, true);
if ($.isFunction(ts.p.sortable.update)) {
ts.p.sortable.update(permutation);
}
setTimeout(function(){ts.p.disableClick=false;}, 50);
}
};
if (ts.p.sortable.options) {
$.extend(sortable_opts, ts.p.sortable.options);
} else if ($.isFunction(ts.p.sortable)) {
ts.p.sortable = { "update" : ts.p.sortable };
}
if (sortable_opts.start) {
var s = sortable_opts.start;
sortable_opts.start = function(e,ui) {
start();
s.call(this,e,ui);
};
} else {
sortable_opts.start = start;
}
if (ts.p.sortable.exclude) {
sortable_opts.items += ":not("+ts.p.sortable.exclude+")";
}
tblrow.sortable(sortable_opts).data("sortable").floating = true;
});
},
columnChooser : function(opts) {
var self = this;
if($("#colchooser_"+$.jgrid.jqID(self[0].p.id)).length ) { return; }
var selector = $('<div id="colchooser_'+self[0].p.id+'" style="position:relative;overflow:hidden"><div><select multiple="multiple"></select></div></div>');
var select = $('select', selector);
function insert(perm,i,v) {
if(i>=0){
var a = perm.slice();
var b = a.splice(i,Math.max(perm.length-i,i));
if(i>perm.length) { i = perm.length; }
a[i] = v;
return a.concat(b);
}
}
opts = $.extend({
"width" : 420,
"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
},
/* 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(opts) {
var buttons = {};
buttons[opts.bSubmit] = function() {
opts.apply_perm();
opts.cleanup(false);
};
buttons[opts.bCancel] = function() {
opts.cleanup(true);
};
return $.extend(true, {
"buttons": buttons,
"close": function() {
opts.cleanup(true);
},
"modal" : opts.modal ? opts.modal : false,
"resizable": opts.resizable ? opts.resizable : true,
"width": opts.width+20
}, opts.dialog_opts || {});
},
/* Function to get the permutation array, and pass it to the
"done" function */
"apply_perm" : function() {
$('option',select).each(function() {
if (this.selected) {
self.jqGrid("showCol", colModel[this.value].name);
} else {
self.jqGrid("hideCol", colModel[this.value].name);
}
});
var perm = [];
//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);
}
},
/* 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 ) {
if(opts.msel == "multiselect") {
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);
}
var colModel = self.jqGrid("getGridParam", "colModel");
var colNames = self.jqGrid("getGridParam", "colNames");
var colMap = {}, fixedCols = [];
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'")+">"+colNames[i]+"</option>");
});
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));
}
}
var dopts = $.isFunction(opts.dlog_opts) ? opts.dlog_opts.call(self, opts) : opts.dlog_opts;
call(opts.dlog, selector, dopts);
var mopts = $.isFunction(opts.msel_opts) ? opts.msel_opts.call(self, opts) : opts.msel_opts;
call(opts.msel, select, mopts);
},
sortableRows : function (opts) {
// Can accept all sortable options and events
return this.each(function(){
var $t = this;
if(!$t.grid) { return; }
// Currently we disable a treeGrid sortable
if($t.p.treeGrid) { return; }
if($.fn.sortable) {
opts = $.extend({
"cursor":"move",
"axis" : "y",
"items": ".jqgrow"
},
opts || {});
if(opts.start && $.isFunction(opts.start)) {
opts._start_ = opts.start;
delete opts.start;
} else {opts._start_=false;}
if(opts.update && $.isFunction(opts.update)) {
opts._update_ = opts.update;
delete opts.update;
} else {opts._update_ = false;}
opts.start = function(ev,ui) {
$(ui.item).css("border-width","0px");
$("td",ui.item).each(function(i){
this.style.width = $t.grid.cols[i].style.width;
});
if($t.p.subGrid) {
var subgid = $(ui.item).attr("id");
try {
$($t).jqGrid('collapseSubGridRow',subgid);
} catch (e) {}
}
if(opts._start_) {
opts._start_.apply(this,[ev,ui]);
}
};
opts.update = function (ev,ui) {
$(ui.item).css("border-width","");
if($t.p.rownumbers === true) {
$("td.jqgrid-rownum",$t.rows).each(function( i ){
$(this).html( i+1+(parseInt($t.p.page,10)-1)*parseInt($t.p.rowNum,10) );
});
}
if(opts._update_) {
opts._update_.apply(this,[ev,ui]);
}
};
$("tbody:first",$t).sortable(opts);
$("tbody:first",$t).disableSelection();
}
});
},
gridDnD : function(opts) {
return this.each(function(){
var $t = this;
if(!$t.grid) { return; }
// Currently we disable a treeGrid drag and drop
if($t.p.treeGrid) { return; }
if(!$.fn.draggable || !$.fn.droppable) { return; }
function updateDnD ()
{
var datadnd = $.data($t,"dnd");
$("tr.jqgrow:not(.ui-draggable)",$t).draggable($.isFunction(datadnd.drag) ? datadnd.drag.call($($t),datadnd) : datadnd.drag);
}
var appender = "<table id='jqgrid_dnd' class='ui-jqgrid-dnd'></table>";
if($("#jqgrid_dnd").html() === null) {
$('body').append(appender);
}
if(typeof opts == 'string' && opts == 'updateDnD' && $t.p.jqgdnd===true) {
updateDnD();
return;
}
opts = $.extend({
"drag" : function (opts) {
return $.extend({
start : function (ev, ui) {
// if we are in subgrid mode try to collapse the node
if($t.p.subGrid) {
var subgid = $(ui.helper).attr("id");
try {
$($t).jqGrid('collapseSubGridRow',subgid);
} catch (e) {}
}
// hack
// drag and drop does not insert tr in table, when the table has no rows
// we try to insert new empty row on the target(s)
for (var i=0;i<$.data($t,"dnd").connectWith.length;i++){
if($($.data($t,"dnd").connectWith[i]).jqGrid('getGridParam','reccount') == "0" ){
$($.data($t,"dnd").connectWith[i]).jqGrid('addRowData','jqg_empty_row',{});
}
}
ui.helper.addClass("ui-state-highlight");
$("td",ui.helper).each(function(i) {
this.style.width = $t.grid.headers[i].width+"px";
});
if(opts.onstart && $.isFunction(opts.onstart) ) { opts.onstart.call($($t),ev,ui); }
},
stop :function(ev,ui) {
if(ui.helper.dropped && !opts.dragcopy) {
var ids = $(ui.helper).attr("id");
if(ids === undefined) { ids = $(this).attr("id"); }
$($t).jqGrid('delRowData',ids );
}
// if we have a empty row inserted from start event try to delete it
for (var i=0;i<$.data($t,"dnd").connectWith.length;i++){
$($.data($t,"dnd").connectWith[i]).jqGrid('delRowData','jqg_empty_row');
}
if(opts.onstop && $.isFunction(opts.onstop) ) { opts.onstop.call($($t),ev,ui); }
}
},opts.drag_opts || {});
},
"drop" : function (opts) {
return $.extend({
accept: function(d) {
if (!$(d).hasClass('jqgrow')) { return d;}
var tid = $(d).closest("table.ui-jqgrid-btable");
if(tid.length > 0 && $.data(tid[0],"dnd") !== undefined) {
var cn = $.data(tid[0],"dnd").connectWith;
return $.inArray('#'+$.jgrid.jqID(this.id),cn) != -1 ? true : false;
}
return false;
},
drop: function(ev, ui) {
if (!$(ui.draggable).hasClass('jqgrow')) { return; }
var accept = $(ui.draggable).attr("id");
var getdata = ui.draggable.parent().parent().jqGrid('getRowData',accept);
if(!opts.dropbyname) {
var j =0, tmpdata = {}, nm;
var dropmodel = $("#"+$.jgrid.jqID(this.id)).jqGrid('getGridParam','colModel');
try {
for (var key in getdata) {
nm = dropmodel[j].name;
if( !(nm == 'cb' || nm =='rn' || nm == 'subgrid' )) {
if(getdata.hasOwnProperty(key) && dropmodel[j]) {
tmpdata[nm] = getdata[key];
}
}
j++;
}
getdata = tmpdata;
} catch (e) {}
}
ui.helper.dropped = true;
if(opts.beforedrop && $.isFunction(opts.beforedrop) ) {
//parameters to this callback - event, element, data to be inserted, sender, reciever
// should return object which will be inserted into the reciever
var datatoinsert = opts.beforedrop.call(this,ev,ui,getdata,$('#'+$.jgrid.jqID($t.p.id)),$(this));
if (typeof datatoinsert != "undefined" && datatoinsert !== null && typeof datatoinsert == "object") { getdata = datatoinsert; }
}
if(ui.helper.dropped) {
var grid;
if(opts.autoid) {
if($.isFunction(opts.autoid)) {
grid = opts.autoid.call(this,getdata);
} else {
grid = Math.ceil(Math.random()*1000);
grid = opts.autoidprefix+grid;
}
}
// NULL is interpreted as undefined while null as object
$("#"+$.jgrid.jqID(this.id)).jqGrid('addRowData',grid,getdata,opts.droppos);
}
if(opts.ondrop && $.isFunction(opts.ondrop) ) { opts.ondrop.call(this,ev,ui, getdata); }
}}, opts.drop_opts || {});
},
"onstart" : null,
"onstop" : null,
"beforedrop": null,
"ondrop" : null,
"drop_opts" : {
"activeClass": "ui-state-active",
"hoverClass": "ui-state-hover"
},
"drag_opts" : {
"revert": "invalid",
"helper": "clone",
"cursor": "move",
"appendTo" : "#jqgrid_dnd",
"zIndex": 5000
},
"dragcopy": false,
"dropbyname" : false,
"droppos" : "first",
"autoid" : true,
"autoidprefix" : "dnd_"
}, opts || {});
if(!opts.connectWith) { return; }
opts.connectWith = opts.connectWith.split(",");
opts.connectWith = $.map(opts.connectWith,function(n){return $.trim(n);});
$.data($t,"dnd",opts);
if($t.p.reccount != "0" && !$t.p.jqgdnd) {
updateDnD();
}
$t.p.jqgdnd = true;
for (var i=0;i<opts.connectWith.length;i++){
var cn =opts.connectWith[i];
$(cn).droppable($.isFunction(opts.drop) ? opts.drop.call($($t),opts) : opts.drop);
}
});
},
gridResize : function(opts) {
return this.each(function(){
var $t = this, gID = $.jgrid.jqID($t.p.id);
if(!$t.grid || !$.fn.resizable) { return; }
opts = $.extend({}, opts || {});
if(opts.alsoResize ) {
opts._alsoResize_ = opts.alsoResize;
delete opts.alsoResize;
} else {
opts._alsoResize_ = false;
}
if(opts.stop && $.isFunction(opts.stop)) {
opts._stop_ = opts.stop;
delete opts.stop;
} else {
opts._stop_ = false;
}
opts.stop = function (ev, ui) {
$($t).jqGrid('setGridParam',{height:$("#gview_"+gID+" .ui-jqgrid-bdiv").height()});
$($t).jqGrid('setGridWidth',ui.size.width,opts.shrinkToFit);
if(opts._stop_) { opts._stop_.call($t,ev,ui); }
};
if(opts._alsoResize_) {
var optstest = "{\'#gview_"+gID+" .ui-jqgrid-bdiv\':true,'" +opts._alsoResize_+"':true}";
opts.alsoResize = eval('('+optstest+')'); // the only way that I found to do this
} else {
opts.alsoResize = $(".ui-jqgrid-bdiv","#gview_"+gID);
}
delete opts._alsoResize_;
$("#gbox_"+gID).resizable(opts);
});
}
});
})(jQuery);