1+ var attachmentWidget = {
2+ button : null ,
3+ config : { } ,
4+ count : 0 ,
5+
6+ readConfig : function ( ) {
7+ var buttonFormGroup = attachmentWidget . button . parents ( '.form-group' ) ;
8+ var disabledLabel = buttonFormGroup . find ( '.attachDisabledLabel' ) ;
9+
10+ if ( disabledLabel . length ) {
11+ attachmentWidget . config . disabledLabel = disabledLabel . html ( ) ;
12+ var required = [ ] ;
13+ buttonFormGroup . find ( '.attachRequiredField' )
14+ . each ( function ( index , field ) {
15+ required . push ( '#' + $ ( field )
16+ . text ( ) ) ;
17+ } ) ;
18+ attachmentWidget . config . basefields = $ ( required . join ( "," ) ) ;
19+ }
20+
21+ attachmentWidget . config . showOn = $ ( '#' + buttonFormGroup . find ( '.showAttachsOn' )
22+ . html ( ) ) ;
23+ attachmentWidget . config . showOnDisplay = attachmentWidget . config . showOn . find ( '.attachedFiles' ) ;
24+ attachmentWidget . count = attachmentWidget . config . showOnDisplay . find ( '.initialAttach' )
25+ . length ;
26+ attachmentWidget . config . showOnEmpty = attachmentWidget . config . showOn . find ( '.showAttachmentsEmpty' )
27+ . html ( ) ;
28+ attachmentWidget . config . enabledLabel = buttonFormGroup . find ( '.attachEnabledLabel' )
29+ . html ( ) ;
30+ } ,
31+
32+ setState : function ( ) {
33+ var enabled = true ;
34+ attachmentWidget . config . fields . each ( function ( ) {
35+ if ( ! $ ( this )
36+ . val ( ) ) {
37+ enabled = false ;
38+ return ;
39+ }
40+ } ) ;
41+ if ( enabled ) {
42+ attachmentWidget . button . removeAttr ( 'disabled' )
43+ . removeClass ( 'disabledAddAttachment' ) ;
44+ attachmentWidget . button . val ( attachmentWidget . config . enabledLabel ) ;
45+ } else {
46+ attachmentWidget . button . attr ( 'disabled' , 'disabled' )
47+ . addClass ( 'disabledAddAttachment' ) ;
48+ attachmentWidget . button . val ( attachmentWidget . config . disabledLabel ) ;
49+ }
50+ } ,
51+
52+ cloneFields : function ( ) {
53+ var html = '<div class="attachedFileInfo">' ;
54+ if ( attachmentWidget . count ) {
55+ html = attachmentWidget . config . showOnDisplay . html ( ) + html ;
56+ }
57+ attachmentWidget . config . fields . each ( function ( ) {
58+ var field = $ ( this ) ;
59+ var container = $ ( this )
60+ . parents ( '.form-group' ) ;
61+ if ( container . find ( ':file' )
62+ . length ) {
63+ html += ' (' + field . val ( ) + ')' ;
64+ } else {
65+ html += ' ' + field . val ( ) ;
66+ }
67+ html += '<span style="display: none;" class="removeField">' ;
68+ html += container . attr ( 'id' ) ;
69+ html += '</span>' ;
70+ container . hide ( ) ;
71+ } ) ;
72+ //html += ' <a href="" class="removeAttach glyphicon glyphicon-remove text-danger"></a>';
73+ html += ' <a href="" class="removeAttach btn btn-default btn-xs">Delete</a>' ;
74+ html += '</div>' ;
75+ attachmentWidget . config . showOnDisplay . html ( html ) ;
76+ attachmentWidget . count += 1 ;
77+ attachmentWidget . initFileInput ( ) ;
78+ } ,
79+
80+ doAttach : function ( ) {
81+ attachmentWidget . cloneFields ( ) ;
82+ attachmentWidget . setState ( ) ;
83+ } ,
84+
85+ removeAttachment : function ( ) {
86+ var attach = $ ( this )
87+ . parent ( '.attachedFileInfo' ) ;
88+ var fields = attach . find ( '.removeField' ) ;
89+ fields . each ( function ( ) {
90+ $ ( '#' + $ ( this )
91+ . html ( ) )
92+ . remove ( ) ;
93+ } ) ;
94+ attach . remove ( ) ;
95+ if ( ! attachmentWidget . config . showOnDisplay . html ( ) ) {
96+ attachmentWidget . config . showOnDisplay . html ( attachmentWidget . config . showOnEmpty ) ;
97+ attachmentWidget . count = 0 ;
98+ }
99+ return false ;
100+ } ,
101+
102+ initTriggers : function ( ) {
103+ attachmentWidget . config . showOnDisplay . on ( 'click' , 'a.removeAttach' , attachmentWidget . removeAttachment ) ;
104+ attachmentWidget . button . click ( attachmentWidget . doAttach ) ;
105+ } ,
106+
107+ initFileInput : function ( ) {
108+ var fieldids = [ ] ;
109+ attachmentWidget . config . basefields . each ( function ( ) {
110+ var field = $ ( this ) ;
111+ var oldcontainer = $ ( this )
112+ . parents ( '.form-group' ) ;
113+ var newcontainer = oldcontainer . clone ( ) ;
114+ var newfield = newcontainer . find ( '#' + field . attr ( 'id' ) ) ;
115+ newfield . attr ( 'name' , newfield . attr ( 'name' ) + '_' + attachmentWidget . count ) ;
116+ newfield . attr ( 'id' , newfield . attr ( 'id' ) + '_' + attachmentWidget . count ) ;
117+ newcontainer . attr ( 'id' , 'container_id_' + newfield . attr ( 'name' ) ) ;
118+ oldcontainer . after ( newcontainer ) ;
119+ oldcontainer . hide ( ) ;
120+ newcontainer . show ( ) ;
121+ fieldids . push ( '#' + newfield . attr ( 'id' ) ) ;
122+ } ) ;
123+ attachmentWidget . config . fields = $ ( fieldids . join ( "," ) ) ;
124+ attachmentWidget . config . fields . change ( attachmentWidget . setState ) ;
125+ attachmentWidget . config . fields . keyup ( attachmentWidget . setState ) ;
126+ } ,
127+
128+ initWidget : function ( ) {
129+ attachmentWidget . button = $ ( this ) ;
130+ attachmentWidget . readConfig ( ) ;
131+ attachmentWidget . initFileInput ( ) ;
132+ attachmentWidget . initTriggers ( ) ;
133+ attachmentWidget . setState ( ) ;
134+ } ,
135+ } ;
136+
137+ var liaisonForm = {
138+ initVariables : function ( ) {
139+ liaisonForm . is_edit_form = liaisonForm . form . attr ( "data-edit-form" ) == "True" ;
140+ liaisonForm . from_groups = liaisonForm . form . find ( '#id_from_groups' ) ;
141+ liaisonForm . from_contact = liaisonForm . form . find ( '#id_from_contact' ) ;
142+ liaisonForm . response_contacts = liaisonForm . form . find ( '#id_response_contacts' ) ;
143+ liaisonForm . to_groups = liaisonForm . form . find ( '#id_to_groups' ) ;
144+ liaisonForm . to_contacts = liaisonForm . form . find ( '#id_to_contacts' ) ;
145+ liaisonForm . cc = liaisonForm . form . find ( '#id_cc_contacts' ) ;
146+ liaisonForm . purpose = liaisonForm . form . find ( '#id_purpose' ) ;
147+ liaisonForm . deadline = liaisonForm . form . find ( '#id_deadline' ) ;
148+ liaisonForm . submission_date = liaisonForm . form . find ( '#id_submitted_date' ) ;
149+ liaisonForm . approval = liaisonForm . form . find ( '#id_approved' ) ;
150+ liaisonForm . initial_approval_label = liaisonForm . form . find ( "label[for='id_approved']" )
151+ . text ( ) ;
152+ liaisonForm . cancel = liaisonForm . form . find ( '#id_cancel' ) ;
153+ liaisonForm . cancel_dialog = liaisonForm . form . find ( '#cancel-dialog' ) ;
154+ liaisonForm . config = { } ;
155+ liaisonForm . related_trigger = liaisonForm . form . find ( '.id_related_to' ) ;
156+ liaisonForm . related_url = liaisonForm . form . find ( '#id_related_to' )
157+ . parent ( )
158+ . find ( '.listURL' )
159+ . text ( ) ;
160+ liaisonForm . related_dialog = liaisonForm . form . find ( '#related-dialog' ) ;
161+ liaisonForm . unrelate_trigger = liaisonForm . form . find ( '.id_no_related_to' ) ;
162+ } ,
163+
164+ render_mails_into : function ( container , person_list , as_html ) {
165+ var html = '' ;
166+
167+ $ . each ( person_list , function ( index , person ) {
168+ if ( as_html ) {
169+ html += person [ 0 ] + ' <<a href="mailto:' + person [ 1 ] + '">' + person [ 1 ] + '</a>><br />' ;
170+ } else {
171+ //html += person[0] + ' <'+person[1]+'>\n';
172+ html += person + '\n' ;
173+ }
174+ } ) ;
175+ container . html ( html ) ;
176+ } ,
177+
178+ toggleApproval : function ( needed ) {
179+ if ( ! liaisonForm . approval . length ) {
180+ return ;
181+ }
182+ if ( ! needed ) {
183+ liaisonForm . approval . prop ( 'checked' , true ) ;
184+ liaisonForm . approval . hide ( ) ;
185+ //$("label[for='id_approved']").text("Approval not required");
186+ var nodes = $ ( "label[for='id_approved']:not(.control-label)" ) [ 0 ] . childNodes ;
187+ nodes [ nodes . length - 1 ] . nodeValue = 'Approval not required' ;
188+ return ;
189+ }
190+ if ( needed && ! $ ( '#id_approved' )
191+ . is ( ':visible' ) ) {
192+ liaisonForm . approval . prop ( 'checked' , false ) ;
193+ liaisonForm . approval . show ( ) ;
194+ //$("label[for='id_approved']").text(initial_approval_label);
195+ nodes = $ ( "label[for='id_approved']:not(.control-label)" ) [ 0 ] . childNodes ;
196+ nodes [ nodes . length - 1 ] . nodeValue = liaisonForm . initial_approval_label ;
197+ return ;
198+ }
199+ } ,
200+
201+ checkPostOnly : function ( post_only ) {
202+ if ( post_only ) {
203+ $ ( "button[name=send]" )
204+ . hide ( ) ;
205+ } else {
206+ $ ( "button[name=send]" )
207+ . show ( ) ;
208+ }
209+ } ,
210+
211+ updateInfo : function ( first_time , sender ) {
212+ // don't overwrite fields when editing existing liaison
213+ if ( liaisonForm . is_edit_form ) {
214+ return false ;
215+ }
216+
217+ var from_ids = liaisonForm . from_groups . val ( ) ;
218+ var to_ids = liaisonForm . to_groups . val ( ) ;
219+ var url = liaisonForm . form . data ( "ajaxInfoUrl" ) ;
220+ $ . ajax ( {
221+ url : url ,
222+ type : 'GET' ,
223+ cache : false ,
224+ async : true ,
225+ dataType : 'json' ,
226+ data : {
227+ from_groups : from_ids ,
228+ to_groups : to_ids
229+ } ,
230+ success : function ( response ) {
231+ if ( ! response . error ) {
232+ if ( ! first_time || ! liaisonForm . cc . text ( ) ) {
233+ liaisonForm . render_mails_into ( liaisonForm . cc , response . cc , false ) ;
234+ }
235+ //render_mails_into(poc, response.poc, false);
236+ if ( sender . attr ( 'id' ) == 'id_to_groups' ) {
237+ liaisonForm . to_contacts . val ( response . to_contacts ) ;
238+ }
239+ if ( sender . attr ( 'id' ) == 'id_from_groups' ) {
240+ liaisonForm . toggleApproval ( response . needs_approval ) ;
241+ liaisonForm . response_contacts . val ( response . response_contacts ) ;
242+ }
243+ liaisonForm . checkPostOnly ( response . post_only ) ;
244+ }
245+ }
246+ } ) ;
247+ return false ;
248+ } ,
249+
250+ updatePurpose : function ( ) {
251+ var deadlinecontainer = liaisonForm . deadline . closest ( '.form-group' ) ;
252+ var value = liaisonForm . purpose . val ( ) ;
253+
254+ if ( value == 'action' || value == 'comment' ) {
255+ liaisonForm . deadline . prop ( 'required' , true ) ;
256+ deadlinecontainer . show ( ) ;
257+ } else {
258+ liaisonForm . deadline . prop ( 'required' , false ) ;
259+ deadlinecontainer . hide ( ) ;
260+ liaisonForm . deadline . val ( '' ) ;
261+ }
262+ } ,
263+
264+ cancelForm : function ( ) {
265+ liaisonForm . cancel_dialog . dialog ( "open" ) ;
266+ } ,
267+
268+ checkSubmissionDate : function ( ) {
269+ var date_str = liaisonForm . submission_date . val ( ) ;
270+ if ( date_str ) {
271+ var sdate = new Date ( date_str ) ;
272+ var today = new Date ( ) ;
273+ if ( Math . abs ( today - sdate ) > 2592000000 ) { // 2592000000 = 30 days in milliseconds
274+ 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' ) ;
275+ }
276+ return true ;
277+ } else
278+ return false ;
279+ } ,
280+
281+ init : function ( ) {
282+ liaisonForm . form = $ ( this ) ;
283+ liaisonForm . initVariables ( ) ;
284+ $ ( '#id_from_groups' )
285+ . select2 ( ) ;
286+ $ ( '#id_to_groups' )
287+ . select2 ( ) ;
288+ liaisonForm . to_groups . change ( function ( ) { liaisonForm . updateInfo ( false , $ ( this ) ) ; } ) ;
289+ liaisonForm . from_groups . change ( function ( ) { liaisonForm . updateInfo ( false , $ ( this ) ) ; } ) ;
290+ liaisonForm . purpose . change ( liaisonForm . updatePurpose ) ;
291+ liaisonForm . form . submit ( liaisonForm . checkSubmissionDate ) ;
292+ $ ( '.addAttachmentWidget' )
293+ . each ( attachmentWidget . initWidget ) ;
294+
295+ liaisonForm . updatePurpose ( ) ;
296+ if ( $ ( '#id_to_groups' )
297+ . val ( ) ) {
298+ $ ( '#id_to_groups' )
299+ . trigger ( 'change' ) ;
300+ }
301+ if ( $ ( '#id_from_groups' )
302+ . val ( ) ) {
303+ $ ( '#id_from_groups' )
304+ . trigger ( 'change' ) ;
305+ }
306+ } ,
307+ } ;
308+
309+ var searchForm = {
310+ // search form, based on doc search feature
311+ init : function ( ) {
312+ searchForm . form = $ ( this ) ;
313+ $ ( "#search-clear-btn" )
314+ . on ( "click" , searchForm . clearForm ) ;
315+ } ,
316+
317+ clearForm : function ( ) {
318+ var form = $ ( this )
319+ . parents ( "form" ) ;
320+ form . find ( "input" )
321+ . val ( "" ) ;
322+ }
323+ } ;
324+
325+ $ ( document )
326+ . ready ( function ( ) {
327+ // use traditional style URL parameters
328+ $ . ajaxSetup ( { traditional : true } ) ;
329+
330+ $ ( 'form.liaisons-form' )
331+ . each ( liaisonForm . init ) ;
332+ $ ( '#liaison_search_form' )
333+ . each ( searchForm . init ) ;
334+ } ) ;
0 commit comments