|
| 1 | +$(document).ready(function () { |
| 2 | + function setupAttachmentWidget() { |
| 3 | + var button = $(this); |
| 4 | + var config = {}; |
| 5 | + var count = 0; |
| 6 | + |
| 7 | + var readConfig = function() { |
| 8 | + var buttonFormGroup = button.parents('.form-group'); |
| 9 | + var disabledLabel = buttonFormGroup.find('.attachDisabledLabel'); |
| 10 | + |
| 11 | + if (disabledLabel.length) { |
| 12 | + config.disabledLabel = disabledLabel.html(); |
| 13 | + var required = []; |
| 14 | + buttonFormGroup.find('.attachRequiredField').each(function(index, field) { |
| 15 | + required.push('#' + $(field).text()); |
| 16 | + }); |
| 17 | + config.basefields = $(required.join(",")); |
| 18 | + } |
| 19 | + |
| 20 | + config.showOn = $('#' + buttonFormGroup.find('.showAttachsOn').html()); |
| 21 | + config.showOnDisplay = config.showOn.find('.attachedFiles'); |
| 22 | + count = config.showOnDisplay.find('.initialAttach').length; |
| 23 | + config.showOnEmpty = config.showOn.find('.showAttachmentsEmpty').html(); |
| 24 | + config.enabledLabel = buttonFormGroup.find('.attachEnabledLabel').html(); |
| 25 | + }; |
| 26 | + |
| 27 | + var setState = function() { |
| 28 | + var enabled = true; |
| 29 | + config.fields.each(function() { |
| 30 | + if (!$(this).val()) { |
| 31 | + enabled = false; |
| 32 | + return; |
| 33 | + } |
| 34 | + }); |
| 35 | + if (enabled) { |
| 36 | + button.removeAttr('disabled').removeClass('disabledAddAttachment'); |
| 37 | + button.val(config.enabledLabel); |
| 38 | + } else { |
| 39 | + button.attr('disabled', 'disabled').addClass('disabledAddAttachment'); |
| 40 | + button.val(config.disabledLabel); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + var cloneFields = function() { |
| 45 | + var html = '<div class="attachedFileInfo">'; |
| 46 | + if (count) { |
| 47 | + html = config.showOnDisplay.html() + html; |
| 48 | + } |
| 49 | + config.fields.each(function() { |
| 50 | + var field = $(this); |
| 51 | + var container= $(this).parents('.form-group'); |
| 52 | + if (container.find(':file').length) { |
| 53 | + html += ' (' + field.val() + ')'; |
| 54 | + } else { |
| 55 | + html += ' ' + field.val(); |
| 56 | + } |
| 57 | + html += '<span style="display: none;" class="removeField">'; |
| 58 | + html += container.attr('id'); |
| 59 | + html += '</span>'; |
| 60 | + container.hide(); |
| 61 | + }); |
| 62 | + html += ' <a href="" class="removeAttach glyphicon glyphicon-remove text-danger"></a>'; |
| 63 | + html += '</div>'; |
| 64 | + config.showOnDisplay.html(html); |
| 65 | + count += 1; |
| 66 | + initFileInput(); |
| 67 | + }; |
| 68 | + |
| 69 | + var doAttach = function() { |
| 70 | + cloneFields(); |
| 71 | + setState(); |
| 72 | + }; |
| 73 | + |
| 74 | + var removeAttachment = function() { |
| 75 | + var link = $(this); |
| 76 | + var attach = $(this).parent('.attachedFileInfo'); |
| 77 | + var fields = attach.find('.removeField'); |
| 78 | + fields.each(function() { |
| 79 | + $('#' + $(this).html()).remove(); |
| 80 | + }); |
| 81 | + attach.remove(); |
| 82 | + if (!config.showOnDisplay.html()) { |
| 83 | + config.showOnDisplay.html(config.showOnEmpty); |
| 84 | + count = 0; |
| 85 | + } |
| 86 | + return false; |
| 87 | + }; |
| 88 | + |
| 89 | + var initTriggers = function() { |
| 90 | + config.showOnDisplay.on('click', 'a.removeAttach', removeAttachment); |
| 91 | + button.click(doAttach); |
| 92 | + }; |
| 93 | + |
| 94 | + var initFileInput = function() { |
| 95 | + var fieldids = []; |
| 96 | + config.basefields.each(function(i) { |
| 97 | + var field = $(this); |
| 98 | + var oldcontainer= $(this).parents('.form-group'); |
| 99 | + var newcontainer= oldcontainer.clone(); |
| 100 | + var newfield = newcontainer.find('#' + field.attr('id')); |
| 101 | + newfield.attr('name', newfield.attr('name') + '_' + count); |
| 102 | + newfield.attr('id', newfield.attr('id') + '_' + count); |
| 103 | + newcontainer.attr('id', 'container_id_' + newfield.attr('name')); |
| 104 | + oldcontainer.after(newcontainer); |
| 105 | + oldcontainer.hide(); |
| 106 | + newcontainer.show(); |
| 107 | + fieldids.push('#' + newfield.attr('id')); |
| 108 | + }); |
| 109 | + config.fields = $(fieldids.join(",")); |
| 110 | + config.fields.change(setState); |
| 111 | + config.fields.keyup(setState); |
| 112 | + }; |
| 113 | + |
| 114 | + var initWidget = function() { |
| 115 | + readConfig(); |
| 116 | + |
| 117 | + initFileInput(); |
| 118 | + initTriggers(); |
| 119 | + |
| 120 | + setState(); |
| 121 | + }; |
| 122 | + |
| 123 | + initWidget(); |
| 124 | + } |
| 125 | + |
| 126 | + $('form.liaisons').each(function() { |
| 127 | + var form = $(this); |
| 128 | + var organization = form.find('#id_organization'); |
| 129 | + var from = form.find('#id_from_field'); |
| 130 | + var poc = form.find('#id_to_poc'); |
| 131 | + var cc = form.find('#id_cc1'); |
| 132 | + var reply = form.find('#id_replyto'); |
| 133 | + var purpose = form.find('#id_purpose'); |
| 134 | + var other_purpose = form.find('#id_purpose_text'); |
| 135 | + var deadline = form.find('#id_deadline_date'); |
| 136 | + var submission_date = form.find('#id_submitted_date'); |
| 137 | + var other_organization = form.find('#id_other_organization'); |
| 138 | + var approval = form.find('#id_approved'); |
| 139 | + |
| 140 | + var render_mails_into = function(container, person_list, as_html) { |
| 141 | + var html=''; |
| 142 | + |
| 143 | + $.each(person_list, function(index, person) { |
| 144 | + if (as_html) { |
| 145 | + html += person[0] + ' <<a href="mailto:'+person[1]+'">'+person[1]+'</a>><br />'; |
| 146 | + } else { |
| 147 | + html += person[0] + ' <'+person[1]+'>\n'; |
| 148 | + } |
| 149 | + }); |
| 150 | + container.html(html); |
| 151 | + }; |
| 152 | + |
| 153 | + var toggleApproval = function(needed) { |
| 154 | + if (!approval.length) { |
| 155 | + return; |
| 156 | + } |
| 157 | + if (needed) { |
| 158 | + approval.removeAttr('disabled'); |
| 159 | + approval.removeAttr('checked'); |
| 160 | + } else { |
| 161 | + approval.attr('checked','checked'); |
| 162 | + approval.attr('disabled','disabled'); |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + var checkPostOnly = function(post_only) { |
| 167 | + if (post_only) { |
| 168 | + $("input[name=send]").hide(); |
| 169 | + } else { |
| 170 | + $("input[name=send]").show(); |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + var updateReplyTo = function() { |
| 175 | + var select = form.find('select[name=from_fake_user]'); |
| 176 | + var option = select.find('option:selected'); |
| 177 | + reply.val(option.attr('title')); |
| 178 | + updateFrom(); |
| 179 | + }; |
| 180 | + |
| 181 | + var userSelect = function(user_list) { |
| 182 | + if (!user_list || !user_list.length) { |
| 183 | + return; |
| 184 | + } |
| 185 | + var link = form.find('a.from_mailto'); |
| 186 | + var select = form.find('select[name=from_fake_user]'); |
| 187 | + var options = ''; |
| 188 | + link.hide(); |
| 189 | + $.each(user_list, function(index, person) { |
| 190 | + options += '<option value="' + person[0] + '" title="' + person[1][1] + '">'+ person[1][0] + ' <' + person[1][1] + '></option>'; |
| 191 | + }); |
| 192 | + select.remove(); |
| 193 | + link.after('<select name="from_fake_user" class="form-control" style="margin-top: 0.5em;">' + options +'</select>'); |
| 194 | + form.find('select[name=from_fake_user]').change(updateReplyTo); |
| 195 | + updateReplyTo(); |
| 196 | + }; |
| 197 | + |
| 198 | + var updateInfo = function(first_time, sender) { |
| 199 | + var entity = organization; |
| 200 | + var to_entity = from; |
| 201 | + if (!entity.is('select') || !to_entity.is('select')) { |
| 202 | + return false; |
| 203 | + } |
| 204 | + var url = form.data("ajaxInfoUrl"); |
| 205 | + $.ajax({ |
| 206 | + url: url, |
| 207 | + type: 'GET', |
| 208 | + cache: false, |
| 209 | + async: true, |
| 210 | + dataType: 'json', |
| 211 | + data: {to_entity_id: organization.val(), |
| 212 | + from_entity_id: to_entity.val()}, |
| 213 | + success: function(response){ |
| 214 | + if (!response.error) { |
| 215 | + if (!first_time || !cc.text()) { |
| 216 | + render_mails_into(cc, response.cc, false); |
| 217 | + } |
| 218 | + render_mails_into(poc, response.poc, true); |
| 219 | + toggleApproval(response.needs_approval); |
| 220 | + checkPostOnly(response.post_only); |
| 221 | + if (sender == 'from') { |
| 222 | + userSelect(response.full_list); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + }); |
| 227 | + return false; |
| 228 | + }; |
| 229 | + |
| 230 | + var updateFrom = function() { |
| 231 | + var reply_to = reply.val(); |
| 232 | + form.find('a.from_mailto').attr('href', 'mailto:' + reply_to); |
| 233 | + }; |
| 234 | + |
| 235 | + var updatePurpose = function() { |
| 236 | + var deadlinecontainer = deadline.closest('.form-group'); |
| 237 | + var othercontainer = other_purpose.closest('.form-group'); |
| 238 | + |
| 239 | + var selected_id = purpose.val(); |
| 240 | + |
| 241 | + if (selected_id == '1' || selected_id == '2' || selected_id == '5') { |
| 242 | + deadlinecontainer.show(); |
| 243 | + } else { |
| 244 | + deadlinecontainer.hide(); |
| 245 | + deadline.val(''); |
| 246 | + } |
| 247 | + |
| 248 | + if (selected_id == '5') { |
| 249 | + othercontainer.show(); |
| 250 | + deadlinecontainer.find("label").removeClass("required"); |
| 251 | + } else { |
| 252 | + othercontainer.hide(); |
| 253 | + other_purpose.val(''); |
| 254 | + deadlinecontainer.find("label").addClass("required"); |
| 255 | + } |
| 256 | + }; |
| 257 | + |
| 258 | + var checkOtherSDO = function() { |
| 259 | + var entity = organization.val(); |
| 260 | + if (entity=='othersdo') { |
| 261 | + other_organization.closest('.form-group').show(); |
| 262 | + other_organization.prop("required", true); |
| 263 | + } else { |
| 264 | + other_organization.closest('.form-group').hide(); |
| 265 | + other_organization.prop("required", false); |
| 266 | + } |
| 267 | + }; |
| 268 | + |
| 269 | + var checkFrom = function(first_time) { |
| 270 | + var reduce_options = form.find('.reducedToOptions'); |
| 271 | + if (!reduce_options.length) { |
| 272 | + updateInfo(first_time, 'from'); |
| 273 | + return; |
| 274 | + } |
| 275 | + var to_select = organization; |
| 276 | + var from_entity = from.val(); |
| 277 | + if (!reduce_options.find('.full_power_on_' + from_entity).length) { |
| 278 | + to_select.find('optgroup').eq(1).hide(); |
| 279 | + to_select.find('option').each(function() { |
| 280 | + if (!reduce_options.find('.reduced_to_set_' + $(this).val()).length) { |
| 281 | + $(this).hide(); |
| 282 | + } else { |
| 283 | + $(this).show(); |
| 284 | + } |
| 285 | + }); |
| 286 | + if (!to_select.find('option:selected').is(':visible')) { |
| 287 | + to_select.find('option:selected').removeAttr('selected'); |
| 288 | + } |
| 289 | + } else { |
| 290 | + to_select.find('optgroup').show(); |
| 291 | + to_select.find('option').show(); |
| 292 | + } |
| 293 | + updateInfo(first_time, 'from'); |
| 294 | + }; |
| 295 | + |
| 296 | + var checkSubmissionDate = function() { |
| 297 | + var date_str = submission_date.val(); |
| 298 | + if (date_str) { |
| 299 | + var sdate = new Date(date_str); |
| 300 | + var today = new Date(); |
| 301 | + if (Math.abs(today-sdate) > 2592000000) { // 2592000000 = 30 days in milliseconds |
| 302 | + return confirm('Submission date ' + date_str + ' differ more than 30 days.\n\nDo you want to continue and post this liaison using that submission date?\n'); |
| 303 | + } |
| 304 | + return true; |
| 305 | + } |
| 306 | + else |
| 307 | + return false; |
| 308 | + }; |
| 309 | + |
| 310 | + |
| 311 | + // init form |
| 312 | + form.find("input[required],select[required],textarea[required]").closest(".form-group").find("label").addClass("required"); |
| 313 | + |
| 314 | + organization.change(function() { updateInfo(false, 'to'); }); |
| 315 | + organization.change(checkOtherSDO); |
| 316 | + from.change(function() { checkFrom(false); }); |
| 317 | + reply.keyup(updateFrom); |
| 318 | + purpose.change(updatePurpose); |
| 319 | + form.submit(checkSubmissionDate); |
| 320 | + |
| 321 | + updateFrom(); |
| 322 | + checkFrom(true); |
| 323 | + updatePurpose(); |
| 324 | + checkOtherSDO(); |
| 325 | + |
| 326 | + form.find('.addAttachmentWidget').each(setupAttachmentWidget); |
| 327 | + }); |
| 328 | +}); |
0 commit comments