-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.better.js
638 lines (524 loc) · 15.9 KB
/
jquery.better.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Better 1.0.0 - jQuery Plugins
* https://github.com/nmakarov
* Copyright (c) 2012 Nick Makarov
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Date: 2013-01-22
*
*//*
* Description:
* A set of jQuery plugins to help simplify common tasks - make a table downloadable, insert a panel etc.
*/
;(function($){
// some private vars
var xyz='xyz';
$.better = function(){
return 'better';
}
$.better.test = function(){
return "better test";
}
$.better.defaults = {};
$.better.plugins = {};
// $.better = {
// id : 'Better'
// ,version : 0.1
// ,test : function(){console.log('Better test.');}
// };
$.fn.better = function(plugin){
var args = [this];
for (i=1; i<arguments.length; i++)
args.push(arguments[i]);
if (typeof $.better.plugins[plugin] == 'function')
return $.better.plugins[plugin].apply(this,args);
else
console.log('plugin `' + plugin + '` is not defined.');
};
})(jQuery);
;(function($){
$.extend($.better.defaults, {
downloadUrl : '/bouncefile.php'
, downloadType : 'text/plain'
, downloadFilename : 'somefile.txt'
, downloadMethod : 'bounce' // 'console'
});
$.extend($.better, {
download : function(options){
if ( ! options)
throw ">>> better.download: no parameters provided.";
// if a string is passed, it is the data.
$.isPlainObject(options) || (options.data = options);
var url = options.downloadUrl || $.better.defaults.downloadUrl
, data = options.data
, filename = options.filename || $.better.defaults.filename || 'somefile.txt'
, type = options.downloadType || $.better.defaults.downloadType
, method = options.downloadMethod || options.method || $.better.defaults.downloadMethod
;
if (method == 'bounce') {
if ( ! url) throw(">>> better.download: no downloadUrl provided.");
var $form = $('<form />', {
method : 'post'
, action : url
, style : 'display:hidden'
}).append($('<input />', {
type : 'hidden'
, name : 'filename'
, value : filename
})).append($('<input />', {
type : 'hidden'
, name : 'data'
, value : data
})).append($('<input />', {
type : 'hidden'
, name : 'type'
, value : type
})).appendTo('html').submit().remove();
}
else if (method == 'console') {
console.log(data);
}
}
});
$.better.plugins.download = function (that, options) {
return that.each(function(){
var $table = $(this)
, data = ''
, csv = ''
;
$("th", $table).each(function(){
csv += $(this).text() + ',';
});
$("tr", $table).each(function(index, tr){
if ($(tr).css('display') == 'none')
return;
$("td", tr).each(function(){
csv += $(this).text() + ',';
});
csv += "\n";
});
// console.log(csv);
$.better.download({data: csv, type : 'text/csv', filename: options.filename});
});
};
/*
Issues
- $container should have explicit dimensions set, otherwise outer* functions won't work
- see how it works with various box model usage - with borders, margins and padding for container and panel
- add option `attrs` - hash will be added to the panel as a set of attrs
*/
$.extend($.better.defaults, {
panel : {
returnPanel : true
}
});
// an array of all created panels:
$.better.plugins.panels = [];
/**
* Recalc dimensions for all panels (called by `onresize` handler)
* @return void
*/
$.better.plugins.panels.recalc = function() {
$.each($.better.plugins.panels, function(index, panel){
panel.recalc();
});
}
/**
* Main .better.panel plugin.
*
* @param {[jQuery]} that Whatever was selected by jQuery selector
* @param {hash} options Plugin options
* @return {[jQuery]} Original set of jQuery els or a reference to the newly created panel.
*/
$.better.plugins.panel = function (that, options) {
var $objects = that
, ret = [];
that.each(function(){
var $container = $(this)
, $panel = options.panel || $("<div />", {class:'panel'})
;
$panel.appendTo('html');
$panel.recalc = function() {
var parentWidth = $container.outerWidth(false)
, parentHeight = $container.outerHeight(true)
, parentLeft = $container.offset().left
, parentTop = $container.offset().top
, offsets = options.offset ? options.offset.split(' ') : [0,0]
, offsetTop = offsets[0] || 0
, offsetLeft = offsets[1] || 0
, left = +parentLeft + +offsetLeft
, top = +parentTop + +offsetTop
;
// left calculations
if (options.height == 'inherit')
$panel.css('height', parentHeight);
if (options.stick.match(/right/))
left += parentWidth;
if (options.pivot.match(/right/))
left -= $panel.outerWidth(true);
// top calculations
if (options.pivot.match(/middle/))
top -= $panel.outerHeight(true)/2;
// make sure dimensions are set:
$panel.css({
position : 'absolute'
, width : $panel.width()
, height : $panel.height()
, top : top
, left : left
});
}
$panel.recalc();
$.better.plugins.panels.push($panel);
ret.push($.better.defaults.panel.returnPanel ? $panel.get(0) : $container.get(0));
});
$objects.length = 0;
Array.prototype.push.apply($objects,ret);
return $objects;
}
// if window resized, containers will resize as well, specifically, if stick == 'right/bottom'.
// panel positions should be adjusted accordingly.
$(window).resize(function(event){
$.better.plugins.panels.recalc();
});
/*
table attributes:
header attributes:
- lookup - JSON string (like '{"0":"No","1":"Yes"}'). If set, column data will be transformed according to this rule.
*/
$.extend($.better.defaults, {
table : {
headerSelector : "th"
}
});
$.better.plugins.table = function(that, options) {
return that.each(function(){
var $table = $(this)
, t = time()
, totals = {}
, options = $.extend({}, $.better.defaults.table, options);
// see if table attributes are set
$.each(options, function(index){
if ($table.attr(index))
options[index] = $table.attr(index);
});
// make sure a `thead` element is set:
if ($table.find("thead").length == 0) {
var $headrow = $("th", $table).parent();
$table.prepend($("<thead/>").append($headrow));
}
// editable stuff
$table.find("tbody td").on("click", function(){
// bail out if the table is not editable:
if ( ! $table.hasClass('editable'))
return true;
var $cell = $(this)
, $th = $("thead th", $table).eq($cell.index())
, $activeCell = getActiveCell();
;
// if a cell is in non-editable column, let the window.onclick handle the `cancelEdit`
if ( ! $th.attr('editable'))
return true;
if ($cell.hasClass('active'))
console.log('already active');
else {
// the cell might abort the cancelEdit
if ( ! cancelEdit($activeCell))
return;
}
// if the cell is already active, do the edit
// if ($cell.hasClass("active"))
startEdit($cell, $activeCell);
$cell.addClass('active');
// else {
// reactivate previously active cell, mark this one as active
// cancelEdit(getActiveCell());
// }
return false;
});
$(window).on("click", function(){
// $table.find("tbody td").removeClass('active');
// console.log('window click');
cancelEdit(getActiveCell());
});
function getActiveCell () {
return $table.find("tbody td[class='active']");
}
function startEdit ($cell, $activeCell) {
var fieldIndex = $cell.index()
, $th = $("thead th", $table).eq(fieldIndex)
, type = $th.attr("editable")
, lookup = $th.data('lookup')
, currentData = $cell.data('cellData')
, newData = null
, hasActiveCell = $activeCell.length>0
;
// if table just activated, do not do any edit
if ( ! hasActiveCell)
return;
switch (type) {
case "state" :
newData = stateAdvance(currentData, lookup);
submitEdit($cell, newData, lookup[newData])
break;
case "text" :
$cell.data('cellData',$cell.text());
addInput($cell);
}
}
function submitEdit ($cell, newData, newDisplay) {
var fieldIndex = $cell.index()
, $th = $("thead th", $table).eq(fieldIndex)
, field = $th.attr('field')
, tableId = $table.attr('id')
, rowId = $cell.parent().attr('id')
, url = $table.attr('submitUrl')
;
$.post(url, {
tableId : tableId
, rowId : rowId
, field : field
, value : newData
}, function(data) {
var res = data.split(':',2),
status = res[0],
message = res[1];
console.log('status: `' + status + '`');
console.log('message: ' + message);
// todo analyze data, if error - throw message
if (status == 'ok') {
$cell.data('cellData', newData);
$cell.fadeOut(50, function(){
$cell.html(newDisplay).fadeIn(50);
});
}
else {
$cell.html($cell.data('oldData'));
return false;
}
});
}
function addInput ($cell) {
var $input = $("<div />")
.height($cell.height())
.css({padding:'0px', margin:'0px'})
.append($("<input />", {type:"text", value:$cell.text()}).css({padding:'', margin:''}))
, saved = {}
, props = ['width', 'height', 'margin', 'padding', 'border']
;
$cell.height($cell.css('height'));
for (var i in props) {
var k = props[i]
, v = $cell.css(k);
// console.log('k:'+k+' v:'+v);
saved[k] = $cell.css(k);
}
// console.log(saved);
$input.appendTo($cell.empty());
for (var i in props) {
var k = props[i]
, v = $cell.css(k);
// console.log('k:'+k+' v:'+v);
}
for (k in props) {
var k = props[i]
$input.css(k, saved[k]);
}
// .width($cell.innerWidth()).appendTo($cell.empty());
}
function stateAdvance (state, states) {
var first, newState, found=false;
for (var k in states) {
if ( ! first)
first = k;
if (found) {
newState = k;
break;
}
if (k == state)
found = true;
}
return newState ? newState : first;
}
function cancelEdit ($cell) {
var fieldIndex = $cell.index()
, $th = $("thead th", $table).eq(fieldIndex)
, type = $th.attr("editable")
, currentData = $cell.data('cellData') || 'abc'
// no active cell, do nothing
if ($cell.length == 0)
return true;
console.log('cancelEdit - ' + currentData);
// todo: possibly, display dialog if the new data is unsaved. And return `false` here if edit should continue
if (type == 'text') {
console.log('here');
$cell.html(currentData);
}
$cell.removeClass('active');
return true;
}
$table.on("edit", function(event, action){
var $cell = getActiveCell();
if ($cell.length == 0)
return;
switch (action) {
case "cancel" : cancelEdit($cell); break;
}
});
// sort rows
$table.on('sort', function(event, fieldname, numeric){
var fieldIndex = $("thead th[field='"+fieldname+"']", $table).index()
, $th = $("thead th", $table).eq(fieldIndex)
, numeric = (numeric === 'numeric') || $th.attr("datatype") || false
, direction = $th.hasClass('asc') ? 'desc' : 'asc'
, res = direction == 'asc' ? -1 : 1
, rows = $table.find("tbody tr").get();
$th.removeClass('asc').removeClass('desc').addClass(direction);
rows.sort(function(a,b){
var $A = $(a).children('td').eq(fieldIndex)
, $B = $(b).children('td').eq(fieldIndex)
, keyA = numeric ? parseInt($A.text(),10) : $A.text().toUpperCase()
, keyB = numeric ? parseInt($B.text(),10) : $B.text().toUpperCase()
;
return (keyA < keyB) ? res : res*(-1);
});
$.each(rows, function(index,row){
$table.children('tbody').append(row);
});
});
$table.on("filter", function(event, fieldname, value, func){
var func = func || "eq"
, fieldIndex = $("thead th[field='"+fieldname+"']", $table).index();
$table.find("tbody tr").show();
if ("undefined" !== typeof fieldname) {
$table.find("tbody tr").each(function(){
var v = $("td:eq("+fieldIndex+")", this).text();
var cond =
func == "eq" ? [].concat(value).indexOf(v) !== -1 :
func == "gte" ? parseInt(v,10) >= parseInt(value,10) :
typeof func == "function" ? func(value, v) :
true
;
if ( ! cond )
$(this).hide();
});
}
calcTotals();
});
// walk the headers and make adjustments to the data
$table.find(options.headerSelector).each(function(index){
var $header = $(this);
if ($header.attr("lookup")) {
var lookup = $.parseJSON($(this).attr("lookup"));
$(this).data('lookup', lookup);
$('tbody tr > td:nth-child('+(index+1)+')',$table).each(function(){
// get the cell text...
var cellData = $(this).text();
// save it as `cellData` data attribute and transform the cell contents
$(this).html(lookup[cellData]).data('cellData', cellData);
});
}
// todo: filter should be assigned to the table, not individual headers.
// that would allow for multi-column filters.
// Additional argument to be passed - field name or field index
//
if ($header.attr("filter")) {
$header.get(0).filter = function(action, values, func){
action = action ? action.toLowerCase() : 'clear';
func = func || "eq";
switch (action) {
case 'apply' :
$table.find("tbody tr").each(function(){
var v = $("td:eq("+index+")", this).text();
var cond =
func == "eq" ? [].concat(values).indexOf(v) !== -1 :
func == "gte" ? parseInt(v,10) >= parseInt(values,10) :
true
;
if ( ! cond )
$(this).hide();
});
break;
// clear filters
default : $table.find("tbody tr").show(); break;
}
calcTotals();
return '123';
};
}
if ($header.attr('total')) {
var total = $header.attr('total');
totals[index] = total;
}
});
function calcTotals() {
var $row = $("<tr />");
$table.find(options.headerSelector).each(function(index){
var t = totals[index]
, res = 0;
try {
// try JSON.
t=JSON.parse(t);
// It is JSON indeed, walk each column
$table.find("tbody").find("td:nth-child("+(index+1)+")").each(function(){
if ($(this).parent().css('display') == 'none')
return;
switch (t.func) {
case 'sum' : res += parseInt($(this).text()); break;
default : res += 1;
}
});
$row.append($("<td />", {html:t.format.sprintf(res)}));
} catch (e) {
// JSON failed - so it is either an empty total or some plain text:
$row.append($("<td />", {html:t}));
}
});
$table.find("tfoot tr").remove();
$table.find("tfoot").append($row);
}
// deal with totals (if any)
if ( ! $.isEmptyObject(totals)) {
// create a `tfoot` element if none:
if ($table.find("tfoot").length == 0)
$table.append($("<tfoot/>").append($("<tr />")));
calcTotals();
}
console.log("elapsed: " + (time()-t));
});
}
function time () {
var d = new Date();
return d.getTime();
}
// fix for IE < 8
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(needle) {
for(var i = 0; i < this.length; i++) {
if(this[i] === needle) {
return i;
}
}
return -1;
};
}
if ( ! String.prototype.sprintf) {
String.prototype.sprintf = function() {
var args = Array.prototype.slice.call( arguments );
return this.replace(/%(i)/g, function (match, number){
return args.shift();
});
}
}
// if ( ! Object.prototype.isEmpty1) {
// Object.prototype.isEmpty1 = function() {
// var empty=true;
// for (var i in this)
// if (this.hasOwnProperty(i)) {empty=false;break;}
// return empty;
// }
// }
})(jQuery);