-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.kv-json-input.js
More file actions
126 lines (121 loc) · 4.3 KB
/
Copy pathjquery.kv-json-input.js
File metadata and controls
126 lines (121 loc) · 4.3 KB
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
/**
* jQuery Key-Value Json Input Plugin
*
* @author Zeng Xi <zengxi@udomain.com.hk>
* @copyright UDomain Web Hosting Company Limited
* @date Dec 19, 2012
* @license GPL Version 2 (or later version) licenses.
* @version 0.2
*/
;(function ($) {
var methods = {
init: function(options){
var settings = $.extend({}, $.fn.kvJsonInput.defaults, options || {});
return this.each(function () {
var $input = $(this);
settings.initData = $.parseJSON($input.val() || '{}');
$input.kvJsonInput('setupHtml', settings);
$input.kvJsonInput('initData', settings);
$(document).on('click.kv-table', '.'+settings.addRowTriggerClass, function(){
$input.kvJsonInput('addRow', settings.templateClass);
});
$(document).on('click.kv-table', '.'+settings.delRowTriggerClass, function(e){
$input.kvJsonInput('delRow', e.target);
});
$input.parents('form').submit(function(e){
$input.kvJsonInput('updated', settings);
return true;
});
});
},
setupHtml: function(options) {
//define css rule to hide template
$('<style type="text/css"> .'+options.templateClass+'{display:none;} .kv-table .del-column{width: 25px;text-align: center;vertical-align: middle;} .kv-table .close{float:none;}</style>').appendTo('head');
var $input = $(this);
var htmlContent = '<table class="'+options.tableClass+' kv-table">\
<thead>\
<tr>\
<th>Key</th>\
<th>Value</th>\
<th class="del-column"></th>\
</tr>\
</thead>\
<tbody>\
<tr class="'+options.templateClass+' kv-table-row">\
<td><input type="text" class="'+options.keyInputClass+'" ></td>\
<td><input type="text" class="'+options.valueInputClass+'" ></td>\
<td class="del-column"><button type="button" class="close '+options.delRowTriggerClass+'">×</button></td>\
</tr>\
</tbody>\
<tfoot>\
<tr>\
<td colspan="3"><button type="button" class="'+options.addRowTriggerClass+' btn-link">Add</button></td>\
</tr>\
</tfoot>\
</table>';
return $input.hide().after(htmlContent);
},
initData: function(options) {
var settings = $.extend({}, $.fn.kvJsonInput.defaults, options || {});
var $table = $(this).next('.kv-table');
for(var item in settings.initData) {
$table.find('tbody>.'+settings.templateClass)
.clone(true) // clone with binded event
.removeClass(settings.templateClass) // remove template mark
.find('.'+settings.keyInputClass)
.val(item)
.attr('required',true)
.end()
.find('.'+settings.valueInputClass)
.val(settings.initData[item])
.attr('required',true)
.end()
.appendTo($table.find('tbody')); // insert into table
}
},
addRow: function(templateClass){
var $table = $(this).next('.kv-table');
return $table.find('tbody>.'+templateClass)
.clone(true) // clone with binded event
.removeClass(templateClass) // remove template mark
.find('input')
.attr('required',true)
.end()
.appendTo($table.find('tbody')); // insert into table
},
delRow: function(delBtn){
return $(delBtn).parents('tr').find('input')
.attr('required', false)
.end()
.fadeOut('normal', function(){$(this).remove();});
},
updated: function(options){
var settings = $.extend({}, $.fn.kvJsonInput.defaults, options || {});
var $table = $(this).next('.kv-table');
var params = {};
$table.find('tbody tr').not('.'+settings.templateClass).each(function(){
params[$(this).find('input.'+settings.keyInputClass).val()] = $(this).find('input.'+settings.valueInputClass).val();
});
return $(this).val(JSON.stringify(params)).trigger('change');
}
};
$.fn.kvJsonInput = function(method){
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.kvJsonInput');
return false;
}
};
$.fn.kvJsonInput.defaults = {
addRowTriggerClass: 'add-row-trigger',
delRowTriggerClass: 'del-row-trigger',
tableClass: 'table table-striped table-bordered table-hover',
templateClass: 'template',
keyInputClass: 'config-key',
valueInputClass: 'config-value',
initData: {}
};
})(jQuery);