This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathTextstringArray.js
More file actions
180 lines (133 loc) · 4.09 KB
/
Copy pathTextstringArray.js
File metadata and controls
180 lines (133 loc) · 4.09 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
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
/// <reference path="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" />
; (function ($) {
$.textstringArray = function (el, options) {
var defaults = {
hiddenId: '',
minimum: 1,
maximum: -1
}
var plugin = this,
$this = $(el),
$rows = $this.find('.textstring-row');
plugin.settings = {}
var init = function () {
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
$this.sortable({
axis: 'y',
containment: $this.closest('.propertyItemContent'),
items: '.textstring-row',
handle: '.textstring-row-sort',
forcePlaceholderSize: true,
placeholder: 'textstring-row-placeholder',
stop: function (event, ui) {
serialize();
}
});
$this.find('.textstring-row-add').click(function () {
plugin.addRow(this);
return false;
});
$this.find('.textstring-row-remove').click(function () {
plugin.removeRow(this);
return false;
});
$this.find('input').blur(function(){
serialize();
});
$this.find('input').keydown(function (e) {
var keyCode = e.keyCode || e.which;
// if ENTER is pressed
if (keyCode == 13) {
e.preventDefault();
// add a new row
return $(this).parent().parent().find('.textstring-row-add').click();
}
/* // if BACKSPACE if pressed and the textstring value is empty
if (keyCode == 8 && $(this).val() == '') {
e.preventDefault();
// remove the row
return $(this).parent().parent().find('.textstring-row-remove').click();
}
*/
});
// Set width of header row columns
// var curWidth = $this.find('.textstring-row-field').first().width();
// $this.find('.textstring-header-row-col').css('width', curWidth);
// toggle visibility of edit controls
updateEditControls();
}
plugin.addRow = function (e) {
var $parent = $(e).parent().parent();
var $row = $parent.clone(true); // clone the row
var $input = $row.find('input');
if ($rows.length < plugin.settings.maximum || plugin.settings.maximum <= 0) {
// clear the text field
$input.val('');
// append the new row
$row.insertAfter($parent);
// set the focus
$input[0].focus();
// re-populate the hidden field
serialize();
// toggle visibility of edit controls
updateEditControls();
}
}
plugin.removeRow = function (e) {
// make sure the user wants to remove the row
if (confirm('Are you sure you want to delete this row?')) {
// check if this is the last row...
if ($rows.length == 1) {
// ... if so, just clear it.
$rows.find('input').val('').get(0).focus();
} else if ($rows.length > plugin.settings.minimum) {
var $parent = $(e).parent().parent();
// set the focus
var $focusElement = $parent.prev('.textstring-row').length > 0 ? $parent.prev('.textstring-row') : $parent.next('.textstring-row');
$focusElement.find('input').get(0).focus();
// remove the row
$parent.remove();
}
// re-populate the hidden field
serialize();
// toggle visibility of edit controls
updateEditControls();
}
}
var serialize = function () {
var values = [];
$rows = $this.find('.textstring-row');
$rows.each(function(i, e1){
var value = [];
$(this).find('.textstring-row-field').each(function(j, e2){
value.push($(this).find('input').val());
});
values.push(value);
});
var $hidden = $this.find(plugin.settings.hiddenId);
$hidden.val(JSON.stringify(values));
}
function updateEditControls() {
var addButton = $this.find('.textstring-row-add'),
removeButton = $this.find('.textstring-row-remove'),
sortButton = $this.find('.textstring-row-sort');
if (plugin.settings.maximum == -1 || plugin.settings.maximum > $rows.length) {
addButton.show();
} else {
addButton.hide();
}
if (plugin.settings.minimum == -1 || plugin.settings.minimum == 0 || plugin.settings.minimum < $rows.length) {
removeButton.show();
} else {
removeButton.hide();
}
if ($rows.length > 1) {
sortButton.show();
} else {
sortButton.hide();
}
}
init();
}
})(jQuery);