Skip to content

Commit d242ac7

Browse files
committed
Add modal editing option rather than rows, add ability to save rows via javascript
1 parent 0778c54 commit d242ac7

File tree

7 files changed

+498
-27
lines changed

7 files changed

+498
-27
lines changed

client/css/ManyField.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.many_field.inactive {
22
display: none;
33
}
4+
5+
.manyfield__outer.loading {
6+
display: none;
7+
}

client/js/ManyField.src.js

Lines changed: 136 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@
3131

3232
wrapManyFields()
3333

34+
$('body').on('change', '[data-inline-save] .field', function(e) {
35+
// saves the record line
36+
var parent = $(this).parents('[data-inline-save]');
37+
var row = $(this).parents('.row');
38+
var url = parent.attr('data-inline-save');
39+
var csrf = $('input[name=SecurityID]').val();
40+
var data = [];
41+
42+
row.find('[name]').each(function(i, field) {
43+
var name = $(field).attr('name');
44+
var value = null;
45+
46+
if ($(field).is(':checkbox')) {
47+
value = $(field).is(":checked");
48+
} else {
49+
value = $(field).val();
50+
}
51+
52+
data.push({
53+
name: name.substr(name.indexOf('[') + 1, name.indexOf(']') - (name.indexOf('[') + 1)),
54+
value: value
55+
})
56+
})
57+
58+
data.push({
59+
name: 'SecurityID',
60+
value: csrf
61+
})
62+
63+
$.post(url, $.param(data), function() {
64+
//..
65+
});
66+
})
67+
3468
$('body').on('click', '.manyfield__add a', function(e) {
3569
e.preventDefault();
3670

@@ -40,29 +74,125 @@
4074

4175
$.get($(this).attr('href'), { index: parents.find('.manyfield__row').length }, function(data) {
4276
var rows = parents.find('.manyfield__row').last()
43-
44-
if (rows && rows.length) {
45-
rows.after(data)
77+
78+
// if this is the inline modal version then we want to open that in
79+
// a popup
80+
if (parents.hasClass('manyfieldmodal')) {
81+
// find the add modal, set the content to that and open it.
82+
var id = parents.attr('id');
83+
var modal = $("#" + id + '_modal');
84+
85+
modal.find('.modal-body').html($(data)
86+
.find('.CompositeField')
87+
.removeClass('manyfield__row').html()
88+
);
89+
90+
modal.modal('show');
4691
} else {
47-
parents.find('.manyfield__outer').append(data)
48-
}
92+
if (rows && rows.length) {
93+
rows.after(data)
94+
} else {
95+
parents.find('.manyfield__outer').append(data)
96+
}
4997

50-
wrapManyFields()
98+
wrapManyFields()
99+
}
51100

52101
$('body').trigger('manyFieldAdded', {
53102
parents
54103
})
55104
})
56105
})
57106

107+
/**
108+
*
109+
*/
58110
$('body').on('click', '.manyfield__remove', function() {
59111
var parent = $(this).parents('.manyfield__row')
60112

113+
if (parent.length < 1) {
114+
parent = $(this).parents('[data-many-id]');
115+
}
116+
61117
parent.remove()
62118

119+
if ($(this).attr('href')) {
120+
$.post($(this).attr('href'));
121+
}
122+
63123
$('body').trigger('manyFieldRemoved', {
64124
parent
65125
})
126+
127+
return false;
128+
})
129+
130+
/**
131+
*
132+
*/
133+
$('body').on('click', '.manyfield__save', function(e) {
134+
var form = $(this).parents('.modal-content').find('form');
135+
136+
if (form.checkValidity()) {
137+
var body = $(this).parents('.modal-content').find('.modal-body')
138+
.addClass('loading')
139+
140+
$.post(form.attr('action'), form.serialize(), function(reply) {
141+
// reply should be the updated content for
142+
form.parents('.manyfield__holder').html(reply);
143+
body.html('');
144+
})
145+
} else {
146+
e.preventDefault();
147+
}
148+
})
149+
150+
/**
151+
*
152+
*/
153+
$('body').on('click', '.manyfieldmodal .manyfield__outer > div', function() {
154+
var parents = $(this).parents('.manyfieldmodal');
155+
var recordId = $(this).data('many-id');
156+
157+
// find the add modal, set the content to that and open it.
158+
var id = parents.attr('id');
159+
var modal = $('#' + id + '_modal');
160+
161+
$.get(modal.data('form-url'), {RecordID: recordId}, function(data) {
162+
modal.find('.modal-body').html(data).removeClass('loading');
163+
modal.modal('show');
164+
});
165+
})
166+
167+
/**
168+
* Ajax load results.
169+
*/
170+
function populateViaAjax(holder) {
171+
var outer = holder.find('.manyfield__outer');
172+
var form = holder.parents('form');
173+
174+
outer.addClass('loading');
175+
176+
177+
$.get(holder.data('ajax-url'), form.serialize(), function(rows) {
178+
outer
179+
.html($(rows).find('.manyfield__outer').html())
180+
.removeClass('loading');
181+
182+
$('body').trigger('manyFieldLoaded', {
183+
holder
184+
})
185+
})
186+
}
187+
188+
$('.manyfield__holder[data-ajax-url]').each(function(i, elem) {
189+
populateViaAjax($(elem));
190+
191+
var form = $(elem).parents('form');
192+
193+
$('body').on('change', '[data-updates-manyfield='+ $(elem).attr('id') + ']', function(e) {
194+
populateViaAjax($(elem));
195+
})
66196
})
67197
})
68198
})(jQuery)

0 commit comments

Comments
 (0)