forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunity.js
More file actions
469 lines (425 loc) · 18.1 KB
/
Copy pathcommunity.js
File metadata and controls
469 lines (425 loc) · 18.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
(function ($) {
$.fn.AttachmentWidget = function() {
return this.each(function () {
var button = $(this);
var fieldset = $(this).parents('.fieldset');
var config = {};
var count = 0;
var readConfig = function() {
var disabledLabel = fieldset.find('.attachDisabledLabel');
if (disabledLabel.length) {
config.disabledLabel = disabledLabel.html();
var required = ''
fieldset.find('.attachRequiredField').each(function(index, field) {
required += '#' + $(field).html() + ',';
});
var fields = fieldset.find(required);
config.basefields = fields;
}
config.showOn = $('#' + fieldset.find('.showAttachsOn').html());
config.showOnDisplay = config.showOn.find('.attachedFiles');
count = config.showOnDisplay.find('.initialAttach').length;
config.showOnEmpty = config.showOn.find('.showAttachmentsEmpty').html();
config.enabledLabel = fieldset.find('.attachEnabledLabel').html();
};
var setState = function() {
var enabled = true;
config.fields.each(function() {
if (!$(this).val()) {
enabled = false;
return;
}
});
if (enabled) {
button.removeAttr('disabled').removeClass('disabledAddAttachment');
button.val(config.enabledLabel);
} else {
button.attr('disabled', 'disabled').addClass('disabledAddAttachment');
button.val(config.disabledLabel);
}
};
var cloneFields = function() {
var html = '<div class="attachedFileInfo">';
if (count) {
html = config.showOnDisplay.html() + html;
}
config.fields.each(function() {
var field = $(this);
var container= $(this).parents('.field');
if (container.find(':file').length) {
html += ' (' + field.val() + ')';
} else {
html += ' ' + field.val();
}
html += '<span style="display: none;" class="removeField">';
html += container.attr('id');
html += '</span>';
container.hide();
});
html += ' <a href="#" class="removeAttach">Remove</a>';
html += '</div>';
config.showOnDisplay.html(html);
count += 1;
initFileInput();
};
var doAttach = function() {
cloneFields();
setState();
};
var removeAttachment = function() {
var link = $(this);
var attach = $(this).parent('.attachedFileInfo');
var fields = attach.find('.removeField');
fields.each(function() {
$('#' + $(this).html()).remove();
});
attach.remove();
if (!config.showOnDisplay.html()) {
config.showOnDisplay.html(config.showOnEmpty);
count = 0;
}
return false;
};
var initTriggers = function() {
config.showOnDisplay.find('a.removeAttach').live('click', removeAttachment);
button.click(doAttach);
};
var initFileInput = function() {
var fieldids = ''
config.basefields.each(function(i) {
var field = $(this);
var oldcontainer= $(this).parents('.field');
var newcontainer= oldcontainer.clone();
var newfield = newcontainer.find('#' + field.attr('id'));
newfield.attr('name', newfield.attr('name') + '_' + count);
newfield.attr('id', newfield.attr('id') + '_' + count);
newcontainer.attr('id', 'container_id_' + newfield.attr('name'));
oldcontainer.after(newcontainer);
oldcontainer.hide();
newcontainer.show();
fieldids += '#' + newfield.attr('id') + ','
});
config.fields = $(fieldids);
config.fields.change(setState);
config.fields.keyup(setState);
};
var initWidget = function() {
readConfig();
initFileInput();
initTriggers();
setState();
};
initWidget();
});
};
$.fn.LiaisonForm = function() {
return this.each(function () {
var form = $(this);
var organization = form.find('#id_organization');
var from = form.find('#id_from_field');
var poc = form.find('#id_to_poc');
var cc = form.find('#id_cc1');
var reply = form.find('#id_replyto');
var purpose = form.find('#id_purpose');
var other_purpose = form.find('#id_purpose_text');
var deadline = form.find('#id_deadline_date');
var submission_date = form.find('#id_submitted_date');
var other_organization = form.find('#id_other_organization');
var approval = form.find('#id_approved');
var cancel = form.find('#id_cancel');
var cancel_dialog = form.find('#cancel-dialog');
var config = {};
var related_trigger = form.find('#id_related_to');
var related_url = form.find('#id_related_to').parent().find('.listURL').text();
var related_dialog = form.find('#related-dialog');
var unrelate_trigger = form.find('#id_no_related_to');
var readConfig = function() {
var confcontainer = form.find('.formconfig');
config.info_update_url = confcontainer.find('.info_update_url').text();
};
var render_mails_into = function(container, person_list, as_html) {
var html='';
$.each(person_list, function(index, person) {
if (as_html) {
html += person[0] + ' <<a href="mailto:'+person[1]+'">'+person[1]+'</a>><br />';
} else {
html += person[0] + ' <'+person[1]+'>\n';
}
});
container.html(html);
};
var toggleApproval = function(needed) {
if (!approval.length) {
return;
}
if (needed) {
approval.removeAttr('disabled');
approval.removeAttr('checked');
} else {
approval.attr('checked','checked');
approval.attr('disabled','disabled');
}
};
var checkPostOnly = function(post_only) {
if (post_only) {
$("input[name=send]").hide();
} else {
$("input[name=send]").show();
}
};
var updateReplyTo = function() {
var select = form.find('select[name=from_fake_user]');
var option = select.find('option:selected');
reply.val(option.attr('title'));
updateFrom();
}
var userSelect = function(user_list) {
if (!user_list || !user_list.length) {
return;
}
var link = form.find('a.from_mailto');
var select = form.find('select[name=from_fake_user]');
var options = '';
link.hide();
$.each(user_list, function(index, person) {
options += '<option value="' + person[0] + '" title="' + person[1][1] + '">'+ person[1][0] + ' <' + person[1][1] + '></option>';
});
select.remove();
link.after('<select name="from_fake_user">' + options +'</select>')
form.find('select[name=from_fake_user]').change(updateReplyTo);
updateReplyTo();
};
var updateInfo = function(first_time) {
var entity = organization;
var to_entity = from;
if (!entity.is('select') || !to_entity.is('select')) {
return false;
}
var url = config.info_update_url;
$.ajax({
url: url,
type: 'GET',
cache: false,
async: true,
dataType: 'json',
data: {to_entity_id: organization.val(),
from_entity_id: to_entity.val()},
success: function(response){
if (!response.error) {
if (!first_time || !cc.text()) {
render_mails_into(cc, response.cc, false);
}
render_mails_into(poc, response.poc, true);
toggleApproval(response.needs_approval);
checkPostOnly(response.post_only);
userSelect(response.full_list);
}
}
});
return false;
};
var updateFrom = function() {
var reply_to = reply.val();
form.find('a.from_mailto').attr('href', 'mailto:' + reply_to);
};
var updatePurpose = function() {
var datecontainer = deadline.parents('.field');
var othercontainer = other_purpose.parents('.field');
var selected_id = purpose.val();
var deadline_required = datecontainer.find('.fieldRequired');
if (selected_id == '1' || selected_id == '2' || selected_id == '5') {
datecontainer.show();
} else {
datecontainer.hide();
deadline.val('');
}
if (selected_id == '5') {
othercontainer.show();
deadline_required.hide();
} else {
othercontainer.hide();
other_purpose.val('');
deadline_required.show();
}
};
var checkOtherSDO = function() {
var entity = organization.val();
if (entity=='othersdo') {
other_organization.parents('.field').show();
} else {
other_organization.parents('.field').hide();
}
};
var cancelForm = function() {
cancel_dialog.dialog("open");
};
var getRelatedLink = function() {
link = $(this).text();;
pk = $(this).nextAll('.liaisonPK').text();
widget = related_trigger.parent();
widget.find('.relatedLiaisonWidgetTitle').text(link);
widget.find('.relatedLiaisonWidgetValue').val(pk);
widget.find('.noRelated').hide();
unrelate_trigger.show();
related_dialog.dialog('close');
return false;
};
var selectNoRelated = function() {
widget = $(this).parent();
widget.find('.relatedLiaisonWidgetTitle').text('');
widget.find('.noRelated').show();
widget.find('.relatedLiaisonWidgetValue').val('');
$(this).hide();
return false;
};
var selectRelated = function() {
trigger = $(this);
widget = $(this).parent();
url = widget.find('.listURL').text();
title = widget.find('.relatedLiaisonWidgetTitle');
related_dialog.html('<img src="/images/ajax-loader.gif" />');
related_dialog.dialog('open');
$.ajax({
url: url,
type: 'GET',
cache: false,
async: true,
dataType: 'html',
success: function(response){
related_dialog.html(response);
related_dialog.find('th a').click(function() {
widget.find('.listURL').text(related_url + $(this).attr('href'));
trigger.click();
return false;
});
related_dialog.find('td a').click(getRelatedLink);
}
});
return false;
};
var checkFrom = function(first_time) {
var reduce_options = form.find('.reducedToOptions');
if (!reduce_options.length) {
updateInfo(first_time);
return;
}
var to_select = organization;
var from_entity = from.val();
if (!reduce_options.find('.full_power_on_' + from_entity).length) {
to_select.find('optgroup').eq(1).hide();
to_select.find('option').each(function() {
if (!reduce_options.find('.reduced_to_set_' + $(this).val()).length) {
$(this).hide();
} else {
$(this).show();
}
});
if (!to_select.find('option:selected').is(':visible')) {
to_select.find('option:selected').removeAttr('selected');
}
} else {
to_select.find('optgroup').show();
to_select.find('option').show();
}
updateInfo(first_time);
};
var checkSubmissionDate = function() {
var date_str = submission_date.val();
if (date_str) {
var sdate = new Date(date_str);
var today = new Date();
if (Math.abs(today-sdate) > 2592000000) { // 2592000000 = 30 days in milliseconds
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');
}
}
};
var initTriggers = function() {
organization.change(function() {updateInfo(false);});
organization.change(checkOtherSDO);
from.change(function() {checkFrom(false);});
reply.keyup(updateFrom);
purpose.change(updatePurpose);
cancel.click(cancelForm);
related_trigger.click(selectRelated);
unrelate_trigger.click(selectNoRelated);
form.submit(checkSubmissionDate);
};
var updateOnInit = function() {
updateFrom();
checkFrom(true);
updatePurpose();
checkOtherSDO();
};
var initDatePicker = function() {
deadline.datepicker({
dateFormat: $.datepicker.ATOM,
changeYear: true
});
submission_date.datepicker({
dateFormat: $.datepicker.ATOM,
changeYear: true
});
};
var initAttachments = function() {
form.find('.addAttachmentWidget').AttachmentWidget();
};
var initDialogs = function() {
cancel_dialog.dialog({
resizable: false,
height:200,
modal: true,
autoOpen: false,
buttons: {
Ok: function() {
window.location='..';
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
related_dialog.dialog({
height: 400,
width: 800,
draggable: true,
modal: true,
autoOpen: false
});
};
var initForm = function() {
readConfig();
initTriggers();
updateOnInit();
initDatePicker();
initAttachments();
initDialogs();
};
initForm();
});
};
$(document).ready(function () {
var container = $('#rules');
var changeInput = function() {
var rule = $(this);
var rule_id = rule.val();
var input = container.find('#id_value');
var select = container.find('.rule_values .' + rule_id + ' select');
container.find('[name="value"]').each(function(index, content) {
$(content).attr('name', '');
});
if (select.length) {
input.hide();
select.attr('name', 'value');
input.next('select').remove();
var newselect = select.clone()
newselect.insertAfter(input);
} else {
input.next('select').remove();
input.attr('name', 'value');
input.show();
}
};
container.find('#id_rule_type').change(changeInput);
});
})(jQuery);