|
31 | 31 |
|
32 | 32 | wrapManyFields()
|
33 | 33 |
|
| 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 | + |
34 | 68 | $('body').on('click', '.manyfield__add a', function(e) {
|
35 | 69 | e.preventDefault();
|
36 | 70 |
|
|
40 | 74 |
|
41 | 75 | $.get($(this).attr('href'), { index: parents.find('.manyfield__row').length }, function(data) {
|
42 | 76 | 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'); |
46 | 91 | } 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 | + } |
49 | 97 |
|
50 |
| - wrapManyFields() |
| 98 | + wrapManyFields() |
| 99 | + } |
51 | 100 |
|
52 | 101 | $('body').trigger('manyFieldAdded', {
|
53 | 102 | parents
|
54 | 103 | })
|
55 | 104 | })
|
56 | 105 | })
|
57 | 106 |
|
| 107 | + /** |
| 108 | + * |
| 109 | + */ |
58 | 110 | $('body').on('click', '.manyfield__remove', function() {
|
59 | 111 | var parent = $(this).parents('.manyfield__row')
|
60 | 112 |
|
| 113 | + if (parent.length < 1) { |
| 114 | + parent = $(this).parents('[data-many-id]'); |
| 115 | + } |
| 116 | + |
61 | 117 | parent.remove()
|
62 | 118 |
|
| 119 | + if ($(this).attr('href')) { |
| 120 | + $.post($(this).attr('href')); |
| 121 | + } |
| 122 | + |
63 | 123 | $('body').trigger('manyFieldRemoved', {
|
64 | 124 | parent
|
65 | 125 | })
|
| 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 | + }) |
66 | 196 | })
|
67 | 197 | })
|
68 | 198 | })(jQuery)
|
0 commit comments